-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
111 lines (91 loc) · 3.22 KB
/
parser.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
function run() {
reset();
const codeRunner = document.getElementById("code_runner");
codeRunner.innerHTML = "";
const codeLines = document.getElementById("code").value.split("\n");
codeLines.forEach(line => {
line = line.trim();
if (line && line[0] !== "*") {
let newRunnerLine = document.createElement("li");
newRunnerLine.appendChild(document.createTextNode(line.toLowerCase()));
codeRunner.appendChild(newRunnerLine);
}
});
parse(codeRunner);
}
function reset() {
robo.hasBox = false;
box.hide();
robo.hide();
box.show(document.getElementById("box_x").value, document.getElementById("box_y").value);
robo.show(document.getElementById("robo_x").value, document.getElementById("robo_y").value);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function parse(codeRunner) {
const codeEditor = document.getElementById("code_editor");
codeEditor.style.display = "none";
codeRunner.style.display = "initial";
let lineCorrect = false;
for (let lineToParse of codeRunner.getElementsByTagName("li")) {
let pauseMilliseconds = 3000;
lineToParse.style.fontWeight = "bold";
const tokens = lineToParse.innerText.split(" ");
tokens[tokens.length - 1] = tokens[tokens.length - 1].replace(/\;$/, '');
if (tokens[0] == "program") {
await sleep(1000);
lineToParse.style.fontWeight = "normal";
continue;
}
else if (tokens[0] == "begin") {
await sleep(1000);
lineToParse.style.fontWeight = "normal";
continue;
}
else if (tokens[0] == "end") {
break;
}
else if (tokens[0] == "say" || tokens[0] == "say:") {
tokens.shift();
robo.say(tokens.join(" "));
await sleep(3000);
robo.shutUp();
lineCorrect = true;
pauseMilliseconds = 0;
}
else if (tokens[0] == "move_sur") {
lineCorrect = robo.move(null, robo.y + parseInt(tokens[1]));
}
else if (tokens[0] == "move_norte") {
lineCorrect = robo.move(null, robo.y - parseInt(tokens[1]));
}
else if (tokens[0] == "move_este") {
lineCorrect = robo.move(robo.x + parseInt(tokens[1]), null);
}
else if (tokens[0] == "move_oeste") {
lineCorrect = robo.move(robo.x - parseInt(tokens[1]), null);
}
else if (tokens[0] == "take_box") {
lineCorrect = robo.getBox();
}
else if (tokens[0] == "put_box") {
lineCorrect = robo.dropBox();
}
else if (tokens[0] == "move") {
lineCorrect = robo.move(tokens[1], tokens[2]);
} else {
alert("¡¡¡Orden " + tokens[0] + " desconocida!!!");
break;
}
if (lineCorrect) {
await sleep(pauseMilliseconds);
} else {
alert("¡¡¡Error al ejecutar la orden " + tokens[0] + "!!!");
break;
}
lineToParse.style.fontWeight = "normal";
}
codeRunner.style.display = "none";
codeEditor.style.display = "initial";
}