-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.js
56 lines (45 loc) · 1.67 KB
/
program.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
function Program(instructionArray) {
this.instructionArray = instructionArray;
this.instructionPointer = -1;
this.functions = {};
this.functionCreationStack = [];
}
Program.prototype.nextInstruction = function () {
return this.instructionArray[++this.instructionPointer];
};
Program.prototype.getInstructionPointer = function () {
return this.instructionPointer;
};
Program.prototype.setInstructionPointer = function (instructionPointer) {
this.instructionPointer = instructionPointer;
};
Program.prototype.setInstructionPointerToFunction = function (functionName) {
var startInstructionPointer =
this.functions[functionName].startInstructionPointer;
if (!startInstructionPointer) {
startInstructionPointer = this.instructionArray.length;
this.instructionArray =
this.instructionArray.concat(this.functions[functionName]);
this.functions[functionName].startInstructionPointer =
startInstructionPointer;
}
this.instructionPointer = startInstructionPointer - 1;
};
Program.prototype.createFunction = function (name) {
this.functions[name] = [];
this.functionCreationStack.push(name);
};
Program.prototype.endCreateFunction = function () {
this.functionCreationStack.pop();
};
Program.prototype.addToFunction = function (instruction) {
var currentFunctionName =
this.functionCreationStack[this.functionCreationStack.length - 1];
this.functions[currentFunctionName].push(instruction);
};
Program.prototype.isCreatingFunction = function () {
return this.functionCreationStack.length;
};
Program.prototype.getFunction = function (name) {
return this.functions[name];
};