-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.py
More file actions
40 lines (32 loc) · 1.06 KB
/
variable.py
File metadata and controls
40 lines (32 loc) · 1.06 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
from decode import decode
def variable(c, variables):
lowered = [i.lower() for i in c]
if lowered.count("assign") != 1 or lowered.count("to") != 1 or lowered.count("as") > 1:
print(c)
print()
print("Syntax Error: Use of keyword in variable declaration")
exit()
to_idx = lowered.index("to")
treat_as_as = False
if "as" in lowered:
treat_as_as = True
value = decode(' '.join(c[1:to_idx]), variables)
var = decode(' '.join(c[to_idx + 1:-2] if treat_as_as else c[to_idx + 1:]), variables)
val_type = c[-1] if treat_as_as else ''
if val_type == "integer":
try:
value = int(value)
except:
print(c)
print()
print("Type Error: Unacceptable Integer Conversion")
exit()
elif val_type == "float":
try:
value = float(value)
except:
print(c)
print()
print("Type Error: Unacceptable Float Conversion")
exit()
variables[var] = value