-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
233 lines (225 loc) · 5 KB
/
compiler.js
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
//define some global stuff
var code = ""; //code that goes in
var compiledcode = "//Compiled into JavaScript by EasyIO"; //code that comes out
var codebytes = []; //stores "bytes" of code, a list of the tokens
var errorlist = [];
function checkString(variable) { //define a string
var passed = false;
if (variable.charAt(0) === "\"" && variable.charAt(variable.length-1) === "\"") {
passed = true;
}
return passed;
}
function checkNum(variable) { //define a number
var passed = false;
if (parseInt(variable)) {
passed = true;
}
return passed;
}
//lexer, converts code into "bytes", essentially keywords
function lex(code) {
var tok = "";
var state = false;
for (var i=0; i<code.length; i++) {
var char = code.charAt(i);
if (char === " " || char === "\n" && state === false) {
if (state === false) {
if(tok !== "") {
codebytes.push(tok);
tok = "";
}
}
else {
tok += char;
}
}
else if (char === "\"") {
state = !state;
tok += char;
if (state === false) {
codebytes.push(tok);
tok = "";
}
}
else if (char === "+" || char === "-" || char === "*" || char === "/") {
if (state === false) {
if (tok === "") {
tok += char;
codebytes.push(tok);
tok = "";
}
else {
codebytes.push(tok);
tok = "";
tok += char;
codebytes.push(tok);
tok = "";
}
}
else {
tok += char;
}
}
else {
tok += char;
}
}
if(tok !== "") {
codebytes.push(tok);
tok = "";
}
}
//check to make sure commands have the right type parameters
function typecheck(codebytes) {
var passed = true;
for (var i=0; i<codebytes.length; i++) {
var byte = codebytes[i];
if (byte === "+" || byte === "-") {
var term1 = codebytes[i-1];
var term2 = codebytes[i+1];
if (!checkNum(term1) && !checkString(term1)) {
errorlist.push("addsubfail");
passed = false;
}
else if (!checkNum(term2) && !checkString(term2)) {
errorlist.push("addsubfail");
passed = false;
}
}
else if (byte === "*" || byte === "/") {
var term1 = codebytes[i-1];
var term2 = codebytes[i+1];
if (!checkNum(term1) || !checkNum(term2)) {
errorlist.push("multdivfail");
passed = false;
}
}
else if (byte === "print") {
i += 1;
if (codebytes[i]) {
var sbyte = codebytes[i];
if (checkNum(sbyte) === false && checkString(sbyte) === false) {
errorlist.push("printfail");
passed = false;
}
}
else {
errorlist.push("printfail");
passed = false;
}
}
}
return passed;
}
//converter, changes variables into javascript vars
function convert(codebytes) {
for (var i=0; i<codebytes.length; i++) {
var byte = codebytes[i];
if (checkString(byte)) {
codebytes[i] = byte.substr(1, byte.length-2)
}
else if (checkNum(byte)) {
codebytes[i] = parseInt(byte);
}
}
}
//solve expressions
function solve(codebytes) {
for (var i=0; i<codebytes.length; i++) {
var byte = codebytes[i];
if (byte === "+") {
codebytes[i-1] += codebytes[i+1];
codebytes.splice(i,i);
i -= 1;
}
else if (byte === "-") {
codebytes[i-1] -= codebytes[i+1];
codebytes.splice(i,i);
i -= 1;
}
else if (byte === "*") {
codebytes[i-1] *= codebytes[i+1];
codebytes.splice(i,i);
i -= 1;
}
else if (byte === "/") {
codebytes[i-1] /= codebytes[i+1];
codebytes.splice(i,i);
i -= 1;
}
}
}
//parser, converts "bytes" into actual javascript code
function parse(codebytes) {
for (var i=0; i<codebytes.length; i++) {
var byte = codebytes[i];
if (byte === "print") {
compiledcode += "\n"
compiledcode += "consolePrint(\"";
i += 1;
var sbyte = codebytes[i];
compiledcode += sbyte + "\");";
}
else {
errorlist.push("parsefail");
i = codebytes.length;
passed = false;
}
}
}
//handle errors
function errorhandle(errorlist) {
if (errorlist.length > 0) {
consoleClear();
iconsole.style.color = "red";
for (var i=0; i<errorlist.length; i++) {
var error = errorlist[i];
if (error === "parsefail") {
consolePrint("Parsing Failed: invalid command.");
}
else if (error === "addsubfail") {
consolePrint("TypeCheck Failed: you can only add/subtract variables.");
}
else if (error === "printfail") {
consolePrint("TypeCheck Failed: you can only print variables.");
}
else if (error === "multdivfail") {
consolePrint("TypeCheck Failed: you can only multiply/divide numbers.");
}
}
}
}
//reset global variables
function reset() {
code = "";
compiledcode = "//Compiled into JavaScript by EasyIO";
codebytes = [];
errorlist = [];
}
//run the lexer, typechecker, parser
function compile(code) {
consoleClear();
reset();
lex(code);
if (errorlist.length === 0) {
progressbar.value = 15;
typecheck(codebytes);
}
if (errorlist.length === 0) {
progressbar.value = 30;
convert(codebytes);
}
if (errorlist.length === 0) {
progressbar.value = 45;
solve(codebytes);
}
if (errorlist.length === 0) {
progressbar.value = 60;
parse(codebytes);
}
if (errorlist.length === 0) {
progressbar.value = 100;
}
errorhandle(errorlist);
}