@designliquido/delegua 0.1.0 → 0.1.1

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 (194) 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 +2 -2
  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 +132 -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 +665 -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/README.md +0 -66
  134. package/babel.config.js +0 -1
  135. package/index.ts +0 -29
  136. package/src/ambiente.ts +0 -56
  137. package/src/avaliador-sintatico/dialetos/egua-classico.ts +0 -983
  138. package/src/avaliador-sintatico/dialetos/index.ts +0 -1
  139. package/src/avaliador-sintatico/index.ts +0 -983
  140. package/src/avaliador-sintatico/parser-error.ts +0 -1
  141. package/src/bibliotecas/bibliotecaGlobal.ts +0 -171
  142. package/src/bibliotecas/importarBiblioteca.ts +0 -32
  143. package/src/construtos/assign-subscript.ts +0 -19
  144. package/src/construtos/atribuir.ts +0 -17
  145. package/src/construtos/binario.ts +0 -19
  146. package/src/construtos/call.ts +0 -19
  147. package/src/construtos/conjunto.ts +0 -19
  148. package/src/construtos/dicionario.ts +0 -17
  149. package/src/construtos/expr.ts +0 -3
  150. package/src/construtos/funcao.ts +0 -17
  151. package/src/construtos/get.ts +0 -17
  152. package/src/construtos/grouping.ts +0 -15
  153. package/src/construtos/index.ts +0 -18
  154. package/src/construtos/isto.ts +0 -15
  155. package/src/construtos/literal.ts +0 -15
  156. package/src/construtos/logical.ts +0 -19
  157. package/src/construtos/subscript.ts +0 -19
  158. package/src/construtos/super.ts +0 -17
  159. package/src/construtos/unario.ts +0 -17
  160. package/src/construtos/variavel.ts +0 -15
  161. package/src/construtos/vetor.ts +0 -15
  162. package/src/declaracoes/index.ts +0 -243
  163. package/src/delegua.ts +0 -156
  164. package/src/estruturas/callable.ts +0 -11
  165. package/src/estruturas/classe.ts +0 -47
  166. package/src/estruturas/funcao-padrao.ts +0 -22
  167. package/src/estruturas/funcao.ts +0 -70
  168. package/src/estruturas/index.ts +0 -6
  169. package/src/estruturas/instancia.ts +0 -30
  170. package/src/estruturas/modulo.ts +0 -11
  171. package/src/excecoes/break-exception.ts +0 -1
  172. package/src/excecoes/continue-exception.ts +0 -1
  173. package/src/excecoes/erro-em-tempo-de-execucao.ts +0 -11
  174. package/src/excecoes/index.ts +0 -4
  175. package/src/excecoes/return-exception.ts +0 -10
  176. package/src/interfaces/avaliador-sintatico-interface.ts +0 -58
  177. package/src/interfaces/index.ts +0 -6
  178. package/src/interfaces/interpretador-interface.ts +0 -52
  179. package/src/interfaces/lexador-interface.ts +0 -24
  180. package/src/interfaces/pilha-interface.ts +0 -7
  181. package/src/interfaces/resolvedor-interface.ts +0 -49
  182. package/src/interfaces/simbolo-interface.ts +0 -6
  183. package/src/interpretador/dialetos/egua-classico.ts +0 -825
  184. package/src/interpretador/dialetos/index.ts +0 -1
  185. package/src/interpretador/index.ts +0 -825
  186. package/src/lexador/dialetos/egua-classico.ts +0 -333
  187. package/src/lexador/dialetos/index.ts +0 -1
  188. package/src/lexador/index.ts +0 -333
  189. package/src/resolvedor/Pilha.ts +0 -29
  190. package/src/resolvedor/ResolverError.ts +0 -8
  191. package/src/resolvedor/dialetos/index.ts +0 -1
  192. package/src/web.ts +0 -75
  193. package/testes/testes.egua +0 -630
  194. package/tsconfig.json +0 -11
@@ -1,983 +0,0 @@
1
- import tiposDeSimbolos from "../tiposDeSimbolos";
2
- import { AvaliadorSintaticoInterface, SimboloInterface } from "../interfaces";
3
- import {
4
- AssignSubscript,
5
- Atribuir,
6
- Binario,
7
- Call,
8
- Dicionario,
9
- Conjunto,
10
- Get,
11
- Grouping,
12
- Literal,
13
- Logical,
14
- Subscript,
15
- Super,
16
- Unario,
17
- Variavel,
18
- Vetor,
19
- Isto,
20
- } from "../construtos";
21
- import {
22
- Block,
23
- Classe,
24
- Continua,
25
- Enquanto,
26
- Escolha,
27
- Escreva,
28
- Expressao,
29
- Fazer,
30
- Funcao,
31
- Importar,
32
- Para,
33
- Pausa,
34
- Retorna,
35
- Se,
36
- Tente,
37
- Var,
38
- } from "../declaracoes";
39
-
40
- import {
41
- ErroAvaliador
42
- } from './parser-error';
43
-
44
- /**
45
- * O avaliador sintático (Parser) é responsável por transformar os símbolos do Lexador em estruturas de alto nível.
46
- * Essas estruturas de alto nível são as partes que executam lógica de programação de fato.
47
- */
48
- export class Parser implements AvaliadorSintaticoInterface {
49
- simbolos: SimboloInterface[];
50
- Delegua: any;
51
-
52
- atual: number;
53
- ciclos: number;
54
-
55
- constructor(Delegua: any, simbolos?: SimboloInterface[]) {
56
- this.simbolos = simbolos;
57
- this.Delegua = Delegua;
58
-
59
- this.atual = 0;
60
- this.ciclos = 0;
61
- }
62
-
63
- sincronizar() {
64
- this.avancar();
65
-
66
- while (!this.estaNoFinal()) {
67
- if (this.voltar().tipo === tiposDeSimbolos.PONTO_E_VIRGULA) return;
68
-
69
- switch (this.peek().tipo) {
70
- case tiposDeSimbolos.CLASSE:
71
- case tiposDeSimbolos.FUNCAO:
72
- case tiposDeSimbolos.VAR:
73
- case tiposDeSimbolos.PARA:
74
- case tiposDeSimbolos.SE:
75
- case tiposDeSimbolos.ENQUANTO:
76
- case tiposDeSimbolos.ESCREVA:
77
- case tiposDeSimbolos.RETORNA:
78
- return;
79
- }
80
-
81
- this.avancar();
82
- }
83
- }
84
-
85
- erro(simbolo: any, mensagemDeErro: any) {
86
- this.Delegua.erro(simbolo, mensagemDeErro);
87
- return new ErroAvaliador();
88
- }
89
-
90
- consumir(tipo: any, mensagemDeErro: any) {
91
- if (this.verificar(tipo)) return this.avancar();
92
- else throw this.erro(this.peek(), mensagemDeErro);
93
- }
94
-
95
- verificar(tipo: any) {
96
- if (this.estaNoFinal()) return false;
97
- return this.peek().tipo === tipo;
98
- }
99
-
100
- verificarProximo(tipo: any) {
101
- if (this.estaNoFinal()) return false;
102
- return this.simbolos[this.atual + 1].tipo === tipo;
103
- }
104
-
105
- peek() {
106
- return this.simbolos[this.atual];
107
- }
108
-
109
- voltar() {
110
- return this.simbolos[this.atual - 1];
111
- }
112
-
113
- seek(posicao: number) {
114
- return this.simbolos[this.atual + posicao];
115
- }
116
-
117
- estaNoFinal() {
118
- return this.peek().tipo === tiposDeSimbolos.EOF;
119
- }
120
-
121
- avancar() {
122
- if (!this.estaNoFinal()) this.atual += 1;
123
- return this.voltar();
124
- }
125
-
126
- match(...argumentos: any[]) {
127
- for (let i = 0; i < argumentos.length; i++) {
128
- const tipoAtual = argumentos[i];
129
- if (this.verificar(tipoAtual)) {
130
- this.avancar();
131
- return true;
132
- }
133
- }
134
-
135
- return false;
136
- }
137
-
138
- primario() {
139
- if (this.match(tiposDeSimbolos.SUPER)) {
140
- const palavraChave = this.voltar();
141
- this.consumir(tiposDeSimbolos.DOT, "Esperado '.' após 'super'.");
142
- const metodo = this.consumir(
143
- tiposDeSimbolos.IDENTIFICADOR,
144
- "Esperado nome do método da SuperClasse."
145
- );
146
- return new Super(palavraChave, metodo);
147
- }
148
- if (this.match(tiposDeSimbolos.COLCHETE_ESQUERDO)) {
149
- const valores = [];
150
- if (this.match(tiposDeSimbolos.COLCHETE_DIREITO)) {
151
- return new Vetor([]);
152
- }
153
- while (!this.match(tiposDeSimbolos.COLCHETE_DIREITO)) {
154
- const valor = this.atribuir();
155
- valores.push(valor);
156
- if (this.peek().tipo !== tiposDeSimbolos.COLCHETE_DIREITO) {
157
- this.consumir(
158
- tiposDeSimbolos.COMMA,
159
- "Esperado vírgula antes da próxima expressão."
160
- );
161
- }
162
- }
163
- return new Vetor(valores);
164
- }
165
- if (this.match(tiposDeSimbolos.CHAVE_ESQUERDA)) {
166
- const chaves = [];
167
- const valores = [];
168
- if (this.match(tiposDeSimbolos.CHAVE_DIREITA)) {
169
- return new Dicionario([], []);
170
- }
171
- while (!this.match(tiposDeSimbolos.CHAVE_DIREITA)) {
172
- let chave = this.atribuir();
173
- this.consumir(
174
- tiposDeSimbolos.DOIS_PONTOS,
175
- "Esperado ':' entre chave e valor."
176
- );
177
- let valor = this.atribuir();
178
-
179
- chaves.push(chave);
180
- valores.push(valor);
181
-
182
- if (this.peek().tipo !== tiposDeSimbolos.CHAVE_DIREITA) {
183
- this.consumir(
184
- tiposDeSimbolos.COMMA,
185
- "Esperado vírgula antes da próxima expressão."
186
- );
187
- }
188
- }
189
- return new Dicionario(chaves, valores);
190
- }
191
- if (this.match(tiposDeSimbolos.FUNCAO)) return this.corpoDaFuncao("funcao");
192
- if (this.match(tiposDeSimbolos.FALSO)) return new Literal(false);
193
- if (this.match(tiposDeSimbolos.VERDADEIRO)) return new Literal(true);
194
- if (this.match(tiposDeSimbolos.NULO)) return new Literal(null);
195
- if (this.match(tiposDeSimbolos.ISTO)) return new Isto(this.voltar());
196
- if (this.match(tiposDeSimbolos.NUMERO, tiposDeSimbolos.TEXTO)) {
197
- return new Literal(this.voltar().literal);
198
- }
199
- if (this.match(tiposDeSimbolos.IDENTIFICADOR)) {
200
- return new Variavel(this.voltar());
201
- }
202
- if (this.match(tiposDeSimbolos.PARENTESE_ESQUERDO)) {
203
- let expr = this.expressao();
204
- this.consumir(
205
- tiposDeSimbolos.PARENTESE_DIREITO,
206
- "Esperado ')' após a expressão."
207
- );
208
- return new Grouping(expr);
209
- }
210
- if (this.match(tiposDeSimbolos.IMPORTAR)) return this.importStatement();
211
-
212
- throw this.erro(this.peek(), "Esperado expressão.");
213
- }
214
-
215
- finalizarChamada(callee: any) {
216
- const argumentos = [];
217
- if (!this.verificar(tiposDeSimbolos.PARENTESE_DIREITO)) {
218
- do {
219
- if (argumentos.length >= 255) {
220
- throw this.erro(
221
- this.peek(),
222
- "Não pode haver mais de 255 argumentos."
223
- );
224
- }
225
- argumentos.push(this.expressao());
226
- } while (this.match(tiposDeSimbolos.COMMA));
227
- }
228
-
229
- const parenteseDireito = this.consumir(
230
- tiposDeSimbolos.PARENTESE_DIREITO,
231
- "Esperado ')' após os argumentos."
232
- );
233
-
234
- return new Call(callee, parenteseDireito, argumentos);
235
- }
236
-
237
- chamar() {
238
- let expr = this.primario();
239
-
240
- while (true) {
241
- if (this.match(tiposDeSimbolos.PARENTESE_ESQUERDO)) {
242
- expr = this.finalizarChamada(expr);
243
- } else if (this.match(tiposDeSimbolos.DOT)) {
244
- let nome = this.consumir(
245
- tiposDeSimbolos.IDENTIFICADOR,
246
- "Esperado nome do método após '.'."
247
- );
248
- expr = new Get(expr, nome);
249
- } else if (this.match(tiposDeSimbolos.COLCHETE_ESQUERDO)) {
250
- const indice = this.expressao();
251
- let closeBracket = this.consumir(
252
- tiposDeSimbolos.COLCHETE_DIREITO,
253
- "Esperado ']' após escrita do indice."
254
- );
255
- expr = new Subscript(expr, indice, closeBracket);
256
- } else {
257
- break;
258
- }
259
- }
260
-
261
- return expr;
262
- }
263
-
264
- unario() {
265
- if (
266
- this.match(
267
- tiposDeSimbolos.NEGACAO,
268
- tiposDeSimbolos.SUBTRACAO,
269
- tiposDeSimbolos.BIT_NOT
270
- )
271
- ) {
272
- const operador = this.voltar();
273
- const direito = this.unario();
274
- return new Unario(operador, direito);
275
- }
276
-
277
- return this.chamar();
278
- }
279
-
280
- exponent() {
281
- let expr = this.unario();
282
-
283
- while (this.match(tiposDeSimbolos.EXPONENCIACAO)) {
284
- const operador = this.voltar();
285
- const direito = this.unario();
286
- expr = new Binario(expr, operador, direito);
287
- }
288
-
289
- return expr;
290
- }
291
-
292
- multiplicar() {
293
- let expr = this.exponent();
294
-
295
- while (
296
- this.match(
297
- tiposDeSimbolos.DIVISAO,
298
- tiposDeSimbolos.MULTIPLICACAO,
299
- tiposDeSimbolos.MODULO
300
- )
301
- ) {
302
- const operador = this.voltar();
303
- const direito = this.exponent();
304
- expr = new Binario(expr, operador, direito);
305
- }
306
-
307
- return expr;
308
- }
309
-
310
- adicionar() {
311
- let expr = this.multiplicar();
312
-
313
- while (this.match(tiposDeSimbolos.SUBTRACAO, tiposDeSimbolos.ADICAO)) {
314
- const operador = this.voltar();
315
- const direito = this.multiplicar();
316
- expr = new Binario(expr, operador, direito);
317
- }
318
-
319
- return expr;
320
- }
321
-
322
- bitFill() {
323
- let expr = this.adicionar();
324
-
325
- while (
326
- this.match(tiposDeSimbolos.MENOR_MENOR, tiposDeSimbolos.MAIOR_MAIOR)
327
- ) {
328
- const operador = this.voltar();
329
- const direito = this.adicionar();
330
- expr = new Binario(expr, operador, direito);
331
- }
332
-
333
- return expr;
334
- }
335
-
336
- bitE() {
337
- let expr = this.bitFill();
338
-
339
- while (this.match(tiposDeSimbolos.BIT_AND)) {
340
- const operador = this.voltar();
341
- const direito = this.bitFill();
342
- expr = new Binario(expr, operador, direito);
343
- }
344
-
345
- return expr;
346
- }
347
-
348
- bitOu() {
349
- let expr = this.bitE();
350
-
351
- while (this.match(tiposDeSimbolos.BIT_OR, tiposDeSimbolos.BIT_XOR)) {
352
- const operador = this.voltar();
353
- const direito = this.bitE();
354
- expr = new Binario(expr, operador, direito);
355
- }
356
-
357
- return expr;
358
- }
359
-
360
- comparar() {
361
- let expr = this.bitOu();
362
-
363
- while (
364
- this.match(
365
- tiposDeSimbolos.MAIOR,
366
- tiposDeSimbolos.MAIOR_IGUAL,
367
- tiposDeSimbolos.MENOR,
368
- tiposDeSimbolos.MENOR_IGUAL
369
- )
370
- ) {
371
- const operador = this.voltar();
372
- const direito = this.bitOu();
373
- expr = new Binario(expr, operador, direito);
374
- }
375
-
376
- return expr;
377
- }
378
-
379
- equality() {
380
- let expr = this.comparar();
381
-
382
- while (this.match(tiposDeSimbolos.DIFERENTE, tiposDeSimbolos.IGUAL_IGUAL)) {
383
- const operador = this.voltar();
384
- const direito = this.comparar();
385
- expr = new Binario(expr, operador, direito);
386
- }
387
-
388
- return expr;
389
- }
390
-
391
- em() {
392
- let expr = this.equality();
393
-
394
- while (this.match(tiposDeSimbolos.EM)) {
395
- const operador = this.voltar();
396
- const direito = this.equality();
397
- expr = new Logical(expr, operador, direito);
398
- }
399
-
400
- return expr;
401
- }
402
-
403
- e() {
404
- let expr = this.em();
405
-
406
- while (this.match(tiposDeSimbolos.E)) {
407
- const operador = this.voltar();
408
- const direito = this.em();
409
- expr = new Logical(expr, operador, direito);
410
- }
411
-
412
- return expr;
413
- }
414
-
415
- ou() {
416
- let expr = this.e();
417
-
418
- while (this.match(tiposDeSimbolos.OU)) {
419
- const operador = this.voltar();
420
- const direito = this.e();
421
- expr = new Logical(expr, operador, direito);
422
- }
423
-
424
- return expr;
425
- }
426
-
427
- atribuir() {
428
- const expr = this.ou();
429
-
430
- if (
431
- this.match(tiposDeSimbolos.IGUAL) ||
432
- this.match(tiposDeSimbolos.MAIS_IGUAL)
433
- ) {
434
- const igual = this.voltar();
435
- const valor = this.atribuir();
436
-
437
- if (expr instanceof Variavel) {
438
- const nome = expr.nome;
439
- return new Atribuir(nome, valor);
440
- } else if (expr instanceof Get) {
441
- const get = expr;
442
- return new Conjunto(get.objeto, get.nome, valor);
443
- } else if (expr instanceof Subscript) {
444
- return new AssignSubscript(expr.callee, expr.indice, valor);
445
- }
446
- this.erro(igual, "Tarefa de atribuição inválida");
447
- }
448
-
449
- return expr;
450
- }
451
-
452
- expressao() {
453
- return this.atribuir();
454
- }
455
-
456
- declaracaoMostrar() {
457
- this.consumir(
458
- tiposDeSimbolos.PARENTESE_ESQUERDO,
459
- "Esperado '(' antes dos valores em escreva."
460
- );
461
-
462
- const valor = this.expressao();
463
-
464
- this.consumir(
465
- tiposDeSimbolos.PARENTESE_DIREITO,
466
- "Esperado ')' após os valores em escreva."
467
- );
468
-
469
- this.consumir(
470
- tiposDeSimbolos.PONTO_E_VIRGULA,
471
- "Esperado ';' após o valor."
472
- );
473
-
474
- return new Escreva(valor);
475
- }
476
-
477
- expressionStatement() {
478
- const expr = this.expressao();
479
- this.consumir(
480
- tiposDeSimbolos.PONTO_E_VIRGULA,
481
- "Esperado ';' após expressão."
482
- );
483
- return new Expressao(expr);
484
- }
485
-
486
- block() {
487
- const declaracoes = [];
488
-
489
- while (
490
- !this.verificar(tiposDeSimbolos.CHAVE_DIREITA) &&
491
- !this.estaNoFinal()
492
- ) {
493
- declaracoes.push(this.declaracao());
494
- }
495
-
496
- this.consumir(tiposDeSimbolos.CHAVE_DIREITA, "Esperado '}' após o bloco.");
497
- return declaracoes;
498
- }
499
-
500
- declaracaoSe() {
501
- this.consumir(
502
- tiposDeSimbolos.PARENTESE_ESQUERDO,
503
- "Esperado '(' após 'se'."
504
- );
505
- const condicao = this.expressao();
506
- this.consumir(
507
- tiposDeSimbolos.PARENTESE_DIREITO,
508
- "Esperado ')' após condição do se."
509
- );
510
-
511
- const thenBranch = this.statement();
512
-
513
- const elifBranches = [];
514
- while (this.match(tiposDeSimbolos.SENAOSE)) {
515
- this.consumir(
516
- tiposDeSimbolos.PARENTESE_ESQUERDO,
517
- "Esperado '(' após 'senaose'."
518
- );
519
- let elifCondition = this.expressao();
520
- this.consumir(
521
- tiposDeSimbolos.PARENTESE_DIREITO,
522
- "Esperado ')' apóes codição do 'senaose."
523
- );
524
-
525
- const branch = this.statement();
526
-
527
- elifBranches.push({
528
- condition: elifCondition,
529
- branch,
530
- });
531
- }
532
-
533
- let elseBranch = null;
534
- if (this.match(tiposDeSimbolos.SENAO)) {
535
- elseBranch = this.statement();
536
- }
537
-
538
- return new Se(condicao, thenBranch, elifBranches, elseBranch);
539
- }
540
-
541
- whileStatement() {
542
- try {
543
- this.ciclos += 1;
544
-
545
- this.consumir(
546
- tiposDeSimbolos.PARENTESE_ESQUERDO,
547
- "Esperado '(' após 'enquanto'."
548
- );
549
- const condicao = this.expressao();
550
- this.consumir(
551
- tiposDeSimbolos.PARENTESE_DIREITO,
552
- "Esperado ')' após condicional."
553
- );
554
- const corpo = this.statement();
555
-
556
- return new Enquanto(condicao, corpo);
557
- } finally {
558
- this.ciclos -= 1;
559
- }
560
- }
561
-
562
- forStatement() {
563
- try {
564
- this.ciclos += 1;
565
-
566
- this.consumir(
567
- tiposDeSimbolos.PARENTESE_ESQUERDO,
568
- "Esperado '(' após 'para'."
569
- );
570
-
571
- let inicializador;
572
- if (this.match(tiposDeSimbolos.PONTO_E_VIRGULA)) {
573
- inicializador = null;
574
- } else if (this.match(tiposDeSimbolos.VAR)) {
575
- inicializador = this.declaracaoDeVariavel();
576
- } else {
577
- inicializador = this.expressionStatement();
578
- }
579
-
580
- let condicao = null;
581
- if (!this.verificar(tiposDeSimbolos.PONTO_E_VIRGULA)) {
582
- condicao = this.expressao();
583
- }
584
-
585
- this.consumir(
586
- tiposDeSimbolos.PONTO_E_VIRGULA,
587
- "Esperado ';' após valores da condicional"
588
- );
589
-
590
- let incrementar = null;
591
- if (!this.verificar(tiposDeSimbolos.PARENTESE_DIREITO)) {
592
- incrementar = this.expressao();
593
- }
594
-
595
- this.consumir(
596
- tiposDeSimbolos.PARENTESE_DIREITO,
597
- "Esperado ')' após cláusulas"
598
- );
599
-
600
- const corpo = this.statement();
601
-
602
- return new Para(inicializador, condicao, incrementar, corpo);
603
- } finally {
604
- this.ciclos -= 1;
605
- }
606
- }
607
-
608
- breakStatement() {
609
- if (this.ciclos < 1) {
610
- this.erro(this.voltar(), "'pausa' deve estar dentro de um loop.");
611
- }
612
-
613
- this.consumir(
614
- tiposDeSimbolos.PONTO_E_VIRGULA,
615
- "Esperado ';' após 'pausa'."
616
- );
617
- return new Pausa();
618
- }
619
-
620
- declaracaoContinue() {
621
- if (this.ciclos < 1) {
622
- this.erro(
623
- this.voltar(),
624
- "'continua' precisa estar em um laço de repetição."
625
- );
626
- }
627
-
628
- this.consumir(
629
- tiposDeSimbolos.PONTO_E_VIRGULA,
630
- "Esperado ';' após 'continua'."
631
- );
632
- return new Continua();
633
- }
634
-
635
- declaracaoRetorna() {
636
- const palavraChave = this.voltar();
637
- let valor = null;
638
-
639
- if (!this.verificar(tiposDeSimbolos.PONTO_E_VIRGULA)) {
640
- valor = this.expressao();
641
- }
642
-
643
- this.consumir(
644
- tiposDeSimbolos.PONTO_E_VIRGULA,
645
- "Esperado ';' após o retorno."
646
- );
647
- return new Retorna(palavraChave, valor);
648
- }
649
-
650
- declaracaoEscolha() {
651
- try {
652
- this.ciclos += 1;
653
-
654
- this.consumir(
655
- tiposDeSimbolos.PARENTESE_ESQUERDO,
656
- "Esperado '{' após 'escolha'."
657
- );
658
- const condicao = this.expressao();
659
- this.consumir(
660
- tiposDeSimbolos.PARENTESE_DIREITO,
661
- "Esperado '}' após a condição de 'escolha'."
662
- );
663
- this.consumir(
664
- tiposDeSimbolos.CHAVE_ESQUERDA,
665
- "Esperado '{' antes do escopo do 'escolha'."
666
- );
667
-
668
- const branches = [];
669
- let defaultBranch = null;
670
- while (
671
- !this.match(tiposDeSimbolos.CHAVE_DIREITA) &&
672
- !this.estaNoFinal()
673
- ) {
674
- if (this.match(tiposDeSimbolos.CASO)) {
675
- let branchConditions = [this.expressao()];
676
- this.consumir(
677
- tiposDeSimbolos.DOIS_PONTOS,
678
- "Esperado ':' após o 'caso'."
679
- );
680
-
681
- while (this.verificar(tiposDeSimbolos.CASO)) {
682
- this.consumir(tiposDeSimbolos.CASO, null);
683
- branchConditions.push(this.expressao());
684
- this.consumir(
685
- tiposDeSimbolos.DOIS_PONTOS,
686
- "Esperado ':' após declaração do 'caso'."
687
- );
688
- }
689
-
690
- const stmts = [];
691
- do {
692
- stmts.push(this.statement());
693
- } while (
694
- !this.verificar(tiposDeSimbolos.CASO) &&
695
- !this.verificar(tiposDeSimbolos.PADRAO) &&
696
- !this.verificar(tiposDeSimbolos.CHAVE_DIREITA)
697
- );
698
-
699
- branches.push({
700
- conditions: branchConditions,
701
- stmts,
702
- });
703
- } else if (this.match(tiposDeSimbolos.PADRAO)) {
704
- if (defaultBranch !== null)
705
- throw new ErroAvaliador(
706
- "Você só pode ter um 'padrao' em cada declaração de 'escolha'."
707
- );
708
-
709
- this.consumir(
710
- tiposDeSimbolos.DOIS_PONTOS,
711
- "Esperado ':' após declaração do 'padrao'."
712
- );
713
-
714
- const stmts = [];
715
- do {
716
- stmts.push(this.statement());
717
- } while (
718
- !this.verificar(tiposDeSimbolos.CASO) &&
719
- !this.verificar(tiposDeSimbolos.PADRAO) &&
720
- !this.verificar(tiposDeSimbolos.CHAVE_DIREITA)
721
- );
722
-
723
- defaultBranch = {
724
- stmts,
725
- };
726
- }
727
- }
728
-
729
- return new Escolha(condicao, branches, defaultBranch);
730
- } finally {
731
- this.ciclos -= 1;
732
- }
733
- }
734
-
735
- importStatement() {
736
- this.consumir(
737
- tiposDeSimbolos.PARENTESE_ESQUERDO,
738
- "Esperado '(' após declaração."
739
- );
740
-
741
- const caminho = this.expressao();
742
-
743
- let closeBracket = this.consumir(
744
- tiposDeSimbolos.PARENTESE_DIREITO,
745
- "Esperado ')' após declaração."
746
- );
747
-
748
- return new Importar(caminho, closeBracket);
749
- }
750
-
751
- tryStatement() {
752
- this.consumir(
753
- tiposDeSimbolos.CHAVE_ESQUERDA,
754
- "Esperado '{' após a declaração 'tente'."
755
- );
756
-
757
- let tryBlock = this.block();
758
-
759
- let catchBlock = null;
760
- if (this.match(tiposDeSimbolos.PEGUE)) {
761
- this.consumir(
762
- tiposDeSimbolos.CHAVE_ESQUERDA,
763
- "Esperado '{' após a declaração 'pegue'."
764
- );
765
-
766
- catchBlock = this.block();
767
- }
768
-
769
- let elseBlock = null;
770
- if (this.match(tiposDeSimbolos.SENAO)) {
771
- this.consumir(
772
- tiposDeSimbolos.CHAVE_ESQUERDA,
773
- "Esperado '{' após a declaração 'pegue'."
774
- );
775
-
776
- elseBlock = this.block();
777
- }
778
-
779
- let finallyBlock = null;
780
- if (this.match(tiposDeSimbolos.FINALMENTE)) {
781
- this.consumir(
782
- tiposDeSimbolos.CHAVE_ESQUERDA,
783
- "Esperado '{' após a declaração 'pegue'."
784
- );
785
-
786
- finallyBlock = this.block();
787
- }
788
-
789
- return new Tente(tryBlock, catchBlock, elseBlock, finallyBlock);
790
- }
791
-
792
- doStatement() {
793
- try {
794
- this.ciclos += 1;
795
-
796
- const doBranch = this.statement();
797
-
798
- this.consumir(
799
- tiposDeSimbolos.ENQUANTO,
800
- "Esperado declaração do 'enquanto' após o escopo do 'fazer'."
801
- );
802
- this.consumir(
803
- tiposDeSimbolos.PARENTESE_ESQUERDO,
804
- "Esperado '(' após declaração 'enquanto'."
805
- );
806
-
807
- const whileCondition = this.expressao();
808
-
809
- this.consumir(
810
- tiposDeSimbolos.PARENTESE_DIREITO,
811
- "Esperado ')' após declaração do 'enquanto'."
812
- );
813
-
814
- return new Fazer(doBranch, whileCondition);
815
- } finally {
816
- this.ciclos -= 1;
817
- }
818
- }
819
-
820
- statement() {
821
- if (this.match(tiposDeSimbolos.FAZER)) return this.doStatement();
822
- if (this.match(tiposDeSimbolos.TENTE)) return this.tryStatement();
823
- if (this.match(tiposDeSimbolos.ESCOLHA)) return this.declaracaoEscolha();
824
- if (this.match(tiposDeSimbolos.RETORNA)) return this.declaracaoRetorna();
825
- if (this.match(tiposDeSimbolos.CONTINUA)) return this.declaracaoContinue();
826
- if (this.match(tiposDeSimbolos.PAUSA)) return this.breakStatement();
827
- if (this.match(tiposDeSimbolos.PARA)) return this.forStatement();
828
- if (this.match(tiposDeSimbolos.ENQUANTO)) return this.whileStatement();
829
- if (this.match(tiposDeSimbolos.SE)) return this.declaracaoSe();
830
- if (this.match(tiposDeSimbolos.ESCREVA)) return this.declaracaoMostrar();
831
- if (this.match(tiposDeSimbolos.CHAVE_ESQUERDA))
832
- return new Block(this.block());
833
-
834
- return this.expressionStatement();
835
- }
836
-
837
- declaracaoDeVariavel() {
838
- const nome = this.consumir(
839
- tiposDeSimbolos.IDENTIFICADOR,
840
- "Esperado nome de variável."
841
- );
842
- let inicializador = null;
843
- if (
844
- this.match(tiposDeSimbolos.IGUAL) ||
845
- this.match(tiposDeSimbolos.MAIS_IGUAL)
846
- ) {
847
- inicializador = this.expressao();
848
- }
849
-
850
- this.consumir(
851
- tiposDeSimbolos.PONTO_E_VIRGULA,
852
- "Esperado ';' após a declaração da variável."
853
- );
854
-
855
- return new Var(nome, inicializador);
856
- }
857
-
858
- funcao(kind: any) {
859
- const nome = this.consumir(
860
- tiposDeSimbolos.IDENTIFICADOR,
861
- `Esperado nome ${kind}.`
862
- );
863
- return new Funcao(nome, this.corpoDaFuncao(kind));
864
- }
865
-
866
- corpoDaFuncao(kind: any) {
867
- this.consumir(
868
- tiposDeSimbolos.PARENTESE_ESQUERDO,
869
- `Esperado '(' após o nome ${kind}.`
870
- );
871
-
872
- let parametros = [];
873
- if (!this.verificar(tiposDeSimbolos.PARENTESE_DIREITO)) {
874
- do {
875
- if (parametros.length >= 255) {
876
- this.erro(this.peek(), "Não pode haver mais de 255 parâmetros");
877
- }
878
-
879
- let paramObj = {};
880
-
881
- if (this.peek().tipo === tiposDeSimbolos.MULTIPLICACAO) {
882
- this.consumir(tiposDeSimbolos.MULTIPLICACAO, null);
883
- paramObj["tipo"] = "wildcard";
884
- } else {
885
- paramObj["tipo"] = "standard";
886
- }
887
-
888
- paramObj["nome"] = this.consumir(
889
- tiposDeSimbolos.IDENTIFICADOR,
890
- "Esperado nome do parâmetro."
891
- );
892
-
893
- if (this.match(tiposDeSimbolos.IGUAL)) {
894
- paramObj["default"] = this.primario();
895
- }
896
-
897
- parametros.push(paramObj);
898
-
899
- if (paramObj["tipo"] === "wildcard") break;
900
- } while (this.match(tiposDeSimbolos.COMMA));
901
- }
902
-
903
- this.consumir(
904
- tiposDeSimbolos.PARENTESE_DIREITO,
905
- "Esperado ')' após parâmetros."
906
- );
907
- this.consumir(
908
- tiposDeSimbolos.CHAVE_ESQUERDA,
909
- `Esperado '{' antes do escopo do ${kind}.`
910
- );
911
-
912
- const corpo = this.block();
913
-
914
- return new Funcao(parametros, corpo);
915
- }
916
-
917
- declaracaoDeClasse() {
918
- const nome = this.consumir(
919
- tiposDeSimbolos.IDENTIFICADOR,
920
- "Esperado nome da classe."
921
- );
922
-
923
- let superClasse = null;
924
- if (this.match(tiposDeSimbolos.HERDA)) {
925
- this.consumir(
926
- tiposDeSimbolos.IDENTIFICADOR,
927
- "Esperado nome da SuperClasse."
928
- );
929
- superClasse = new Variavel(this.voltar());
930
- }
931
-
932
- this.consumir(
933
- tiposDeSimbolos.CHAVE_ESQUERDA,
934
- "Esperado '{' antes do escopo da classe."
935
- );
936
-
937
- const metodos = [];
938
- while (
939
- !this.verificar(tiposDeSimbolos.CHAVE_DIREITA) &&
940
- !this.estaNoFinal()
941
- ) {
942
- metodos.push(this.funcao("método"));
943
- }
944
-
945
- this.consumir(
946
- tiposDeSimbolos.CHAVE_DIREITA,
947
- "Esperado '}' após o escopo da classe."
948
- );
949
- return new Classe(nome, superClasse, metodos);
950
- }
951
-
952
- declaracao() {
953
- try {
954
- if (
955
- this.verificar(tiposDeSimbolos.FUNCAO) &&
956
- this.verificarProximo(tiposDeSimbolos.IDENTIFICADOR)
957
- ) {
958
- this.consumir(tiposDeSimbolos.FUNCAO, null);
959
- return this.funcao("funcao");
960
- }
961
- if (this.match(tiposDeSimbolos.VAR)) return this.declaracaoDeVariavel();
962
- if (this.match(tiposDeSimbolos.CLASSE)) return this.declaracaoDeClasse();
963
-
964
- return this.statement();
965
- } catch (erro) {
966
- this.sincronizar();
967
- return null;
968
- }
969
- }
970
-
971
- analisar(simbolos?: SimboloInterface[]) {
972
- if (simbolos) {
973
- this.simbolos = simbolos;
974
- }
975
-
976
- const declaracoes = [];
977
- while (!this.estaNoFinal()) {
978
- declaracoes.push(this.declaracao());
979
- }
980
-
981
- return declaracoes;
982
- }
983
- }