-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmbeddedDARTEdgeAI.js
More file actions
433 lines (368 loc) · 14.5 KB
/
EmbeddedDARTEdgeAI.js
File metadata and controls
433 lines (368 loc) · 14.5 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Section 1 Emulated FPGA Hardware for Html Editor
//Embedded DART Edge AI FPGA EMULATOR
//https://raw.githubusercontent.com/radicaldeepscale/DARTEdgeAI/main/EmbeddedDARTEdgeAI.js
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DARTLexer {
public List<String> lex(String code) {
List<String> tokens = new ArrayList<>();
// Rule 0: Opening and Closing Brackets
applyRule(code, "\\(.*\\)", "BRACKETS BLOCK", tokens);
// Rule 1: Denoting characters for Memory Allocation
applyRule(code, "''.*", "DENOTION BLOCK", tokens);
// Rule 2: Accessing memory
applyRule(code, "::.*", "ACCESS BLOCK", tokens);
// Rule 3: Combining DATA
applyRule(code, "\\|.*", "COMBINING BLOCK", tokens);
// Rule 4: Peripheral access
applyRule(code, "\\|::\\|.*", "PERIPHERAL BLOCK", tokens);
// Rule 5: Function Begin
applyRule(code, "\\(.*\\):-: \\{.*", "FUNCTION BEGIN BLOCK", tokens);
// Rule 6: Language pattern "Is the same as / and, or, comparison"
applyRule(code, "==.*", "COMPARISON BLOCK", tokens);
// Rule 7: Language pattern "is not the same as"
applyRule(code, "-=.*", "NOT BLOCK", tokens);
// Rule 8: Combine strings with numbers or objects
applyRule(code, "\\+=.*", "STRINGS OR OBJECTS BLOCK", tokens);
// Rule 9: Function End
applyRule(code, "}.*", "FUNCTION END BLOCK", tokens);
// Rule 10: Comment out commands
applyRule(code, "//.*", "COMMENT BLOCK 1", tokens);
applyRule(code, "/\\*.*\\*/", "COMMENT BLOCK 2", tokens);
// Rule 11: Define variables
applyRule(code, "var \\(.*\\).*", "VARIABLE BLOCK", tokens);
// Rule 12: Write code to text file
applyRule(code, "\\(file\\)::.*", "WRITE TO FILE BLOCK", tokens);
// Rule 13: Compile Code Language Patterns
applyRule(code, "}:::.*", "FINISH CODE FOR BUILD AND EXECUTE BLOCK", tokens);
// Rule 14: Variables must be declared immediately after the open curly brace
applyRule(code, "\\{.*var \\(.*\\).*", "VARIABLE DECLARATION BLOCK", tokens);
// Rule 15: Variable assignment
applyRule(code, "\\(\\).*==\\(\\).*", "VARIABLE ASSIGNMENT BLOCK", tokens);
// Rule 16: Variables can be set not equal to one another
applyRule(code, "\\(\\).*-=\\(\\).*", "VARIABLE SET TO NOT BLOCK", tokens);
// Rule 17: Explicit Character Patterns - 'with'
applyRule(code, "with.*", "WITH BLOCK", tokens);
// Rule 18: Explicit Character Patterns - 'place'
applyRule(code, "place.*", "PLACE BLOCK", tokens);
// Rule 19: Explicit Character Patterns - 'andwith'
applyRule(code, "andwith.*", "COMBINE STATEMENTS BLOCK 1", tokens);
// Rule 20: Explicit Character Patterns - 'thenplace'
applyRule(code, "thenplace.*", "ADD TO STATEMENT BLOCK", tokens);
// Rule 21: Explicit Character Patterns - 'when'
applyRule(code, "when.*", "STATEMENT OPERATION CONTEXT BLOCK", tokens);
// Rule 22: 'irin' takes the user input from console or editor
applyRule(code, "irin.*", "TAKE USER INPUT BLOCK", tokens);
// Rule 23: 'irout' prints the function or statement result to console or editor
applyRule(code, "irout.*", "DISPLAY OUTPUT BLOCK", tokens);
// Rule 24: Grouping functions
applyRule(code, "\\|';'\\|.*", "GROUP FUNCTION BLOCK", tokens);
// Rule 25: Binding as interpreter function or troubleshooter
applyRule(code, "\\|'-';\\|.*", "INTERPRETER SYNTAX ACCESS BLOCK 1", tokens);
return tokens;
}
private void applyRule(String code, String regex, String tokenType, List<String> tokens) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(code);
while (matcher.find()) {
String token = matcher.group().trim();
tokens.add(token + " (" + tokenType + ")");
}
}
}
// Section 2 Operand Check for loaded .edge in Html Editor
function lex(code) {
let tokens = [];
for (let i = 0; i < code.length; i++) {
let currentChar = code.charAt(i);
// Rule 0: Opening and Closing Brackets
if (currentChar === "(") {
let bracketsBlock = "";
i++;
while (i < code.length && code.charAt(i) !== ")") {
bracketsBlock += code.charAt(i);
i++;
}
tokens.push({ type: "BRACKETS BLOCK", value: bracketsBlock });
continue;
}
// Rule 1: Denoting characters for Memory Allocation
if (currentChar === "'") {
let denotionBlock = "";
while (i < code.length && code.charAt(i) !== "'") {
denotionBlock += code.charAt(i);
i++;
}
tokens.push({ type: "DENOTION BLOCK", value: denotionBlock });
continue;
}
// Rule 2: Accessing memory
if (currentChar === ":" && code.charAt(i + 1) === ":") {
let accessBlock = "";
i += 2;
while (i < code.length && code.charAt(i) !== "::") {
accessBlock += code.charAt(i);
i++;
}
tokens.push({ type: "ACCESS BLOCK", value: accessBlock });
continue;
}
// Rule 3: Combining DATA
if (currentChar === "|") {
let combiningBlock = "";
while (i < code.length && code.charAt(i) !== "|") {
combiningBlock += code.charAt(i);
i++;
}
tokens.push({ type: "COMBINING BLOCK", value: combiningBlock });
continue;
}
// Rule 4: Peripheral access
if (currentChar === "|" && code.charAt(i + 1) === ":" && code.charAt(i + 2) === "|") {
let peripheralBlock = "";
i += 3;
while (i < code.length && code.charAt(i) !== "|") {
peripheralBlock += code.charAt(i);
i++;
}
tokens.push({ type: "PERIPHERAL BLOCK", value: peripheralBlock });
continue;
}
// Rule 5: Function Begin
if (currentChar === "(" && code.charAt(i + 1) === ":" && code.charAt(i + 2) === "-" && code.charAt(i + 3) === ":" && code.charAt(i + 4) === " ") {
let functionBeginBlock = "";
i += 5;
while (i < code.length && code.charAt(i) !== "{") {
functionBeginBlock += code.charAt(i);
i++;
}
tokens.push({ type: "FUNCTION BEGIN BLOCK", value: functionBeginBlock });
continue;
}
// Rule 6: Language pattern "Is the same as / and, or, comparison"
if (currentChar === "=" && code.charAt(i + 1) === "=") {
let comparisonBlock = "";
i += 2;
while (i < code.length && code.charAt(i) !== "=") {
comparisonBlock += code.charAt(i);
i++;
}
tokens.push({ type: "COMPARISON BLOCK", value: comparisonBlock });
continue;
}
// Rule 7: Language pattern "is not the same as"
if (currentChar === "-" && code.charAt(i + 1) === "=") {
let notBlock = "";
i += 2;
while (i < code.length && code.charAt(i) !== "=") {
notBlock += code.charAt(i);
i++;
}
tokens.push({ type: "NOT BLOCK", value: notBlock });
continue;
}
// Rule 8: Combine strings with numbers or objects
if (currentChar === "+" && code.charAt(i + 1) === "=") {
let stringsOrObjectsBlock = "";
i += 2;
while (i < code.length && code.charAt(i) !== "=") {
stringsOrObjectsBlock += code.charAt(i);
i++;
}
tokens.push({ type: "STRINGS OR OBJECTS BLOCK", value: stringsOrObjectsBlock });
continue;
}
// Rule 9: Function End
if (currentChar === "}") {
tokens.push({ type: "FUNCTION END BLOCK" });
continue;
}
// Rule 10: Comment out commands
if (currentChar === "/" && code.charAt(i + 1) === "/") {
let commentBlock = "";
i += 2;
while (i < code.length && code.charAt(i) !== "\n") {
commentBlock += code.charAt(i);
i++;
}
tokens.push({ type: "COMMENT BLOCK 1", value: commentBlock });
continue;
}
if (currentChar === "/" && code.charAt(i + 1) === "*" && code.charAt(i + 2) === "*") {
let commentBlock = "";
i += 3;
while (i < code.length && code.charAt(i) !== "*" && code.charAt(i + 1) !== "/") {
commentBlock += code.charAt(i);
i++;
}
tokens.push({ type: "COMMENT BLOCK 2", value: commentBlock });
continue;
}
// Rule 11: Define variables
if (currentChar === "v" && code.substr(i, 4) === "var (") {
let variableBlock = "";
i += 4;
while (i < code.length && code.charAt(i) !== ")") {
variableBlock += code.charAt(i);
i++;
}
tokens.push({ type: "VARIABLE BLOCK", value: variableBlock });
continue;
}
// Rule 12: Write code to text file
if (currentChar === "(" && code.substr(i, 7) === "(file)::") {
let writeFileBlock = "";
i += 7;
while (i < code.length && code.charAt(i) !== ")") {
writeFileBlock += code.charAt(i);
i++;
}
tokens.push({ type: "WRITE TO FILE BLOCK", value: writeFileBlock });
continue;
}
// Rule 13: Compile Code Language Patterns
if (currentChar === "}" && code.substr(i, 4) === "}:::") {
let finishCodeBlock = "";
i += 4;
while (i < code.length) {
finishCodeBlock += code.charAt(i);
i++;
}
tokens.push({ type: "FINISH CODE FOR BUILD AND EXECUTE BLOCK", value: finishCodeBlock });
continue;
}
// Rule 14: Variables must be declared immediately after the open curly brace
if (currentChar === "{" && code.substr(i + 1, 4) === "var (") {
let variableDeclarationBlock = "";
i += 5;
while (i < code.length && code.charAt(i) !== ")") {
variableDeclarationBlock += code.charAt(i);
i++;
}
tokens.push({ type: "VARIABLE DECLARATION BLOCK", value: variableDeclarationBlock });
continue;
}
// Rule 15: Variable assignment
if (currentChar === "(" && code.charAt(i + 1) === ")" && code.charAt(i + 2) === "=" && code.charAt(i + 3) === "(" && code.charAt(i + 4) === ")") {
let variableAssignmentBlock = "";
i += 5;
while (i < code.length && code.charAt(i) !== "=") {
variableAssignmentBlock += code.charAt(i);
i++;
}
tokens.push({ type: "VARIABLE ASSIGNMENT BLOCK", value: variableAssignmentBlock });
continue;
}
// Rule 16: Variables can be set not equal to one another
if (currentChar === "(" && code.charAt(i + 1) === ")" && code.charAt(i + 2) === "-" && code.charAt(i + 3) === "=" && code.charAt(i + 4) === "(" && code.charAt(i + 5) === ")") {
let variableSetToNotBlock = "";
i += 6;
while (i < code.length && code.charAt(i) !== "=") {
variableSetToNotBlock += code.charAt(i);
i++;
}
tokens.push({ type: "VARIABLE SET TO NOT BLOCK", value: variableSetToNotBlock });
continue;
}
// Rule 17: Explicit Character Patterns - 'with'
if (currentChar === "w" && code.substr(i, 4) === "with") {
let withBlock = "";
i += 4;
while (i < code.length && code.charAt(i) !== " ") {
withBlock += code.charAt(i);
i++;
}
tokens.push({ type: "WITH BLOCK", value: withBlock });
continue;
}
// Rule 18: Explicit Character Patterns - 'place'
if (currentChar === "p" && code.substr(i, 5) === "place") {
let placeBlock = "";
i += 5;
while (i < code.length && code.charAt(i) !== " ") {
placeBlock += code.charAt(i);
i++;
}
tokens.push({ type: "PLACE BLOCK", value: placeBlock });
continue;
}
// Rule 19: Explicit Character Patterns - 'andwith'
if (currentChar === "a" && code.substr(i, 7) === "andwith") {
let combineStatementsBlock1 = "";
i += 7;
while (i < code.length && code.charAt(i) !== " ") {
combineStatementsBlock1 += code.charAt(i);
i++;
}
tokens.push({ type: "COMBINE STATEMENTS BLOCK 1", value: combineStatementsBlock1 });
continue;
}
// Rule 20: Explicit Character Patterns - 'thenplace'
if (currentChar === "t" && code.substr(i, 10) === "thenplace") {
let addToStatementBlock = "";
i += 10;
while (i < code.length && code.charAt(i) !== " ") {
addToStatementBlock += code.charAt(i);
i++;
}
tokens.push({ type: "ADD TO STATEMENT BLOCK", value: addToStatementBlock });
continue;
}
// Rule 21: Explicit Character Patterns - 'when'
if (currentChar === "w" && code.substr(i, 4) === "when") {
let statementOperationContextBlock = "";
i += 4;
while (i < code.length && code.charAt(i) !== " ") {
statementOperationContextBlock += code.charAt(i);
i++;
}
tokens.push
// Rule 22: 'irin' takes the user input from console or editor
if (currentChar === "i" && code.substr(i, 4) === "irin") {
let takeUserInputBlock = "";
i += 4;
while (i < code.length && code.charAt(i) !== " ") {
takeUserInputBlock += code.charAt(i);
i++;
}
tokens.push({ type: "TAKE USER INPUT BLOCK", value: takeUserInputBlock });
continue;
}
// Rule 23: 'irout' prints the function or statement result to console or editor
if (currentChar === "i" && code.substr(i, 5) === "irout") {
let displayOutputBlock = "";
i += 5;
while (i < code.length && code.charAt(i) !== " ") {
displayOutputBlock += code.charAt(i);
i++;
}
tokens.push({ type: "DISPLAY OUTPUT BLOCK", value: displayOutputBlock });
continue;
}
// Rule 24: Grouping functions
if (currentChar === "|" && code.charAt(i + 1) === "'" && code.charAt(i + 2) === ";" && code.charAt(i + 3) === "'" && code.charAt(i + 4) === "|") {
let groupFunctionBlock = "";
i += 5;
while (i < code.length && code.charAt(i) !== "|") {
groupFunctionBlock += code.charAt(i);
i++;
}
tokens.push({ type: "GROUP FUNCTION BLOCK", value: groupFunctionBlock });
continue;
}
// Rule 25: Binding as interpreter function or troubleshooter
if (currentChar === "|" && code.charAt(i + 1) === "'" && code.charAt(i + 2) === "-" && code.charAt(i + 3) === "'" && code.charAt(i + 4) === ";" && code.charAt(i + 5) === "'" && code.charAt(i + 6) === "|") {
let interpreterSyntaxAccessBlock1 = "";
i += 7;
while (i < code.length && code.charAt(i) !== "|") {
interpreterSyntaxAccessBlock1 += code.charAt(i);
i++;
}
tokens.push({ type: "INTERPRETER SYNTAX ACCESS BLOCK 1", value: interpreterSyntaxAccessBlock1 });
continue;
}
}
return tokens;
}