This repository was archived by the owner on Oct 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.js
More file actions
258 lines (258 loc) · 9.8 KB
/
Copy pathcontrol.js
File metadata and controls
258 lines (258 loc) · 9.8 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
var stack = [];
var stackWithTheBeginningsOfTheLoops = []; // To implement "continue"s...
var stackWithTheEndsOfTheLoops = []; // To implement "break"s...
var hasElse = [];
isAssembly = false;
function compileString(str) {
str = str.replace(/^\s*/, "");
var commentSign;
if (/^syntax\s*gas/i.test(str)) {
syntax = "gas";
return;
} else if (/^syntax\s*fasm/i.test(str)) {
syntax = "fasm";
return;
} else if (/^syntax\s/i.test(str))
throw "Driver error: Unrecognized syntax name!";
if (syntax == "fasm") commentSign = ";";
else if (syntax == "gas") commentSign = "#";
else throw 'Driver error: Unrecognized syntax name "' + syntax + '"!';
if (/^verboseMode\s*on/i.test(str)) {
verboseMode = true;
return;
} else if (/^verboseMode\s*off/i.test(str)) {
verboseMode = false;
return;
} else if (/^verboseMode\s*/i.test(str))
throw 'Driver error: Please specify "VerboseMode" (for debugging the compiler) to be either "on" or "off"!';
if (/^AsmEnd/.test(str)) {
asm(commentSign + str);
asm(commentSign + "Inline assembly ended.");
isAssembly = false;
return;
}
if (/^AsmStart/.test(str)) {
isAssembly = true;
asm(commentSign + str);
asm(commentSign + "Inline assembly begins.");
return;
}
if (isAssembly) {
asm(str);
return;
}
asm(commentSign + str);
if (!str || str[0] == ";") {
if (verboseMode)
asm(commentSign + "The entire line is a comment, moving on...");
return;
}
var isString = false;
for (var i = 0; i < str.length; i++)
if (str[i] == '"' || str[i] == "'") isString = !isString;
else if (str[i] == ";" && !isString) {
//Obrisi komentar, ako se slucajno nesto kao '<=' ili ':=' nalazi u komentaru, da ne smeta parseru.
str = str.substr(0, i);
break;
}
if (syntax == "gas") {
if (verboseMode)
asm(
commentSign +
"We don't know which mode the assembler is in right now, so let's switch it to the intel_syntax mode."
);
asm(".intel_syntax noprefix");
}
if (verboseMode) asm(commentSign + "Initializing the FPU stack...");
asm("finit");
if (str.indexOf("<=") + 1) {
if (verboseMode) asm(commentSign + "Type of the directive is: constant.");
var constantName = str.substr(0, str.indexOf("<="));
constantName = constantName.replace(/\s*$/, "");
var constantValue = str.substr(str.indexOf("<=") + "<=".length);
asm("jmp " + constantName + "$");
if (syntax == "fasm") asm(constantName + " db " + constantValue);
else {
asm(constantName + ":");
asm(".ascii " + constantValue);
}
asm(constantName + "$:");
} else if (str.indexOf(":=") + 1) {
if (verboseMode) asm(commentSign + "Type of the directive is: assignment.");
var variableName = str.substr(0, str.indexOf(":="));
variableName = variableName.replace(/\s*$/, "");
var arth = str.substr(str.indexOf(":=") + ":=".length);
if (verboseMode) asm(commentSign + "Calculating the rvalue...");
parseArth(tokenizeArth(arth)).compile();
if (verboseMode)
asm(commentSign + 'Storing the top of the FPU stack into "edx".');
if (syntax == "fasm") asm("fstp dword [result]");
else asm("fstp dword ptr [result]");
if (syntax == "fasm") asm("mov edx, dword [result]");
else asm("mov edx, dword ptr [result]");
if (variableName.indexOf("[") + 1 || variableName.indexOf("(") + 1) {
var indexOfBracket =
(variableName.indexOf("[") + 1 || variableName.indexOf("(") + 1) - 1;
var arrayName = variableName.substr(0, indexOfBracket);
arrayName = arrayName.replace(/\s*$/, "");
var subscript = variableName.substr(
indexOfBracket + 1,
variableName.length - indexOfBracket - 2
);
if (verboseMode) asm(commentSign + "Calculating the l-value...");
parseArth(tokenizeArth(subscript)).compile();
if (verboseMode)
asm(commentSign + 'Moving the pointer from "st0" to "ebx".');
if (syntax == "fasm") asm("fistp dword [result]");
else asm("fistp dword ptr [result]");
if (syntax == "fasm") asm("mov ebx, dword [result]");
else asm("mov ebx, dword ptr [result]");
if (verboseMode)
asm(
commentSign +
'Storing the r-value (now in "edx") where "ebx" points to.'
);
if (syntax == "fasm") asm("mov dword [" + arrayName + "+4*ebx],edx");
else asm("mov dword ptr [" + arrayName + "+4*ebx],edx");
} else {
if (verboseMode)
asm(
commentSign + 'Storing the r-value (now in "edx") into the variable.'
);
if (syntax == "fasm") asm("mov dword [" + variableName + "],edx");
else asm("mov dword ptr [" + variableName + "],edx");
}
} else if (/^If\s/.test(str)) {
if (verboseMode)
asm(commentSign + "Type of the directive is: if-statement.");
var arth = str.substr("If ".length);
if (verboseMode) asm(commentSign + "Calculating the expression...");
parseArth(tokenizeArth(arth)).compile();
if (verboseMode)
asm(commentSign + "Comparing the just-calculated expression with 0...");
if (syntax == "fasm") asm("fistp dword [result]");
else asm("fistp dword ptr [result]");
if (syntax == "fasm") asm("mov eax, dword [result]");
else asm("mov eax, dword ptr [result]");
asm("test eax,eax");
if (verboseMode)
asm(commentSign + "Branching based on whether the expression is 0...");
var label1 = "ElseLabel" + Math.floor(Math.random() * 1000000);
var label2 = "EndIfLabel" + Math.floor(Math.random() * 1000000);
stack.push(label2);
stack.push(label1);
asm("jz " + label1);
hasElse.push(false);
} else if (/^EndIf/.test(str)) {
if (verboseMode)
asm(commentSign + "Type of the directive is: EndIf-statement.");
if (hasElse.pop()) asm(stack.pop() + ":");
else {
asm(stack.pop() + ":");
asm(stack.pop() + ":");
}
} else if (/^ElseIf\s/.test(str)) {
if (verboseMode)
asm(commentSign + "Type of the directive is: ElseIf-statement.");
var labelOfElse = stack.pop();
var labelOfEndIf = stack.pop();
if (verboseMode)
asm(
commentSign + "If the expression in the If-statement evaluates to 1..."
);
asm("jmp " + labelOfEndIf);
if (verboseMode) asm(commentSign + "If it evaluates to 0...");
asm(labelOfElse + ":");
var arth = str.substr("ElseIf ".length);
if (verboseMode)
asm(
commentSign + "Evaluating the expression after the ElseIf keyword..."
);
parseArth(tokenizeArth(arth)).compile();
if (verboseMode) asm(commentSign + "Comparing that expression to 0...");
if (syntax == "fasm") asm("fistp dword [result]");
else asm("fistp dword ptr [result]");
if (syntax == "fasm") asm("mov eax, dword [result]");
else asm("mov eax, dword ptr [result]");
asm("test eax,eax");
if (verboseMode)
asm(commentSign + "Branching based on whether it was 0...");
var newLabelOfElse = "ElseLabel" + Math.floor(Math.random() * 1000000);
stack.push(labelOfEndIf);
stack.push(newLabelOfElse);
asm("jz " + newLabelOfElse);
} else if (/^Else/.test(str)) {
if (verboseMode)
asm(commentSign + "Type of the directive: Else-statement.");
var labelOfElse = stack.pop();
var labelOfEndIf = stack.pop();
asm("jmp " + labelOfEndIf);
asm(labelOfElse + ":");
stack.push(labelOfEndIf);
hasElse.pop();
hasElse.push(true);
} else if (/^While\s/.test(str)) {
if (verboseMode)
asm(commentSign + "Type of the directive: beginning of the while-loop");
var arth = str.substr("While ".length);
var label1 = "WhileLabel" + Math.floor(Math.random() * 1000000);
stack.push(label1);
stackWithTheBeginningsOfTheLoops.push(label1);
if (verboseMode)
asm(
commentSign +
"Marking where the evaluation of the expression begins (because it needs to be repeated once we come to the end of the loop)."
);
asm(label1 + ":");
if (verboseMode)
asm(commentSign + 'Evaluating the expression after the "While" keyword');
parseArth(tokenizeArth(arth)).compile();
if (verboseMode) asm(commentSign + "Comparing the expression to 0...");
var label2 = "EndWhileLabel" + Math.floor(Math.random() * 1000000);
stackWithTheEndsOfTheLoops.push(label2);
if (syntax == "fasm") asm("fistp dword [result]");
else asm("fistp dword ptr [result]");
if (syntax == "fasm") asm("mov eax, dword [result]");
else asm("mov eax, dword ptr [result]");
asm("test eax,eax");
if (verboseMode) asm(commentSign + "Branching based on whether it is 0...");
asm("je " + label2);
stack.push(label2);
} else if (/^EndWhile/.test(str)) {
if (verboseMode)
asm(commentSign + "Type of the directive: end of the while-loop.");
var endWhileLabel = stack.pop();
var whileLabel = stack.pop();
asm("jmp " + whileLabel);
asm(endWhileLabel + ":");
stackWithTheBeginningsOfTheLoops.pop();
stackWithTheEndsOfTheLoops.pop();
} else if (/^Break/.test(str)) {
asm("jmp " + stackWithTheEndsOfTheLoops[stackWithTheEndsOfTheLoops.length - 1]);
}
else if (/^Continue/.test(str)) {
asm("jmp " + stackWithTheBeginningsOfTheLoops[stackWithTheBeginningsOfTheLoops.length - 1]);
}
else {
if (verboseMode)
asm(
commentSign +
'Type of the statement: unrecognized, passing it unchanged to "compiler.js".'
);
parseArth(tokenizeArth(str)).compile();
if (verboseMode)
asm(
commentSign + 'Storing the just-computed expression into "result"...'
);
if (syntax == "fasm") asm("fstp dword [result]");
else asm("fstp dword ptr [result]");
}
if (syntax == "gas") {
if (verboseMode)
asm(
commentSign +
"We don't know what comes next, whichever program will control the assembler next will likely expect it to be in the att_syntax mode."
);
asm(".att_syntax");
}
}