from Lexer import Lexer from Types import number string from Func impo

 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
from Lexer import Lexer
from Types import number, string
from Func import fact
standart_env = {
'equal?': lambda (x, y): rec_eval(x) == rec_eval(y),
'if': lambda (x, y, z): rec_eval(y) if rec_eval(x) else rec_eval(z),
'!': lambda (x): fact(rec_eval(x[0])),
'+': lambda (x, y): rec_eval(x) + rec_eval(y),
'-': lambda (x, y): rec_eval(x) - rec_eval(y),
'*': lambda (x, y): rec_eval(x) * rec_eval(y),
'/': lambda (x, y): rec_eval(x) / rec_eval(y),
'>': lambda (x, y): rec_eval(x) > rec_eval(y),
'<': lambda (x, y): rec_eval(x) < rec_eval(y),
'=': lambda (x, y): rec_eval(x) == rec_eval(y),
'>=': lambda (x, y): rec_eval(x) >= rec_eval(y),
'<=': lambda (x, y): rec_eval(x) <= rec_eval(y),
}
type_process = {
'Number': number,
'String': string
}
def rec_eval(body):
if isinstance(body, list):
item = Lexer.lexem_str(body[0])
if item in standart_env:
return standart_env[item](body[1:])
else:
val, type = body[0], body[1]
return type_process[type](val) if type in type_process else val
pass