forked from tangentstorm/PL0-Language-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl0_to_dpans.py
executable file
·293 lines (244 loc) · 9.13 KB
/
pl0_to_dpans.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
#!/usr/bin/env python
#
# Copyright (c) 2012 Michal J Wallace. <http://www.michaljwallace.com/>
# Copyright (c) 2012 Charles R Childers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
"""
This translates the pl0 syntax tree to the equivalent
representation in retroforth ( http://retroforth.org/ )
"""
from pl0_node_visitor import StackingNodeVisitor
import sys
import pl0_parser
import StringIO
import os
import types
# AST->retro translator for operators
ops = {
'DIVIDE' : '/', # integer div
'MODULO' : 'mod',
'TIMES' : '*',
'PLUS' : '+',
'MINUS' : '-',
}
rel_ops = {
'LT' : '<',
'LTE' : '<=',
'GT' : '>',
'GTE' : '>=',
'E' : '=',
'NE' : '<>',
}
UNKNOWN, VAR, CONST, PROCEDURE = range(4)
CATEGORY = "UNKNOWN VAR CONST PROCEDURE".split()
class RetroTranspiler(StackingNodeVisitor):
stacksize = 256 # cells
def __init__(self):
super(RetroTranspiler, self)
# negative signs are not included in number tokens,
# because a lexer cannot distinguish the "-1" in
# the expression "x-1" from "-1" as a negative numer.
#
# the parser does this as part of the "expression"
# rule, so "-1" is parsed as "-(1)". As an optimization,
# we set the following flag in accept_expression when
# we can see that the value between the parens is a
# numeric literal. Then we can emit a negative literal
# rather than emittting code to multiply the positive
# literal by -1.
self.negate = False
self.name_op = "@"
self.proc_path = [] # for nested procedure defs
self.local_defs = {} # symbol table for top level / current proc
self.scope = [] # list of symbol tables for lexical scope
self.scope.append(self.local_defs)
def local_vars(self):
"""
returns a list of names of variables to preserve
when calling functions recursively
"""
return [ key for (key, (kind, _)) in self.local_defs.items()
if kind == VAR ]
def lookup(self, name):
for frame in reversed( self.scope ):
if name in frame :
return frame[ name ]
else: pass
else: raise LookupError( "name not found: %s. scope is %r"
% ( name, self.scope ))
def visit( self, node ):
"""like visit_node but works with generators"""
result = self.visit_node( node )
if isinstance( result, types.GeneratorType ):
result = "\n".join( result )
return result
#-- simple numbers -----------------
def accept_number(self, nid, value):
if self.negate:
print "-{0}".format(value),
self.negate = False
else:
sys.stdout.write(" ")
print value,
# logically, print ("!") would come much later
# but i'm putting these in implementation order,
# and i want this up front so i can see the
# results of running the code.
def accept_print(self, nid, expr):
self.visit( expr )
print ' . ',
#-- expressions --------------------
# example: a + b * c + d * e + f
# becomes: a b c * + d e * + f +
# term = factor {("*"|"/") factor}.
def accept_term( self, nid, *factors_tup ):
factors = list( factors_tup )
self.visit( factors.pop( 0 ))
for operator, operand in factors:
self.visit( operand )
print ops[ operator ],
# expression = [ "+"|"-"] term { ("+"|"-") term}.
def accept_expression(self, nid, sign, *terms_tup):
terms = list( terms_tup )
# handle negative numbers and unary minus
negate_value = False
negate_after = False
if sign == "MINUS":
assert len(terms_tup) == 1
if terms_tup[0][1][0] == "NUMBER":
self.negate = True
else:
negate_after = True
# generate the normal expression
for node in terms:
if node[0]=="TERM":
self.visit(node)
else:
operator, term = node
self.visit( term )
print ops[ operator ],
if negate_after:
print "-1 *",
#-- named constants ----------------
def accept_define(self, nid, name, value):
self.local_defs[ name ] = (CONST, value)
def accept_name(self, nid, name):
category, value = self.lookup( name )
if category == CONST:
sys.stdout.write(" {0} ".format(value))
elif category == VAR:
sys.stdout.write(' ')
sys.stdout.write(value)
sys.stdout.write(' ')
sys.stdout.write(self.name_op)
sys.stdout.write(' ')
else:
raise Exception("unhandled name: (%s:%s) . scope is: %r"
% [CATEGORY[category], value,
self.scope])
#-- named variables & assignment ---
def accept_variables(self, nid, *names):
for nid, name in names:
print "variable " + name + "\n",
self.local_defs[ name ] = (VAR, name)
def accept_set(self, nid, name, expr):
self.visit( expr )
self.name_op = "!"
self.visit( name )
self.name_op = "@"
#-- flow control -------------------
def accept_odd(self, nid, expr):
self.visit( expr )
print "2 mod 1 =",
def accept_condition(self, nid, lhs, rel, rhs):
self.visit( lhs )
self.visit( rhs )
sys.stdout.write(' ' + rel_ops[ rel ] + ' ')
def accept_if(self, nid, cond, stmt):
# retro's quotations ([..]) are high level functional constructs.
# they are nice, but incur a small extra runtime overhead.
# TODO: go back and use plain old jumps for speed
self.visit( cond )
sys.stdout.write(" if ")
self.visit( stmt )
sys.stdout.write(" then ")
def accept_while(self, nid, cond, stmt):
# retro actually doesn't have a standard "loop
# with the test at the start". the "while" word
# it offers is more like "repeat <stmt> until not <cond>"
# so we'll use quotations for now.
# essentially, we're saying:
#
# IF <cond> THEN REPEAT <stmt> UNTIL NOT <cond>;
#
# which in functional-style retro is:
#
# <cond> [ [ <stmt> <cond> ] while ] ifTrue
#
# TODO: lower level/faster WHILE implementation
print " begin ",
self.visit( cond )
print " while ",
self.visit( stmt )
print " repeat ",
#-- procedures ---------------------
def accept_program(self, nid, block):
(blocknid, procs, consts, vars, stmt) = block
self.visit_expressions([procs, consts, vars])
sys.stdout.write(": run ")
self.visit(stmt)
sys.stdout.write(" ;\n")
print "run\n"
# for recursion, we need to maintain a stack
def accept_procedure(self, nid, name, block):
(blocknid, procs, consts, vars, stmt) = block
self.proc_path.append(name)
self.scope.append(self.local_defs)
self.visit_expressions([procs, consts, vars])
sys.stdout.write(": " + name + " ")
self.visit(stmt)
sys.stdout.write(" ;\n")
self.proc_path.pop()
self.scope.pop()
self.local_defs = self.scope[-1]
def accept_call(self, nid, name):
# this detects simple recursion
#TODO: full recursion check
#TODO: tail call optimization
#TODO: only push/pop shadowed variables
recursive = name in self.proc_path
def call(): print name,
keep = self.local_vars()
if recursive:
for ident in keep:
sys.stdout.write(ident + ' @ ')
print " recurse ",
for ident in reversed( keep ):
sys.stdout.write(ident + ' ! ')
else:
call()
if __name__ == '__main__':
code = sys.stdin.read()
parser = pl0_parser.Parser()
parser.input(code)
program = parser.p_program()
compiler = RetroTranspiler()
compiler.visit_node(program)