-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinterpreter.cb
142 lines (136 loc) · 4.24 KB
/
interpreter.cb
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
from native reference os.path;
from native reference subprocess;
from native reference sys;
from native reference shutil;
include parse;
include error;
class Interpreter()
{
function Interpret(code)
{
subprocess.call(["python", "output.py"]);
}
}
class Main()
{
function GetCode(filePath)
{
if os.path.isfile(filePath)
{
with open(filePath, 'r') as file
{
return file.read();
}
}
else
{
Error("Input file not found");
}
}
function HandleArgs()
{
if sys.argv[1] == "--help" or sys.argv[1] == "-h"
{
Error("Command line arguments: \n--help -h: Prints help message \n--version -b: Prints the version of the interpreter \n--run -r (default) [file]: Runs the interpreter on the file specified \n--transpile -t [file] [address]: Converts the file specified into python code and saves it to the address specified \n--compile -c [file] [address]: Compiles the file specified into an executable and saves it to the address specified");
}
else if sys.argv[1] == "--run" or sys.argv[1] == "-r"
{
if len(sys.argv) < 3
{
Error("Invalid number of arguments");
}
else
{
if os.path.isfile(sys.argv[2])
{
parser = Parser(this.GetCode((sys.argv[2])));
interpreter = Interpreter();
interpreter.Interpret(parser.code);
}
else
{
Error("File not found");
}
}
}
else if os.path.isfile(sys.argv[1])
{
parser = Parser(this.GetCode(sys.argv[1]));
interpreter = Interpreter();
interpreter.Interpret(parser.code);
}
else if sys.argv[1] == "--transpile" or sys.argv[1] == "-t"
{
if len(sys.argv) < 4
{
Error("Invalid number of arguments");
}
else
{
if os.path.isfile(sys.argv[2])
{
parser = Parser(this.GetCode((sys.argv[2])));
with open(sys.argv[3], "w") as f
{
f.write(parser.code);
}
}
else
{
Error("Input file not found");
}
}
}
else if sys.argv[1] == "--compile" or sys.argv[1] == "-c"
{
if len(sys.argv) < 4
{
Error("Invalid number of arguments");
}
else
{
if os.path.isfile(sys.argv[2])
{
parser = Parser(this.GetCode((sys.argv[2])));
fileName = sys.argv[3].split(".")[0];
with open(fileName + ".py", "w") as f
{
f.write(parser.code);
}
if (os.path.isfile(fileName + ".exe"))
{
os.remove(fileName + ".exe");
}
subprocess.call(["PyInstaller", fileName + ".py", "--onefile", "--log-level", "ERROR"]);
os.rename("dist/{}".format(fileName+".exe"),"./"+sys.argv[3]);
os.remove(fileName + ".py");
os.remove(fileName + ".spec");
shutil.rmtree("build");
shutil.rmtree("dist");
}
else
{
Error("File not found");
}
}
}
else
{
Error("Invalid argument");
}
if (os.path.isfile("output.py"))
{
os.remove("output.py");
}
}
function CheckArgs()
{
if len(sys.argv) < 2:
Error("Invalid number of arguments");
this.HandleArgs();
}
function Main()
{
this.CheckArgs();
}
}