-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESDLang.py
235 lines (218 loc) · 7.24 KB
/
ESDLang.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
#Imports
import random
from typing import (List, Any)
import time
#Create lists to store information about the variables and functions
vars={}
functions={}
#Random number
def radno(input_list: List[Any]):
return random.randint(int(input_list[1]),int(input_list[2]))
#maths operations
def mathsops(input_list: List[Any]):
val1=float(vars[input_list[0]]) if input_list[0] in vars else float(input_list[0])
cond=input_list[1]
val2=float(vars[input_list[2]] if input_list[2] in vars else input_list[2])
match cond:
case "+":
return val1+val2
case "-":
return val1-val2
case "/":
return val1/val2
case "*":
return val1*val2
case _:
return ValueError
#Input handler
def ins(string: str):
if string.endswith(" "):
return input(string[6:])
else:
return input(string[6:]+" ")
#If a string converts to float
def canfloat(string):
try:
float(string)
return True
except ValueError:
return False
#Handles variables
def variablehandling(input_list: List[Any]):
global vars
input_list.pop(0)
varname=input_list[0]
input_list.pop(0)
varval=input_list[0]
varvalstring=' '.join(input_list)
if varname in vars:
if varval.startswith("-"):
tosub=float(vars[varname])
vars[varname]=tosub+float(varval)
elif varval.startswith("+"):
toadd=float(vars[varname])
vars[varname]=toadd+float(varval)
else:
if varvalstring.startswith("input"):
vars[varname]=ins(varvalstring)
elif varvalstring.startswith("radnom"):
vars[varname]=float(radno(varvalstring.split()))
elif len(varvalstring)>1:
vars[varname]=varvalstring
else:
if canfloat(varval):
vars[varname]=float(varval)
else:
vars[varname]=varval
else:
if len(varvalstring.split())==1:
if canfloat(varval):
vars[varname]=float(varval)
else:
vars[varname]=varval
else:
if input_list[1] in ["*","/","+","-"] and len(input_list)==3:
vars[varname]=float(mathsops(input_list))
elif len(varvalstring.split())>1:
if varvalstring.startswith("input"):
vars[varname]=ins(varvalstring)
elif varvalstring.startswith("radnom"):
vars[varname]=float(radno(varvalstring.split()))
else:
vars[varname]=varvalstring
#Print function
def printah(input_list: List[Any]):
input_list.remove("write")
for part in input_list:
if part in vars:
print(vars[part], end=" ")
else:
print(part, end=" ")
print()
#Checks if conditions return true or false
def condcheck(input_list: List[Any]):
if len(input_list) != 3:
print("a conditioned statement requires 3 arguments, ", len(input_list), "are given")
else:
val1=float(input_list[0]) if canfloat(input_list[0]) else input_list[0]
val2=float(input_list[2].strip("\n")) if canfloat(input_list[2]) else input_list[2].strip("\n")
cond=input_list[1]
if val1 in vars:
val1=vars[val1]
if val2 in vars:
val2=vars[val2.strip("\n")]
match cond:
case">=":
if val1 >= val2:
return True
else:
return False
case"<=":
if val1 <= val2:
return True
else:
return False
case"=":
if val1 == val2:
return True
else:
return False
case"x":
if val1 != val2:
return True
else:
return False
case">":
if val1 > val2:
return True
else:
return False
case"<":
if val1 < val2:
return True
else:
return False
case _:
print("error: condition to judge if statement on is not properly defined")
#interprets a line from a file and dispatches the appropriate data from that line to the appropriate functions
def interpret(file, cond=...):
userInput = file.readline()
if userInput.startswith("var"):
variablehandling(userInput.strip("\n").split())
elif userInput.startswith("function"):
if userInput not in functions:
functionlogger(file, userInput.strip("\n").split(), userInput)
else:
print("Error, 2 functions with same name")
elif userInput == "quit\n":
return True
elif userInput.startswith("write"):
printah(userInput.strip("/n").split())
elif userInput.startswith("if"):
ifchecker(userInput.strip("/n").split(), file)
elif userInput.startswith("else"):
ln=...
while ln != "endstat\n":
ln=file.readline()
elif userInput == "endwhile\n":
file.seek(0)
ln=...
while ln != cond:
ln=file.readline()
elif userInput.startswith("while"):
whilechecker(userInput.strip("\n").split(), file)
elif len(userInput.strip("\n").split()) == 3 and userInput.strip("\n").split()[1] in ["*","/","+","-"]:
print(mathsops(userInput.split()))
elif userInput.split()[0] in functions:
functioncalled(file, userInput, userInput.strip("\n").split())
elif userInput.startswith("endfunction"):
return True
#Handles if statements
def ifchecker(input_list: List[Any], file):
input_list.remove("if")
cond=condcheck(input_list)
if cond != True:
ln=...
while ln != "else\n":
ln=file.readline()
#handles while statements
def whilechecker(input_list: List[Any], file):
input_list.remove("while")
condition="while "+' '.join(input_list)+'\n'
while condcheck(input_list):
if interpret(file, condition):
break
ln = ...
while ln != "endwhile\n":
ln=file.readline()
#stores functions and their starting line
def functionlogger(file, input_list: List[Any], line):
input_list.remove("function")
functions[input_list[0]]=line
ln=...
while ln != "endfunction "+input_list[0]+"\n":
ln=file.readline()
#when a function is called, this function finds the function in the code, executes it and then returns to the line after it was called
def functioncalled(file, line, input_list: List[Any]):
file.seek(0)
currentline=...
while currentline != functions[input_list[0]]:
currentline=file.readline()
end="endfunction "+input_list[0]
while True:
if interpret(file):
break
file.seek(0)
ln=...
while ln != line:
ln=file.readline()
#Main function
def main(fileName="trun.esdla"):
print("ESDLang 7 ...")
time.sleep(0.5)
with open(fileName, 'r') as file:
while True:
if interpret(file):
break
if __name__ == "__main__": #Make sure to use this so it doesn't run main() when imported
main()