@designliquido/delegua 0.29.4 → 0.30.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.
Files changed (53) hide show
  1. package/bin/package.json +1 -1
  2. package/fontes/analisador-semantico/dialetos/analisador-semantico-birl.d.ts +2 -2
  3. package/fontes/analisador-semantico/dialetos/analisador-semantico-mapler.d.ts +2 -11
  4. package/fontes/analisador-semantico/dialetos/analisador-semantico-mapler.js.map +1 -1
  5. package/fontes/analisador-semantico/dialetos/analisador-semantico-visualg.d.ts +72 -0
  6. package/fontes/analisador-semantico/dialetos/analisador-semantico-visualg.js +280 -0
  7. package/fontes/analisador-semantico/dialetos/analisador-semantico-visualg.js.map +1 -0
  8. package/fontes/analisador-semantico/dialetos/index.d.ts +1 -0
  9. package/fontes/analisador-semantico/dialetos/index.js +1 -0
  10. package/fontes/analisador-semantico/dialetos/index.js.map +1 -1
  11. package/fontes/avaliador-sintatico/dialetos/avaliador-sintatico-birl.js +2 -2
  12. package/fontes/avaliador-sintatico/dialetos/avaliador-sintatico-birl.js.map +1 -1
  13. package/fontes/avaliador-sintatico/dialetos/potigol/avaliador-sintatico-potigol.js +1 -1
  14. package/fontes/avaliador-sintatico/dialetos/potigol/avaliador-sintatico-potigol.js.map +1 -1
  15. package/fontes/avaliador-sintatico/dialetos/visualg/avaliador-sintatico-visualg.d.ts +3 -0
  16. package/fontes/avaliador-sintatico/dialetos/visualg/avaliador-sintatico-visualg.js +32 -4
  17. package/fontes/avaliador-sintatico/dialetos/visualg/avaliador-sintatico-visualg.js.map +1 -1
  18. package/fontes/construtos/definir-valor.d.ts +4 -4
  19. package/fontes/construtos/definir-valor.js.map +1 -1
  20. package/fontes/construtos/funcao.d.ts +3 -2
  21. package/fontes/construtos/funcao.js.map +1 -1
  22. package/fontes/declaracoes/enquanto.d.ts +3 -2
  23. package/fontes/declaracoes/enquanto.js.map +1 -1
  24. package/fontes/declaracoes/fazer.d.ts +4 -2
  25. package/fontes/declaracoes/fazer.js.map +1 -1
  26. package/fontes/declaracoes/para.d.ts +2 -2
  27. package/fontes/declaracoes/tente.d.ts +2 -2
  28. package/fontes/formatadores/delegua.d.ts +64 -9
  29. package/fontes/formatadores/delegua.js +572 -61
  30. package/fontes/formatadores/delegua.js.map +1 -1
  31. package/fontes/interfaces/dialetos/interpretador-birl-interface.js.map +1 -0
  32. package/fontes/interfaces/funcao-hipotetica-interface.d.ts +3 -0
  33. package/fontes/interfaces/funcao-hipotetica-interface.js +3 -0
  34. package/fontes/interfaces/funcao-hipotetica-interface.js.map +1 -0
  35. package/fontes/interfaces/tipos-dados-interface.d.ts +1 -1
  36. package/fontes/interfaces/variavel-hipotetica-interface.d.ts +7 -0
  37. package/fontes/interfaces/variavel-hipotetica-interface.js +3 -0
  38. package/fontes/interfaces/variavel-hipotetica-interface.js.map +1 -0
  39. package/fontes/interpretador/dialetos/birl/comum.d.ts +1 -1
  40. package/fontes/interpretador/dialetos/birl/interpretador-birl-com-depuracao.d.ts +1 -1
  41. package/fontes/interpretador/dialetos/egua-classico/interpretador-egua-classico.d.ts +1 -1
  42. package/fontes/interpretador/dialetos/egua-classico/interpretador-egua-classico.js.map +1 -1
  43. package/fontes/interpretador/dialetos/visualg/comum.d.ts +3 -1
  44. package/fontes/interpretador/dialetos/visualg/comum.js +69 -1
  45. package/fontes/interpretador/dialetos/visualg/comum.js.map +1 -1
  46. package/fontes/interpretador/dialetos/visualg/interpretador-visualg-com-depuracao.js +2 -63
  47. package/fontes/interpretador/dialetos/visualg/interpretador-visualg-com-depuracao.js.map +1 -1
  48. package/fontes/interpretador/dialetos/visualg/interpretador-visualg.js +2 -63
  49. package/fontes/interpretador/dialetos/visualg/interpretador-visualg.js.map +1 -1
  50. package/package.json +1 -1
  51. package/fontes/interfaces/dialeto/interpretador-birl-interface.js.map +0 -1
  52. /package/fontes/interfaces/{dialeto → dialetos}/interpretador-birl-interface.d.ts +0 -0
  53. /package/fontes/interfaces/{dialeto → dialetos}/interpretador-birl-interface.js +0 -0
@@ -4,79 +4,590 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.FormatadorDelegua = void 0;
7
+ const construtos_1 = require("../construtos");
7
8
  const delegua_1 = __importDefault(require("../tipos-de-simbolos/delegua"));
8
9
  /**
9
10
  * O formatador de código Delégua.
10
11
  * Normalmente usado por IDEs, mas pode ser usado por linha de comando ou programaticamente.
11
12
  */
12
13
  class FormatadorDelegua {
13
- constructor() {
14
- this.indentacao = 0;
15
- this.blocoAberto = true;
14
+ constructor(quebraLinha, tamanhoIndentacao = 4) {
15
+ this.quebraLinha = quebraLinha;
16
+ this.tamanhoIndentacao = tamanhoIndentacao;
17
+ this.indentacaoAtual = 0;
18
+ this.codigoFormatado = '';
19
+ this.devePularLinha = true;
20
+ this.deveIndentar = true;
21
+ }
22
+ visitarDeclaracaoClasse(declaracao) {
23
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}classe ${declaracao.simbolo.lexema} {${this.quebraLinha}`;
24
+ this.indentacaoAtual += this.tamanhoIndentacao;
25
+ for (let propriedade of declaracao.propriedades) {
26
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}${propriedade.nome.lexema}: ${propriedade.tipo || 'qualquer'}${this.quebraLinha}`;
27
+ }
28
+ this.codigoFormatado += `${this.quebraLinha}`;
29
+ for (let metodo of declaracao.metodos) {
30
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}${metodo.simbolo.lexema}`;
31
+ this.visitarExpressaoFuncaoConstruto(metodo.funcao);
32
+ }
33
+ this.indentacaoAtual -= this.tamanhoIndentacao;
34
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`;
35
+ }
36
+ visitarDeclaracaoConst(declaracao) {
37
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}constante ${declaracao.simbolo.lexema}`;
38
+ if (declaracao.inicializador) {
39
+ this.codigoFormatado += ` = `;
40
+ this.formatarDeclaracaoOuConstruto(declaracao.inicializador);
41
+ }
42
+ if (this.devePularLinha) {
43
+ this.codigoFormatado += this.quebraLinha;
44
+ }
45
+ }
46
+ visitarDeclaracaoConstMultiplo(declaracao) {
47
+ // TODO: Até então o código nunca passa por aqui, porque o avaliador sintático
48
+ // converte `ConstMultiplo` em várias declarações `const`.
49
+ // Talvez mudar a avaliação sintática para não fazer mais isso.
50
+ console.log(declaracao);
51
+ }
52
+ visitarDeclaracaoDeAtribuicao(expressao) {
53
+ if ([
54
+ delegua_1.default.MAIS_IGUAL,
55
+ delegua_1.default.MENOS_IGUAL,
56
+ delegua_1.default.MULTIPLICACAO_IGUAL,
57
+ delegua_1.default.DIVISAO_IGUAL,
58
+ delegua_1.default.DIVISAO_INTEIRA_IGUAL,
59
+ delegua_1.default.MODULO_IGUAL,
60
+ ].includes(expressao.valor.operador.tipo)) {
61
+ this.visitarExpressaoBinaria(expressao.valor);
62
+ }
63
+ else {
64
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`;
65
+ this.codigoFormatado += `${expressao.simbolo.lexema} = `;
66
+ this.formatarDeclaracaoOuConstruto(expressao.valor);
67
+ }
68
+ this.codigoFormatado += `${this.quebraLinha}`;
69
+ }
70
+ visitarDeclaracaoDeExpressao(declaracao) {
71
+ this.codigoFormatado += ' '.repeat(this.indentacaoAtual);
72
+ this.formatarDeclaracaoOuConstruto(declaracao.expressao);
73
+ }
74
+ visitarDeclaracaoDefinicaoFuncao(declaracao) {
75
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}função `;
76
+ if (declaracao.simbolo) {
77
+ this.codigoFormatado += `${declaracao.simbolo.lexema}`;
78
+ }
79
+ this.formatarDeclaracaoOuConstruto(declaracao.funcao);
80
+ }
81
+ visitarDeclaracaoEnquanto(declaracao) {
82
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}enquanto `;
83
+ this.formatarDeclaracaoOuConstruto(declaracao.condicao);
84
+ this.formatarDeclaracaoOuConstruto(declaracao.corpo);
85
+ }
86
+ visitarDeclaracaoEscolha(declaracao) {
87
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}escolha `;
88
+ this.formatarDeclaracaoOuConstruto(declaracao.identificadorOuLiteral);
89
+ this.codigoFormatado += ` {${this.quebraLinha}`;
90
+ this.indentacaoAtual += this.tamanhoIndentacao;
91
+ for (let caminho of declaracao.caminhos) {
92
+ for (let declaracoes of caminho.condicoes) {
93
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}caso `;
94
+ this.formatarDeclaracaoOuConstruto(declaracoes);
95
+ this.codigoFormatado += ':';
96
+ this.codigoFormatado += this.quebraLinha;
97
+ }
98
+ for (let declaracoes of caminho.declaracoes) {
99
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`;
100
+ this.formatarDeclaracaoOuConstruto(declaracoes);
101
+ }
102
+ }
103
+ for (let padrao of declaracao.caminhoPadrao.declaracoes) {
104
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}padrao:`;
105
+ this.codigoFormatado += this.quebraLinha;
106
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`;
107
+ this.formatarDeclaracaoOuConstruto(padrao);
108
+ }
109
+ this.indentacaoAtual -= this.tamanhoIndentacao;
110
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`;
111
+ }
112
+ visitarDeclaracaoEscreva(declaracao) {
113
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}escreva(`;
114
+ for (let argumento of declaracao.argumentos) {
115
+ this.formatarDeclaracaoOuConstruto(argumento);
116
+ }
117
+ this.codigoFormatado += `)${this.quebraLinha}`;
118
+ }
119
+ visitarDeclaracaoFazer(declaracao) {
120
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}fazer {${this.quebraLinha}`;
121
+ this.indentacaoAtual += this.tamanhoIndentacao;
122
+ for (let declaracaoBloco of declaracao.caminhoFazer.declaracoes) {
123
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
124
+ }
125
+ this.indentacaoAtual -= this.tamanhoIndentacao;
126
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}} enquanto `;
127
+ this.formatarDeclaracaoOuConstruto(declaracao.condicaoEnquanto);
128
+ this.codigoFormatado += `${this.quebraLinha}`;
129
+ }
130
+ visitarDeclaracaoImportar(declaracao) {
131
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}importar(`;
132
+ this.formatarDeclaracaoOuConstruto(declaracao.caminho);
133
+ this.codigoFormatado += `)`;
134
+ }
135
+ visitarDeclaracaoPara(declaracao) {
136
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}para `;
137
+ this.devePularLinha = false;
138
+ if (declaracao.inicializador) {
139
+ if (Array.isArray(declaracao.inicializador)) {
140
+ this.deveIndentar = false;
141
+ for (let declaracaoInicializador of declaracao.inicializador) {
142
+ this.formatarDeclaracaoOuConstruto(declaracaoInicializador);
143
+ }
144
+ this.deveIndentar = true;
145
+ }
146
+ else {
147
+ this.formatarDeclaracaoOuConstruto(declaracao.inicializador);
148
+ }
149
+ }
150
+ this.codigoFormatado += `; `;
151
+ this.formatarDeclaracaoOuConstruto(declaracao.condicao);
152
+ this.codigoFormatado += `; `;
153
+ this.formatarDeclaracaoOuConstruto(declaracao.incrementar);
154
+ this.devePularLinha = true;
155
+ this.codigoFormatado += ` {${this.quebraLinha}`;
156
+ this.indentacaoAtual += this.tamanhoIndentacao;
157
+ for (let declaracaoBloco of declaracao.corpo.declaracoes) {
158
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
159
+ }
160
+ this.indentacaoAtual -= this.tamanhoIndentacao;
161
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`;
162
+ }
163
+ visitarDeclaracaoParaCada(declaracao) {
164
+ throw new Error('Método não implementado.');
165
+ }
166
+ visitarDeclaracaoSe(declaracao) {
167
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}se `;
168
+ this.formatarDeclaracaoOuConstruto(declaracao.condicao);
169
+ this.codigoFormatado += ` {${this.quebraLinha}`;
170
+ this.indentacaoAtual += this.tamanhoIndentacao;
171
+ for (let declaracaoBloco of declaracao.caminhoEntao.declaracoes) {
172
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
173
+ }
174
+ this.indentacaoAtual -= this.tamanhoIndentacao;
175
+ if (declaracao.caminhoSenao) {
176
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}} senão {${this.quebraLinha}`;
177
+ this.indentacaoAtual += this.tamanhoIndentacao;
178
+ for (let declaracaoBloco of declaracao.caminhoSenao.declaracoes) {
179
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
180
+ }
181
+ this.indentacaoAtual -= this.tamanhoIndentacao;
182
+ }
183
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`;
184
+ }
185
+ visitarDeclaracaoTente(declaracao) {
186
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}tente {${this.quebraLinha}`;
187
+ this.indentacaoAtual += this.tamanhoIndentacao;
188
+ for (let declaracaoBloco of declaracao.caminhoTente) {
189
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
190
+ }
191
+ this.indentacaoAtual -= this.tamanhoIndentacao;
192
+ if (declaracao.caminhoPegue) {
193
+ this.codigoFormatado += `} pegue {${this.quebraLinha}`;
194
+ if (declaracao.caminhoPegue instanceof construtos_1.FuncaoConstruto) {
195
+ // Se tem um parâmetro de erro.
196
+ }
197
+ else {
198
+ // Se não tem um parâmetro de erro.
199
+ this.indentacaoAtual += this.tamanhoIndentacao;
200
+ for (let declaracaoBloco of declaracao.caminhoPegue) {
201
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
202
+ }
203
+ }
204
+ }
205
+ this.indentacaoAtual -= this.tamanhoIndentacao;
206
+ if (declaracao.caminhoFinalmente) {
207
+ this.codigoFormatado += `} finalmente {${this.quebraLinha}`;
208
+ this.indentacaoAtual += this.tamanhoIndentacao;
209
+ for (let declaracaoBloco of declaracao.caminhoFinalmente) {
210
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
211
+ }
212
+ }
213
+ this.indentacaoAtual -= this.tamanhoIndentacao;
214
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`;
215
+ }
216
+ visitarDeclaracaoVar(declaracao) {
217
+ if (this.deveIndentar) {
218
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`;
219
+ }
220
+ this.codigoFormatado += `var ${declaracao.simbolo.lexema}`;
221
+ if (declaracao.inicializador) {
222
+ this.codigoFormatado += ` = `;
223
+ this.formatarDeclaracaoOuConstruto(declaracao.inicializador);
224
+ }
225
+ if (this.devePularLinha) {
226
+ this.codigoFormatado += this.quebraLinha;
227
+ }
228
+ }
229
+ visitarDeclaracaoVarMultiplo(declaracao) {
230
+ // TODO: Até então o código nunca passa por aqui, porque o avaliador sintático
231
+ // converte `VarMultiplo` em várias declarações `var`.
232
+ // Talvez mudar a avaliação sintática para não fazer mais isso.
233
+ console.log(declaracao);
234
+ }
235
+ visitarExpressaoAcessoIndiceVariavel(expressao) {
236
+ this.formatarDeclaracaoOuConstruto(expressao.entidadeChamada);
237
+ this.codigoFormatado += `[`;
238
+ this.formatarDeclaracaoOuConstruto(expressao.indice);
239
+ this.codigoFormatado += `]`;
240
+ }
241
+ visitarExpressaoAcessoMetodo(expressao) {
242
+ this.formatarDeclaracaoOuConstruto(expressao.objeto);
243
+ this.codigoFormatado += '.';
244
+ this.codigoFormatado += expressao.simbolo.lexema;
245
+ }
246
+ visitarExpressaoAgrupamento(expressao) {
247
+ this.codigoFormatado += '(';
248
+ this.formatarDeclaracaoOuConstruto(expressao.expressao);
249
+ this.codigoFormatado += ')';
250
+ }
251
+ visitarExpressaoAtribuicaoPorIndice(expressao) {
252
+ this.formatarDeclaracaoOuConstruto(expressao.objeto);
253
+ this.codigoFormatado += '[';
254
+ this.formatarDeclaracaoOuConstruto(expressao.indice);
255
+ this.codigoFormatado += '] = ';
256
+ this.formatarDeclaracaoOuConstruto(expressao.valor);
257
+ this.codigoFormatado += this.quebraLinha;
258
+ }
259
+ visitarExpressaoAtribuicaoPorIndicesMatriz(expressao) {
260
+ // Implementar somente para VisuAlg que tem atribuição diferente de Delégua - matriz[1, 2] = 1
261
+ throw new Error('Método não implementado.');
262
+ }
263
+ visitarExpressaoAcessoElementoMatriz(expressao) {
264
+ // Implementar somente para VisuAlg que tem acesso ao elemento da matriz diferente de Delégua - var valor = matriz[1, 2]
265
+ throw new Error('Método não implementado.');
266
+ }
267
+ visitarExpressaoBinaria(expressao) {
268
+ this.formatarDeclaracaoOuConstruto(expressao.esquerda);
269
+ switch (expressao.operador.tipo) {
270
+ case delegua_1.default.ADICAO:
271
+ this.codigoFormatado += ` + `;
272
+ break;
273
+ case delegua_1.default.DIVISAO:
274
+ this.codigoFormatado += ` / `;
275
+ break;
276
+ case delegua_1.default.DIVISAO_IGUAL:
277
+ this.codigoFormatado += ` /= `;
278
+ break;
279
+ case delegua_1.default.DIVISAO_INTEIRA:
280
+ this.codigoFormatado += ` \\ `;
281
+ break;
282
+ case delegua_1.default.DIVISAO_INTEIRA_IGUAL:
283
+ this.codigoFormatado += ` \\= `;
284
+ break;
285
+ case delegua_1.default.EXPONENCIACAO:
286
+ this.codigoFormatado += ` ** `;
287
+ break;
288
+ case delegua_1.default.IGUAL_IGUAL:
289
+ this.codigoFormatado += ` == `;
290
+ break;
291
+ case delegua_1.default.MAIOR:
292
+ this.codigoFormatado += ` > `;
293
+ break;
294
+ case delegua_1.default.MAIOR_IGUAL:
295
+ this.codigoFormatado += ` >= `;
296
+ break;
297
+ case delegua_1.default.MAIS_IGUAL:
298
+ this.codigoFormatado += ` += `;
299
+ break;
300
+ case delegua_1.default.MENOR:
301
+ this.codigoFormatado += ` < `;
302
+ break;
303
+ case delegua_1.default.MENOR_IGUAL:
304
+ this.codigoFormatado += ` <= `;
305
+ break;
306
+ case delegua_1.default.MENOS_IGUAL:
307
+ this.codigoFormatado += ` -= `;
308
+ break;
309
+ case delegua_1.default.MODULO:
310
+ this.codigoFormatado += ` % `;
311
+ break;
312
+ case delegua_1.default.MODULO_IGUAL:
313
+ this.codigoFormatado += ` %= `;
314
+ break;
315
+ case delegua_1.default.MULTIPLICACAO:
316
+ this.codigoFormatado += ` * `;
317
+ break;
318
+ case delegua_1.default.MULTIPLICACAO_IGUAL:
319
+ this.codigoFormatado += ` * `;
320
+ break;
321
+ case delegua_1.default.SUBTRACAO:
322
+ this.codigoFormatado += ` - `;
323
+ break;
324
+ }
325
+ this.formatarDeclaracaoOuConstruto(expressao.direita);
326
+ }
327
+ formatarBlocoOuVetorDeclaracoes(declaracoes) {
328
+ this.codigoFormatado += ` {${this.quebraLinha}`;
329
+ this.indentacaoAtual += this.tamanhoIndentacao;
330
+ for (let declaracaoBloco of declaracoes) {
331
+ this.formatarDeclaracaoOuConstruto(declaracaoBloco);
332
+ }
333
+ this.indentacaoAtual -= this.tamanhoIndentacao;
334
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`;
335
+ }
336
+ visitarExpressaoBloco(declaracao) {
337
+ this.formatarBlocoOuVetorDeclaracoes(declaracao.declaracoes);
338
+ }
339
+ visitarExpressaoContinua(declaracao) {
340
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}continua${this.quebraLinha}`;
341
+ }
342
+ visitarExpressaoDeChamada(expressao) {
343
+ this.formatarDeclaracaoOuConstruto(expressao.entidadeChamada);
344
+ this.codigoFormatado += '(';
345
+ for (let argumento of expressao.argumentos) {
346
+ this.formatarDeclaracaoOuConstruto(argumento);
347
+ this.codigoFormatado += ', ';
348
+ }
349
+ if (expressao.argumentos.length > 0) {
350
+ this.codigoFormatado = this.codigoFormatado.slice(0, -2);
351
+ }
352
+ this.codigoFormatado += ')';
353
+ }
354
+ visitarExpressaoDefinirValor(expressao) {
355
+ this.formatarDeclaracaoOuConstruto(expressao.objeto);
356
+ this.codigoFormatado += `.${expressao.nome.lexema} = `;
357
+ this.formatarDeclaracaoOuConstruto(expressao.valor);
358
+ this.codigoFormatado += `${this.quebraLinha}`;
359
+ }
360
+ visitarExpressaoDeleguaFuncao(expressao) {
361
+ throw new Error('Método não implementado.');
362
+ }
363
+ visitarExpressaoDeVariavel(expressao) {
364
+ this.codigoFormatado += expressao.simbolo.lexema;
365
+ }
366
+ visitarExpressaoDicionario(expressao) {
367
+ throw new Error('Método não implementado.');
368
+ }
369
+ visitarExpressaoExpressaoRegular(expressao) {
370
+ this.codigoFormatado += `||${expressao.valor}||`;
371
+ }
372
+ visitarDeclaracaoEscrevaMesmaLinha(declaracao) {
373
+ throw new Error('Método não implementado.');
374
+ }
375
+ visitarExpressaoFalhar(expressao) {
376
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}falhar`;
377
+ if (expressao.explicacao) {
378
+ this.codigoFormatado += ` `;
379
+ this.formatarDeclaracaoOuConstruto(expressao.explicacao);
380
+ }
381
+ this.codigoFormatado += `${this.quebraLinha}`;
382
+ }
383
+ visitarExpressaoFimPara(declaracao) {
384
+ throw new Error('Método não implementado.');
385
+ }
386
+ visitarExpressaoFormatacaoEscrita(declaracao) {
387
+ throw new Error('Método não implementado.');
388
+ }
389
+ visitarExpressaoFuncaoConstruto(expressao) {
390
+ this.codigoFormatado += `(`;
391
+ for (let argumento of expressao.parametros) {
392
+ this.codigoFormatado += `${argumento.nome.lexema}${argumento.tipoDado && argumento.tipoDado.tipo ? `: ${argumento.tipoDado.tipo}, ` : ', '}`;
393
+ }
394
+ if (expressao.parametros.length > 0) {
395
+ this.codigoFormatado = this.codigoFormatado.slice(0, -2);
396
+ }
397
+ this.codigoFormatado += `) `;
398
+ this.formatarBlocoOuVetorDeclaracoes(expressao.corpo);
399
+ }
400
+ visitarExpressaoIsto(expressao) {
401
+ this.codigoFormatado += `isto`;
402
+ }
403
+ visitarExpressaoLeia(expressao) {
404
+ throw new Error('Método não implementado.');
405
+ }
406
+ visitarExpressaoLeiaMultiplo(expressao) {
407
+ throw new Error('Método não implementado.');
408
+ }
409
+ visitarExpressaoLiteral(expressao) {
410
+ if (typeof expressao.valor === 'string') {
411
+ this.codigoFormatado += `'${expressao.valor}'`;
412
+ return;
413
+ }
414
+ this.codigoFormatado += expressao.valor;
415
+ }
416
+ visitarExpressaoLogica(expressao) {
417
+ throw new Error('Método não implementado.');
418
+ }
419
+ visitarExpressaoRetornar(declaracao) {
420
+ this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}retorna`;
421
+ if (declaracao.valor) {
422
+ this.codigoFormatado += ` `;
423
+ this.formatarDeclaracaoOuConstruto(declaracao.valor);
424
+ }
425
+ this.codigoFormatado += `${this.quebraLinha}`;
426
+ }
427
+ visitarExpressaoSuper(expressao) {
428
+ // TODO: `expressao.metodo` deveria ser um `Construto`, não um `Simbolo` aqui.
429
+ this.codigoFormatado += `super.${expressao.metodo.lexema}()`;
430
+ console.log(expressao);
431
+ }
432
+ visitarExpressaoSustar(declaracao) {
433
+ throw new Error('Método não implementado.');
434
+ }
435
+ visitarExpressaoTipoDe(expressao) {
436
+ throw new Error('Método não implementado.');
437
+ }
438
+ visitarExpressaoUnaria(expressao) {
439
+ let operador;
440
+ switch (expressao.operador.tipo) {
441
+ case delegua_1.default.INCREMENTAR:
442
+ operador = `++`;
443
+ break;
444
+ case delegua_1.default.DECREMENTAR:
445
+ operador = `--`;
446
+ break;
447
+ }
448
+ switch (expressao.incidenciaOperador) {
449
+ case 'ANTES':
450
+ this.codigoFormatado += operador;
451
+ this.formatarDeclaracaoOuConstruto(expressao.operando);
452
+ break;
453
+ case 'DEPOIS':
454
+ this.formatarDeclaracaoOuConstruto(expressao.operando);
455
+ this.codigoFormatado += operador;
456
+ break;
457
+ }
458
+ if (this.devePularLinha) {
459
+ this.codigoFormatado += this.quebraLinha;
460
+ }
461
+ }
462
+ visitarExpressaoVetor(expressao) {
463
+ this.codigoFormatado += '[';
464
+ for (let valor of expressao.valores) {
465
+ this.formatarDeclaracaoOuConstruto(valor);
466
+ this.codigoFormatado += ', ';
467
+ }
468
+ if (expressao.valores.length > 0) {
469
+ this.codigoFormatado = this.codigoFormatado.slice(0, -2);
470
+ }
471
+ this.codigoFormatado += ']';
472
+ }
473
+ formatarDeclaracaoOuConstruto(declaracaoOuConstruto) {
474
+ switch (declaracaoOuConstruto.constructor.name) {
475
+ case 'AcessoIndiceVariavel':
476
+ this.visitarExpressaoAcessoIndiceVariavel(declaracaoOuConstruto);
477
+ break;
478
+ case 'AcessoMetodoOuPropriedade':
479
+ this.visitarExpressaoAcessoMetodo(declaracaoOuConstruto);
480
+ break;
481
+ case 'Agrupamento':
482
+ this.visitarExpressaoAgrupamento(declaracaoOuConstruto);
483
+ break;
484
+ case 'AtribuicaoPorIndice':
485
+ this.visitarExpressaoAtribuicaoPorIndice(declaracaoOuConstruto);
486
+ break;
487
+ case 'Atribuir':
488
+ this.visitarDeclaracaoDeAtribuicao(declaracaoOuConstruto);
489
+ break;
490
+ case 'Binario':
491
+ this.visitarExpressaoBinaria(declaracaoOuConstruto);
492
+ break;
493
+ case 'Bloco':
494
+ this.visitarExpressaoBloco(declaracaoOuConstruto);
495
+ break;
496
+ case 'Chamada':
497
+ this.visitarExpressaoDeChamada(declaracaoOuConstruto);
498
+ break;
499
+ case 'Classe':
500
+ this.visitarDeclaracaoClasse(declaracaoOuConstruto);
501
+ break;
502
+ case 'Continua':
503
+ this.visitarExpressaoContinua(declaracaoOuConstruto);
504
+ break;
505
+ case 'DefinirValor':
506
+ this.visitarExpressaoDefinirValor(declaracaoOuConstruto);
507
+ break;
508
+ case 'Escolha':
509
+ this.visitarDeclaracaoEscolha(declaracaoOuConstruto);
510
+ break;
511
+ case 'Enquanto':
512
+ this.visitarDeclaracaoEnquanto(declaracaoOuConstruto);
513
+ break;
514
+ case 'Escreva':
515
+ this.visitarDeclaracaoEscreva(declaracaoOuConstruto);
516
+ break;
517
+ case 'Expressao':
518
+ this.visitarDeclaracaoDeExpressao(declaracaoOuConstruto);
519
+ break;
520
+ case 'ExpressaoRegular':
521
+ this.visitarExpressaoExpressaoRegular(declaracaoOuConstruto);
522
+ break;
523
+ case 'Falhar':
524
+ this.visitarExpressaoFalhar(declaracaoOuConstruto);
525
+ break;
526
+ case 'Fazer':
527
+ this.visitarDeclaracaoFazer(declaracaoOuConstruto);
528
+ break;
529
+ case 'FuncaoConstruto':
530
+ this.visitarExpressaoFuncaoConstruto(declaracaoOuConstruto);
531
+ break;
532
+ case 'FuncaoDeclaracao':
533
+ this.visitarDeclaracaoDefinicaoFuncao(declaracaoOuConstruto);
534
+ break;
535
+ case 'Importar':
536
+ this.visitarDeclaracaoImportar(declaracaoOuConstruto);
537
+ break;
538
+ case 'Isto':
539
+ this.visitarExpressaoIsto(declaracaoOuConstruto);
540
+ break;
541
+ case 'Literal':
542
+ this.visitarExpressaoLiteral(declaracaoOuConstruto);
543
+ break;
544
+ case 'Para':
545
+ this.visitarDeclaracaoPara(declaracaoOuConstruto);
546
+ break;
547
+ case 'Retorna':
548
+ this.visitarExpressaoRetornar(declaracaoOuConstruto);
549
+ break;
550
+ case 'Se':
551
+ this.visitarDeclaracaoSe(declaracaoOuConstruto);
552
+ break;
553
+ case 'Super':
554
+ this.visitarExpressaoSuper(declaracaoOuConstruto);
555
+ break;
556
+ case 'Tente':
557
+ this.visitarDeclaracaoTente(declaracaoOuConstruto);
558
+ break;
559
+ case 'Unario':
560
+ this.visitarExpressaoUnaria(declaracaoOuConstruto);
561
+ break;
562
+ case 'Const':
563
+ this.visitarDeclaracaoConst(declaracaoOuConstruto);
564
+ break;
565
+ case 'Var':
566
+ this.visitarDeclaracaoVar(declaracaoOuConstruto);
567
+ break;
568
+ case 'Variavel':
569
+ this.visitarExpressaoDeVariavel(declaracaoOuConstruto);
570
+ break;
571
+ case 'Vetor':
572
+ this.visitarExpressaoVetor(declaracaoOuConstruto);
573
+ break;
574
+ default:
575
+ console.log(declaracaoOuConstruto.constructor.name);
576
+ break;
577
+ }
16
578
  }
17
579
  /**
18
580
  * Devolve código formatado de acordo com os símbolos encontrados.
19
- * @param simbolos Um vetor de símbolos.
20
- * @param quebraLinha O símbolo de quebra de linha. Normalmente `\r\n` para Windows e `\n` para outros sistemas operacionais.
21
- * @param tamanhoIndentacao O tamanho de cada bloco de indentação (por padrão, 4)
581
+ * @param {Declaracao[]} declaracoes Um vetor de declarações.
22
582
  * @returns Código Delégua formatado.
23
583
  */
24
- formatar(simbolos, quebraLinha, tamanhoIndentacao = 4) {
25
- let resultado = '';
26
- let deveQuebrarLinha = false;
27
- for (let simbolo of simbolos) {
28
- switch (simbolo.tipo) {
29
- case delegua_1.default.CHAVE_ESQUERDA:
30
- this.indentacao += tamanhoIndentacao;
31
- resultado += '{' + quebraLinha;
32
- resultado += ' '.repeat(this.indentacao);
33
- break;
34
- case delegua_1.default.CHAVE_DIREITA:
35
- this.indentacao -= tamanhoIndentacao;
36
- resultado += quebraLinha + ' '.repeat(this.indentacao) + '}' + quebraLinha;
37
- break;
38
- case delegua_1.default.ESCREVA:
39
- deveQuebrarLinha = true;
40
- case delegua_1.default.ENQUANTO:
41
- case delegua_1.default.FAZER:
42
- case delegua_1.default.FINALMENTE:
43
- case delegua_1.default.PARA:
44
- case delegua_1.default.PEGUE:
45
- case delegua_1.default.RETORNA:
46
- case delegua_1.default.SE:
47
- case delegua_1.default.SENAO:
48
- case delegua_1.default.SENÃO:
49
- case delegua_1.default.TENTE:
50
- case delegua_1.default.VARIAVEL:
51
- resultado += quebraLinha + ' '.repeat(this.indentacao) + simbolo.lexema + ' ';
52
- break;
53
- case delegua_1.default.PARENTESE_ESQUERDO:
54
- resultado += '(';
55
- break;
56
- case delegua_1.default.PARENTESE_DIREITO:
57
- resultado = resultado.trimEnd();
58
- resultado += ')';
59
- if (deveQuebrarLinha) {
60
- deveQuebrarLinha = false;
61
- resultado += quebraLinha;
62
- }
63
- else {
64
- resultado += ' ';
65
- }
66
- break;
67
- case delegua_1.default.FUNCAO:
68
- case delegua_1.default.FUNÇÃO:
69
- case delegua_1.default.IDENTIFICADOR:
70
- case delegua_1.default.IGUAL:
71
- case delegua_1.default.IGUAL_IGUAL:
72
- resultado += simbolo.lexema + ' ';
73
- break;
74
- default:
75
- resultado += simbolo.lexema;
76
- break;
77
- }
584
+ formatar(declaracoes) {
585
+ this.codigoFormatado = '';
586
+ this.indentacaoAtual = 0;
587
+ for (let declaracao of declaracoes) {
588
+ this.formatarDeclaracaoOuConstruto(declaracao);
78
589
  }
79
- return resultado;
590
+ return this.codigoFormatado;
80
591
  }
81
592
  }
82
593
  exports.FormatadorDelegua = FormatadorDelegua;