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
|
from structs import *
def parseExpression(token, prev, tokens):
idx = 0
args = []
prev = token
while tokens[idx].type_ != TokenType.CLOSE_PAREN:
token = tokens[idx]
inc = 1
if token.type_ == TokenType.OPEN_PAREN:
expr, inc = parseExpression(token, prev, tokens[idx+1:])
args.append(expr)
elif token.type_ in (TokenType.STRING, TokenType.TRUE, TokenType.FALSE, TokenType.INT, TokenType.FLOAT):
expr, inc = parseLiteral(token, prev, tokens[idx+1:])
args.append(expr)
elif token.type_ in (TokenType.INT_TYPE, TokenType.FLOAT_TYPE, TokenType.STRING_TYPE, TokenType.ANY_TYPE):
expr, inc = parseType(token, prev, tokens[idx+1:])
args.append(expr)
else:
expr, inc = parseSymbol(token, prev, tokens[idx+1:])
args.append(expr)
idx += inc
prev = token
return List(args), idx + 2 # parens
def parseSymbol(token, prev, tokens):
return Symbol(token.text), 1
def parseLiteral(token, prev, tokens):
if token.type_ == TokenType.STRING:
return String(token.value), 1
elif token.type_ == TokenType.INT:
return Int(token.value), 1
elif token.type_ == TokenType.FLOAT:
return Float(token.value), 1
elif token.type_ in (TokenType.TRUE, TokenType.FALSE):
return Bool(token.value), 1
else:
return Literal(token.value), 1
def parseType(token, prev, tokens):
return Type(token.text), 1
def parse(tokens):
idx = 0
prev = None
exprs = []
while tokens[idx].type_ != TokenType.EOF:
token = tokens[idx]
counter = 1
if token.type_ == TokenType.OPEN_PAREN:
expr, counter = parseExpression(token, prev, tokens[idx+1:])
exprs.append(expr)
elif token.type_ in (TokenType.FALSE, TokenType.TRUE, TokenType.STRING, TokenType.INT, TokenType.FLOAT):
lit, counter = parseLiteral(token, prev, tokens[idx+1:])
exprs.append(lit)
elif token.type_ in (TokenType.INT_TYPE, TokenType.FLOAT_TYPE, TokenType.STRING_TYPE, TokenType.ANY_TYPE):
typ, counter = parseType(token, prev, tokens[idx+1:])
exprs.append(typ)
else:
sym, counter = parseSymbol(token, prev, tokens[idx+1:])
exprs.append(sym)
idx += counter
prev = token
return exprs
|