-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.py
149 lines (123 loc) · 3.37 KB
/
calc.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
import ply.lex as lex
import ply.yacc as yacc
from decimal import Decimal
import decimal
from decimal_math import sin, cos, pi, exp
functions = ['sqrt','sin','cos','ln','log']
tokens = [
'NAME','NUMBER','FUNCTION',
'PLUS','MINUS','TIMES','DIVIDE','RAISE_TO','EQUALS','SET_TO',
'LPAREN','RPAREN',
]
# Tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_RAISE_TO = r'\^'
t_EQUALS = r'='
t_SET_TO = r'->'
t_LPAREN = r'\('
t_RPAREN = r'\)'
def t_NAME(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.value = t.value.lower();
if t.value in functions or t.value[:4] == 'log_':
t.type = 'FUNCTION'
return t
def t_NUMBER(t):
r'\d+(\.\d+)?'
t.value = Decimal(t.value)
return t
# Ignored characters
t_ignore = " \t"
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lex.lex()
# Parsing rules
precedence = (
('left','PLUS','MINUS'),
('left','TIMES','DIVIDE'),
('right','UMINUS'),
('right','RAISE_TO'),
)
names = {'e': exp(Decimal(1)), 'pi': pi(), '_last': Decimal(0)}
reserved_names = ['e','pi','_last']
def p_statement_assign(t):
'statement : NAME EQUALS expression'
if t[1] in reserved_names:
print "Name %s reserved" % t[1]
else:
names[t[1]] = t[3]
def p_set_last_to(t):
'statement : SET_TO NAME'
if t[2] in reserved_names:
print "Name %s reserved" % t[2]
else:
names[t[2]] = names['_last']
def p_statement_expr(t):
'statement : expression'
names['_last'] = t[1]
print(t[1])
def p_expression_binop(t):
'''expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression'''
if t[2] == '+' : t[0] = t[1] + t[3]
elif t[2] == '-': t[0] = t[1] - t[3]
elif t[2] == '*': t[0] = t[1] * t[3]
elif t[2] == '/': t[0] = t[1] / t[3]
def p_expression_uminus(t):
'expression : MINUS expression %prec UMINUS'
t[0] = -t[2]
def p_expression_exponent(t):
'expression : expression RAISE_TO expression'
t[0] = t[1] ** t[3]
def p_expression_function(t):
'expression : FUNCTION LPAREN expression RPAREN'
if t[1] == 'sqrt':
t[0] = t[3].sqrt()
elif t[1] == 'sin':
t[0] = sin(t[3])
elif t[1] == 'cos':
t[0] = cos(t[3])
elif t[1] == 'ln':
t[0] = t[3].ln()
elif t[1] == 'log':
t[0] = t[3].log10()
elif t[1][:4] == 'log_':
try:
b = Decimal(t[1][4:])
t[0] = t[3].log10() / b.log10()
except decimal.InvalidOperation:
print "Cannot evaluate %s%s%s%s. " \
"Setting it to 0." % (t[1], t[2], t[3], t[4])
t[0] = Decimal(0)
def p_expression_group(t):
'expression : LPAREN expression RPAREN'
t[0] = t[2]
def p_expression_number(t):
'expression : NUMBER'
t[0] = t[1]
def p_expression_name(t):
'expression : NAME'
try:
t[0] = names[t[1]]
except LookupError:
print("Undefined name '%s'" % t[1])
t[0] = Decimal(0)
def p_error(t):
print("Syntax error at '%s'" % t.value)
yacc.yacc()
while 1:
try:
s = raw_input('calc > ') # Use raw_input on Python 2
except EOFError:
break
yacc.parse(s)