-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantics.py
202 lines (168 loc) · 5.31 KB
/
semantics.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
# Author : Macauley Scullion
#Import files
import symtable as sym
import errorhandling as err
from parseTreeGeneration import Node
# Node evaluation function - will evaluate the root node or passed node through a swither and call function, require bool for type check loop then setting loop
def eval(node, line):
switcher = {
"Oassign": equal,
"OAdd": add,
"OSub": sub,
"OMulti": mult,
"ODivide": div,
"Identifier": var,
"KeywordInt": var,
"KeywordSTRING": var,
"Integer": const,
"String": const,
"KeywordIF": ifstate,
"KeywordPRINT": printstate,
"Comma": comma,
"End_of_File": eof
}
function = switcher[node.type]
return function(node, line)
# EOF function (dummy function to prevent KeyError in the switcher)
def eof(node, line):
return
# Equal function - evals right side, assigns left return value from right eval
def equal(node, line):
print("\t Equal node")
#Check node type for keywords, if not - lookup left node value
if (node.lhn.type == "KeywordInt"):
#print("key int")
l = check_lookup(node.lhn.rhn, line)
elif (node.lhn.type == "KeywordSTRING"):
#print("key str")
l = check_lookup(node.lhn.rhn, line)
else:
#print("else")
l = check_lookup(node.lhn, line)
r = eval(node.rhn, line)
type_check_assign(node, r, line)
sym.set_attribute(l,r)
return
# Add function - returns left and right addition
def add(node, line):
print("\t Add node")
l = eval(node.lhn, line)
r = eval(node.rhn, line)
#type check
type_check_sum(l, r, line)
return l + r
# Sub function - returns left and right subtraction
def sub(node, line):
print("\t Sub node")
l = eval(node.lhn, line)
r = eval(node.rhn, line)
#type check
type_check_sum(l, r, line)
return l - r
# mult function - returns left and right multiplication
def mult(node, line):
print("\t Multi node")
l = eval(node.lhn, line)
r = eval(node.rhn, line)
#type check
type_check_sum(l, r, line)
return l * r
# div function - returns left and right division
def div(node, line):
print("\t Div node")
l = eval(node.lhn, line)
r = eval(node.rhn, line)
#type check
type_check_sum(l, r, line)
return int(l / r)
# var function - returns variable value after lookup
def var(node, line):
print("\t Variable node")
var = check_lookup(node, line)
#tyep check
#if type is int convert to int
if (var.type == 'Int'):
return int(var.value)
#if not leave as str
else:
return var.value
# const function - returns the node value
def const(node, line):
print("\t Constant node")
#return value
return node.value
# comma function - evaluates left and right of print punctuation
def comma(node, line):
l = eval(node.lhn, line)
r = eval(node.rhn, line)
return
# If function - used to get child nodes of comparison
def ifstate(node, line):
print("\t If node")
if (node.rhn.type == "OperationEqual"):
l = eval(node.rhn.lhn, line)
r = eval(node.rhn.rhn, line)
type_check_if(l,r, line)
return
else:
err.errorifcompare(line)
# Print function - used to evaluated node within print function
def printstate(node, line):
print("\t Print node")
r = eval(node.rhn, line)
return
# Type check sums, 'left' operator 'right'
def type_check_sum (left, right, line):
if (isinstance(left, int) and isinstance(right, int)):
return
else:
#If anything other Int's are summed
err.errornodetypesum(line)
# Type checking assignments, 'left' equals 'right'
def type_check_assign(node, right, line):
if (node.lhn.type == "KeywordInt"):
#print("key int")
l = node.lhn.rhn
elif (node.lhn.type == "KeywordSTRING"):
#print("key str")
l = node.lhn.rhn
else:
#print("else")
l = node.lhn
if (l.type == "Identifier"):
if (check_lookup(l, line) != False):
l = check_lookup(l, line)
#print("sym grabbed")
if ((l.type == "Int") and (isinstance(right, int))):
#print("both int")
return
elif (l.type == "String" and isinstance(right, str)):
#print("both str")
return
else:
err.errornodetypeassign(line)
else:
err.errornodetypeassign(line)
# Type check if - function which checks if if statement both match
def type_check_if(left, right, line):
if (isinstance(left, int) and isinstance(right, int)):
return
if (isinstance(left, str) and isinstance(right, str)):
return
else:
#add error explain only ints can be added
err.errorifnodes(line)
# Check lookup - function returns symbol if found, if not it'll run an error
def check_lookup(node, line):
if (sym.lookup(node.value) != False):
symbol = sym.lookup(node.value)
line_check(symbol, line)
return symbol
else:
err.errornotdeclared(node.value, line)
# Line check - checks if the variable is used before its declared
def line_check(symbol, line):
if (line < symbol.line):
err.errorbeforedeclared(symbol.name, line)
else:
return