-
Notifications
You must be signed in to change notification settings - Fork 1
/
constants.py
307 lines (227 loc) · 7.22 KB
/
constants.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
Phi - Programmation Heuristique Interface
constants.py - Contains all the constants and datatypes used in the program
----------------
Author: Tanay Kar
----------------
"""
import typing
r_ID = r"[a-zA-Z_][a-zA-Z0-9_]*"
r_Num = r"(\d+)\.?(\d+)*"
r_Assign = r"="
r_Comma = r"\,"
r_Plus = r"\+"
r_Minus = r"-"
r_Mult = r"\*"
r_Div = r"/"
r_Dot = r"\."
r_Caret = r"\^"
r_lParen = r"\("
r_rParen = r"\)"
r_lBrace = r"\{"
r_rBrace = r"\}"
r_Colon = r"\:"
# Keywords
keywords = {
"func": "FUNC",
"print": "PRINT",
"show": "SHWTBL",
"return": "RETURN",
"plot": "PLT",
"solve": "SOLVE",
"integrate": "INTG",
"wrt": "WRT",
"from": "FROM",
"to": "TO",
"for": "FOR",
}
# multiline definitions end with '{' and inline definitions are self-contained
FuncMode = typing.Literal["multiline", "inline"]
# Tokens
class Token:
def __init__(self, type, value=None):
self.type = type
self.value = value if value else type
if self.type == "ID" or self.type == "NUMBER":
self.base_type = "VARIABLE"
def __str__(self):
return (
f"< Token {self.type} :: '{self.value}' >"
if self.value
else f"< Token {self.type} >"
)
def __repr__(self):
return self.__str__()
class TupleToken(Token):
def __init__(self):
super().__init__(type="TUPLE")
self.variables = []
self.values = []
def add(self, token):
self.values.append(str(token))
self.variables.append(token)
def __str__(self):
return f"< Tuple :: {' ,'.join(self.values)} >"
# Nodes
class BinOpNode:
def __init__(self, left, operator, right):
self.type = "BINOP"
self.left = left
self.operator = operator
self.right = right
def __str__(self) -> str:
return f"({self.left} {self.operator} {self.right})"
def __repr__(self) -> str:
return self.__str__()
class FactorNode:
def __init__(self, value, sign="+"):
self.type = "FACTOR"
if sign == "-":
self.value = value.value
else:
self.value = value
self.sign = sign
def __str__(self) -> str:
return f"<FACTOR {self.value} sign={self.sign}>"
def __repr__(self) -> str:
return self.__str__()
class ExpressionNode:
def __init__(self, expression, type_hint=None):
self.type = "EXPRESSION"
self.base_type = "VARIABLE"
self.expression = expression
# Used for a special case when the expression is a single number
self.type_hint = type_hint
self.value = self.expression
def __str__(self) -> str:
return f"< Expression {self.expression}>"
def __repr__(self) -> str:
return self.__str__()
class DeclarationNode:
def __init__(self, func: Token, args: TupleToken):
self.type = "FUNCTION"
self.base_type = "VARIABLE"
self.function_name = func.value
self.args = args
self.value = self.function_name, self.args.values
def __str__(self):
return f"< Function {self.function_name} args {self.args.variables}>"
def __repr__(self):
return self.__str__()
class LineNode:
def __init__(self, tokens: list, specid: str, primarykeyword: str, grammar: dict):
self.type = "LINE"
self.tokens = tokens
self.grammar = grammar
self.mastergrammar = specid
self.primarykeyword = primarykeyword
self.function = self.grammar[self.primarykeyword]["function"]
def __str__(self) -> str:
return f"[LINE grammar {self.mastergrammar[0]}:{self.mastergrammar[0:]} <Contents{self.tokens}>]"
def __repr__(self) -> str:
return self.__str__()
# Instruction Blocks
class AssignmentBlock:
def __init__(self, variable, value) -> None:
self.type = "ASSIGN"
self.variable = variable
self.value = value
def __str__(self) -> str:
return f"[Assignment Block {self.variable} = {self.value}]"
def __repr__(self) -> str:
return self.__str__()
class EquationBlock:
def __init__(self, name, lhs, rhs) -> None:
self.type = "EQUATION"
self.name = name.value
self.lhs = lhs
self.rhs = rhs
def __str__(self) -> str:
return f"[Equation Block {self.name} {self.lhs} = {self.rhs}]"
def __repr__(self) -> str:
return self.__str__()
class FunctionDeclarationBlock:
def __init__(self, function, mode: FuncMode, commands=None) -> None:
self.type = "FUNCDCLR"
self.function = function
self.mode = mode
self.commands = commands if commands else []
def __str__(self) -> str:
return f"[Function Declaration Block {self.function}{self.commands if self.commands else ''}]"
def __repr__(self) -> str:
return self.__str__()
class PrintBlock:
def __init__(self, expression) -> None:
self.type = "PRINT"
self.expression = expression
def __str__(self) -> str:
return f"[Print Block {self.expression}]"
def __repr__(self) -> str:
return self.__str__()
class ReturnBlock:
def __init__(self, expression) -> None:
self.type = "RETURN"
self.expression = expression
def __str__(self) -> str:
return f"[Return Block {self.expression}]"
def __repr__(self) -> str:
return self.__str__()
class EndFuncBlock:
def __init__(self) -> None:
self.type = "ENDFUNC"
pass
def __str(self) -> str:
return f"[EndFunc Block]"
def __repr__(self) -> str:
return self.__str__()
class ShowTableBlock:
def __init__(self, function, num=None) -> None:
self.type = "SHWTBL"
self.function = function
self.num = num
def __str__(self) -> str:
return f"[ShowTable Block {self.function} {self.num if self.num else ''}]"
def __repr__(self) -> str:
return self.__str__()
class SolveBlock:
def __init__(self, function) -> None:
self.type = "SOLVE"
self.function = function
def __str__(self) -> str:
return f"[Solve Block {self.function}]"
def __repr__(self) -> str:
return self.__str__()
class EquationSolveBlock:
def __init__(self, eq , var) -> None:
self.type = "EQSOLVE"
self.eq = eq
self.var = var
def __str__(self) -> str:
return f"[Equation Solve Block {self.eq} {self.var}]"
def __repr__(self) -> str:
return self.__str__()
class PlotBlock:
def __init__(self, function) -> None:
self.type = "PLOT"
self.function = function
def __str__(self) -> str:
return f"[Plot Block {self.function}]"
def __repr__(self) -> str:
return self.__str__()
class IntegrateBlock:
def __init__(self, function, var, limits=None, plot=False) -> None:
self.type = "INTEGRATE"
self.function = function
self.var = var
self.to_plot = plot
self.limits = limits
if limits:
self.definite = True
else:
self.definite = False
def __str__(self) -> str:
return (
f"[Integrate Block {self.function} {self.limits if self.definite else ''}]"
)
def __repr__(self) -> str:
return self.__str__()