-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecInterpreter.py
322 lines (264 loc) · 9.01 KB
/
recInterpreter.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
import AST
import copy
from svgwrite import text, shapes, drawing
from AST import addToClass
from functools import reduce
from lex import reserved_words
class Transformation:
def __init__(self, type):
self.type = type
self.arguments = {}
def __setitem__(self, key, value):
self.arguments[key] = value
def add_arguments(self, arguments):
for key in arguments:
self[key] = arguments[key]
def __getitem__(self, item):
return self.arguments[item]
operator = {
'+': lambda x, y: x+y,
'-': lambda x, y: x-y,
'*': lambda x, y: x*y,
'/': lambda x, y: x/y,
'%': lambda x, y: x%y,
}
conditional_operator = {
'==': lambda x, y: x == y,
'<=': lambda x, y: x <= y,
'>=': lambda x, y: x >= y,
'<': lambda x, y: x < y,
'>': lambda x, y: x > y,
}
vars = {}
dwg = None
step = 0
@addToClass(AST.ProgramNode)
def execute(self):
for c in self.children:
c.execute()
@addToClass(AST.TokenNode)
def execute(self):
if isinstance(self.tok, str) and not self.real_string:
if self.tok.upper() == 'STEP':
return step
try:
return vars[self.tok]
except KeyError:
if self.tok in reserved_words:
return self.tok
else:
print("Error: variable %s undefined!" % self.tok)
return self.tok
@addToClass(AST.OpNode)
def execute(self):
args = [c.execute() for c in self.children]
if len(args) == 1:
args.insert(0,0)
return reduce(operator[self.op], args)
@addToClass(AST.AssignNode)
def execute(self):
vars[self.children[0].tok] = self.children[1].execute()
@addToClass(AST.WhileNode)
def execute(self):
while self.children[0].execute():
self.children[1].execute()
@addToClass(AST.IfNode)
def execute(self):
if self.children[0].execute():
self.children[1].execute()
@addToClass(AST.ForNode)
def execute(self):
for x in range(self.children[0].execute(), self.children[1].execute()):
self.children[2].execute()
global step
step = x
@addToClass(AST.ApplyNode)
def execute(self):
transformation = self.children[0].execute()
for shape in self.children[1].execute():
if transformation.type.upper() == 'ROTATE':
shape.rotate(transformation['angle'], (transformation['c']['x'], transformation['c']['y']))
elif transformation.type.upper() == 'SCALE':
shape.scale(transformation['sx'], transformation['sy'])
elif transformation.type.upper() == 'TRANSLATE':
shape.translate(transformation['p'][0]['x'], transformation['p'][0]['y'])
elif transformation.type.upper() == 'HIDE':
shape['visibility'] = 'hidden' if transformation['h'] else 'visible'
dwg.add(shape.copy())
@addToClass(AST.ArgumentNode)
def execute(self):
if len(self.children) > 1:
args = [c.execute() for c in self.children]
else:
args = self.children[0].execute()
return [self.key, args]
@addToClass(AST.ArgumentsNode)
def execute(self):
arguments = {}
for argument in self.children:
if argument.execute()[0] == 'p':
if 'p' in arguments:
arguments[argument.execute()[0]].append(argument.execute()[1])
else:
arguments[argument.execute()[0]] = [argument.execute()[1]]
else:
arguments[argument.execute()[0]] = argument.execute()[1]
return arguments
@addToClass(AST.ShapeNode)
def execute(self):
shape_type = self.children[0].execute()
arguments = copy.deepcopy(self.children[1].execute())
if 'fill_color' in arguments:
arguments['fill'] = arguments.pop('fill_color')
if 'border_width' in arguments:
arguments['stroke-width'] = arguments.pop('border_width')
if 'border_color' in arguments:
arguments['stroke'] = arguments.pop('border_color')
if 'c' in arguments:
center = arguments.pop('c')
arguments['cx'] = center.pop('x')
arguments['cy'] = center.pop('y')
if 'size' in arguments:
arguments['font-size'] = arguments.pop('size')
if 'font' in arguments:
arguments['font-family'] = arguments.pop('font')
shape = None
if shape_type.upper() == 'LINE':
shape = shapes.Line()
if 'width' in arguments:
arguments['stroke-width'] = arguments.pop('width')
if 'fill' in arguments:
arguments['stroke'] = arguments.pop('fill')
if 'p1' in arguments:
p1 = arguments.pop('p1')
arguments['x1'] = p1.pop('x')
arguments['y1'] = p1.pop('y')
if 'p2' in arguments:
p1 = arguments.pop('p2')
arguments['x2'] = p1.pop('x')
arguments['y2'] = p1.pop('y')
print(arguments)
elif shape_type.upper() == 'CIRCLE':
shape = shapes.Circle()
elif shape_type.upper() == 'RECT':
if 'o' in arguments:
center = arguments.pop('o')
arguments['x'] = center.pop('x')
arguments['y'] = center.pop('y')
shape = shapes.Rect()
elif shape_type.upper() == 'ELLIPSE':
shape = shapes.Ellipse()
elif shape_type.upper() == 'CUSTOMSHAPE':
points = []
for point in arguments.pop('p'):
points.append((point.pop('x'), point.pop('y')))
if 'close' in arguments and arguments.pop('close') == True:
shape = shapes.Polygon(points)
else:
shape = shapes.Polyline(points)
elif shape_type.upper() == 'TEXT':
shape = text.Text(arguments.pop('content'))
if 'p' in arguments:
center = arguments.pop('p')[0]
arguments['x'] = center.pop('x')
arguments['y'] = center.pop('y')
else:
print("Error: shape type %s undefined!" % shape_type)
shape.update(arguments)
return shape
@addToClass(AST.PointNode)
def execute(self):
arguments = self.children[0].execute()
return {'x': arguments["x"], 'y': arguments["y"]}
@addToClass(AST.ColorNode)
def execute(self):
arguments = self.children[1].execute()
type = arguments[0].execute().upper()
if type == 'NAME':
return arguments[1].execute()
elif type == 'HEX':
color = arguments[1].execute()
if color[0:1] == '#':
return '%s' % arguments[1].execute()
else:
return '#%s' % arguments[1].execute()
elif type == 'RGB':
return "rgb(%d,%d,%d)" % (int(arguments[1][0].execute()), int(arguments[1][1].execute()), int(arguments[1][2].execute()))
@addToClass(AST.TransformationNode)
def execute(self):
transformation = Transformation(self.children[0].tok)
transformation.add_arguments(self.children[1].execute())
return transformation
@addToClass(AST.ApplyBodyNode)
def execute(self):
shapes = []
for shape in self.children:
shapes.append(shape.execute())
return shapes
@addToClass(AST.ConditionalNode)
def execute(self):
if self.nbargs == 0:
return self.children[0].execute()
else:
args = [c.execute() for c in self.children]
return reduce(conditional_operator[self.operator], args)
def displayMenu(files):
examples = ' | '.join(files)
print('')
print('')
print('-----------------------------------------------')
print('--------- Welcome -- PNP Compiler -------------')
print('-----------------------------------------------')
print('-------------- Alexandre Perez & Mirco Nasuti -')
print('')
print('Compile a PNP file by typing: compile <filepath>')
print('Exit by typing: exit')
print('')
print('You can try some examples by typing : ')
print('compile examples/'+examples)
def compile(filepath):
import os
if not os.path.exists(filepath):
print("File not found")
return
global dwg
filename = os.path.basename(filepath)[:-4]
print('start compiling: ' + filename)
prog = open(filepath).read()
vars = {}
dwg = drawing.Drawing(filename=filename+'.svg', debug=False, profile='tiny')
ast = parse(prog)
ast.execute()
dwg.save()
print('Compiled and saved in: ' + filename + '.svg')
# http://stackoverflow.com/a/2605125
class Discarder(object):
def write(self, text):
pass # do nothing
if __name__ == "__main__":
import sys
# discard output
oldstdout = sys.stdout
oldstderr = sys.stderr
sys.stdout = Discarder()
sys.stderr = Discarder()
from parse import parse
sys.stdout = oldstdout
sys.stderr = oldstderr
if len(sys.argv) > 1:
compile(sys.argv[1])
sys.exit(0)
path = 'examples/'
ext = 'pnp'
from os import listdir
from os.path import isfile, join
files = [ f for f in listdir(path) if isfile(join(path,f)) and f[-3:].upper() == ext.upper() ]
inputValue = ''
while not inputValue.__eq__('exit'):
displayMenu(files)
inputValue = input('PNP > ')
if inputValue.startswith('compile '):
try:
compile(inputValue[8:])
except:
print("Error:", sys.exc_info()[0])