-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.py
140 lines (113 loc) · 5.38 KB
/
interpreter.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
from ast import operator
from values import Number, Variables
from operation import conversion, addition, substraction, multiplication
def apply_minus(node):
if node['variables'] is None:
return {'constant': -node['constant'], 'variables': None}
tmp = {'constant': -node['constant'], 'variables': []}
for i in node['variables']:
tmp['variables'].append({'name': i['name'], 'coef': -i['coef'], 'variable': i['variable']})
return tmp
class Interpreter:
def __init__(self):
self.counter = True
def visit(self, node):
if type(node).__name__ == "NumberNode":
var = []
for i in node.value['variables']:
var.append(Variables(i['coef'], i['variable']))
return Number(node.value['constant'], var)
elif self.counter:
node_a = self.visit(node.node_a)
node_b = self.visit(node.node_b)
return eval(f"{conversion.conversion[type(node).__name__]}.linear(node_a, node_b)")
def visit_AddNode(self, node):
node_a = self.visit(node.node_a).value
node_b = self.visit(node.node_b).value
commun, type = self.get_commun(node_a, node_b)
if type == 0:
return Number({'constant': node_a['constant'] + node_b['constant'], 'variables': None})
elif type == 1:
return Number({'constant': node_a['constant'] + node_b['constant'], 'variables': node_a['variables']})
elif type == 2:
return Number({'constant': node_a['constant'] + node_b['constant'], 'variables': node_b['variables']})
else:
tmp = {'constant': node_a['constant'] + node_b['constant'], 'variables': []}
for c in commun[0]:
tmp['variables'].append({'name': self.get_name(c[0]['variable']), 'coef': c[0]['coef'] + c[1]['coef'],
'variable': c[0]['variable']})
for nn in commun[1]:
tmp['variables'].append(nn)
return Number(tmp)
def visit_SubtractNode(self, node):
node_a = self.visit(node.node_a).value
node_b = apply_minus(self.visit(node.node_b).value)
commun, type = self.get_commun(node_a, node_b)
if type == 0:
return Number({'constant': node_a['constant'] + node_b['constant'], 'variables': None})
elif type == 1:
return Number({'constant': node_a['constant'] + node_b['constant'], 'variables': node_a['variables']})
elif type == 2:
return Number({'constant': node_a['constant'] + node_b['constant'], 'variables': node_b['variables']})
else:
tmp = {'constant': node_a['constant'] + node_b['constant'], 'variables': []}
for c in commun[0]:
tmp['variables'].append({'name': self.get_name(c[0]['variable']), 'coef': c[0]['coef'] + c[1]['coef'],
'variable': c[0]['variable']})
for nn in commun[1]:
tmp['variables'].append(nn)
return Number(tmp)
def visit_MultiplyNode(self, node):
node_a = self.visit(node.node_a).value
node_b = apply_minus(self.visit(node.node_b).value)
commun, type = self.get_commun(node_a, node_b, False)
if type == 0:
return Number({'constant': node_a['constant'] * node_b['constant'], 'variables': None})
elif type == 1:
return Number({'constant': node_a['constant'] * node_b['constant'], 'variables': node_a['variables']})
elif type == 2:
return Number({'constant': node_a['constant'] * node_b['constant'], 'variables': node_b['variables']})
return Number(self.visit(node.node_a).value * self.visit(node.node_b).value)
def visit_DivideNode(self, node):
try:
return Number(self.visit(node.node_a).value / self.visit(node.node_b).value)
except:
raise Exception("Runtime math error")
def multiply(self, var, factor):
tmp = []
for i in var:
tmp.append({'name': var['name'], 'coef': var['coef'] * factor, 'variable': var['variable']})
return tmp
def is_the_same(self, var1, var2, tx):
if var1['name'] == var2['name']:
if not tx:
return True
for var in var1['variable'].keys():
if var1['variable'][var] != var2['variable'][var]:
return False
return True
else:
return False
def get_name(self, var):
return ''.join(var.keys())
def get_commun(self, node_a, node_b, tx=True):
if node_a['variables'] is None and node_b['variables'] is None:
return None, 0
elif node_a['variables'] is not None and node_b['variables'] is None:
return None, 1
elif node_a['variables'] is None and node_b['variables'] is not None:
return None, 2
var_1 = [variable for variable in node_a['variables']]
var_2 = [variable for variable in node_b['variables']]
same = []
not_same = []
for v in var_1:
is_equal = False
for index, c in enumerate(var_2):
if self.is_the_same(v, c, tx):
same.append((v, c))
var_2.pop(index)
is_equal = True
if not is_equal:
not_same.append(v)
return (same, not_same + var_2), 3