@designliquido/delegua 0.1.0 → 0.1.4

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.
Files changed (193) hide show
  1. package/bin/delegua +2 -2
  2. package/bin/delegua.cmd +1 -1
  3. package/bin/package.json +51 -0
  4. package/index.js +27 -0
  5. package/index.js.map +1 -0
  6. package/package.json +4 -4
  7. package/src/ambiente.js +49 -0
  8. package/src/ambiente.js.map +1 -0
  9. package/src/avaliador-sintatico/dialetos/egua-classico.js +635 -0
  10. package/src/avaliador-sintatico/dialetos/egua-classico.js.map +1 -0
  11. package/src/avaliador-sintatico/dialetos/index.js +14 -0
  12. package/src/avaliador-sintatico/dialetos/index.js.map +1 -0
  13. package/src/avaliador-sintatico/index.js +635 -0
  14. package/src/avaliador-sintatico/index.js.map +1 -0
  15. package/src/avaliador-sintatico/parser-error.js +27 -0
  16. package/src/avaliador-sintatico/parser-error.js.map +1 -0
  17. package/src/bibliotecas/bibliotecaGlobal.js +97 -0
  18. package/src/bibliotecas/bibliotecaGlobal.js.map +1 -0
  19. package/src/bibliotecas/importarBiblioteca.js +32 -0
  20. package/src/bibliotecas/importarBiblioteca.js.map +1 -0
  21. package/src/construtos/assign-subscript.js +35 -0
  22. package/src/construtos/assign-subscript.js.map +1 -0
  23. package/src/construtos/atribuir.js +34 -0
  24. package/src/construtos/atribuir.js.map +1 -0
  25. package/src/construtos/binario.js +35 -0
  26. package/src/construtos/binario.js.map +1 -0
  27. package/src/construtos/call.js +35 -0
  28. package/src/construtos/call.js.map +1 -0
  29. package/src/construtos/conjunto.js +35 -0
  30. package/src/construtos/conjunto.js.map +1 -0
  31. package/src/construtos/dicionario.js +34 -0
  32. package/src/construtos/dicionario.js.map +1 -0
  33. package/src/construtos/expr.js +11 -0
  34. package/src/construtos/expr.js.map +1 -0
  35. package/src/construtos/funcao.js +34 -0
  36. package/src/construtos/funcao.js.map +1 -0
  37. package/src/construtos/get.js +34 -0
  38. package/src/construtos/get.js.map +1 -0
  39. package/src/construtos/grouping.js +33 -0
  40. package/src/construtos/grouping.js.map +1 -0
  41. package/src/construtos/index.js +31 -0
  42. package/src/construtos/index.js.map +1 -0
  43. package/src/construtos/isto.js +33 -0
  44. package/src/construtos/isto.js.map +1 -0
  45. package/src/construtos/literal.js +33 -0
  46. package/src/construtos/literal.js.map +1 -0
  47. package/src/construtos/logical.js +35 -0
  48. package/src/construtos/logical.js.map +1 -0
  49. package/src/construtos/subscript.js +35 -0
  50. package/src/construtos/subscript.js.map +1 -0
  51. package/src/construtos/super.js +34 -0
  52. package/src/construtos/super.js.map +1 -0
  53. package/src/construtos/unario.js +34 -0
  54. package/src/construtos/unario.js.map +1 -0
  55. package/src/construtos/variavel.js +33 -0
  56. package/src/construtos/variavel.js.map +1 -0
  57. package/src/construtos/vetor.js +33 -0
  58. package/src/construtos/vetor.js.map +1 -0
  59. package/src/declaracoes/index.js +249 -0
  60. package/src/declaracoes/index.js.map +1 -0
  61. package/src/delegua.js +129 -0
  62. package/src/delegua.js.map +1 -0
  63. package/src/estruturas/callable.js +16 -0
  64. package/src/estruturas/callable.js.map +1 -0
  65. package/src/estruturas/classe.js +57 -0
  66. package/src/estruturas/classe.js.map +1 -0
  67. package/src/estruturas/funcao-padrao.js +38 -0
  68. package/src/estruturas/funcao-padrao.js.map +1 -0
  69. package/src/estruturas/funcao.js +81 -0
  70. package/src/estruturas/funcao.js.map +1 -0
  71. package/src/estruturas/index.js +19 -0
  72. package/src/estruturas/index.js.map +1 -0
  73. package/src/estruturas/instancia.js +28 -0
  74. package/src/estruturas/instancia.js.map +1 -0
  75. package/src/estruturas/modulo.js +15 -0
  76. package/src/estruturas/modulo.js.map +1 -0
  77. package/src/excecoes/break-exception.js +27 -0
  78. package/src/excecoes/break-exception.js.map +1 -0
  79. package/src/excecoes/continue-exception.js +27 -0
  80. package/src/excecoes/continue-exception.js.map +1 -0
  81. package/src/excecoes/erro-em-tempo-de-execucao.js +31 -0
  82. package/src/excecoes/erro-em-tempo-de-execucao.js.map +1 -0
  83. package/src/excecoes/index.js +17 -0
  84. package/src/excecoes/index.js.map +1 -0
  85. package/src/excecoes/return-exception.js +30 -0
  86. package/src/excecoes/return-exception.js.map +1 -0
  87. package/src/interfaces/avaliador-sintatico-interface.js +3 -0
  88. package/src/interfaces/avaliador-sintatico-interface.js.map +1 -0
  89. package/src/interfaces/index.js +19 -0
  90. package/src/interfaces/index.js.map +1 -0
  91. package/src/interfaces/interpretador-interface.js +3 -0
  92. package/src/interfaces/interpretador-interface.js.map +1 -0
  93. package/src/interfaces/lexador-interface.js +3 -0
  94. package/src/interfaces/lexador-interface.js.map +1 -0
  95. package/src/interfaces/pilha-interface.js +3 -0
  96. package/src/interfaces/pilha-interface.js.map +1 -0
  97. package/src/interfaces/resolvedor-interface.js +3 -0
  98. package/src/interfaces/resolvedor-interface.js.map +1 -0
  99. package/src/interfaces/simbolo-interface.js +3 -0
  100. package/src/interfaces/simbolo-interface.js.map +1 -0
  101. package/src/interpretador/dialetos/egua-classico.js +656 -0
  102. package/src/interpretador/dialetos/egua-classico.js.map +1 -0
  103. package/src/interpretador/dialetos/index.js +14 -0
  104. package/src/interpretador/dialetos/index.js.map +1 -0
  105. package/src/interpretador/index.js +665 -0
  106. package/src/interpretador/index.js.map +1 -0
  107. package/src/lexador/dialetos/egua-classico.js +287 -0
  108. package/src/lexador/dialetos/egua-classico.js.map +1 -0
  109. package/src/lexador/dialetos/index.js +14 -0
  110. package/src/lexador/dialetos/index.js.map +1 -0
  111. package/src/lexador/index.js +287 -0
  112. package/src/lexador/index.js.map +1 -0
  113. package/src/resolvedor/Pilha.js +27 -0
  114. package/src/resolvedor/Pilha.js.map +1 -0
  115. package/src/resolvedor/ResolverError.js +29 -0
  116. package/src/resolvedor/ResolverError.js.map +1 -0
  117. package/src/resolvedor/{index.ts → dialetos/egua-classico.js} +147 -228
  118. package/src/resolvedor/dialetos/egua-classico.js.map +1 -0
  119. package/src/resolvedor/dialetos/index.js +14 -0
  120. package/src/resolvedor/dialetos/index.js.map +1 -0
  121. package/src/resolvedor/{dialetos/egua-classico.ts → index.js} +147 -228
  122. package/src/resolvedor/index.js.map +1 -0
  123. package/src/{tiposDeSimbolos.ts → tiposDeSimbolos.js} +5 -2
  124. package/src/tiposDeSimbolos.js.map +1 -0
  125. package/src/web.js +66 -0
  126. package/src/web.js.map +1 -0
  127. package/.github/CONTRIBUTING.md +0 -37
  128. package/.github/workflows/principal.yml +0 -22
  129. package/.release-it.json +0 -9
  130. package/.vscode/launch.json +0 -64
  131. package/.vscode/settings.json +0 -3
  132. package/LICENSE +0 -21
  133. package/babel.config.js +0 -1
  134. package/index.ts +0 -29
  135. package/src/ambiente.ts +0 -56
  136. package/src/avaliador-sintatico/dialetos/egua-classico.ts +0 -983
  137. package/src/avaliador-sintatico/dialetos/index.ts +0 -1
  138. package/src/avaliador-sintatico/index.ts +0 -983
  139. package/src/avaliador-sintatico/parser-error.ts +0 -1
  140. package/src/bibliotecas/bibliotecaGlobal.ts +0 -171
  141. package/src/bibliotecas/importarBiblioteca.ts +0 -32
  142. package/src/construtos/assign-subscript.ts +0 -19
  143. package/src/construtos/atribuir.ts +0 -17
  144. package/src/construtos/binario.ts +0 -19
  145. package/src/construtos/call.ts +0 -19
  146. package/src/construtos/conjunto.ts +0 -19
  147. package/src/construtos/dicionario.ts +0 -17
  148. package/src/construtos/expr.ts +0 -3
  149. package/src/construtos/funcao.ts +0 -17
  150. package/src/construtos/get.ts +0 -17
  151. package/src/construtos/grouping.ts +0 -15
  152. package/src/construtos/index.ts +0 -18
  153. package/src/construtos/isto.ts +0 -15
  154. package/src/construtos/literal.ts +0 -15
  155. package/src/construtos/logical.ts +0 -19
  156. package/src/construtos/subscript.ts +0 -19
  157. package/src/construtos/super.ts +0 -17
  158. package/src/construtos/unario.ts +0 -17
  159. package/src/construtos/variavel.ts +0 -15
  160. package/src/construtos/vetor.ts +0 -15
  161. package/src/declaracoes/index.ts +0 -243
  162. package/src/delegua.ts +0 -156
  163. package/src/estruturas/callable.ts +0 -11
  164. package/src/estruturas/classe.ts +0 -47
  165. package/src/estruturas/funcao-padrao.ts +0 -22
  166. package/src/estruturas/funcao.ts +0 -70
  167. package/src/estruturas/index.ts +0 -6
  168. package/src/estruturas/instancia.ts +0 -30
  169. package/src/estruturas/modulo.ts +0 -11
  170. package/src/excecoes/break-exception.ts +0 -1
  171. package/src/excecoes/continue-exception.ts +0 -1
  172. package/src/excecoes/erro-em-tempo-de-execucao.ts +0 -11
  173. package/src/excecoes/index.ts +0 -4
  174. package/src/excecoes/return-exception.ts +0 -10
  175. package/src/interfaces/avaliador-sintatico-interface.ts +0 -58
  176. package/src/interfaces/index.ts +0 -6
  177. package/src/interfaces/interpretador-interface.ts +0 -52
  178. package/src/interfaces/lexador-interface.ts +0 -24
  179. package/src/interfaces/pilha-interface.ts +0 -7
  180. package/src/interfaces/resolvedor-interface.ts +0 -49
  181. package/src/interfaces/simbolo-interface.ts +0 -6
  182. package/src/interpretador/dialetos/egua-classico.ts +0 -825
  183. package/src/interpretador/dialetos/index.ts +0 -1
  184. package/src/interpretador/index.ts +0 -825
  185. package/src/lexador/dialetos/egua-classico.ts +0 -333
  186. package/src/lexador/dialetos/index.ts +0 -1
  187. package/src/lexador/index.ts +0 -333
  188. package/src/resolvedor/Pilha.ts +0 -29
  189. package/src/resolvedor/ResolverError.ts +0 -8
  190. package/src/resolvedor/dialetos/index.ts +0 -1
  191. package/src/web.ts +0 -75
  192. package/testes/testes.egua +0 -630
  193. package/tsconfig.json +0 -11
@@ -1,333 +0,0 @@
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
- };
@@ -1 +0,0 @@
1
- export * from './egua-classico'
@@ -1,333 +0,0 @@
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 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) {
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
- };
@@ -1,29 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
1
- export class ResolverError extends Error {
2
- mensagem: String;
3
-
4
- constructor(mensagem) {
5
- super(mensagem);
6
- this.mensagem = mensagem;
7
- }
8
- }
@@ -1 +0,0 @@
1
- export * from './egua-classico'