-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
53 lines (44 loc) · 1.44 KB
/
parser.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
def read_token(code):
code = code.strip()
if code[0] == "(":
# Token is a list
remaining = code[1:]
tokens = []
while len(remaining) > 0 and remaining[0] != ")":
token, remaining = read_token(remaining)
tokens.append(token)
remaining = remaining.lstrip()
if len(remaining) > 0 and remaining[0] == ")":
remaining = remaining[1:].lstrip()
return tokens, remaining
elif code[0] == "\"":
pass # TODO: token is a String
else:
remaining = code
token = ""
while len(remaining) > 0 and remaining[0] != " " and remaining[0] != '\n' and remaining[0] != ")":
token += remaining[0]
remaining = remaining[1:]
try:
temp = int(token)
return temp, remaining
except ValueError:
try:
temp = float(token)
return temp, remaining
except ValueError:
return token, remaining
def parse(code):
"""Tokenizes a code string, parses and returns an AST"""
tokens = []
remaining = code
while True:
token, remaining = read_token(remaining)
tokens.append(token)
if remaining == "":
return tokens
def unparse(code):
if isinstance(code, list):
return "(" + " ".join([unparse(expression) for expression in code]) + ")"
else:
return str(code)