-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_perl.def
579 lines (514 loc) · 16.5 KB
/
output_perl.def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
include: output.def
include: macros_output/case.def
include: macros_output/for.def
include: macros_output/scope.def
include: macros_output/sumcode.def
include: macros_output/perl_like.def
page: output_perl, output_main
output_dir: lib/MyDef
ext: pl
type: pm
package: MyDef::output_perl
subcode:: _autoload
$global $perl = $^X
$if $perl!~/^\//
$perl = "/usr/bin/perl"
subcode: init_page
$if $page->{package} and !$page->{type}
MyDef::set_page_extension("pm", 1)
$elif !$page->{package} and $page->{type} eq "pm"
$page->{package}=$page->{_pagename}
$if $page->{_pageext} eq "pm"
$page->{autolist}=1
$call survey_functions
$map dbl_list, globals, uses
#----------------------------------------
subcode: parsecode
$(set:parse_condition=1)
$if $l!~/;\s*$/
$call parsecode_scope
$call parsecode_case_support, if, elsif, perl_style
$if $l=~/^\s*\$(\w+)(\s*$|\s+\S.*)$/ -> $func, $param
$param=~s/^\s+//
$call parsecode_function
$elif $l=~/^NOOP POST_MAIN/
$call list_functions
return 0
# $call check_hash_equal
$call check_termination
check_fcall($l)
push @$out, $l
return 0;
fncode: check_fcall($l)
$while $l=~/\b(\w+)\(/g
$call add_function, $1, nowarn
fncode: add_use($m)
$call dbl_push_list, uses, $m
subcode: parsecode_function
# TODO: think about $loopvar
$if $func =~ /^global$/
$call var_declare, global
$elif $func =~ /^my$/ and $param !~/^\s*[=+\-*\/]/
$call var_declare, my
$elif $func =~ /^use$/
add_use($param)
return 0
$call parse_func_list
$elif $func eq "sub"
$if $param=~/^(\w+)\((.*)\)/
&call return_newblock, sub
push @src, "sub $1 {"
push @src, "INDENT"
push @src, "my ($2)=\@_;"
push @src, "BLOCK"
push @src, "DEDENT"
push @src, "}"
$else
return single_block("sub $param {", "}", "sub")
$elif $func =~ /^(while)$/
$call check_regex_capture, $param
$if $regex_capture
&call return_newblock, while
push @src, "while ($param) {"
push @src, "INDENT"
push @src, $regex_capture
push @src, "BLOCK"
push @src, "DEDENT"
push @src, "}"
$elif $param=~/\/.*\/\w*\s*$/
return single_block("while ($param) {", "}", "while")
$else
$call parsecode_while_support, c_style
$elif $func =~ /^for(each)?$/
$if $1 or $param=~/ in /
$call parsecode_foreach
$else
$call parsecode_for_support, c_style
return 0
$elif $func =~ /^loop$/
return single_block("while (1) {", "}", "while")
$elif $func eq "sumcode" or $func eq "sum"
$if $param=~/^\((.*?)\)\s+(.*)/
my $dimstr=$1
$param=$2
$call parsecode_sum_simple, perl
$elif $func eq "sumcode"
$call parsecode_sum_support, perl
$elif $func eq "source-$param"
return "SKIPBLOCK"
$call @parsecode_func_other
# -------
macros:
sumcode_idx: \$_$1
# -------
fncode: parse_condition($t)
$if $t=~/^!?\/|[!=]~\s*\//
# regex
$elif $t=~/[^!=><]=[^="]/
$if $t!~/["'].*=.*['"]/
$call warn, assignment in condition [$t]?
$elif $t=~/\$(?:eq|ne)/
#-- to supress warning ----
$if $t=~/(.*?)(\S+)\s+(\$eq|\$ne)\s+(.*)/
$if $3 eq '$eq'
$t=$1."$2 && $2 eq $4"
$else
#-- remember to use () when necessary
$t=$1."!$2 || $2 ne $4"
return $t
#- TODO: it would be nice to turn ".*" or '.*' or (.*) into excaped or recursive match automatically
subcode: check_regex_capture(t)
my $regex_capture
$if $(t)=~/(.*\S\/\w*)\s*->\s*([^\/]+?)\s*$/
$(t) = $1
my @tlist=MyDef::utils::proper_split($2)
my $i=1
my (@t1, @t2, @t3, @t4)
$foreach $v in @tlist
my $non_local
$if $v=~/(.*)\*$/
$v = $1
$non_local=1
$if $v ne "-"
$if $non_local
push @t3, $v
push @t4, '$'.$i
$else
push @t1, $v
push @t2, '$'.$i
$i++
$if @t1
$regex_capture = "my (".join(', ', @t1).") = (".join(', ', @t2).");"
$if @t3
$if $regex_capture
$regex_capture.="\n"
$regex_capture .= "(".join(', ', @t3).") = (".join(', ', @t4).");"
#----------------------------------------
subcode:: parsecode_func_other
$elif $func =~ /^loopvar$/
$call add_loop_var
return 0
subcode: add_loop_var
my @tlist=MyDef::utils::proper_split($param)
my $block=MyDef::compileutil::get_named_block("...")
$foreach $v in @tlist
push @$block, "my $v;"
#----------------------------------------
subcode: for_i0_i1_step
$call for_i0_i1_step_default, loop_var
$param = "my $param"
subcode: if_perl_style
$call check_regex_capture, $cond
push @src, "$case ($cond) {";
push @src, "INDENT";
$if $regex_capture
$foreach $l in split /\n/, $regex_capture
push @src, $l
push @src, "BLOCK";
push @src, "DEDENT";
push @src, "}";
subcode: elif_perl_style
$call if_perl_style
subcode: else_perl_style
$call push_single_block, "else {", "}"
# -------------------------
subcode: parsecode_foreach
$if $param=~/^(.*?)\s+in\s+(.*)/ -> $v, $t
$if $v!~/,/
$call each_single
$else
my @v = split /,\s*/, $v
$if @v==2 and $t=~/^%/
$call each_hash
$else
$call each_fancy
die "foreach with mismatched keys and lists\n"
$else
# $for @list { ... }
$call each_implicit
# -----------------------
subcode: each_single
$v=~s/^my\s+//
return single_block("foreach my $v ($t) {", "}", "foreach")
subcode: each_implicit
$if $param=~/^(%.*)/
return single_block("while (my (\$k, \$v) = each $1) {", "}", "foreach")
$else
return single_block("foreach ($param) {", "}", "foreach")
subcode: each_hash
my ($k, $v)=@v
return single_block("while (my ($k, $v)=each $t) {", "}", "foreach")
subcode: each_fancy
my @t=MyDef::utils::proper_split($t)
$if $#v==$#t
unshift @v, '$_i'
$if $#v==$#t+1 and $v[0]=~/^\$_?[ijk]/
$if $#v==1
$call each_idx
$else
$call each_zip
# ---------------
subcode: each_idx
my ($idx, $v)=@v
&call return_newblock, foreach
push @src, "my $idx = -1;"
push @src, "foreach my $v ($t) {"
push @src, "INDENT"
push @src, "$idx++;"
push @src, "BLOCK"
push @src, "DEDENT"
push @src, "}"
subcode: each_zip
my $idx=shift @v
$call @check_tlist
&call return_newblock, foreach
push @src, "for (my $idx = 0; $idx < $t[0]; $idx++) {"
push @src, "INDENT"
$for $i=0:@v
my $a=$v[$i]
$if $t[$i]=~/^@(\w+)$/
push @src, "my $a = \$$1"."[$idx];"
$elif $t[$i]=~/^@(.+)/
push @src, "my $a = $1"."->[$idx];"
push @src, "BLOCK"
push @src, "DEDENT"
push @src, "}"
subcode: check_tlist
$for $v in @v
$if $v eq $idx
$call warn, "foreach zip: dummy variable $idx is in conflict\n"
$for $t in @t
$if $t!~/^@/
die "foreach zip error: $t is not an array.\n"
subcode: loop_var
$if !$var
$var="\$i"
$elif $var=~/^(\w+)/
$var='$'.$var
#----------------------------------------
subcode: dumpout
# needed for wrapping up &case support
parsecode("NOOP")
$if $out->[0] eq "EVAL"
# A mode to skip #! and accessories
shift @$out
$else
my $pagetype = $page->{_pageext}
$if !$pagetype or $pagetype eq "pl"
push @$f, "#!$perl\n"
$call dump_accessories
# ---------------
subcode: dump_accessories
my @tmp_out
$(set:p=$MyDef::page->{$1})
$if !$(p:relax)
push @tmp_out, "use strict;"
$(if:0)
push @tmp_out, "use warnings;"
$if !$(p:nowarn)
push @tmp_out, "no warnings qw(uninitialized);"
$elif $(p:nowarn) ne "none"
push @tmp_out, "no warnings qw($(p:nowarn));"
$if @uses
$foreach $v in @uses
push @tmp_out, "use $v;"
push @tmp_out, "NEWLINE?"
$if $(p:package)
push @tmp_out, "package ".$(p:package).";"
push @tmp_out, "NEWLINE?"
push @tmp_out, "DUMP_STUB frame_init"
$if @globals
$foreach $v in @globals
push @tmp_out, "our $v;"
push @tmp_out, "NEWLINE?"
$call dump_fn_block
push @tmp_out, "DUMP_STUB global_init"
unshift @$out, @tmp_out
$if $(p:package) or $page->{type} eq "pm"
push @$out, "NEWLINE?"
push @$out, "1;"
#-----------------------------------------------
# output a sub from $code
fncode: parse_function($name, $code)
push @fn_decls, "sub $name;"
push @$out, "sub $name {"
push @$out, "INDENT"
my $params=$code->{params}
$if $params and @$params
my (@plist, @mlist, @default_list)
$foreach $p in @{$code->{params}}
$if $p=~/^\w+$/
# to make subcode/fncode compatible
push @plist, '$'.$p
push @mlist, $p
$elif $p=~/^(.+?)=(.*)/
push @plist, $1
push @default_list, $p
$else
push @plist, $p
my $pline=join(", ", @plist)
push @$out, "my ($pline) = \@_;"
$if @default_list
$call check_set_default
$if @mlist
$call set_macro
$code->{scope}="list_sub"
MyDef::compileutil::list_sub($code)
push @$out, "DEDENT"
push @$out, "}"
push @$out, "NEWLINE"
subcode: set_macro
my $macro = $code->{macros}
$if !$macro
$macro = {}
$code->{macros} = $macro
$foreach $p in @mlist
$macro->{$p} = '$'.$p
subcode: check_set_default
$for $a in @default_list
$if $a=~/(\S+)=(.*)/ and $2 ne '0'
push @$out, "$1 = $2 if !$1;"
#---- for extensions ----
fncode: inject_function($name, $params, $source)
my $t_code={'type'=>"fn", name=>$name, params=>$params, 'source'=>$source}
$MyDef::def->{codes}->{$name}=$t_code
#-----------------------------------------------
subcode: check_hash_equal
$if $l=~/[({][^)}]*=[^>]/
$call warn, Miss typed = in hash? [$l]
subcode: check_termination
$if $l=~/^\s*$/
# NOOP
$elif $l=~/^break\s*((not|flag)_\w+)?\s*$/
$if $1
$call @break_flag
push @$out, "\$$1 = 1;"
$l="last;"
$elif $l=~/^continue\s*$/
$l="next;"
$elif $l=~/^\s*(for|while|if|else if)\s*\(.*\)\s*$/
# NOOP
$elif $l=~/^\s*}/
# Check the formal blockend
# Inline blocks will get ; appended.
$elif $l!~/[,:\(\[\{;]\s*$/
$l.=";";
$else
# NOOP
# ---------------------
subcode: break_flag
my $stub_idx = $MyDef::compileutil::stub_idx
my $blkname="-$stub_idx"
my $blk = MyDef::compileutil::get_named_block($blkname)
my $t = "my \$$1;"
...
$foreach $_l in @$blk
$if $_l eq $t
break flag_exist
$if !$flag_exist
push @$blk, $t
#----------------------------------------
subcode:: parsecode_func_other
$elif $func eq "dump"
$call parsecode_dump
$elif $func =~/^(print|die|warn)$/
$call parsecode_print
subcode: parsecode_dump
my $hash_var
$if $param =~/^%\{(\S+)\}$/
$hash_var = $1
$elif $param =~/^%(\$\S+)$/
$hash_var = $1
$elif $param=~/^%(\w+)$/
$hash_var = "\\$param"
$if $hash_var
add_use("Data::Dumper")
push @$out, "print Dumper($hash_var);"
$else
my @tlist
$foreach $t in split /,\s*/, $param
push @tlist, "'$t = '"
push @tlist, $t
push @tlist, "', '"
$tlist[-1] = '"\n"'
push @$out, "print ". join(', ', @tlist) .";"
return
subcode: parsecode_print
my $str=$param
my $printf_args
my $need_escape
$if $str=~/^\s*\"(.*)\"\s*$/
$str=$1
$elif $str=~/^\s*\"([^"]+)\",\s*(.+)$/
$str = $1
$printf_args=$2
check_fcall($2)
$else
$need_escape=1
my %colors=(red=>31,green=>32,yellow=>33,blue=>34,magenta=>35,cyan=>36)
my @fmt_list
my @arg_list
my @group
my $n_escape=0
&call parse_loop, $str
&call if_lex, \$
&call if_lex, (red|green|yellow|blue|magenta|cyan)
#----------------------------------------
#- $print $green ---------
push @fmt_list, "\\x1b[$colors{$1}m"
$n_escape++
&call if_lex, \{
push @group, $1
$else
push @fmt_list, '$'
&call if_lex, (\\.)
push @fmt_list, $1
$elif $str=~/\G"/gc
$if $need_escape
push @fmt_list, "\\\""
$else
push @fmt_list, "\""
&call if_lex, \}
#----------------------------------------
#- $print $green{some text}
$if @group
pop @group
$if !@group
push @fmt_list, "\\x1b[0m"
$n_escape=0
$else
my $c=$group[-1]
push @fmt_list, "\\x1b[$colors{$c}m"
$n_escape++
$else
push @fmt_list, '}'
$elif $str=~/\G[^\$\}"]+/gc
push @fmt_list, $&
my $tail=$fmt_list[-1]
$if $tail=~/(.*)-$/
$fmt_list[-1]=$1
$elif $tail!~/\\n$/
push @fmt_list, "\\n"
$if $n_escape
push @fmt_list, "\\x1b[0m"
$if $func =~/^(die|warn)$/
$if $printf_args
push @$out, "$func sprintf \"".join('',@fmt_list)."\", $printf_args;"
$else
push @$out, "$func \"".join('',@fmt_list)."\";"
$else
my $print_target = MyDef::compileutil::get_macro_word("print_to", 1)
my $p
$if $print_target=~/^\@/
$if $printf_args
$p = "push $print_target, sprintf"
$else
$p = "push $print_target,"
$else
$if $printf_args
$p = "printf"
$else
$p = "print"
$if $print_target
$p.=" $print_target"
$if $printf_args
push @$out, "$p \"".join('',@fmt_list)."\", $printf_args;"
$else
push @$out, "$p \"".join('',@fmt_list).'";'
return
#---- scope for sumcode -------
subcode: var_declare(scope)
# $call @remove_trailing_semi
my @tlist=MyDef::utils::proper_split($param)
$foreach $v in @tlist
my ($name, $var)
$call @check_array_dims
$(if:scope=global)
$if $v=~/^([\$\@\%]\w+)\s*(:)?=\s*(.*)/ -> $name, $op, $val
$call dbl_push_key, globals, $name, "$name = $val"
$if $op
push @$out, "$name = $val;"
$else
$call dbl_push, globals, $v
$if $var
# [fix later]
# $global_scope->{var_hash}->{$name}=$var
$(else)
push @$out, "my $v;"
$if $var
$cur_scope->{var_hash}->{$name}=$var
return 0
subcode: remove_trailing_semi
# not needed as any with ; gets by passed anyway
$param=~s/\s*;\s*$//
subcode: check_array_dims
$if $v=~/\@(\w+)\[(.*)\](.*)/
$name=$1
$v='@'.$1.$3
$var={}
my @tlist=split /,\s*/, $2
my $i=0
$foreach $t in @tlist
$i++
$var->{"dim$i"}=$t
#----