forked from Dukweeno/Duckuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuckuino.js
243 lines (203 loc) · 6.9 KB
/
Duckuino.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
234
235
236
237
238
239
240
241
242
243
/**
* Duckuino, an open source project licenced under MIT License
* GitHub repo can be found at the following link:
* - https://github.com/Nurrl/Duckuino
*/
/* jshint esversion: 6 */
/* Function to request files */
function getFile(sUrl) {
/* Init request */
var oReq = new XMLHttpRequest();
/* Sending request */
oReq.open("get", sUrl, false);
oReq.overrideMimeType("text/plain");
oReq.send(null);
/* Getting response */
if (oReq.readyState == 4 && (oReq.status == 200 || oReq.status === 0)) {
return oReq.responseText;
} else {
return undefined;
}
}
class Duckuino {
constructor() {}
listModules() {
/* List all modules in the moduleList file */
if (!this.moduleArray) {
this.moduleArray = getFile("modules/modules").split('\n');
this.moduleArray.pop();
}
/* Return the list */
return this.moduleArray;
}
loadModule(moduleName) {
/* Check if module exists */
if (this.listModules().indexOf(moduleName) == -1) {
console.error("Error: This module doesn't exist !");
/* Module is not loaded */
this.loadedModule = undefined;
} else {
/* Load module *//* jshint evil:true */
this.loadedModule = eval(getFile("modules/" + moduleName + ".js"));
}
}
/* TO-DO: getModuleInfos() {} */
compileCode(compileStr) {
/* Init timer */
var timerStart = window.performance.now();
/* Check if module loaded */
if (this.loadedModule === undefined) {
return {
compiledCode: undefined,
compileTime: -1,
returnCode: 9,
returnMessage: "Error: No module loaded !",
errorList: this.errorList
};
}
/* Check if code is empty */
if (compileStr === undefined || compileStr === "") {
return {
compiledCode: undefined,
compileTime: -1,
returnCode: 9,
returnMessage: "Error: No input was entered !",
errorList: this.errorList
};
}
/* Trim whitespaces and tabs */
compileStr = compileStr.replace(/^ +| +$/gm, '');
compileStr = compileStr.replace(/\t/g, '');
/* Errors */
this.errorList = [];
/* Preset all used vars */
this.dataStorage = new Object();
this.compiledCode = '';
var commandKnown;
var lineStr;
var lastLine = ''; var lastLineCount = 0;
/* Cut the input in lines */
var lineArray = compileStr.split('\n');
/* Loop every line */
for (var i = 0; i < lineArray.length; i++)
{
/* Line empty, skip */
if (lineArray[i] === '' || lineArray[i] === '\n')
continue;
/* Reset vars */
commandKnown = false;
lineStr = '';
/* Split lines in words */
var argList = lineArray[i].split(' ');
var argOne = argList[0];
/* Parse commands */
if (this.loadedModule.functionMap[argOne] !== undefined) {
var µ = new Object({
keyMap: this.loadedModule.keyMap,
/**
* Pushes the error to the global error list.
*/
throwError: function(thisPtr, currentLine) {
return function(errorMessage) {
thisPtr.errorList.push({
errorMessage: errorMessage,
errorLine: currentLine
});
};
}(this, (i + 1)),
/**
* This one is to get the lastLine, and in the same way trim
* it from from the current compiledCode, this is a workaround for
* the REPLAY command.
*/
trimLast: function(thisPtr, lastLine, lastLineCount) {
return function() {
var tmpVar = thisPtr.compiledCode.split('\n');
tmpVar.splice(-lastLineCount, lastLineCount - 1);
thisPtr.compiledCode = tmpVar.join('\n');
return lastLine;
};
}(this, lastLine, lastLineCount),
/**
* Those two function are used to store persistent data, i.e:
* Default Delay.
*/
setData: function(thisPtr) {
return function(dataName, dataValue) {
thisPtr.dataStorage[dataName] = dataValue;
};
}(this),
getData: function(thisPtr) {
return function(dataName) {
return thisPtr.dataStorage[dataName];
};
}(this),
});
/* Execute the function and add the returned string to the current string */
lineStr += this.loadedModule.functionMap[argOne](argList, µ);
/* Post process the line */
lineStr = this.loadedModule.postLine(lineStr, µ);
} else { /* Parse keystokes */
var strokeArray = Array();
for(var y = 0; y < argList.length; y++) {
if(this.loadedModule.commandMap[argList[y]] !== undefined) {
/* Push key to Array */
strokeArray.push(this.loadedModule.commandMap[argList[y]]);
} else if(this.loadedModule.comboMap[argList[y]] !== undefined) {
/* Push key to Array */
strokeArray.push(this.loadedModule.comboMap[argList[y]]);
} else if(this.loadedModule.keyMap[argList[y]] !== undefined && y != 0) {
/* Push key to Array */
strokeArray.push('"' + this.loadedModule.keyMap[argList[y]] + '"');
} else {
/* If command unknown, throw error */
this.errorList.push({
errorMessage: "Unknown command or key: '" + argList[y] + "'",
errorLine: (i + 1)
});
}
}
/* Transform key array to string */
lineStr += this.loadedModule.computeKeys(strokeArray);
}
/* Calculate line count */
lastLineCount = lineStr.split('\n').length;
/* Append this compiled line to global output */
lastLine = lineStr;
this.compiledCode += lineStr;
}
/* Stop timer */
var timerEnd = window.performance.now();
var timeElapsed = (timerEnd - timerStart).toFixed(2);
/* Return error if error and code if not */
if (this.errorList.length > 0) {
/* Return error(s) */
return {
compiledCode: undefined,
compileTime: -1,
returnCode: 1,
returnMessage: function(errorList) {
var errorString;
if(errorList.length > 1) {
errorString = "The compiler returned some errors:\n";
} else {
errorString = "The compiler returned an error:\n";
}
errorList.forEach(function(errorObj) {
errorString += "Line " + errorObj.errorLine + " -> " + errorObj.errorMessage + "\n";
});
return errorString;
}(this.errorList),
errorList: this.errorList
};
} else {
/* Return the compiled code */
return {
compiledCode: this.loadedModule.getFinalCode(this.compiledCode),
compileTime: timeElapsed,
returnCode: 0,
returnMessage: "Info: Code successfully parsed in " + timeElapsed + "ms !"
};
}
}
}