-
Notifications
You must be signed in to change notification settings - Fork 0
/
oaf_expr.py
85 lines (77 loc) · 3.44 KB
/
oaf_expr.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
import oaf_state as state
import oaf_quad as quad
import oaf_sem as sem
# ID found
def add_operand(operand):
state.operand_stack.append(operand)
def add_operator(operator):
state.operator_stack.append(operator)
if(operator == '#'):
state.last_operator = None
else:
state.last_operator = state.operator_stack[-1]
def pop_operator():
state.operator_stack.pop()
if(len(state.operator_stack) > 0):
state.last_operator = state.operator_stack[-1]
else:
state.last_operator = None
def push_expr():
state.operator_stack.append('#')
state.last_operator = None
def pop_expr():
state.operator_stack.pop()
if(len(state.operator_stack) > 0):
state.last_operator = state.operator_stack[-1]
else:
state.last_operator = None
def set_assign_type(type, ops):
if(not state.arr_parsing and not state.func_parsing):
for x in range(ops):
state.assign_list.pop()
state.assign_list.append(type)
def generate_quad(hierarchy):
q = quad.Quad()
if(hierarchy == 0):
if(state.last_operator == 'u+' or state.last_operator == 'u-' or state.last_operator == 'u!'):
q.set_quad(state.operator_stack.pop(), None, state.operand_stack.pop(), "t" + str(state.temp_counter))
set_assign_type(q.result[1][0], 1)
state.temp_counter += 1
elif(hierarchy == 1):
if(state.last_operator == '*' or state.last_operator == '/'):
# id, [type, address, size]
q.set_quad(state.operator_stack.pop(), state.operand_stack.pop(), state.operand_stack.pop(), "t" + str(state.temp_counter))
set_assign_type(q.result[1][0], 2)
state.temp_counter += 1
elif(hierarchy == 2):
if(state.last_operator == '+' or state.last_operator == '-'):
q.set_quad(state.operator_stack.pop(), state.operand_stack.pop(), state.operand_stack.pop(), "t" + str(state.temp_counter))
set_assign_type(q.result[1][0], 2)
state.temp_counter += 1
elif(hierarchy == 3):
if(state.last_operator == '==' or state.last_operator == '<=' or state.last_operator == '>=' or state.last_operator == '<>' or state.last_operator == '<' or state.last_operator == '>'):
q.set_quad(state.operator_stack.pop(), state.operand_stack.pop(), state.operand_stack.pop(), "t" + str(state.temp_counter))
set_assign_type(q.result[1][0], 2)
state.temp_counter += 1
elif(hierarchy == 4):
if(state.last_operator == '&&' or state.last_operator == '||'):
q.set_quad(state.operator_stack.pop(), state.operand_stack.pop(), state.operand_stack.pop(), "t" + str(state.temp_counter))
set_assign_type(q.result[1][0], 2)
state.temp_counter += 1
elif(hierarchy == 5):
if(state.last_operator == '='):
q.set_quad(state.operator_stack.pop(), None, state.operand_stack.pop(), state.operand_stack.pop())
set_assign_type(sem.get_type("=", ["", [state.assign_list[0]]], ["", [q.result[1][0]]]), 1)
if(q.operator != None):
state.quads.append(q)
if(q.operator != "="):
state.operand_stack.append(q.result)
#state.assign_list.append(q.result[1][0])
else:
del(q)
if(len(state.operator_stack) > 0):
state.last_operator = state.operator_stack[-1]
if(state.last_operator == '#'):
state.last_operator = None
else:
state.last_operator = None