-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_python.def
269 lines (244 loc) · 8 KB
/
output_python.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
include: output.def
include: macros/print.def
include: macros/re.def
include: macros_output/perl_like.def
page: output_python, output_main
output_dir: lib/MyDef
ext: py
package: MyDef::output_python
comment: #
subcode: init_page
$global $PYTHON3=1
$if $MyDef::def->{macros}->{PYTHON3}
$PYTHON3=1
$if $MyDef::def->{macros}->{PYTHON2}
undef $PYTHON3
$if 0 # if it is a module
$page->{autolist} = 1
$call survey_functions
$map dbl_list, globals, imports, imports_future
$if !$PYTHON3
add_import_future("print_function")
add_import_future("division")
subcode: parsecode
$if $l=~/^\$(\w+)\s*(.*?)\s*$/
my ($func, $param)=($1, $2)
$if $func eq "global"
my $use_G=MyDef::compileutil::get_macro_word("use_G", 1)
# my @tlist=split /,\s*/, $param
my @tlist = MyDef::utils::proper_split($param)
my (@vlist, @assigns)
$foreach $v in @tlist
$if $v=~/^(\w+)\s*:=\s*(.*)/ -> $name, $val
push @vlist, $name
$if $use_G
push @assigns, "G.$name = $val"
$else
push @assigns, "$name = $val"
$if $val =~ /^\[/
$val = '[]'
$elif $val =~ /^\{/
$val = '{}'
$elif $val =~ /^\d/
$val = "0"
$else
$val = "''"
$call dbl_push_key, globals, $name, "$name = $val"
$elif $v=~/^(\w+)/
$call dbl_push_key, globals, $1, $v
push @vlist, $1
$if !$use_G
push @$out, 'global '. join(", ", @vlist)
$foreach $l in @assigns
push @$out, $l
return 0
$elif $func eq "my"
$foreach $v in MyDef::utils::proper_split($param)
push @$out, $v
$elif $func eq "import"
add_import($param)
return 0
$call @parse_func_list
$elif $func=~/^(if|elif|while)$/
$call @check_if_regex
$call @check_while_step
$param=~s/^!\s*/not /
check_fcall($param)
push @$out, "$func $param:"
return 0
$elif $func=~/^(for(each)?)$/
check_import($param)
$if $param =~/^(\w+)=(.*)/
my ($v, $t) =($1, $2)
$if $t=~/^0:([^:]+)$/
$t=$1
$else
$t=~s/:/,/g
push @$out, "for $v in range($t):"
$elif $param =~/^(.*) in (.*)/ -> $v, $t
$call @check_for_zip
push @$out, "for $param:"
$elif $param =~/^(\S+)$/
# $for N
push @$out, "for _i in range($1):"
$else
$call warn, "Unrecognized: [\$$func $param]...\n"
return 0
$elif $func=~/^def$/
$if $param=~/^\w+$/
$param.="()"
push @$out, "$func $param:"
return 0
$elif $func=~/^do$/
&call return_newblock, do
push @src, "while 1: $(comment) \$do"
push @src, "INDENT"
push @src, "BLOCK"
push @src, "break"
push @src, "DEDENT"
$elif $func eq "else"
push @$out, "else:"
return 0
$elif $func eq "dump"
my $fmt
$foreach $t in split /,\s*/, $param
$fmt.="$t = %s, "
$fmt=~s/, $//
push @$out, "print(\"$fmt\" % ($param))"
return 0
$elif $func =~ /^(print|die|warn)$/
$call parsecode_print
return 0
$elif $func eq "if_match"
$call if_match
return 0
$elif $l=~/^NOOP POST_MAIN/
$call list_functions
return 0
# ---------------------
$call @parsecode_filter
check_import($l)
check_fcall($l)
push @$out, $l
return 0
subcode: check_while_step
$if $func eq "while" and $param=~/^(.+);\s*(.*)/
&call return_newblock, while
push @src, "while $1:","INDENT","BLOCK",$2,"DEDENT"
#---------------------------------
subcode: parsecode_filter
$if $l=~/^(def|if|elif|else|while|for)\b(.*?):?\s*$/
# fix forgotten :
$l="$1$2:"
$elif !$PYTHON3 and $l=~/\binput\s*\(/
$l = $`."raw_input(".$';
$elif $l=~/^(print)\s+(.*)$/
$l="$1($2)"
$elif $l=~/^DUMP_STUB\s+(\w+)/
# for regex_compile
$global %stub
$stub{$1}++
$elif $l=~/^(.+)(\+\+|\-\-)\s*$/
$if $2 eq '++'
$l= "$1+=1"
$else
$l= "$1-=1"
#----------------------------------------
fncode: add_import($t)
$call dbl_push, imports, $t
fncode: add_import_future($t)
$call dbl_push, imports_future, $t
subcode: dumpout
$call @fix_template
$if !$PYTHON3
$if @imports_future
my $t = join(", ", @imports_future)
push @$f, "from __future__ import $t\n"
push @$f, "\n"
$if @imports
$foreach $t in @imports
$if $t=~/(.*)\s+(from\s+\S+)$/
push @$f, "$2 import $1\n"
$else
push @$f, "import $t\n"
push @$f, "\n"
$if @globals
my $use_G=MyDef::compileutil::get_macro_word("use_G", 1)
$if $use_G
push @$f, "class G:\n"
$foreach $t in @globals
push @$f, " $t\n"
$else
$foreach $t in @globals
push @$f, "$t\n"
push @$f, "\n"
my @tmp_out
$if !$stub{"regex_compile"}
push @tmp_out, "DUMP_STUB regex_compile"
$call @dump_fn_block
push @tmp_out, "DUMP_STUB global_init"
push @tmp_out, "NEWLINE?"
unshift @$out, @tmp_out
push @$out, "DUMP_STUB global_exit"
subcode: fix_template
my $i=0
$while $out->[$i]=~/^\s*$|^\s*#|^from .* import|^import /
$if $out->[$i]=~/^import\s+(.*)/
push @imports, $1
$elif $out->[$i]=~/^from\s+(.*)\s+import\s+(.*)/
$if $1 eq "__future__"
push @imports_future, $2
$else
push @imports, "$2 from $1"
$i++
$if $i>0
$while $i>0
shift @$out
$i--
#----------------------------------------
fncode: check_import($l)
$while $l=~/\b(re|os|sys|copy|glob)\./g
add_import($1)
fncode: check_fcall($l)
$while $l=~/\b([\w\.]+)\(/g -> $fn
$if $fn=~/^\w+$/
$call add_function, $fn, nowarn
# -----------------------------------
fncode: parse_function($name, $code)
my $pline
my $params=$code->{params}
$if $#$params>=0
$pline=join(", ", @$params)
push @$out, "def $name($pline):"
push @$out, "INDENT"
$call @check_sub_functions
$code->{scope}="list_sub"
MyDef::compileutil::list_sub($code)
push @$out, "DEDENT"
push @$out, "NEWLINE"
subcode: check_sub_functions
my $codes=$code->{codes}
my $cnt=0
&call inherit_macros, $code
&call iter_subcodes, fn
$if !$code->{_listed}
parse_function($name, $code)
$cnt++
$if $cnt>0
push @$out, "NEWLINE"
#---------------------------
subcode: check_for_zip
$if $v=~/,/ and $t!~/^zip\(/ and $t!~/\.items\(\)/
my @v=MyDef::utils::proper_split($v)
my @t=MyDef::utils::proper_split($t)
$if $#v>0
$if $#v==$#t
push @$out, "for $v in zip($t):"
return 0
$elif $#v==$#t+1 and $v[0]=~/^_?[ijklmn]/
$if $#t==0
push @$out, "for $v in enumerate($t):"
return 0
$else
push @$out, "for $v in zip(range(len($t[0])), $t):"
return 0