@designliquido/delegua 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/CONTRIBUTING.md +37 -0
- package/.github/workflows/principal.yml +3 -1
- package/.release-it.json +2 -1
- package/.vscode/launch.json +43 -8
- package/README.md +9 -1
- package/bin/delegua +2 -2
- package/bin/delegua.cmd +1 -1
- package/index.ts +29 -0
- package/package.json +9 -4
- package/src/ambiente.ts +56 -0
- package/src/avaliador-sintatico/dialetos/egua-classico.ts +983 -0
- package/src/avaliador-sintatico/dialetos/index.ts +1 -0
- package/src/avaliador-sintatico/index.ts +983 -0
- package/src/avaliador-sintatico/parser-error.ts +1 -0
- package/src/{lib/globalLib.js → bibliotecas/bibliotecaGlobal.ts} +34 -34
- package/src/{lib/importStdlib.js → bibliotecas/importarBiblioteca.ts} +9 -9
- package/src/construtos/assign-subscript.ts +19 -0
- package/src/construtos/atribuir.ts +17 -0
- package/src/construtos/binario.ts +19 -0
- package/src/construtos/call.ts +19 -0
- package/src/construtos/conjunto.ts +19 -0
- package/src/construtos/dicionario.ts +17 -0
- package/src/construtos/expr.ts +3 -0
- package/src/construtos/funcao.ts +17 -0
- package/src/construtos/get.ts +17 -0
- package/src/construtos/grouping.ts +15 -0
- package/src/construtos/index.ts +18 -0
- package/src/construtos/isto.ts +15 -0
- package/src/construtos/literal.ts +15 -0
- package/src/construtos/logical.ts +19 -0
- package/src/construtos/subscript.ts +19 -0
- package/src/construtos/super.ts +17 -0
- package/src/construtos/unario.ts +17 -0
- package/src/construtos/variavel.ts +15 -0
- package/src/construtos/vetor.ts +15 -0
- package/src/{stmt.js → declaracoes/index.ts} +79 -51
- package/src/delegua.ts +156 -0
- package/src/estruturas/callable.ts +11 -0
- package/src/estruturas/{classe.js → classe.ts} +11 -7
- package/src/estruturas/{funcaoPadrao.js → funcao-padrao.ts} +8 -4
- package/src/estruturas/funcao.ts +70 -0
- package/src/estruturas/index.ts +6 -0
- package/src/estruturas/{instancia.js → instancia.ts} +12 -9
- package/src/estruturas/modulo.ts +11 -0
- package/src/excecoes/break-exception.ts +1 -0
- package/src/excecoes/continue-exception.ts +1 -0
- package/src/excecoes/erro-em-tempo-de-execucao.ts +11 -0
- package/src/excecoes/index.ts +4 -0
- package/src/excecoes/return-exception.ts +10 -0
- package/src/interfaces/avaliador-sintatico-interface.ts +58 -0
- package/src/interfaces/index.ts +6 -0
- package/src/interfaces/interpretador-interface.ts +52 -0
- package/src/interfaces/lexador-interface.ts +24 -0
- package/src/interfaces/pilha-interface.ts +7 -0
- package/src/interfaces/resolvedor-interface.ts +49 -0
- package/src/interfaces/simbolo-interface.ts +6 -0
- package/src/interpretador/dialetos/egua-classico.ts +825 -0
- package/src/interpretador/dialetos/index.ts +1 -0
- package/src/interpretador/index.ts +825 -0
- package/src/lexador/dialetos/egua-classico.ts +333 -0
- package/src/lexador/dialetos/index.ts +1 -0
- package/src/{lexer.js → lexador/index.ts} +38 -21
- package/src/resolvedor/Pilha.ts +29 -0
- package/src/resolvedor/ResolverError.ts +8 -0
- package/src/resolvedor/dialetos/egua-classico.ts +412 -0
- package/src/resolvedor/dialetos/index.ts +1 -0
- package/src/{resolver.js → resolvedor/index.ts} +101 -114
- package/src/{tiposDeSimbolos.js → tiposDeSimbolos.ts} +21 -22
- package/src/web.ts +75 -0
- package/tsconfig.json +11 -0
- package/indice.js +0 -14
- package/src/ambiente.js +0 -53
- package/src/delegua.js +0 -102
- package/src/erro.js +0 -18
- package/src/estruturas/callable.js +0 -5
- package/src/estruturas/funcao.js +0 -62
- package/src/estruturas/modulo.js +0 -9
- package/src/expr.js +0 -228
- package/src/interpretador.js +0 -802
- package/src/parser.js +0 -822
- package/src/web.js +0 -70
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { LexadorInterface, SimboloInterface } from "../../interfaces";
|
|
2
|
+
import tiposDeSimbolos from "../../tiposDeSimbolos";
|
|
3
|
+
|
|
4
|
+
const palavrasReservadas = {
|
|
5
|
+
e: tiposDeSimbolos.E,
|
|
6
|
+
em: tiposDeSimbolos.EM,
|
|
7
|
+
classe: tiposDeSimbolos.CLASSE,
|
|
8
|
+
senao: tiposDeSimbolos.SENAO,
|
|
9
|
+
falso: tiposDeSimbolos.FALSO,
|
|
10
|
+
para: tiposDeSimbolos.PARA,
|
|
11
|
+
funcao: tiposDeSimbolos.FUNCAO,
|
|
12
|
+
se: tiposDeSimbolos.SE,
|
|
13
|
+
senaose: tiposDeSimbolos.SENAOSE,
|
|
14
|
+
nulo: tiposDeSimbolos.NULO,
|
|
15
|
+
ou: tiposDeSimbolos.OU,
|
|
16
|
+
escreva: tiposDeSimbolos.ESCREVA,
|
|
17
|
+
retorna: tiposDeSimbolos.RETORNA,
|
|
18
|
+
super: tiposDeSimbolos.SUPER,
|
|
19
|
+
isto: tiposDeSimbolos.ISTO,
|
|
20
|
+
verdadeiro: tiposDeSimbolos.VERDADEIRO,
|
|
21
|
+
var: tiposDeSimbolos.VAR,
|
|
22
|
+
fazer: tiposDeSimbolos.FAZER,
|
|
23
|
+
enquanto: tiposDeSimbolos.ENQUANTO,
|
|
24
|
+
pausa: tiposDeSimbolos.PAUSA,
|
|
25
|
+
continua: tiposDeSimbolos.CONTINUA,
|
|
26
|
+
escolha: tiposDeSimbolos.ESCOLHA,
|
|
27
|
+
caso: tiposDeSimbolos.CASO,
|
|
28
|
+
padrao: tiposDeSimbolos.PADRAO,
|
|
29
|
+
importar: tiposDeSimbolos.IMPORTAR,
|
|
30
|
+
tente: tiposDeSimbolos.TENTE,
|
|
31
|
+
pegue: tiposDeSimbolos.PEGUE,
|
|
32
|
+
finalmente: tiposDeSimbolos.FINALMENTE,
|
|
33
|
+
herda: tiposDeSimbolos.HERDA
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
class Simbolo implements SimboloInterface {
|
|
37
|
+
lexema: string;
|
|
38
|
+
tipo: string;
|
|
39
|
+
literal: string;
|
|
40
|
+
linha: string;
|
|
41
|
+
|
|
42
|
+
constructor(tipo, lexema, literal, linha) {
|
|
43
|
+
this.tipo = tipo;
|
|
44
|
+
this.lexema = lexema;
|
|
45
|
+
this.literal = literal;
|
|
46
|
+
this.linha = linha;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
toString() {
|
|
50
|
+
return this.tipo + " " + this.lexema + " " + this.literal;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* O Lexador é responsável por transformar o código em uma coleção de tokens de linguagem.
|
|
56
|
+
* Cada token de linguagem é representado por um tipo, um lexema e informações da linha de código em que foi expresso.
|
|
57
|
+
* Também é responsável por mapear as palavras reservadas da linguagem, que não podem ser usadas por outras
|
|
58
|
+
* estruturas, tais como nomes de variáveis, funções, literais, classes e assim por diante.
|
|
59
|
+
*/
|
|
60
|
+
export class LexerEguaClassico implements LexadorInterface {
|
|
61
|
+
Delegua: any;
|
|
62
|
+
codigo: any;
|
|
63
|
+
simbolos: any;
|
|
64
|
+
inicio: any;
|
|
65
|
+
atual: any;
|
|
66
|
+
linha: any;
|
|
67
|
+
|
|
68
|
+
constructor(Delegua: any, codigo?: any) {
|
|
69
|
+
this.Delegua = Delegua;
|
|
70
|
+
this.codigo = codigo;
|
|
71
|
+
|
|
72
|
+
this.simbolos = [];
|
|
73
|
+
|
|
74
|
+
this.inicio = 0;
|
|
75
|
+
this.atual = 0;
|
|
76
|
+
this.linha = 1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
eDigito(caractere: any) {
|
|
80
|
+
return caractere >= "0" && caractere <= "9";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
eAlfabeto(caractere: any) {
|
|
84
|
+
return (caractere >= "a" && caractere <= "z") || (caractere >= "A" && caractere <= "Z") || caractere == "_";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
eAlfabetoOuDigito(caractere: any) {
|
|
88
|
+
return this.eDigito(caractere) || this.eAlfabeto(caractere);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
eFinalDoCodigo() {
|
|
92
|
+
return this.atual >= this.codigo.length;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
avancar() {
|
|
96
|
+
this.atual += 1;
|
|
97
|
+
return this.codigo[this.atual - 1];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
adicionarSimbolo(tipo: any, literal: any = null) {
|
|
101
|
+
const texto = this.codigo.substring(this.inicio, this.atual);
|
|
102
|
+
this.simbolos.push(new Simbolo(tipo, texto, literal, this.linha));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
match(esperado: any) {
|
|
106
|
+
if (this.eFinalDoCodigo()) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (this.codigo[this.atual] !== esperado) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.atual += 1;
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
peek() {
|
|
119
|
+
if (this.eFinalDoCodigo()) return "\0";
|
|
120
|
+
return this.codigo.charAt(this.atual);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
peekNext() {
|
|
124
|
+
if (this.atual + 1 >= this.codigo.length) return "\0";
|
|
125
|
+
return this.codigo.charAt(this.atual + 1);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
voltar() {
|
|
129
|
+
return this.codigo.charAt(this.atual - 1);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
analisarTexto(texto: string = '"') {
|
|
133
|
+
while (this.peek() !== texto && !this.eFinalDoCodigo()) {
|
|
134
|
+
if (this.peek() === "\n") this.linha = +1;
|
|
135
|
+
this.avancar();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (this.eFinalDoCodigo()) {
|
|
139
|
+
this.Delegua.lexerError(
|
|
140
|
+
this.linha,
|
|
141
|
+
this.voltar(),
|
|
142
|
+
"Texto não finalizado."
|
|
143
|
+
);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
this.avancar();
|
|
148
|
+
|
|
149
|
+
const valor = this.codigo.substring(this.inicio + 1, this.atual - 1);
|
|
150
|
+
this.adicionarSimbolo(tiposDeSimbolos.TEXTO, valor);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
analisarNumero() {
|
|
154
|
+
while (this.eDigito(this.peek())) {
|
|
155
|
+
this.avancar();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (this.peek() == "." && this.eDigito(this.peekNext())) {
|
|
159
|
+
this.avancar();
|
|
160
|
+
|
|
161
|
+
while (this.eDigito(this.peek())) {
|
|
162
|
+
this.avancar();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const numeroCompleto = this.codigo.substring(this.inicio, this.atual);
|
|
167
|
+
this.adicionarSimbolo(tiposDeSimbolos.NUMERO, parseFloat(numeroCompleto));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
identificarPalavraChave() {
|
|
171
|
+
while (this.eAlfabetoOuDigito(this.peek())) {
|
|
172
|
+
this.avancar();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const codigo = this.codigo.substring(this.inicio, this.atual);
|
|
176
|
+
const tipo = codigo in palavrasReservadas ? palavrasReservadas[codigo] : tiposDeSimbolos.IDENTIFICADOR;
|
|
177
|
+
|
|
178
|
+
this.adicionarSimbolo(tipo);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
scanToken() {
|
|
182
|
+
const caractere = this.avancar();
|
|
183
|
+
|
|
184
|
+
switch (caractere) {
|
|
185
|
+
case "[":
|
|
186
|
+
this.adicionarSimbolo(tiposDeSimbolos.COLCHETE_ESQUERDO);
|
|
187
|
+
break;
|
|
188
|
+
case "]":
|
|
189
|
+
this.adicionarSimbolo(tiposDeSimbolos.COLCHETE_DIREITO);
|
|
190
|
+
break;
|
|
191
|
+
case "(":
|
|
192
|
+
this.adicionarSimbolo(tiposDeSimbolos.PARENTESE_ESQUERDO);
|
|
193
|
+
break;
|
|
194
|
+
case ")":
|
|
195
|
+
this.adicionarSimbolo(tiposDeSimbolos.PARENTESE_DIREITO);
|
|
196
|
+
break;
|
|
197
|
+
case "{":
|
|
198
|
+
this.adicionarSimbolo(tiposDeSimbolos.CHAVE_ESQUERDA);
|
|
199
|
+
break;
|
|
200
|
+
case "}":
|
|
201
|
+
this.adicionarSimbolo(tiposDeSimbolos.CHAVE_DIREITA);
|
|
202
|
+
break;
|
|
203
|
+
case ",":
|
|
204
|
+
this.adicionarSimbolo(tiposDeSimbolos.COMMA);
|
|
205
|
+
break;
|
|
206
|
+
case ".":
|
|
207
|
+
this.adicionarSimbolo(tiposDeSimbolos.DOT);
|
|
208
|
+
break;
|
|
209
|
+
case "-":
|
|
210
|
+
if (this.match("=")) {
|
|
211
|
+
this.adicionarSimbolo(tiposDeSimbolos.MENOR_IGUAL);
|
|
212
|
+
}
|
|
213
|
+
this.adicionarSimbolo(tiposDeSimbolos.SUBTRACAO);
|
|
214
|
+
break;
|
|
215
|
+
case "+":
|
|
216
|
+
if (this.match("=")) {
|
|
217
|
+
this.adicionarSimbolo(tiposDeSimbolos.MAIS_IGUAL);
|
|
218
|
+
}
|
|
219
|
+
this.adicionarSimbolo(tiposDeSimbolos.ADICAO);
|
|
220
|
+
break;
|
|
221
|
+
case ":":
|
|
222
|
+
this.adicionarSimbolo(tiposDeSimbolos.DOIS_PONTOS);
|
|
223
|
+
break;
|
|
224
|
+
case ";":
|
|
225
|
+
this.adicionarSimbolo(tiposDeSimbolos.PONTO_E_VIRGULA);
|
|
226
|
+
break;
|
|
227
|
+
case "%":
|
|
228
|
+
this.adicionarSimbolo(tiposDeSimbolos.MODULO);
|
|
229
|
+
break;
|
|
230
|
+
case "*":
|
|
231
|
+
if (this.peek() === "*") {
|
|
232
|
+
this.avancar();
|
|
233
|
+
this.adicionarSimbolo(tiposDeSimbolos.EXPONENCIACAO);
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
this.adicionarSimbolo(tiposDeSimbolos.MULTIPLICACAO);
|
|
237
|
+
break;
|
|
238
|
+
case "!":
|
|
239
|
+
this.adicionarSimbolo(
|
|
240
|
+
this.match("=") ? tiposDeSimbolos.DIFERENTE : tiposDeSimbolos.NEGACAO
|
|
241
|
+
);
|
|
242
|
+
break;
|
|
243
|
+
case "=":
|
|
244
|
+
this.adicionarSimbolo(
|
|
245
|
+
this.match("=") ? tiposDeSimbolos.IGUAL_IGUAL : tiposDeSimbolos.IGUAL
|
|
246
|
+
);
|
|
247
|
+
break;
|
|
248
|
+
|
|
249
|
+
case "&":
|
|
250
|
+
this.adicionarSimbolo(tiposDeSimbolos.BIT_AND);
|
|
251
|
+
break;
|
|
252
|
+
|
|
253
|
+
case "~":
|
|
254
|
+
this.adicionarSimbolo(tiposDeSimbolos.BIT_NOT);
|
|
255
|
+
break;
|
|
256
|
+
|
|
257
|
+
case "|":
|
|
258
|
+
this.adicionarSimbolo(tiposDeSimbolos.BIT_OR);
|
|
259
|
+
break;
|
|
260
|
+
|
|
261
|
+
case "^":
|
|
262
|
+
this.adicionarSimbolo(tiposDeSimbolos.BIT_XOR);
|
|
263
|
+
break;
|
|
264
|
+
|
|
265
|
+
case "<":
|
|
266
|
+
if (this.match("=")) {
|
|
267
|
+
this.adicionarSimbolo(tiposDeSimbolos.MENOR_IGUAL);
|
|
268
|
+
} else if (this.match("<")) {
|
|
269
|
+
this.adicionarSimbolo(tiposDeSimbolos.MENOR_MENOR);
|
|
270
|
+
} else {
|
|
271
|
+
this.adicionarSimbolo(tiposDeSimbolos.MENOR);
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
|
|
275
|
+
case ">":
|
|
276
|
+
if (this.match("=")) {
|
|
277
|
+
this.adicionarSimbolo(tiposDeSimbolos.MAIOR_IGUAL);
|
|
278
|
+
} else if (this.match(">")) {
|
|
279
|
+
this.adicionarSimbolo(tiposDeSimbolos.MAIOR_MAIOR);
|
|
280
|
+
} else {
|
|
281
|
+
this.adicionarSimbolo(tiposDeSimbolos.MAIOR);
|
|
282
|
+
}
|
|
283
|
+
break;
|
|
284
|
+
|
|
285
|
+
case "/":
|
|
286
|
+
if (this.match("/")) {
|
|
287
|
+
while (this.peek() != "\n" && !this.eFinalDoCodigo()) this.avancar();
|
|
288
|
+
} else {
|
|
289
|
+
this.adicionarSimbolo(tiposDeSimbolos.DIVISAO);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
|
|
293
|
+
// Esta sessão ignora espaços em branco na tokenização
|
|
294
|
+
case " ":
|
|
295
|
+
case "\r":
|
|
296
|
+
case "\t":
|
|
297
|
+
break;
|
|
298
|
+
|
|
299
|
+
// tentativa de pulhar linha com \n que ainda não funciona
|
|
300
|
+
case "\n":
|
|
301
|
+
this.linha += 1;
|
|
302
|
+
break;
|
|
303
|
+
|
|
304
|
+
case '"':
|
|
305
|
+
this.analisarTexto('"');
|
|
306
|
+
break;
|
|
307
|
+
|
|
308
|
+
case "'":
|
|
309
|
+
this.analisarTexto("'");
|
|
310
|
+
break;
|
|
311
|
+
|
|
312
|
+
default:
|
|
313
|
+
if (this.eDigito(caractere)) this.analisarNumero();
|
|
314
|
+
else if (this.eAlfabeto(caractere)) this.identificarPalavraChave();
|
|
315
|
+
else this.Delegua.lexerError(this.linha, caractere, "Caractere inesperado.");
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
scan(codigo?: any) {
|
|
320
|
+
if (codigo) {
|
|
321
|
+
this.codigo = codigo;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
while (!this.eFinalDoCodigo()) {
|
|
325
|
+
this.inicio = this.atual;
|
|
326
|
+
this.scanToken();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
this.simbolos.push(new Simbolo(tiposDeSimbolos.EOF, "", null, this.linha));
|
|
330
|
+
|
|
331
|
+
return this.simbolos;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './egua-classico'
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { LexadorInterface, SimboloInterface } from "../interfaces";
|
|
2
|
+
import tiposDeSimbolos from "../tiposDeSimbolos";
|
|
2
3
|
|
|
3
4
|
const palavrasReservadas = {
|
|
4
5
|
e: tiposDeSimbolos.E,
|
|
@@ -32,16 +33,21 @@ const palavrasReservadas = {
|
|
|
32
33
|
herda: tiposDeSimbolos.HERDA
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
class Simbolo {
|
|
36
|
-
|
|
36
|
+
class Simbolo implements SimboloInterface {
|
|
37
|
+
lexema: string;
|
|
38
|
+
tipo: string;
|
|
39
|
+
literal: string;
|
|
40
|
+
linha: string;
|
|
41
|
+
|
|
42
|
+
constructor(tipo, lexema, literal, linha) {
|
|
37
43
|
this.tipo = tipo;
|
|
38
|
-
this.
|
|
44
|
+
this.lexema = lexema;
|
|
39
45
|
this.literal = literal;
|
|
40
46
|
this.linha = linha;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
toString() {
|
|
44
|
-
return this.tipo + " " + this.
|
|
50
|
+
return this.tipo + " " + this.lexema + " " + this.literal;
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
|
|
@@ -51,8 +57,15 @@ class Simbolo {
|
|
|
51
57
|
* Também é responsável por mapear as palavras reservadas da linguagem, que não podem ser usadas por outras
|
|
52
58
|
* estruturas, tais como nomes de variáveis, funções, literais, classes e assim por diante.
|
|
53
59
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
60
|
+
export class Lexer implements LexadorInterface {
|
|
61
|
+
Delegua: any;
|
|
62
|
+
codigo: any;
|
|
63
|
+
simbolos: any;
|
|
64
|
+
inicio: any;
|
|
65
|
+
atual: any;
|
|
66
|
+
linha: any;
|
|
67
|
+
|
|
68
|
+
constructor(Delegua: any, codigo?: any) {
|
|
56
69
|
this.Delegua = Delegua;
|
|
57
70
|
this.codigo = codigo;
|
|
58
71
|
|
|
@@ -63,15 +76,15 @@ module.exports = class Lexer {
|
|
|
63
76
|
this.linha = 1;
|
|
64
77
|
}
|
|
65
78
|
|
|
66
|
-
eDigito(caractere) {
|
|
79
|
+
eDigito(caractere: any) {
|
|
67
80
|
return caractere >= "0" && caractere <= "9";
|
|
68
81
|
}
|
|
69
82
|
|
|
70
|
-
eAlfabeto(caractere) {
|
|
83
|
+
eAlfabeto(caractere: any) {
|
|
71
84
|
return (caractere >= "a" && caractere <= "z") || (caractere >= "A" && caractere <= "Z") || caractere == "_";
|
|
72
85
|
}
|
|
73
86
|
|
|
74
|
-
eAlfabetoOuDigito(caractere) {
|
|
87
|
+
eAlfabetoOuDigito(caractere: any) {
|
|
75
88
|
return this.eDigito(caractere) || this.eAlfabeto(caractere);
|
|
76
89
|
}
|
|
77
90
|
|
|
@@ -84,12 +97,12 @@ module.exports = class Lexer {
|
|
|
84
97
|
return this.codigo[this.atual - 1];
|
|
85
98
|
}
|
|
86
99
|
|
|
87
|
-
adicionarSimbolo(tipo, literal = null) {
|
|
100
|
+
adicionarSimbolo(tipo: any, literal: any = null) {
|
|
88
101
|
const texto = this.codigo.substring(this.inicio, this.atual);
|
|
89
102
|
this.simbolos.push(new Simbolo(tipo, texto, literal, this.linha));
|
|
90
103
|
}
|
|
91
104
|
|
|
92
|
-
match(esperado) {
|
|
105
|
+
match(esperado: any) {
|
|
93
106
|
if (this.eFinalDoCodigo()) {
|
|
94
107
|
return false;
|
|
95
108
|
}
|
|
@@ -116,7 +129,7 @@ module.exports = class Lexer {
|
|
|
116
129
|
return this.codigo.charAt(this.atual - 1);
|
|
117
130
|
}
|
|
118
131
|
|
|
119
|
-
analisarTexto(texto = '"') {
|
|
132
|
+
analisarTexto(texto: string = '"') {
|
|
120
133
|
while (this.peek() !== texto && !this.eFinalDoCodigo()) {
|
|
121
134
|
if (this.peek() === "\n") this.linha = +1;
|
|
122
135
|
this.avancar();
|
|
@@ -206,21 +219,21 @@ module.exports = class Lexer {
|
|
|
206
219
|
this.adicionarSimbolo(tiposDeSimbolos.ADICAO);
|
|
207
220
|
break;
|
|
208
221
|
case ":":
|
|
209
|
-
this.adicionarSimbolo(tiposDeSimbolos.
|
|
222
|
+
this.adicionarSimbolo(tiposDeSimbolos.DOIS_PONTOS);
|
|
210
223
|
break;
|
|
211
224
|
case ";":
|
|
212
|
-
this.adicionarSimbolo(tiposDeSimbolos.
|
|
225
|
+
this.adicionarSimbolo(tiposDeSimbolos.PONTO_E_VIRGULA);
|
|
213
226
|
break;
|
|
214
227
|
case "%":
|
|
215
|
-
this.adicionarSimbolo(tiposDeSimbolos.
|
|
228
|
+
this.adicionarSimbolo(tiposDeSimbolos.MODULO);
|
|
216
229
|
break;
|
|
217
230
|
case "*":
|
|
218
231
|
if (this.peek() === "*") {
|
|
219
232
|
this.avancar();
|
|
220
|
-
this.adicionarSimbolo(tiposDeSimbolos.
|
|
233
|
+
this.adicionarSimbolo(tiposDeSimbolos.EXPONENCIACAO);
|
|
221
234
|
break;
|
|
222
235
|
}
|
|
223
|
-
this.adicionarSimbolo(tiposDeSimbolos.
|
|
236
|
+
this.adicionarSimbolo(tiposDeSimbolos.MULTIPLICACAO);
|
|
224
237
|
break;
|
|
225
238
|
case "!":
|
|
226
239
|
this.adicionarSimbolo(
|
|
@@ -273,7 +286,7 @@ module.exports = class Lexer {
|
|
|
273
286
|
if (this.match("/")) {
|
|
274
287
|
while (this.peek() != "\n" && !this.eFinalDoCodigo()) this.avancar();
|
|
275
288
|
} else {
|
|
276
|
-
this.adicionarSimbolo(tiposDeSimbolos.
|
|
289
|
+
this.adicionarSimbolo(tiposDeSimbolos.DIVISAO);
|
|
277
290
|
}
|
|
278
291
|
break;
|
|
279
292
|
|
|
@@ -303,14 +316,18 @@ module.exports = class Lexer {
|
|
|
303
316
|
}
|
|
304
317
|
}
|
|
305
318
|
|
|
306
|
-
scan() {
|
|
319
|
+
scan(codigo?: any) {
|
|
320
|
+
if (codigo) {
|
|
321
|
+
this.codigo = codigo;
|
|
322
|
+
}
|
|
323
|
+
|
|
307
324
|
while (!this.eFinalDoCodigo()) {
|
|
308
325
|
this.inicio = this.atual;
|
|
309
326
|
this.scanToken();
|
|
310
327
|
}
|
|
311
328
|
|
|
312
329
|
this.simbolos.push(new Simbolo(tiposDeSimbolos.EOF, "", null, this.linha));
|
|
313
|
-
|
|
330
|
+
|
|
314
331
|
return this.simbolos;
|
|
315
332
|
}
|
|
316
333
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PilhaInterface } from "../interfaces";
|
|
2
|
+
|
|
3
|
+
export class Pilha implements PilhaInterface {
|
|
4
|
+
pilha: any[];
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
this.pilha = [];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
empilhar(item) {
|
|
11
|
+
this.pilha.push(item);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
eVazio() {
|
|
15
|
+
return this.pilha.length === 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
peek() {
|
|
19
|
+
if (this.eVazio())
|
|
20
|
+
throw new Error("Pilha vazia.");
|
|
21
|
+
return this.pilha[this.pilha.length - 1];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
removerUltimo() {
|
|
25
|
+
if (this.eVazio())
|
|
26
|
+
throw new Error("Pilha vazia.");
|
|
27
|
+
return this.pilha.pop();
|
|
28
|
+
}
|
|
29
|
+
}
|