-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrum.py
266 lines (226 loc) · 7.42 KB
/
grum.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python
#Grum is a Lisp like language
#Inspiration from PeterNorvig's lisp interpreter in python
#The Motive is to study different facets of a programing language
#Recursion
#Tail call optmiization
#Type check
#Garbage collector
# ...
import logging
#A function is a bunch of instructions in a different environment
class bcolors:
HEADER = '\033[95m'
BADBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class environment:
" Enviroment 'or frame' for executing instructions "
def __init__ (self):
#Stack of environments[dictionary]
self.stackframe = [{}];
self.envno = 0;
def load(self,var):
"Returns a varaible value or a function address"
return self.stackframe[self.envno][var];
def store(self,var,val):
self.stackframe[self.envno][var] = val;
def moveback(self):
self.envno -=1;
def movefront(self):
self.envno +=1;
class errortrace:
"Tracing back error"
linenumber = 0;
pos = 0
category = "Syntax Error"
stack = []
Memory = [];
message = "None"
errorlevel = "FAIL";
exp = '';
def throw(self):
s=''
if self.category == "Syntax Error":
s+= bcolors.FAIL + "Execution Failed :: Syntax Error"+"\n"
s+= self.msg + bcolors.ENDC+"\n"
s+=bcolors.BADBLUE+"Error @ Line : "+ str(self.linenumber)+bcolors.ENDC+"\n"
elif self.category == "print":
s+= bcolors.OKGREEN + self.msg + bcolors.ENDC;
elif self.category == "Unimplemented Error":
s = bcolors.FAIL + "Execution Failed :: Unimplemented Error\n"
s+= self.msg + bcolors.ENDC+"\n"
s+= bcolors.BADBLUE+"Error @ Line : "+ str(self.linenumber)+bcolors.ENDC+"\n"
else:
s+="Error notdefined";
if self.category is not 'print':
raise ValueError(s)
else:
print s
errors = errortrace();
#env= environment();
env ={};
;
def validateparenthesis(exp):
cnt = pos = 0;
res = 0;
for each in exp:
cnt+=(each == '[');
cnt-=(each ==']');
pos +=1;
if cnt<0:
res = pos;
return (res,False)
return (pos, True and (cnt ==0));
def tokenize(e,r):
l=[];
while e[r] !=']':
if e[r] != '[':
l.append(terminal(e[r]));
r+=1;
else:
(r,p)= tokenize(e,r+1);
l.append(p);
r+=1
return (r,l);
#Parse expressions here
def parse(exp):
"""
Splits input expression and asks tokenizer to tokenize them
Returns tokenised list or throws error """
global errors
pos, proceed = validateparenthesis(exp);
r =[];
if proceed is True :
e = exp.replace('[',' [ ').replace(']',' ] ').split();
(_,r) = tokenize(e,1);
else:
errors.msg = "FAIL: Unmatched Paranthesis "
errors.pos = pos;
errors.throw();
return r;
def terminal(s):
try:
r = int(s);
e = 'int'
except:
try:
r =float(s);
e = 'float';
except: #variable name is else ? what happens
r = s
e = 'string'
if s in ['if','?','else','var','main','end','=','+','-','*','/','%','<','>','==','!=','quote','while']:
e = 'keyword';
return (r,e);
#Evaluator evaluates an Expression
#An expression always returns an integer
def evaluator(stms):
global env;
global errors;
result = None;
logging.info('evaluating %s' + str(stms))
if stms == []:
result = 0
#terminals
if stms[0][1] == 'int'or stms[0][1] == 'float':
result = stms[0][0];
elif stms[0][1] =='string':
result = env[stms[0][0]];
#terminals
if stms[0][1] == 'int'or stms[0][1] == 'float' or stms[0][1] == 'string':
exp1 = stms
#unary
elif stms[0][0] in['?','quote']:
[_,exp]=stms;
if stms[0][0] == '?':
result = evaluator(exp);
errors.category = 'print';
errors.msg = str(result);
errors.throw();
elif stms[0][0] == 'quote':
errors.msg ="Quotes are Unimplemented"
errors.category ="Unimplemented Error"
errors.throw();
#binary
elif stms[0][0] in ['+','-','*','/','%','>','<','==','!=','=','while']:
[exp1,exp2] = stms[1];
if stms[0][0] == 'while':
while(evaluator(exp1)):
result = evaluator(exp2);
if stms[0][0] == '=':
result = evaluator(exp2)
env.update({exp1[0][0]:result})
elif stms[0][0] == '+':
e1 = evaluator(exp1)
e2 = evaluator(exp2)
logging.info(e1)
logging.info(e2)
result = e1 + e2
elif stms[0][0] == '-':
result = evaluator(exp1)- evaluator(exp2)
elif stms[0][0] == '*':
result = evaluator(exp1)* evaluator(exp2)
elif stms[0][0] == '/':
errors.msg = "Runtime error: Division by 0"
e2 = evaluator(exp2);
if e2!=0:
result = evaluator(exp1)/e2;
else:
errors.throw();
elif stms[0][0] == '>':
result = True if evaluator(exp1)>evaluator(exp2) else 0;
elif stms[0][0] == '<':
result = True if evaluator(exp1)<evaluator(exp2) else 0;
elif stms[0][0] == '==':
result = True if evaluator(exp1) == evaluator(exp2) else 0;
#ifelse
elif stms[0][0] == 'if' :
try:
[a,exp1,exp2,b,exp3] = stms;
assert(a[0]=='if' and b[0]=='else');
logging.info("Parsed if else successfully ....");
except:
raise SyntaxError("Incorrect if statement");
result = evaluator(exp2) if (evaluator(exp1)==True) else evaluator(exp3);
#Compund expressions
elif result is None:
logging.info('Can be Compund of expression')
for each in stms:
result = evaluator(each);
#Todo : store return a value in stack
#Cannot resolve statement
if result is None:
raise SyntaxError("Incorrect statement");
return result;
def repl():
" Prompt- read evaluate print "
while True:
evaluator(parse(raw_input('grumpy : ')));
def main(argv):
""" Welcome to grumpy, an interpreter for grum in python.
This is a toy interpreter written as an aid to a systems course
Author: GR R (i.e) Gowtham Rangarjan """
prg='';
if '--log' in argv :
logging.basicConfig(level=logging.INFO);
print main.__doc__
if len(argv) >=1:
f = open(argv[0],'r');
prg = ''
for each in f.readlines():
each = each.strip();
if len(each)>=1:
if each[0] =='#':
continue;
prg+=each;
evaluator(parse(prg));
else:
repl();
import sys
if __name__== '__main__':
main(sys.argv[1:])