forked from Ezhil-Language-Foundation/Ezhil-Lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezhil_serializer.py
358 lines (291 loc) · 9.91 KB
/
ezhil_serializer.py
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
#!/usr/bin/python
##This Python file uses the following encoding: utf-8
##
## (C) 2008-2015 Muthiah Annamalai,
## Licensed under GPL Version 3
##
## Module has elements of PARSE-TREE AST
##
import codecs
import functools
import sys
from .ezhil_scanner import EzhilToken, Token
from .transform import TransformVisitor
PYTHON3 = (sys.version[0] == '3')
if PYTHON3:
unicode = str
try:
import graphviz as gv
graph = functools.partial(gv.Graph, format='svg')
digraph = functools.partial(gv.Digraph, format='svg')
except ImportError as ie:
pass
import xml.sax.saxutils
class Tag(object):
def __init__(self, fileobj, name, tab=0, attrs={}):
object.__init__(self)
self.tagname = name
self.attrs = attrs
self.fileobj = fileobj
self.tabstr = u" " * tab
if len(attrs) > 0:
pfx = u" "
else:
pfx = u""
serialized_attrs = pfx + u" ".join([
u"%s=\"%s\" " % (unicode(k), unicode(v)) for k, v in attrs.items()
])
self.fileobj.write(u"%s<%s%s>\n" %
(self.tabstr, self.tagname, serialized_attrs))
def disp(self, content):
self.fileobj.write(u"%s\n" % content)
def __del__(self):
self.fileobj.write(u"%s</%s>\n" % (self.tabstr, self.tagname))
class SerializerXML(TransformVisitor):
def __init__(self, interpreter, debug=False, filename=None):
""" base class to write transform methods """
self.tab = 0
self.filename = filename
self.file = sys.stdout
if filename:
self.file = codecs.open(filename, "w", "UTF-8")
self.file.write(u"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
tobj = Tag(self.file, u"ezhil", self.tab)
self.incr()
TransformVisitor.__init__(self, interpreter, debug)
self.decr()
def __del__(self):
if self.filename:
self.file.close()
def disp_basic(self, tagname, contents):
escaped_contents = xml.sax.saxutils.escape(u"%s" % contents)
self.file.write(u"%s<%s>%s</%s>\n" %
(self.tabstr(), tagname, escaped_contents, tagname))
def tabstr(self):
return u" " * self.tab
def incr(self):
self.tab += 1
def decr(self):
self.tab -= 1
def update_line(self, obj):
pass
return
def visit_identifier(self, IDobj):
self.disp_basic(u"ID", unicode(IDobj.id))
return
def visit_string(self, string):
self.disp_basic(u"STR", string)
return
def visit_number(self, num):
self.disp_basic(u"NUM", num)
return
def visit_expr_call(self, expr_call):
tobj = Tag(self.file, name=u"EXPRCALL", tab=self.tab)
self.incr()
tobj_id = Tag(self.file, name=u"FUNCID", tab=self.tab)
self.incr()
expr_call.func_id.visit(self)
del tobj_id
self.decr()
self.incr()
tobj_args = Tag(self.file, name=u"FUNCARGS", tab=self.tab)
expr_call.arglist.visit(self)
del tobj_args
self.decr()
self.decr()
return
def visit_expr_list(self, expr_list):
tobj = Tag(self.file, name=u"EXPRLIST", tab=self.tab)
self.incr()
for pos, exp_itr in enumerate(expr_list.exprs):
exp_itr.visit(self)
self.decr()
return
def visit_stmt_list(self, stmt_list):
tobj = Tag(self.file, name=u"STMTLIST", tab=self.tab)
self.incr()
for stmt in stmt_list.List:
stmt.visit(self)
self.decr()
return
def visit_stmt(self, stmt):
tobj = Tag(self.file, name=u"STMT", tab=self.tab)
## is this a recipe for getting stuck in a loop?
stmt.visit(self)
return
def visit_expr(self, expr):
escaped_tok_kind = xml.sax.saxutils.escape(
Token.get_name(expr.binop.kind))
tobj = Tag(self.file,
name=u"EXPR",
tab=self.tab,
attrs={u"binop": escaped_tok_kind})
self.incr()
tobj_term = Tag(self.file, name=u"TERM", tab=self.tab)
self.incr()
expr.term.visit(self)
del tobj_term
self.decr()
toktype = EzhilToken.token_types[expr.binop.kind]
expr.next_expr.visit(self)
self.decr()
return
def visit_return_stmt(self, ret_stmt):
tobj = Tag(self.file, name=u"RETURN", tab=self.tab)
keyword = u"பின்கொடு"
# return may have optional argument
if hasattr(ret_stmt.rvalue, 'visit'):
ret_stmt.rvalue.visit(self)
return
def visit_break_stmt(self, break_stmt):
tobj = Tag(self.file, name=u"BREAK", tab=self.tab)
keyword = u"நிறுத்து" #EzhilToken.Keywords["break"]
return
def visit_continue_stmt(self, cont_stmt):
tobj = Tag(self.file, name=u"CONTINUE", tab=self.tab)
keyword = u"தொடர்" #EzhilToken.Keywords["continue"]
return
def visit_else_stmt(self, else_stmt):
tobj = Tag(self.file, name=u"ELSE", tab=self.tab)
self.incr()
keyword = u"இல்லை"
else_stmt.stmt.visit(self)
self.decr()
return
def visit_if_elseif_stmt(self, if_elseif_stmt):
tobj = Tag(self.file, name=u"IF", tab=self.tab)
self.incr()
# condition expression
if_elseif_stmt.expr.visit(self)
# IF kw
keyword_if = u"ஆனால்"
# True-Body
if_elseif_stmt.body.visit(self)
# False-Body - optionally present
if hasattr(if_elseif_stmt.next_stmt, 'visit'):
tobj_else = Tag(self.file, name=u"ELSE", tab=self.tab)
self.incr()
if_elseif_stmt.next_stmt.visit(self)
del tobj_else
self.decr()
self.visit_end_kw()
self.decr()
def visit_end_kw(self):
# END kw
#tobj = Tag(name="END",tab=self.tab)
keyword_end = u"முடி"
def visit_while_stmt(self, while_stmt):
"""
@( itr < L ) வரை
சமம்= சமம் + input[itr]*wts[itr]
itr = itr + 1
முடி"""
tobj_while = Tag(self.file, name="WHILE", tab=self.tab)
self.incr()
# condition expression
tobj_while_cond = Tag(self.file, name=u"WHILE_COND", tab=self.tab)
self.incr()
while_stmt.expr.visit(self)
self.decr()
del tobj_while_cond
# While kw
keyword_while = u"வரை"
# Body
while_stmt.body.visit(self)
self.visit_end_kw()
self.decr()
return
# foreach is transformed at the AST-level
# so its really a MACRO here
def visit_for_stmt(self, for_stmt):
"""
@( x = -1 , x < 0, x = x + 1 ) ஆக
பதிப்பி x, "கருவேபில"
முடி
"""
tobj_for = Tag(self.file, name=u"FOR", tab=self.tab)
self.incr()
# condition expression
tobj_for_init = Tag(self.file, name=u"FOR_INIT", tab=self.tab)
for_stmt.expr_init.visit(self)
del tobj_for_init
tobj_for_cond = Tag(self.file, name=u"FOR_COND", tab=self.tab)
for_stmt.expr_cond.visit(self)
del tobj_for_cond
tobj_for_update = Tag(self.file, name=u"FOR_UPDATE", tab=self.tab)
for_stmt.expr_update.visit(self)
del tobj_for_update
# For kw
keyword_for = u"ஆக"
# Body
for_stmt.body.visit(self)
self.visit_end_kw()
self.decr()
return
def visit_assign_stmt(self, assign_stmt):
tobj_assign = Tag(self.file, name=u"ASSIGN", tab=self.tab)
self.incr()
tobj_assign_lval = Tag(self.file, name=u"ASSIGN_LVAL", tab=self.tab)
self.incr()
assign_stmt.lvalue.visit(self)
self.decr()
del tobj_assign_lval
tobj_assign_rval = Tag(self.file, name=u"ASSIGN_RVAL", tab=self.tab)
self.incr()
assign_stmt.rvalue.visit(self)
self.decr()
del tobj_assign_rval
self.decr()
return
def visit_print_stmt(self, print_stmt):
tobj = Tag(self.file, name=u"PRINT", tab=self.tab)
keyword = u"பதிப்பி"
self.incr()
print_stmt.exprlst.visit(self)
self.decr()
return
def visit_eval_stmt(self, eval_stmt):
tobj = Tag(self.file, name=u"EVALSTMT", tab=self.tab)
self.incr()
eval_stmt.expr.visit(self)
self.decr()
return
def visit_arg_list(self, arg_list):
tobj = Tag(self.file, name=u"ARGLIST", tab=self.tab)
self.incr()
L = len(arg_list.get_list())
for pos, arg in enumerate(arg_list.get_list()):
if hasattr(arg, 'visit'):
arg.visit(self)
self.decr()
return
def visit_value_list(self, value_list):
for value in value_list.args:
value.visit(self)
return
def visit_function(self, fndecl_stmt):
"""
நிரல்பாகம் fibonacci_தமிழ்( x )
@( x <= 1 ) ஆனால்
ஈ = 1
இல்லை
ஈ = fibonacci_தமிழ்( x - 1 ) + fibonacci_தமிழ்( x - 2 )
முடி
பின்கொடு ஈ
முடி
"""
tobj = Tag(self.file,
name=u"FUNCTION",
tab=self.tab,
attrs={u"name": fndecl_stmt.name})
# Function kw
keyword_fn = u"நிரல்பாகம்"
# name of function
# arglist expression
fndecl_stmt.arglist.visit(self)
# Body expression
fndecl_stmt.body.visit(self)
return
def visit_binary_expr(self, binexpr):
self.visit_expr(binexpr)
return