Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.py
More file actions
Latest commit
159 lines (136 loc) · 3.75 KB
/
Copy pathcalc.py
File metadata and controls
159 lines (136 loc) · 3.75 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
#################################################
# The Command Line (CL) -- PythonCalc #
# #
# https://github.com/Command-Line/pythoncalc #
#################################################
importply.lexaslex
importply.yaccasyacc
importsys
# Create a list to hold all of the token names
tokens= [
'INT',
'FLOAT',
'NAME',
'PLUS',
'MINUS',
'DIVIDE',
'MULTIPLY',
'EQUALS'
]
# Use regular expressions to define what each token is
t_PLUS=r'\+'
t_MINUS=r'\-'
t_MULTIPLY=r'\*'
t_DIVIDE=r'\/'
t_EQUALS=r'\='
# Ply's special t_ignore variable allows us to define characters the lexer will ignore.
# We're ignoring spaces.
t_ignore=r' '
# More complicated tokens, such as tokens that are more than 1 character in length
# are defined using functions.
# A float is 1 or more numbers followed by a dot (.) followed by 1 or more numbers again.
deft_FLOAT(t):
r'\d+\.\d+'
t.value=float(t.value)
returnt
# An int is 1 or more numbers.
deft_INT(t):
r'\d+'
t.value=int(t.value)
returnt
# A NAME is a variable name. A variable can be 1 or more characters in length.
# The first character must be in the ranges a-z A-Z or be an underscore.
# Any character following the first character can be a-z A-Z 0-9 or an underscore.
deft_NAME(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type='NAME'
returnt
# Skip the current token and output 'Illegal characters' using the special Ply t_error function.
deft_error(t):
print("Illegal characters!")
t.lexer.skip(1)
# Build the lexer
lexer=lex.lex()
# Ensure our parser understands the correct order of operations.
# The precedence variable is a special Ply variable.
precedence= (
('left', 'PLUS', 'MINUS'),
('left', 'MULTIPLY', 'DIVIDE')
)
# Define our grammar. We allow expressions, var_assign's and empty's.
defp_calc(p):
'''
calc : expression
| var_assign
| empty
'''
print(run(p[1]))
defp_var_assign(p):
'''
var_assign : NAME EQUALS expression
'''
# Build our tree
p[0] = ('=', p[1], p[3])
# Expressions are recursive.
defp_expression(p):
'''
expression : expression MULTIPLY expression
| expression DIVIDE expression
| expression PLUS expression
| expression MINUS expression
'''
# Build our tree.
p[0] = (p[2], p[1], p[3])
defp_expression_int_float(p):
'''
expression : INT
| FLOAT
'''
p[0] =p[1]
defp_expression_var(p):
'''
expression : NAME
'''
p[0] = ('var', p[1])
# Output to the user that there is an error in the input as it doesn't conform to our grammar.
# p_error is another special Ply function.
defp_error(p):
print("Syntax error found!")
defp_empty(p):
'''
empty :
'''
p[0] =None
# Build the parser
parser=yacc.yacc()
# Create the environment upon which we will store and retreive variables from.
env= {}
# The run function is our recursive function that 'walks' the tree generated by our parser.
defrun(p):
globalenv
iftype(p) ==tuple:
ifp[0] =='+':
returnrun(p[1]) +run(p[2])
elifp[0] =='-':
returnrun(p[1]) -run(p[2])
elifp[0] =='*':
returnrun(p[1]) *run(p[2])
elifp[0] =='/':
returnrun(p[1]) /run(p[2])
elifp[0] =='=':
env[p[1]] =run(p[2])
return''
elifp[0] =='var':
ifp[1] notinenv:
return'Undeclared variable found!'
else:
returnenv[p[1]]
else:
returnp
# Create a REPL to provide a way to interface with our calculator.
whileTrue:
try:
s=input('>> ')
exceptEOFError:
break
parser.parse(s)