-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.py
45 lines (36 loc) · 1.24 KB
/
parse.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
import math
import operator as op
program = "(begin (define r 10) (* pi (* r r)))"
Symbol = str # Lisp Symbol is implemented as a Python str
Number = (int, float) # Lisp Number is implemented as a Python int or float
Atom = (Symbol, Number) # Lisp Atom is a Symbol or Number
List = list # Lisp List is implemented as a Python list
Exp = (Atom, List) # Lisp expression is an Atom or List
Env = dict # Lisp environment which is essentially a dictionary
# of {'var':'value'}
def tokenize(chars):
return chars.replace('(',' ( ').replace(')',' ) ').split()
def parse(chars):
return read_from_tokens(tokenize(chars))
def read_from_tokens(parsed):
if len(parsed)==0:
raise SyntaxError('invalid syntax')
parse =parsed.pop(0)
if parse =='(':
l=[]
while parsed[0] != ')':
l.append(read_from_tokens(parsed))
parsed.pop(0)
return l
elif parse ==')':
raise SyntaxError('unexpected )')
else:
return atom(parse)
def atom(string):
try:
return int(string)
except ValueError:
try :
return float(string)
except ValueError:
return Symbol(string)