-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueExpression.py
More file actions
189 lines (173 loc) · 5.13 KB
/
ValueExpression.py
File metadata and controls
189 lines (173 loc) · 5.13 KB
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
from Helpers import *
from IntLit import *
from Var import *
import os
class ValueExpression:
def __init__(self, path):
self.path = path
self.op = BIN_OPS[path.split('/')[-1]]
self.rands = []
self.walk()
def walk(self):
children = os.listdir(self.path)
children.sort()
for child in children:
fullpath = self.path + '/' + child
child = os.listdir(fullpath)[0]
fullpath = fullpath + '/' + child
if isIntLit(child):
self.rands.append(IntLit(fullpath))
elif isValueExpression(child):
self.rands.append(ValueExpression(fullpath))
elif isFuncCall(child):
self.rands.append(FuncCall(fullpath))
elif isVar(child):
self.rands.append(Var(fullpath))
def __str__(self):
return "(%s %s %s)" % (self.rands[0], self.op, self.rands[1])
class FuncCall:
def __init__(self, path):
self.path = path
self.functor = path.split('/')[-1][:-2]
self.args = []
self.walk()
self.toStrings = {
'ifelse':self.ifelseStr,
}
def walk(self):
children = os.listdir(self.path)
children.sort()
for child in children:
fullpath = self.path + '/' + child
child = os.listdir(fullpath)[0]
fullpath = fullpath + '/' + child
if isIntLit(child):
self.args.append(IntLit(fullpath))
elif isValueExpression(child):
self.args.append(ValueExpression(fullpath))
elif isFuncCall(child):
self.args.append(FuncCall(fullpath))
elif isVar(child):
self.args.append(Var(fullpath))
elif isListLit(child):
self.args.append(ListLit(fullpath))
elif isLambda(child):
self.args.append(Lambda(fullpath))
def ifelseStr(self):
code = str(self.args[1]) + ' if '
code += str(self.args[0]) + ' else '
code += str(self.args[2])
return code
def definedStr(self):
code = self.functor + '('
code += ', '.join([str(e) for e in self.args]) + ')'
return code
def __str__(self):
return self.toStrings.get(self.functor, self.definedStr)()
class Definition:
def __init__(self, path):
self.path = path
parts = path.split('/')[-1].split('_')
self.name = parts[0].lstrip(':')
self.formals = parts[1:]
self.cache = self.name + '.d'
self.walk()
def walk(self):
child = os.listdir(self.path)[0]
fullpath = self.path + '/' + child
if isIntLit(child):
self.body = IntLit(fullpath)
elif isValueExpression(child):
self.body = ValueExpression(fullpath)
elif isFuncCall(child):
self.body = FuncCall(fullpath)
elif isVar(child):
self.body = Var(fullpath)
elif isListLit(child):
self.body = ListLit(fullpath)
def __str__(self):
formalstring = '(' + ','.join(self.formals)
if len(self.formals) > 0:
formalstring += ','
formalstring += ')'
code = 'def ' + self.name
code += formalstring + ': \n'
code += '\ttry:\n'
code += '\t\tif not ' + formalstring + ' in ' + self.cache + ':\n\t\t\t'
code += self.cache + '[' + formalstring + '] = ' + str(self.body) + '\n\t'
code += 'except:\n\t\t'
code += 'return ' + str(self.body) + '\n\t'
code += 'return ' + self.cache + '[' + formalstring + ']\n'
code += self.cache + ' = {}'
return code
class ListLit:
def __init__(self, path):
self.path = path
self.elems = []
self.walk()
def walk(self):
children = [int(c) for c in os.listdir(self.path)]
children.sort()
children = [str(c) for c in children]
for child in children:
fullpath = self.path + '/' + child
child = os.listdir(fullpath)[0]
fullpath = fullpath + '/' + child
if isIntLit(child):
self.elems.append(IntLit(fullpath))
elif isValueExpression(child):
self.elems.append(ValueExpression(fullpath))
elif isFuncCall(child):
self.elems.append(FuncCall(fullpath))
elif isVar(child):
self.elems.append(Var(fullpath))
def __str__(self):
code = '[' + ', '.join([str(e) for e in self.elems]) + ']'
return code
class Lambda:
def __init__(self, path):
self.path = path
parts = path.split('/')[-1].split('_')
self.formals = parts[1:]
self.args = []
self.walk()
def walk(self):
children = os.listdir(self.path)
children.sort()
self.isCall = True if len(children) == 2 else False
fullpath = self.path + '/' + children[0]
child = os.listdir(fullpath)[0]
fullpath = fullpath + '/' + child
if isIntLit(child):
self.body = IntLit(fullpath)
elif isValueExpression(child):
self.body = ValueExpression(fullpath)
elif isFuncCall(child):
self.body = FuncCall(fullpath)
elif isVar(child):
self.body = Var(fullpath)
elif isListLit(child):
self.body = ListLit(fullpath)
if self.isCall:
fullpath = self.path + '/' + children[1]
children = os.listdir(fullpath)
children.sort()
for child in children:
path = fullpath + '/' + child
child = os.listdir(path)[0]
path = path + '/' + child
if isIntLit(child):
self.args.append(IntLit(path))
elif isValueExpression(child):
self.args.append(ValueExpression(path))
elif isFuncCall(child):
self.args.append(Var(child))
self.args.append(Var(path))
elif isListLit(child):
self.args.append(ListLit(path))
def __str__(self):
formalstring = ','.join(self.formals)
code = '(lambda ' + formalstring + ' : ' + str(self.body) + ')'
if self.isCall:
code += '(' + ','.join([str(arg) for arg in self.args]) + ')'
return code