-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
62 lines (46 loc) · 1.73 KB
/
parser.py
File metadata and controls
62 lines (46 loc) · 1.73 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
from mtoken import TokenType
from nodes import *
class Parser:
def __init__(self, tokens):
self.tokens = iter(tokens)
self.advance()
def raise_error(self):
raise Exception("Invalid Syntax")
def advance(self):
try:
self.current_token = next(self.tokens)
except StopIteration:
self.current_token = None
def parser(self):
if self.current_token is None:
return None
result = self.expr()
if self.current_token is not None:
self.raise_error()
return result
def expr(self):
result = self.term()
while self.current_token is not None and self.current_token.type in (TokenType.PLUS, TokenType.MINUS):
if self.current_token.type == TokenType.PLUS:
self.advance()
result = AddNode(result, self.term())
elif self.current_token.type == TokenType.MINUS:
self.advance()
result = NodeSubtract(result, self.term())
return result
def term(self):
result = self.factor()
while self.current_token is not None and self.current_token.type in (TokenType.MULTIPLY, TokenType.DIVIDE):
if self.current_token.type == TokenType.MULTIPLY:
self.advance()
result = NodeMultiply(result, self.factor())
elif self.current_token.type == TokenType.DIVIDE:
self.advance()
result = NodeDivide(result, self.factor())
return result
def factor(self):
token = self.current_token
if token.type == TokenType.NUMBER:
self.advance()
return NumberNode(token.value)
self.raise_error()