-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter2.py
165 lines (123 loc) · 4.38 KB
/
interpreter2.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Token types
# EOF (end-of-file) token is used to indicate that
# there is no more input left for lexical analysis
(INTEGER, PLUS, MINUS, MULTIPLY, DIVIDE, EOF) = ('INTEGER', 'PLUS', 'MINUS', 'MULTIPLY', 'DIVIDE', 'EOF')
class Token(object):
def __init__(self, type, value):
# type: INTEGER, PLUS, or EOF
self.type = type
# value: 1...9
self.value = value
def __str__(self):
"""String representation of the class instance.
Examples:
Token(INTEGER, 3)
Token(PLUS '+')
"""
return 'Token({type}: {value})'.format(type=self.type,
value=repr(self.value))
def __repr__(self):
return self.__str__()
class Interpreter(object):
def __init__(self, text):
# client string input, e.g. "3+5"
self.text = text
# self.pos is an index into self.text
self.pos = 0
# current token instance
self.current_token = None
self.current_char = self.text[self.pos]
def error(self):
raise Exception('Error parsing input')
def go_ahead(self):
self.pos += 1
if self.pos > len(self.text) - 1:
self.current_char = None
else:
self.current_char = self.text[self.pos]
def skip_whitespace(self):
while self.current_char is not None and self.current_char.isspace():
self.go_ahead()
def integer(self):
"""Return a (multidigit) integer consumed from the input."""
result = ''
while self.current_char is not None and self.current_char.isdigit():
result += self.current_char
self.go_ahead()
return int(result)
def check(self, token_type):
if self.current_token.type == token_type:
self.current_token = self.get_next_token()
else:
self.error()
def get_next_token(self):
"""Lexical analyzer (also known as scanner or tokenizer)
This method is responsible for breaking a sentence
apart into tokens. One token at a time.
"""
while self.current_char is not None:
if self.current_char.isspace():
self.skip_whitespace()
continue
if self.current_char.isdigit():
return Token(INTEGER, self.integer())
if self.current_char == '+':
self.go_ahead()
return Token(PLUS, '+')
if self.current_char == '-':
self.go_ahead()
return Token(MINUS, '-')
if self.current_char == '*':
self.go_ahead()
return Token(MULTIPLY, '*')
if self.current_char == '/':
self.go_ahead()
return Token(DIVIDE, '/')
self.error()
return Token(EOF, None)
def expr(self):
"""
expr -> INTEGER PLUS INTEGER
"""
# set current token to the first token taken from the input
self.current_token = self.get_next_token()
result = self.current_token.value
left = Token(INTEGER, result)
self.check(INTEGER)
while self.current_token is not None and self.current_token.type != EOF:
left = Token(INTEGER, result)
operator = self.current_token
if operator.type == PLUS:
self.check(PLUS)
elif operator.type == MINUS:
self.check(MINUS)
elif operator.type == MULTIPLY:
self.check(MULTIPLY)
elif operator.type == DIVIDE:
self.check(DIVIDE)
right = self.current_token
self.check(INTEGER)
if operator.type == PLUS:
result = left.value + right.value
elif operator.type == MINUS:
result = left.value - right.value
elif operator.type == MULTIPLY:
result = left.value * right.value
elif operator.type == DIVIDE:
result = float(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()