-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpretor.py
More file actions
205 lines (185 loc) · 4.65 KB
/
interpretor.py
File metadata and controls
205 lines (185 loc) · 4.65 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
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
from sys import *
import re
def check(string):
try:
result = re.match(r'\$[a-z]([a-z]|[0-9])*',string)
if result.group(0) ==string:
return True
else:
return False
except:
pass
tokens=[]
num_stack=[]
symbols={}
def open_file(filename):
data=open(filename,"r").read()
data += "<EOF>"
return data
def lex(filecontents):
tok=""
state=0
string=""
expr=""
var=""
varstarted=0
isexpr=0
filecontents = list(filecontents)
for char in filecontents:
tok+=char
if tok==" ":
if state==0:
tok=""
else:
tok=" "
elif tok=="\n" or tok=="<EOF>":
if expr!="" and isexpr==1:
tokens.append("EXPR:"+expr)
expr=""
elif expr!="" and isexpr==0:
tokens.append("NUM:"+expr)
expr=""
elif var!="":
tokens.append("VAR:"+var)
var=""
varstarted=0
tok=""
elif tok=="=" and state==0:
if expr!="" and isexpr==0:
tokens.append("NUM:"+expr)
expr=""
if var!="" and check(var):
tokens.append("VAR:"+var)
var=""
varstarted=0
if tokens[-1]=="EQUALS":
tokens[-1]="EQEQ"
else:
tokens.append("EQUALS")
tok=""
elif tok=='$' and state==0:
varstarted=1
var+=tok
tok=""
elif varstarted==1:
if tok=="<" or tok==">":
if var!="":
tokens.append("VAR:"+var)
var=""
varstarted=0
var+=tok
tok = ""
elif tok=="DISPLAY" or tok=="paathuko":
tokens.append("PRINT")
tok=""
elif tok=="ENDIF" or tok=="endif":
tokens.append("ENDIF")
tok=""
elif tok=="IF" or tok=="if":
tokens.append("IF")
tok=""
elif tok=="THEN" or tok=="then":
if expr!="" and isexpr==0:
tokens.append("NUM:"+expr)
expr=""
tokens.append("THEN")
tok=""
elif tok=="INPUT" or tok=="input":
tokens.append("INPUT")
tok=""
elif tok=="0" or tok=="1" or tok=="2" or tok=="2" or tok=="3" or tok=="4" or tok=="5" or tok=="6" or tok=="7" or tok=="8" or tok=="9":
expr+=tok
tok=""
elif tok=="+" or tok=="-" or tok=="(" or tok==")" or tok=="%" or tok=="*" or tok=="/":
isexpr=1
expr+=tok
tok=""
elif tok=="\t":
tok=""
elif tok=="\"" or tok==" \"":
if state==0:
state=1
elif state==1:
tokens.append("STRING:"+string)
string=""
state=0
tok=""
elif state==1:
string+=tok
tok=""
return tokens
def evaluate(express):
return eval(express)
def assign(name,value):
symbols[name[4:]]=value
def getValue(name):
if name in symbols:
return symbols[name]
else:
return "VARIABLE ERROR : Undefined Variable Found"
exit()
def printer(value):
if value[:6]=="STRING":
print(value[8:])
elif value[:3]=="NUM":
print(value[4:])
elif value[:4]=="EXPR":
print(evaluate(value[5:]))
def getInput(string, variable):
x = input(string+" ")
x = "\""+x
symbols[variable]="STRING:"+x
def find_end(i,toks,check):
for i in range(i,len(toks)):
if toks[i]==check:
return i
print("ERROR : If statement not closed")
exit()
def parser(toks):
i=0
while(i < len(toks)):
if toks[i]=="ENDIF":
i+=1
elif toks[i] + " " + toks[i+1][0:6] == "PRINT STRING" or toks[i] + " " +toks[i+1][0:3]=="PRINT NUM"or toks[i] + " " +toks[i+1][0:3]=="PRINT VAR" or toks[i] + " " +toks[i+1][0:4]=="PRINT EXPR":
if toks[i+1][0:6]=="STRING":
printer(toks[i+1])
elif toks[i+1][0:3]=="NUM":
printer(toks[i+1])
elif toks[i+1][0:4]=="EXPR":
printer(toks[i+1])
elif toks[i+1][:3]=="VAR":
printer(getValue(toks[i+1][4:]))
i+=2
elif toks[i][:3] +" "+ toks[i+1]+" "+toks[i+2][0:6]=="VAR EQUALS STRING" or toks[i][:3] +" "+ toks[i+1]+" "+toks[i+2][0:3]=="VAR EQUALS NUM"or toks[i][:3] +" "+ toks[i+1]+" "+toks[i+2][0:3]=="VAR EQUALS VAR" or toks[i][:3] +" "+ toks[i+1]+" "+toks[i+2][0:4]=="VAR EQUALS EXPR":
if toks[i+2][:6]=="STRING":
assign(toks[i],toks[i+2])
elif toks[i+2][:3]=="NUM":
assign(toks[i],toks[i+2])
elif toks[i+2][:4]=="EXPR":
assign(toks[i],"NUM:"+str(evaluate(toks[i+2][5:])))
elif toks[i+2][:3]=="VAR":
assign(toks[i],getValue(toks[i+2][4:]))
i+=3
elif toks[i] +" "+ toks[i+1][:6] +" "+ toks[i+2][:3] =="INPUT STRING VAR":
getInput(toks[i+1][8:],toks[i+2][4:])
i+=3
elif toks[i] +" "+toks[i+1][:3]+" "+toks[i+2]+" "+toks[i+3][:3]+" "+toks[i+4]=="IF NUM EQEQ NUM THEN":
if toks[i+1][4:] == toks[i+3][4:]:
i+=5
else:
i+= find_end(i,toks,"ENDIF")
elif toks[i]+" "+toks[i+1][:3]+" "+toks[i+2]+" "+toks[i+3][:3]+" "+toks[i+4]=="IF VAR EQEQ NUM THEN":
#print("value :",getValue(toks[i+1][4:])[8:])
#print("num :",toks[i+3][4:])
if getValue(toks[i+1][4:])[4:] == toks[i+3][4:]:
i+=5
elif getValue(toks[i+1][4:])[8:] == toks[i+3][4:]:
i+=5
else:
i+= find_end(i,toks,"ENDIF")
def run():
data = open_file(argv[1])
toks=lex(data)
#print(toks)
parser(toks)
run()