-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
129 lines (93 loc) · 3.19 KB
/
parser.py
File metadata and controls
129 lines (93 loc) · 3.19 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
from token_ import Token
import sys
def Binary(left, operator, right):
return f"({operator.lexeme} {left} {right})"
def Unary(operator, right):
return f"({operator.lexeme} {right})"
def Literal(expr):
if expr == None:
return "nil"
return str(expr).lower()
def Grouping(expr):
return f"(group {expr})"
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.current = 0
self.errors = []
def expression(self):
return self.equality()
def equality(self):
expr = self.comparison()
while(self.match(["BANG_EQUAL","EQUAL_EQUAL"])):
operator = self.previous()
right = self.comparison()
expr = Binary(expr, operator, right)
return expr
def comparison(self):
expr = self.term()
while(self.match(["GREATER","GREATER_EQUAL","LESS","LESS_EQUAL"])):
operator = self.previous()
right = self.term()
expr = Binary(expr, operator, right)
return expr
def factor(self):
expr = self.unary()
while(self.match(["SLASH","STAR"])):
operator = self.previous()
right = self.unary()
expr = Binary(expr, operator, right)
return expr
def unary(self):
if self.match(["BANG","MINUS"]):
operator = self.previous()
right = self.unary()
return Unary(operator, right)
return self.primary()
def primary(self):
if self.match(["FALSE"]):
return Literal(False)
if self.match(["TRUE"]):
return Literal(True)
if self.match(["NIL"]):
return Literal(None)
if self.match(["NUMBER","STRING"]):
return Literal(self.previous().literal)
if self.match(["LEFT_PAREN"]):
expr = self.expression()
self.consume("RIGHT_PAREN","Unmatched parentheses.")
return Grouping(expr)
self.error(self.peek(), "Expect expression")
def term(self):
expr = self.factor()
while(self.match(["MINUS","PLUS"])):
operator = self.previous()
right = self.factor()
expr = Binary(expr, operator, right)
return expr
def match(self,types):
for type in types:
if self.check(type):
self.advance()
return True
return False
def check(self, type):
if self.is_at_end():
return False
return self.peek().type == type
def advance(self):
if not self.is_at_end():
self.current += 1
return self.previous()
def is_at_end(self):
return self.peek().type == "EOF"
def peek(self):
return self.tokens[self.current]
def previous(self):
return self.tokens[self.current - 1]
def consume(self, type, message):
if(self.check(type)):
return self.advance()
self.error(self.peek(), message)
def error(self, token, message):
self.errors.append(f"[line {token.line}] Error: {message}")