-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconocedor.js
130 lines (116 loc) · 2.34 KB
/
reconocedor.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
const QE = -1; // Estado de error
// Matriz de transición
const MT_MATRIX = {
// ROBOT
'0,ROBOT': 1,
'1,ID': 2,
'2,,': 1,
'2,(': 3,
'3,NUM': 4,
'4,,': 5,
'5,NUM': 6,
'6,)': 2,
// AVANZAR
'0,AVANZAR': 7,
'7,ID': 8,
'8,,': 9,
'9,NUM': 10,
// GIRAR
'0,GIRAR': 11,
'11,ID': 12,
'12,,': 13,
'13,IZQ': 14,
'13,DER': 14,
// TELEPORT
'0,TELEPORT': 15,
'15,ID': 16,
'16,(': 17,
'17,NUM': 18,
'18,,': 19,
'19,NUM': 20,
'20,)': 21,
};
const finalStates = {
2: 'ROBOT',
10: 'AVANZAR',
14: 'GIRAR',
21: 'TELEPORT',
};
// Funciones auxiliares
const scanner = (command) => {
return command.match(/[a-zA-Z][a-zA-Z\d]*|\d+|[(),]/g);
};
const MT = (q, token) => {
// PALABRAS RESERVADAS
if (['ROBOT', 'AVANZAR', 'GIRAR', 'TELEPORT', 'IZQ', 'DER'].includes(token)) {
return MT_MATRIX[`${q},${token}`] || QE;
}
// ID
else if (/^[a-zA-Z][a-zA-Z\d]*$/.test(token)) {
return MT_MATRIX[`${q},ID`] || QE;
}
// NUM
else if (/^\d+$/.test(token)) {
return MT_MATRIX[`${q},NUM`] || QE;
}
// OTROS
else {
return MT_MATRIX[`${q},${token}`] || QE;
}
};
const getParameters = (tokens, commandType) => {
switch (commandType) {
case 'ROBOT':
let params = {};
let i = 1;
while (i < tokens.length) {
if (i + 1 < tokens.length && tokens[i + 1] === '(') {
params[tokens[i]] = [
parseInt(tokens[i + 2]),
parseInt(tokens[i + 4]),
];
i += 7;
} else {
params[tokens[i]] = null;
i += 2;
}
}
return params;
case 'AVANZAR':
return {
id: tokens[1],
pasos: parseInt(tokens[3]),
};
case 'GIRAR':
return {
id: tokens[1],
direccion: tokens[3],
};
case 'TELEPORT':
return {
id: tokens[1],
coords: [parseInt(tokens[3]), parseInt(tokens[5])],
};
default:
return {};
}
};
const evalCommand = (command) => {
const tokens = scanner(command);
let q = 0;
let i = 0;
// console.log('Tokens:', tokens);
while (i < tokens.length && q !== QE) {
q = MT(q, tokens[i]);
i++;
}
if (q in finalStates) {
return {
commandType: finalStates[q],
parameters: getParameters(tokens, finalStates[q]),
};
} else {
return null;
}
};
export { evalCommand };