-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcodeInterpreter.js
More file actions
292 lines (280 loc) · 8.83 KB
/
codeInterpreter.js
File metadata and controls
292 lines (280 loc) · 8.83 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Generated by CoffeeScript 1.8.0
(function() {
window.CodeInterpreter = (function() {
/**
* Parses the list of commands and determines
* the number of uses each one has remaining.
* @param commands the list of commands to interpret
*/
function CodeInterpreter(commands) {
var command;
this.commands = commands;
this.buildNeededParsers();
this.usesRemaining = {};
for (command in this.commands) {
this.usesRemaining[command] = this.commands[command]['maxUses'];
}
return;
}
/**
* Builds the parsers needed by the scanText function.
* Uses the current list of commands.
*/
CodeInterpreter.prototype.buildNeededParsers = function() {
var command;
for (command in this.commands) {
this.commands[command]['parser'] = {
'exec': this.buildParser(command)
};
}
};
/**
* Identifies the command executed by the line.
* Returns null if no command is found.
* @param line the line to identify
* @return the identified command, null if none was found
*/
CodeInterpreter.prototype.identifyCommand = function(line) {
var command, found;
for (command in this.commands) {
found = this.commands[command]['parser'].exec(line);
if (found !== null) {
return command;
}
}
return null;
};
/**
* Scans the line for the command being used and
* determines its parameters
* @param line the line to scan for commands
* @return the command used and it's parameters
*/
CodeInterpreter.prototype.scanCommand = function(line) {
var command, parameters, result;
for (command in this.commands) {
result = this.commands[command]['parser'].exec(line);
if (result !== null) {
parameters = this.processCommand(command, result[1]);
return {
"command": command,
"parameters": parameters
};
}
}
return null;
};
/**
*
*/
CodeInterpreter.prototype.executeCommands = function(commandMap) {
var commandCard, _i, _len, _ref;
_ref = this.commandStack;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
commandCard = _ref[_i];
//highlight the line first, then display results
commandMap.highlightCommand(commandCard.line);
commandCard.parameters.push(commandCard.line);
commandMap[commandCard.command].apply(commandMap, commandCard.parameters);
}
commandMap.finishedParsingStartGame();
};
/**
* Scans the selected text and returns a dictionary
* which contains, for each command which which this
* interpretor was constructed, how many uses they have
* remaining.
* @param text the text to scan through
* @return the number of uses remaining for each command
*/
CodeInterpreter.prototype.scanText = function(text) {
var command, currentLine, parameters, result;
this.commandStack = [];
for (command in this.commands) {
this.usesRemaining[command] = this.commands[command]['maxUses'];
}
currentLine = 1;
while (text !== "") {
result = null;
for (command in this.commands) {
result = this.commands[command]['parser'].exec(text);
if (result !== null) {
this.usesRemaining[command]--;
parameters = this.processCommand(command, result[1]);
this.commandStack.push({
command: command,
parameters: parameters,
line: currentLine
});
break;
}
}
if (result === null) {
result = /^\s+/.exec(text);
}
if (result === null) {
result = /^;/.exec(text);
}
if (result === null) {
result = /^\/\/.*(?:\n || $)/.exec(text);
}
if (result === null) {
result = /^\/\*(.*\n)*\*\//.exec(text);
}
if (result === null) {
result = /^.*(?:\n || $)/.exec(text);
}
if (result === null) {
if (text.charAt(0) === '\n') {
currentLine++;
}
text = text.substring(1);
} else {
currentLine += result[0].replace(/[^\n]+/g, "").length;
text = text.substring(result[0].length);
}
}
return this.usesRemaining;
};
/**
* Processes a found command.
* @param command the command to process
* @param innerText the text inside the command to process
* @return the list of parameters for the command
*/
CodeInterpreter.prototype.processCommand = function(command, innerText) {
var parameters, result;
if (typeof innerText === "undefined" || innerText === null || innerText === "") {
return [];
}
parameters = [];
while (innerText !== "") {
result = null;
if (result === null) {
result = /^\s/.exec(innerText);
}
if (result === null) {
result = /^(?:"(.*?)")|(?:'(.*?)')/.exec(innerText);
if (result !== null) {
if (result[1] !== null) {
parameters.push(result[1]);
} else {
parameters.push(result[2]);
}
}
}
if (result === null) {
result = /^(?:[^,]+)/.exec(innerText);
if (result !== null) {
parameters.push(result[0]);
}
}
if (result === null) {
innerText = innerText.substring(1);
} else {
innerText = innerText.substring(result[0].length);
}
}
return parameters;
};
/**
* Creates a parser that will recognize the command.
* It will return an object of the form
* {
* '0': The matched string.
* '1': The inner parameters of the command.
* }
* or it will return null.
* The form of the return object is so that
* it will act somewhat like a RegExp object.
* @param command the command to build a parser for
* @return function representing the command parser
*/
CodeInterpreter.prototype.buildParser = function(command) {
return function(text) {
var commandIndex, commandMatch, commandMatched, continueMatch, innerEndIndex, innerStartIndex, openParenthesis, state, textIndex;
textIndex = 0;
commandIndex = 0;
innerStartIndex = null;
innerEndIndex = null;
state = "beforeCommand";
openParenthesis = 0;
commandMatched = null;
continueMatch = true;
while (continueMatch) {
if (textIndex > text.length) {
continueMatch = false;
}
switch (state) {
case "beforeCommand":
switch (text.charAt(textIndex)) {
case ' ':
break;
case command.charAt(commandIndex):
state = "atCommand";
commandIndex++;
break;
default:
commandMatch = false;
continueMatch = false;
}
break;
case "atCommand":
switch (text.charAt(textIndex)) {
case ' ':
if (commandIndex !== command.length) {
commandMatch = false;
continueMatch = false;
}
break;
case '(':
if (commandIndex === command.length) {
commandMatch = true;
innerStartIndex = textIndex + 1;
openParenthesis = 1;
state = "insideCommand";
} else {
commandMatch = false;
continueMatch = false;
}
break;
case command.charAt(commandIndex):
commandIndex++;
break;
default:
commandMatch = false;
continueMatch = false;
}
break;
case "insideCommand":
switch (text.charAt(textIndex)) {
case '(':
openParenthesis++;
break;
case ')':
openParenthesis--;
if (openParenthesis === 0) {
innerEndIndex = textIndex - 1;
continueMatch = false;
}
break;
case '\n':
innerEndIndex = textIndex;
continueMatch = false;
}
}
textIndex++;
}
if (!commandMatch) {
return null;
}
return {
'0': text.substring(0, textIndex),
'1': text.substring(innerStartIndex, innerEndIndex + 1),
'reason': ""
};
};
};
return CodeInterpreter;
})();
}).call(this);