-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
249 lines (222 loc) · 6.21 KB
/
main.py
File metadata and controls
249 lines (222 loc) · 6.21 KB
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
from hulk.parser.lr1 import LR1Parser
from hulk.lexer.regex_table import G
from hulk.hulk_ast import *
from hulk.hulk_lexer import Lexer
from hulk.lexer.regex_table import table
from cmp.evaluation import evaluate_reverse_parse_plus
from cmp.formatVisitor import FormatVisitor
from hulk.semantic_check.semantic_check_pipeline import semantic_check_pipeline
from hulk.tree_interpeter.interpeter import Interpreter
import sys
sys.setrecursionlimit(1000000)
load = True
save = not load
parser = LR1Parser(G=G, load=load, save=save)
lexer = Lexer(table=table, eof=G.EOF, load=load, save=save)
code_example = r"""
function tan(x: Number): Number => sin(x) / cos(x);
function cot(x) => 1 / tan(x);
function operate(x, y) {
print(x + y);
print(x - y);
print(x * y);
print(x / y);
}
function fib(n) => if (n == 0 | n == 1) 1 else fib(n-1) + fib(n-2);
function fact(x) => let f = 1 in for (i in range(1, x+1)) f := f * i;
function gcd(a, b) => while (a > 0)
let m = a % b in {
b := a;
a := m;
};
protocol Hashable {
hash(): Number;
}
protocol Equatable extends Hashable {
equals(other: Object): Boolean;
}
type Point(x,y) {
x = x;
y = y;
getX() => self.x;
getY() => self.y;
setX(x) => self.x := x;
setY(y) => self.y := y;
hash() : Number {
5;
}
}
type PolarPoint(phi, rho) inherits Point(rho * sin(phi), rho * cos(phi)) {
rho() => sqrt(self.getX() ^ 2 + self.getY() ^ 2);
}
type Knight inherits Person {
name() => "Sir" @@ base();
}
type Person(firstname, lastname) {
firstname = firstname;
lastname = lastname;
name() => self.firstname @@ self.lastname;
hash() : Number {
5;
}
}
type Superman {
}
type Bird {
}
type Plane {
}
type A {
hello() => print("A");
}
type B inherits A {
hello() => print("B");
}
type C inherits A {
hello() => print("C");
}
{
42;
print(42);
print((((1 + 2) ^ 3) * 4) / 5);
print("Hello World");
print("The message is \"Hello World\"");
print("The meaning of life is " @ 42);
print(sin(2 * PI) ^ 2 + cos(3 * PI / log(4, 64)));
{
print(42);
print(sin(PI/2));
print("Hello World");
}
print(tan(PI) ** 2 + cot(PI) ** 2);
let msg = "Hello World" in print(msg);
let number = 42, text = "The meaning of life is" in
print(text @ number);
let number = 42 in
let text = "The meaning of life is" in
print(text @ number);
let number = 42 in (
let text = "The meaning of life is" in (
print(text @ number)
)
);
let a = 6, b = a * 7 in print(b);
let a = 6 in
let b = a * 7 in
print(b);
let a = 5, b = 10, c = 20 in {
print(a+b);
print(b*c);
print(c/a);
};
let a = (let b = 6 in b * 7) in print(a);
print(let b = 6 in b * 7);
let a = 20 in {
let a = 42 in print(a);
print(a);
};
let a = 7, a = 7 * 6 in print(a);
let a = 7 in
let a = 7 * 6 in
print(a);
let a = 0 in {
print(a);
a := 1;
print(a);
};
let a = 0 in
let b = a := 1 in {
print(a);
print(b);
};
let a = 42 in if (a % 2 == 0) print("Even") else print("odd");
let a = 42 in print(if (a % 2 == 0) "even" else "odd");
let a = 42 in
if (a % 2 == 0) {
print(a);
print("Even");
}
else print("Odd");
let a = 42, mod = a % 3 in
print(
if (mod == 0) "Magic"
elif (mod % 3 == 1) "Woke"
else "Dumb"
);
let a = 10 in while (a >= 0) {
print(a);
a := a - 1;
};
for (x in range(0, 10)) print(x);
let iterable = range(0, 10) in
while (iterable.next())
let x = iterable.current() in
print(x);
let pt = new Point(0, 0) in
print("x: " @ pt.getX() @ "; y: " @ pt.getY());
let pt = new Point(3,4) in
print("x: " @ pt.getX() @ "; y: " @ pt.getY());
let pt = new PolarPoint(3,4) in
print("rho: " @ pt.rho());
let p = new Knight("Phil", "Collins") in
print(p.name());
let p: Person = new Knight("Phil", "Collins") in print(p.name());
let x: Number = 42 in print(x);
let x = new Superman() in
print(
if (x is Bird) "It's bird!"
elif (x is Plane) "It's a plane!"
else "No, it's Superman!"
);
let x = 42 in print(x);
let total = ({ print("Total"); 5; }) + 6 in print(total);
let x : A = if (rand() < 0.5) new B() else new C() in
if (x is B)
let y : B = x as B in {
y.hello();
}
else {
print("x cannot be downcasted to B");
};
let numbers = [1,2,3,4,5,6,7,8,9] in
for (x in numbers)
print(x);
let numbers = [1,2,3,4,5,6,7,8,9] in print(numbers[7]);
let squares = [x^2 || x in range(1,10)] in print(squares);
let squares = [x^2 || x in range(1,10)] in for (x in squares) print(x);
let x : Hashable = new Person("juan", "pablo") in print(x.hash());
let x : Hashable = new Point(0,0) in print(x.hash());
}
"""
print(code_example)
tokens = lexer(code_example)
for token in tokens:
print(
f"row: {token.row}, column: {token.column}, token type: {token.token_type}, lex: {token.lex}"
)
if lexer.errors:
print("Lexer Errors:")
for le in lexer.errors:
print("\033[91m" + "\t💥" + le + "\033[0m")
exit(1)
parse, operations = parser(tokens)
# print(parse)
if parser.errors:
print("Parser Errors:")
for error in parser.errors:
print("\033[91m" + "\t💥" + error + "\033[0m")
exit(1)
ast = evaluate_reverse_parse_plus(parse, operations, tokens)
print(ast)
print(" - - - -- - - - - VISITOR - -- - - - - - - - - -- ")
a = FormatVisitor()
a.visit(ast)
print(a.ans)
ast, errors, context, scope = semantic_check_pipeline(ast, True)
if errors:
exit(1)
print("\n=================== TREE INTERPRETER ======================")
interpreter = Interpreter(context=context)
result = interpreter.visit(ast)
print("\n=================== FINISH ================================")
print("Interpreter Evaluation: " + str(result))