-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalc.py
More file actions
159 lines (127 loc) · 3.39 KB
/
calc.py
File metadata and controls
159 lines (127 loc) · 3.39 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
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
from traceback import format_exc
_debug = False
_line = '' # input string (stream)
_look = 0 # looked char (current next char)
_cur = '' # _current char
class ParseError(Exception):
def __init__(self):
self.text = 'Parse error at %d character in "%s"' % (_look, _line)
self.n = _look
def next_(): # string (char)
"""Return next character of the input stream"""
global _line, _look, _cur
_look += 1
if _look > len(_line):
_cur = '\0'
else:
_cur = _line[_look - 1]
return _cur
def require(s): # bool
"""Check input stream for the string"""
for i in range(len(s)):
if next_() != s[i]:
return False
return True
_digits = "0123456789"
def reqenum(s, call_next=False): # bool
"""Check input stream for the characters"""
global _cur
if call_next:
next_()
for i in range(len(s)):
if _cur == s[i]:
return True
return False
def skip_blank():
"""Skip blank characters from input stream"""
global _cur
while _cur != '\0' and _cur <= ' ':
next_()
def _fun0(): pass
def _fun1(): pass
def _fun2(): pass
def _number(): # int
"""Number recognize function"""
global _cur
result = ""
while _cur.isdigit():
result = result + _cur
next_()
if _cur == '.':
if result == "":
result = "0"
result = result + _cur
if not reqenum(_digits, True):
raise ParseError
while _cur.isdigit():
result = result + _cur
next_()
return int(result)
def _fun2(): # int
"""Expression level 2"""
global _cur
result, sign = 0, False
next_()
skip_blank()
if not reqenum("(+-" + _digits):
raise ParseError
if _cur == '+':
next_()
if _cur == '-':
sign = True
next_()
skip_blank()
while _cur == '(' or _cur.isdigit():
if _cur == '(':
result = _fun0()
skip_blank()
require(')')
else:
result = _number()
skip_blank()
return result * (sign and -1 or 1)
def _fun1(): # int
"""Expression level 1"""
global _cur
result = _fun2()
skip_blank()
while _cur == '*' or _cur == '/':
result = result * (_cur == '*' and _fun1() or (1 / _fun1()))
return result
def _fun0(): # int
"""Expression level 0"""
global _cur
result = _fun1()
skip_blank()
while _cur == '+' or _cur == '-':
result = result + (_cur == '-' and -1 or 1) * _fun1()
return result
def compute(s):
"""Compute a mathematics expression"""
global _line, _look, _cur
_line, _look = s, 0
result = _fun0()
if _cur != '\0':
raise ParseError
return result
###########################################################################
# TEST CODE
###########################################################################
if __name__ == "__main__":
while True:
print(">", end='')
line = input()
if len(line) == 0: break
try:
result = compute(line)
except ParseError as ex:
print(' ' * ex.n + "^")
print(ex.text)
if _debug:
print(format_exc())
except Exception as ex:
print("Some exception :(")
print(format_exc())
else:
print(result)
print()