-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc1.py
125 lines (101 loc) · 3.31 KB
/
calc1.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
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
INTEGER, PLUS, MINUS, EOF = 'INTEGER', 'PLUS', 'MINUS', 'EOF'
class Token(object):
def __init__(self, type, value):
#token type: INTEGER, PLUS, or EOF
self.type = type
#token value: 0-9, '+'
self.value = value
def __str__(self):
"""
Examples:
Token(INTEGER, 3)
Token(PLUS, '+')
"""
return 'Token({type}, {value})'.format(
type=self.type,
value=repr(self.value)
)
def __repre__(self):
return self.__str__()
class Interpreter(object):
def __init__(self, text):
self.text = text
self.pos = 0
self.current_token = None
def error(self, msg='Error parsing input'):
raise Exception(msg)
def nextCharIsInt(self):
if self.pos + 1 > len(self.text) - 1:
return False
return self.text[self.pos+1].isdigit()
def get_next_token(self):
"""
Lexical analyzer
"""
text = self.text
if self.pos > len(text) - 1:
return Token(EOF, None)
current_char = text[self.pos]
if current_char.isdigit():
buffer = int(current_char)
while self.nextCharIsInt():
self.pos += 1
current_char = text[self.pos]
buffer = buffer * 10 + int(current_char)
token = Token(INTEGER, int(buffer))
self.pos += 1
return token
if current_char == '+':
token = Token(PLUS, current_char)
self.pos += 1
return token
if current_char == '-':
token = Token(MINUS, current_char)
self.pos += 1
return token
if current_char == ' ':
self.pos += 1
return self.get_next_token()
self.error("Current_char is " + current_char)
def eat(self, token_type):
if self.current_token.type == token_type:
self.current_token = self.get_next_token()
else:
self.error('self.current_token:'+str(self.current_token)+', token_type is ' + token_type)
def expr(self):
"""
expr -> INTEGER PLUS INTEGER
"""
self.current_token = self.get_next_token()
left = self.current_token
self.eat(INTEGER)
print("Left " + str(left))
op = self.current_token
if op.value == '+':
self.eat(PLUS)
elif op.value == '-':
self.eat(MINUS)
else:
self.error('Unknow op ' + str(op))
print("Op " + str(op))
right = self.current_token
self.eat(INTEGER)
print("Right " + str(right))
if op.value == '+':
result = left.value + right.value
elif op.value == '-':
result = left.value - right.value
return result
def main():
while True:
try:
text = input('calc > ')
except EOFError:
break
if not text:
continue
interpreter = Interpreter(text)
result = interpreter.expr()
print(result)
if __name__ == '__main__':
main()