-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.js
217 lines (157 loc) · 6.29 KB
/
interpreter.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
var script = "";
var hasErrored = false;
function addLineToScript(newLine) {
script += newLine + "\n";
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, "g"), replace);
}
function parseConditionalExpression(expression) {
expression = replaceAll(expression, " is ", " === ");
expression = replaceAll(expression, " is not ", " != ");
expression = replaceAll(expression, " and ", " && ");
expression = replaceAll(expression, " or ", " || ");
expression = expression.replace(/([a-zA-Z0-9]+) is inside of ([a-zA-Z0-9]+)/g, "$2.indexOf($1) > -1");
return expression;
}
function showError(error, parseError) {
document.getElementById("error").className = "alert alert-danger";
document.getElementById("error-text").innerHTML += error + "<br>";
if(parseError) {
document.getElementById("error-title").innerHTML = "Oh no! There are errors in your code:<br>";
} else {
document.getElementById("error-title").innerHTML = "Oh no! There was an error while trying to run your code:<br>";
}
hasErrored = true;
}
function hideError() {
document.getElementById("error").className = "hidden";
document.getElementById("error-text").innerHTML = "";
hasErrored = false;
}
function parseFunctionCalls(expression) {
return expression.replace(/<([^<>]+?) with ([^<>]+?)>/g, function(x,y,z) {
return intoFunctionName(y) + "(" + z + ")";
});
}
function removeIndent(expression) {
return expression.replace(/^ +/, "");
}
function intoFunctionName(expression) {
return replaceAll(expression, " ", "_");
}
function checkForError(matches, lineNumber) {
if (matches == null) {
showError("We can't understand line " + (lineNumber + 1), true);
return false;
}
return true;
}
function execute() {
var x = document.getElementById("text").value.split("\n");
script = "";
hideError();
for (var i = 0; i < x.length; i++) {
x[i] = parseFunctionCalls(x[i]);
x[i] = removeIndent(x[i]);
var line = x[i].split(" ");
var matches = "";
if (line[0] === "") {
// Do nothing
} else if (line[0] === "Set") {
matches = /Set (.+) to (.+)./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("var " + matches[1] + " = " + matches[2]);
}
} else if (line[0] === "Show") {
matches = /Show (.+)./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("alert(" + matches[1] + ")");
}
} else if (line[0] === "Ask") {
matches = /Ask (".+") for (.+)./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("var " + matches[2] + " = prompt(" + matches[1] + ")");
}
} else if (line[0] === "If") {
matches = /If (.+), then do:/g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("if(" + parseConditionalExpression(matches[1]) + ") {");
}
} else if (line[0] === "End.") {
addLineToScript("}");
} else if (line[0] === "Or") {
if(line[1] == "else") {
addLineToScript("} else {");
} else {
matches = /Or if (.+), then do:/g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("} else if(" + parseConditionalExpression(matches[1]) + ") {");
}
}
} else if (line[0] === "While") {
matches = /While (.+) do:/g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("while(" + parseConditionalExpression(matches[1]) + ") {");
}
} else if (line[0] === "Count") {
matches = /Count until (.+) reaches (-?\d+):/g.exec(x[i]);
if(checkForError(matches, i)) {
var incrementor = "--";
if (parseInt(matches[2]) > 0) {
incrementor = "++";
}
addLineToScript("for(" + matches[1] + " = 0; " + matches[1] + " != " + matches[2] + "; " + matches[1] + incrementor + ") {");
}
} else if (x[i].charAt(0) === "(" && x[i].charAt(x[i].length - 1) === ")") {
matches = /\((.+)\)/g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("// " + matches[1]);
}
} else if (line[0] === "Insert") {
matches = /Insert (.+) into (.+)./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript(matches[2] + ".push(" + matches[1] + ");");
}
} else if (line[0] === "Remove") {
matches = /Remove (.+) from (.+)./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript(matches[2] + ".splice((" + matches[2] + ".indexOf(" + matches[1] + ")), 1)");
}
} else if (line[0] === "For") {
matches = /For every (.+) in (.+) do:/g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("for (var ___ = 0; ___ < " + matches[2] + ".length; ___++) {");
addLineToScript("var " + matches[1] + " = " + matches[2] + "[___];");
}
} else if (line[0] === "Define") {
matches = /Define (.+) with (.+):/g.exec(x[i]);
if(checkForError(matches, i)) {
matches[1] = intoFunctionName(matches[1]);
addLineToScript("function " + matches[1] + "(" + matches[2] + ") {");
}
} else if (x[i].slice(-2) === ").") {
matches = /^([^ ]+?\(.+?\))./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript(matches[1]);
}
} else if (line[0] === "Return") {
matches = /Return (.+)./g.exec(x[i]);
if(checkForError(matches, i)) {
addLineToScript("return " + matches[1]);
}
} else {
showError("We can't understand line " + (i + 1), true);
return
}
}
if(!hasErrored) {
hideError();
console.log(script);
try {
new Function(script)();
} catch(err) {
showError(err, false);
}
}
}