-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyser.py
144 lines (136 loc) · 5.58 KB
/
analyser.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
'''
Phi - Programmation Heuristique Interface
analyser.py - Analyser for Phi
----------------
Author: Tanay Kar
----------------
'''
import constants as const
class SpecificAnalyser:
def __init__(self,ast):
self.ast = ast
self.specicific_ast = []
self.analyse()
def analyse(self):
'''Segregates the AST into different blocks based on the master grammar'''
for i in self.ast:
match i.mastergrammar[0]:
case 'A':
# Assignment + Equation
if i.mastergrammar == 'A01':
self.specicific_ast.append(const.AssignmentBlock(
variable=i.tokens[0],
value=i.tokens[2]
))
elif i.mastergrammar == 'A02':
self.specicific_ast.append(const.EquationBlock(
name=i.tokens[0],
lhs=i.tokens[2],
rhs=i.tokens[4],
))
else:
# Incase I add anymore grammer rules to 'A'
pass
case 'B':
# Multiline Function Declaration
if i.mastergrammar in ('B01', 'B02','B03'):
self.specicific_ast.append(const.FunctionDeclarationBlock(
function=i.tokens[1],
commands=i.tokens[3],
mode='inline',
))
else:
self.specicific_ast.append(const.FunctionDeclarationBlock(
function=i.tokens[1],
mode='multiline',
))
case 'C':
# Inline Function Declaration
self.specicific_ast.append(const.FunctionDeclarationBlock(
function=i.tokens[0],
commands=i.tokens[2],
mode='inline',
))
case 'D':
# Print
self.specicific_ast.append(const.PrintBlock(
expression=i.tokens[1],
))
case 'E':
# Return
self.specicific_ast.append(const.ReturnBlock(
expression=i.tokens[1],
))
case 'F':
# End Function
self.specicific_ast.append(const.EndFuncBlock())
case 'G':
# Show Table
if i.mastergrammar == 'G01':
self.specicific_ast.append(const.ShowTableBlock(
function=i.tokens[1],
))
elif i.mastergrammar == 'G02':
self.specicific_ast.append(const.ShowTableBlock(
function=i.tokens[2],
num=i.tokens[1],
))
else:
# Incase I add anymore grammer rules to 'G'
pass
case 'H':
# Plot
self.specicific_ast.append(const.PlotBlock(
function=i.tokens[1],
))
case 'I':
# Solve
if i.mastergrammar == 'I01':
self.specicific_ast.append(const.SolveBlock(
function=i.tokens[1],
))
elif i.mastergrammar == 'I02':
self.specicific_ast.append(const.EquationSolveBlock(
eq=i.tokens[1],
var=i.tokens[3],
))
else:
# Incase I add anymore grammer rules to 'I'
pass
case 'J':
# Integrate
if i.mastergrammar == 'J01':
self.specicific_ast.append(const.IntegrateBlock(
function=i.tokens[1],
var=i.tokens[3],
))
elif i.mastergrammar == 'J02':
self.specicific_ast.append(const.IntegrateBlock(
function=i.tokens[1],
var=i.tokens[3],
plot=True,
))
elif i.mastergrammar == 'J03':
self.specicific_ast.append(const.IntegrateBlock(
function=i.tokens[1],
var=i.tokens[3],
limits=[i.tokens[5],i.tokens[7]],
))
elif i.mastergrammar == 'J04':
self.specicific_ast.append(const.IntegrateBlock(
function=i.tokens[1],
var=i.tokens[3],
limits=[i.tokens[5],i.tokens[7]],
plot=True,
))
else:
# Incase I add anymore grammer rules to 'J'
pass
if __name__ == '__main__':
from read import read_file
ast = read_file('main.phi')
analyser = SpecificAnalyser(ast)
analyser.analyse()
for i in analyser.specicific_ast:
print(i)
print()