-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntryPoint.cs
149 lines (118 loc) · 3.63 KB
/
EntryPoint.cs
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
using propcalc;
bool[] mode = new bool[7];
const int no_table = 0;
const int props = 1;
const int compile = 2;
const int steps = 3;
const int output = 4;
const int shell = 5;
const int no_calc = 6;
bool fileIsCompiled = false;
string rawCode = "";
string extension;
string filenoext;
// Program execution
getArguments();
if (mode[shell] && onShell())
exitProgram(0);
if (mode[output])
onFileOutput();
else
onConsoleOutput();
if (onFile())
exitProgram(0);
// On success end program
void getArguments() {
if (args.Length == 0)
args = new[] {"shell"}; // Default execution path is shell
extension = Path.GetExtension(args[0]);
filenoext = Path.GetFileNameWithoutExtension(args[0]);
for (int i = 0; i < args.Length; ++i) {
switch (args[i]) {
case "no_table": mode[no_table] = true; break;
case "props" : mode[props] = true; break;
case "compile" : mode[compile] = true; break;
case "steps" : mode[steps] = true; break;
case "out" : mode[output] = true; break;
case "shell" : mode[shell] = true; break;
case "no_calc" : mode[no_calc] = true; break;
}
}
}
bool onShell() {
Console.WriteLine("Enter a formula:");
rawCode = Console.ReadLine() ?? string.Empty;
Console.WriteLine();
if (mode[output])
onFileOutput();
else
onConsoleOutput();
parse();
return true;
}
void onFileOutput() {
FileStream stream = new(filenoext + "_out.txt", FileMode.Create);
StreamWriter writer = new(stream);
writer.AutoFlush = false;
Console.SetOut(writer);
}
void onConsoleOutput() {
StreamWriter writer = new(Console.OpenStandardOutput());
writer.AutoFlush = false;
Console.SetOut(writer);
}
bool onFile() {
switch (extension) {
case ".pcl":
fileIsCompiled = true;
interpret(args[0]);
break;
default:
fileIsCompiled = false;
parse();
break;
}
return true;
}
void parse() {
var (tokens, metadata) = Tokenizer.tokenize(args[0], ref rawCode, mode[shell]);
if (rawCode.Length == 0) // No code, exit program
exitProgram(3);
var split = Splitter.splitBrackets(tokens);
var (atoms, atomBools) = Atomizer.atomize(split);
compileCode(atoms, atomBools, metadata);
}
void compileCode(
Deque< Deque<string> > atoms,
Deque< (bool, bool) > atomBools,
string[] metadata)
{
if (mode[steps])
Steps.showSteps(atoms, atomBools);
if (mode[compile]) {
string compiledFilename = Compiler.compileToFile(atoms, atomBools, metadata, rawCode);
interpret(compiledFilename);
}
else {
Deque<object> compilation = Compiler.compileInMemory(atoms, atomBools, metadata, rawCode);
interpret(compilation);
}
}
void interpret(object interpretableObject) {
Interpreter interpreter;
if (interpretableObject is string compiledFilename)
interpreter = new Interpreter(compiledFilename);
else if (interpretableObject is Deque<object> compilation)
interpreter = new Interpreter(compilation);
else throw new Exception("Object cannot be interpreted.");
if (!mode[no_calc])
interpreter.interpret(!mode[no_table], mode[props]);
if (fileIsCompiled && mode[steps])
Steps.showSteps(args[0]);
if (mode[props] && !mode[no_calc])
interpreter.properties();
}
void exitProgram(int exitCode) {
Console.Out.Flush();
Environment.Exit(exitCode);
}