@@ -20,15 +20,24 @@ use constant EXIT_PARAM_ERR => 2; # couldn't understand the command line
20
20
21
21
# === Command line parsing ============================================== {{{1
22
22
23
- # files/scripts to load, in order. Each element is [isfile, text].
24
- # Package var so we can localize it.
23
+ # Files/scripts to load, in order. Each element is either [isfile, text]
24
+ # or {...} for pragmas.
25
+ # It is a package var so we can localize it.
25
26
our @_Sources;
26
27
27
28
my $dr_save_source = sub {
28
29
my ($which , $text ) = @_ ;
29
30
push @_Sources, [$which eq ' f' , $text ];
30
31
}; # dr_save_source
31
32
33
+ my $dr_save_pragma = sub {
34
+ my ($kind , $text ) = @_ ;
35
+ $kind = ' B' if $kind eq ' backend' ;
36
+ $kind = ' L' if $kind eq ' language' ;
37
+
38
+ push @_Sources, { $kind => $text };
39
+ }; # dr_save_pragma
40
+
32
41
my %CMDLINE_OPTS = (
33
42
# hash from internal name to array reference of
34
43
# [getopt-name, getopt-options, optional default-value]
@@ -37,7 +46,7 @@ my %CMDLINE_OPTS = (
37
46
# They are listed in alphabetical order by option name,
38
47
# lowercase before upper, although the code does not require that order.
39
48
40
- # BACKEND => ['b', '|backend=s'], # TODO
49
+ # BACKEND => ['b', '|backend=s', $dr_save_pragma], # TODO
41
50
42
51
# DUMP_VARS => ['d', '|dump-variables', false],
43
52
# DEBUG => ['D','|debug', false],
@@ -50,7 +59,7 @@ my %CMDLINE_OPTS = (
50
59
# INCLUDE => ['i','|include=s@'],
51
60
# KEEP_GOING => ['k','|keep-going',false], #not in gawk
52
61
# LIB => ['l','|load=s@'],
53
- LANGUAGE => [' L' ,' |language=s' ],
62
+ LANGUAGE => [' L' ,' |language=s' , $dr_save_pragma ],
54
63
# --man reserved
55
64
# OUTPUT_FILENAME => ['o','|output=s', ""], # conflict with gawk
56
65
# OPTIMIZE => ['O','|optimize'],
@@ -162,16 +171,43 @@ sub Main {
162
171
# they stick around as long as $core does.
163
172
164
173
my $cmd_line_idx = 0; # Number the `-e`s on the command line
165
- foreach my $lrSource (@{$opts {SOURCES }}) {
166
- my ($is_file , $text ) = @$lrSource ;
167
- if ($is_file ) {
168
- $core -> load_script_file($text );
169
- } else {
170
- $core -> load_script_text($text ,
171
- " (cmd line script #@{[++$cmd_line_idx ]})" ,
172
- true); # true => add a Ln if there isn't one in the script
174
+ my $curr_lang = undef ; # current -L, if any.
175
+ # my $curr_backend = undef; # to do
176
+
177
+ foreach my $rItem (@{$opts {SOURCES }}) {
178
+
179
+ if (ref $rItem eq ' ARRAY' ) { # source file or text
180
+ my $lrSource = $rItem ;
181
+ my ($is_file , $text ) = @$lrSource ;
182
+
183
+ if ($is_file ) {
184
+ $core -> load_script_file(filename => $text ,
185
+ $curr_lang ? (language => $curr_lang ) : ()
186
+ );
187
+
188
+ } else {
189
+ $core -> load_script_text(text => $text ,
190
+ filename => " (cmd line script #@{[++$cmd_line_idx ]})" ,
191
+ $curr_lang ? (language => $curr_lang ) :
192
+ (auto_language => true)
193
+ # true => add a Ln if there isn't one in the script
194
+ );
195
+ }
196
+
197
+ } else { # pragma
198
+ my $hrPragma = $rItem ;
199
+
200
+ if (exists $hrPragma -> {L }) {
201
+ $curr_lang = $hrPragma -> {L };
202
+ # say "# Language is now $curr_lang"
203
+ }
204
+
205
+ if (exists $hrPragma -> {B }) {
206
+ die ' Backend selection is not yet supported' ;
207
+ }
173
208
}
174
- } # foreach source
209
+
210
+ } # foreach source item
175
211
176
212
# read from stdin if no input files specified.
177
213
push @$lrArgs , ' -' unless @$lrArgs || $opts {NO_INPUT };
0 commit comments