-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeOn file runner.py
120 lines (108 loc) · 4.55 KB
/
CodeOn file runner.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
import os
import sys
import time
from pathlib import Path
variables = {
}
file = input("What would you like to open to run in CodeOn commands: ")
f = open(file, 'r')
lines = f.readlines()
os.system("cls")
print("Reading file...")
time.sleep(0.5)
os.system("cls")
def exit():
current_script = Path(__file__).resolve()
parent_dir = current_script.parent
target_script_path = None
for root, dirs, files in os.walk(parent_dir):
if "CodeOn.py" in files:
target_script_path = Path(root) / "updatecon.py"
break
if target_script_path:
os.system(f'python {target_script_path}')
sys.exit()
else:
print("ACTION REQUIRED: THE FILE 'CodeOn.py' CANNOT BE FOUND. PLEASE REDOWNLOAD CODEON TO FIX THIS ISSUE.")
sys.exit()
def main():
for line_number, v in enumerate(lines, start=1):
if v == "exit":
print("file done reading, exiting file...")
exit()
elif v == "clear":
os.system('cls')
continue
elif "print " in v:
arguments = v.split()
if not arguments[1] == "var":
print(arguments[1])
else:
if arguments[2] in variables:
print(variables[arguments[2]])
else:
print(f'Traceback: [Line {line_number}] no such variable as {arguments[2]}')
exit()
elif "math " in v:
arguments = v.split()
if arguments[1] == "+":
floatednum1 = float(arguments[2])
floatednum2 = float(arguments[3])
output = floatednum1 + floatednum2
print(arguments[2] + " + " + arguments[3] + " = " + str(output))
elif arguments[1] == "-":
floatednum1 = float(arguments[2])
floatednum2 = float(arguments[3])
output = floatednum1 - floatednum2
print(arguments[2] + " - " + arguments[3] + " = " + str(output))
elif arguments[1] == "*":
floatednum1 = float(arguments[2])
floatednum2 = float(arguments[3])
output = floatednum1 * floatednum2
print(arguments[2] + " * " + arguments[3] + " = " + str(output))
elif arguments[1] == "/":
floatednum1 = float(arguments[2])
floatednum2 = float(arguments[3])
output = floatednum1 / floatednum2
print(arguments[2] + " / " + arguments[3] + " = " + str(output))
elif "variable " in v:
arguments = v.split()
if arguments[2] == "math":
if arguments[3] == "+":
floatednum1 = float(arguments[4])
floatednum2 = float(arguments[5])
output = floatednum1 + floatednum2
variables[str(arguments[1])] = output
elif arguments[3] == "-":
floatednum1 = float(arguments[4])
floatednum2 = float(arguments[5])
output = floatednum1 - floatednum2
variables[str(arguments[1])] = output
elif arguments[3] == "*":
floatednum1 = float(arguments[4])
floatednum2 = float(arguments[5])
output = floatednum1 * floatednum2
variables[str(arguments[1])] = output
elif arguments[3] == "/":
floatednum1 = float(arguments[4])
floatednum2 = float(arguments[5])
output = floatednum1 / floatednum2
variables[str(arguments[1])] = output
else:
variables[str(arguments[1])] = "math"
else:
variables[str(arguments[1])] = str(arguments[2])
if __name__ == "__main__":
main()
lastline3 = len(lines)
lastline2 = lastline3 - 1
lastline = lines[lastline2]
if not lastline == "exit":
print("Traceback (no ext): Expected exit at line " + str(lastline2) + ", you can clear this error by adding an exit statement at the end of your code.")
endfile = input("Would you like to add an exit line to clear the error? (y/n)")
if endfile == "y":
with open(file, "a") as file_write:
file_write.write("\nexit")
exit()
else:
exit()