@designliquido/delegua 0.61.3 → 0.63.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 (86) hide show
  1. package/README.md +17 -9
  2. package/analisador-semantico/analisador-semantico.d.ts.map +1 -1
  3. package/analisador-semantico/analisador-semantico.js +22 -24
  4. package/analisador-semantico/analisador-semantico.js.map +1 -1
  5. package/avaliador-sintatico/avaliador-sintatico.js +7 -7
  6. package/avaliador-sintatico/avaliador-sintatico.js.map +1 -1
  7. package/avaliador-sintatico/comum.js +11 -10
  8. package/avaliador-sintatico/comum.js.map +1 -1
  9. package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.d.ts +13 -2
  10. package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.d.ts.map +1 -1
  11. package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.js +43 -1
  12. package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.js.map +1 -1
  13. package/avaliador-sintatico/dialetos/avaliador-sintatico-tenda.d.ts.map +1 -1
  14. package/avaliador-sintatico/dialetos/avaliador-sintatico-tenda.js +32 -34
  15. package/avaliador-sintatico/dialetos/avaliador-sintatico-tenda.js.map +1 -1
  16. package/bibliotecas/biblioteca-global.d.ts.map +1 -1
  17. package/bibliotecas/biblioteca-global.js +11 -13
  18. package/bibliotecas/biblioteca-global.js.map +1 -1
  19. package/bibliotecas/dialetos/egua-classico/biblioteca-global.js +9 -9
  20. package/bibliotecas/dialetos/egua-classico/biblioteca-global.js.map +1 -1
  21. package/bibliotecas/dialetos/pitugues/biblioteca-global.d.ts.map +1 -1
  22. package/bibliotecas/dialetos/pitugues/biblioteca-global.js +9 -11
  23. package/bibliotecas/dialetos/pitugues/biblioteca-global.js.map +1 -1
  24. package/bin/package.json +1 -1
  25. package/formatadores/formatador-delegua.js +1 -1
  26. package/formatadores/formatador-delegua.js.map +1 -1
  27. package/interfaces/retornos/retorno-lexador.d.ts +2 -2
  28. package/interfaces/retornos/retorno-lexador.d.ts.map +1 -1
  29. package/interpretador/dialetos/pitugues/comum.js +2 -2
  30. package/interpretador/dialetos/pitugues/comum.js.map +1 -1
  31. package/interpretador/interpretador-base.js +10 -10
  32. package/interpretador/interpretador-base.js.map +1 -1
  33. package/interpretador/interpretador.d.ts.map +1 -1
  34. package/interpretador/interpretador.js +7 -8
  35. package/interpretador/interpretador.js.map +1 -1
  36. package/lexador/dialetos/lexador-pitugues.d.ts +3 -3
  37. package/lexador/dialetos/lexador-pitugues.d.ts.map +1 -1
  38. package/lexador/dialetos/lexador-pitugues.js +7 -6
  39. package/lexador/dialetos/lexador-pitugues.js.map +1 -1
  40. package/lexador/dialetos/lexador-tenda.d.ts +2 -2
  41. package/lexador/dialetos/lexador-tenda.d.ts.map +1 -1
  42. package/lexador/dialetos/localizacao.d.ts +9 -0
  43. package/lexador/dialetos/localizacao.d.ts.map +1 -0
  44. package/lexador/dialetos/{pragma.js → localizacao.js} +1 -1
  45. package/lexador/dialetos/localizacao.js.map +1 -0
  46. package/package.json +1 -1
  47. package/tradutores/index.d.ts +2 -0
  48. package/tradutores/index.d.ts.map +1 -1
  49. package/tradutores/index.js +2 -0
  50. package/tradutores/index.js.map +1 -1
  51. package/tradutores/mermaid/aresta-fluxograma.d.ts +7 -0
  52. package/tradutores/mermaid/aresta-fluxograma.d.ts.map +1 -0
  53. package/tradutores/mermaid/aresta-fluxograma.js +11 -0
  54. package/tradutores/mermaid/aresta-fluxograma.js.map +1 -0
  55. package/tradutores/mermaid/diagrama-classe.d.ts +16 -0
  56. package/tradutores/mermaid/diagrama-classe.d.ts.map +1 -0
  57. package/tradutores/mermaid/diagrama-classe.js +33 -0
  58. package/tradutores/mermaid/diagrama-classe.js.map +1 -0
  59. package/tradutores/mermaid/index.d.ts +5 -0
  60. package/tradutores/mermaid/index.d.ts.map +1 -0
  61. package/tradutores/mermaid/index.js +21 -0
  62. package/tradutores/mermaid/index.js.map +1 -0
  63. package/tradutores/mermaid/subgrafo-classe.d.ts +14 -0
  64. package/tradutores/mermaid/subgrafo-classe.d.ts.map +1 -0
  65. package/tradutores/mermaid/subgrafo-classe.js +45 -0
  66. package/tradutores/mermaid/subgrafo-classe.js.map +1 -0
  67. package/tradutores/mermaid/vertice-fluxograma.d.ts +9 -0
  68. package/tradutores/mermaid/vertice-fluxograma.d.ts.map +1 -0
  69. package/tradutores/mermaid/vertice-fluxograma.js +16 -0
  70. package/tradutores/mermaid/vertice-fluxograma.js.map +1 -0
  71. package/tradutores/tradutor-assembly-arm.d.ts +98 -0
  72. package/tradutores/tradutor-assembly-arm.d.ts.map +1 -0
  73. package/tradutores/tradutor-assembly-arm.js +715 -0
  74. package/tradutores/tradutor-assembly-arm.js.map +1 -0
  75. package/tradutores/tradutor-assembly-x64.d.ts +82 -4
  76. package/tradutores/tradutor-assembly-x64.d.ts.map +1 -1
  77. package/tradutores/tradutor-assembly-x64.js +612 -87
  78. package/tradutores/tradutor-assembly-x64.js.map +1 -1
  79. package/tradutores/tradutor-mermaidjs.d.ts +10 -16
  80. package/tradutores/tradutor-mermaidjs.d.ts.map +1 -1
  81. package/tradutores/tradutor-mermaidjs.js +107 -43
  82. package/tradutores/tradutor-mermaidjs.js.map +1 -1
  83. package/umd/delegua.js +1998 -378
  84. package/lexador/dialetos/pragma.d.ts +0 -9
  85. package/lexador/dialetos/pragma.d.ts.map +0 -1
  86. package/lexador/dialetos/pragma.js.map +0 -1
@@ -0,0 +1,715 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TradutorAssemblyARM = void 0;
4
+ const construtos_1 = require("../construtos");
5
+ const declaracoes_1 = require("../declaracoes");
6
+ class TradutorAssemblyARM {
7
+ constructor(alvo = 'linux-arm') {
8
+ this.alvo = alvo;
9
+ this.indentacao = 0;
10
+ this.contadorLabels = 0;
11
+ this.variaveis = new Map();
12
+ this.registradoresDisponiveis = ['r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10'];
13
+ this.pilhaRegistradores = [];
14
+ this.bss = '.bss\n';
15
+ this.data = '.data\n';
16
+ this.dicionarioConstrutos = {
17
+ AcessoIndiceVariavel: this.traduzirAcessoIndiceVariavel.bind(this),
18
+ AcessoMetodoOuPropriedade: this.trazudirConstrutoAcessoMetodo.bind(this),
19
+ Agrupamento: this.traduzirConstrutoAgrupamento.bind(this),
20
+ AtribuicaoPorIndice: this.traduzirConstrutoAtribuicaoPorIndice.bind(this),
21
+ Atribuir: this.traduzirConstrutoAtribuir.bind(this),
22
+ Binario: this.traduzirConstrutoBinario.bind(this),
23
+ Chamada: this.traduzirConstrutoChamada.bind(this),
24
+ DefinirValor: this.traduzirConstrutoDefinirValor.bind(this),
25
+ FuncaoConstruto: this.traduzirFuncaoConstruto.bind(this),
26
+ Isto: () => 'this',
27
+ Literal: this.traduzirConstrutoLiteral.bind(this),
28
+ Logico: this.traduzirConstrutoLogico.bind(this),
29
+ TipoDe: this.traduzirConstrutoTipoDe.bind(this),
30
+ Unario: this.traduzirConstrutoUnario.bind(this),
31
+ Variavel: this.traduzirConstrutoVariavel.bind(this),
32
+ Vetor: this.traduzirConstrutoVetor.bind(this),
33
+ };
34
+ this.dicionarioDeclaracoes = {
35
+ Bloco: this.traduzirDeclaracaoBloco.bind(this),
36
+ Enquanto: this.traduzirDeclaracaoEnquanto.bind(this),
37
+ Continua: () => 'b .continue_label',
38
+ Escolha: this.traduzirDeclaracaoEscolha.bind(this),
39
+ Escreva: this.traduzirDeclaracaoEscreva.bind(this),
40
+ Expressao: this.traduzirDeclaracaoExpressao.bind(this),
41
+ Fazer: this.traduzirDeclaracaoFazer.bind(this),
42
+ Falhar: this.traduzirDeclaracaoFalhar.bind(this),
43
+ FuncaoDeclaracao: this.traduzirDeclaracaoFuncao.bind(this),
44
+ Importar: this.traduzirDeclaracaoImportar.bind(this),
45
+ Leia: this.traduzirDeclaracaoLeia.bind(this),
46
+ Para: this.traduzirDeclaracaoPara.bind(this),
47
+ ParaCada: this.traduzirDeclaracaoParaCada.bind(this),
48
+ Retorna: this.traduzirDeclaracaoRetorna.bind(this),
49
+ Se: this.traduzirDeclaracaoSe.bind(this),
50
+ Sustar: () => 'b .break_label',
51
+ Classe: this.traduzirDeclaracaoClasse.bind(this),
52
+ Tente: this.traduzirDeclaracaoTente.bind(this),
53
+ Const: this.traduzirDeclaracaoConst.bind(this),
54
+ Var: this.traduzirDeclaracaoVar.bind(this),
55
+ };
56
+ this.indentacao = 0;
57
+ // Seleciona o símbolo de entrada conforme a plataforma alvo.
58
+ // Para linux-arm: programa standalone com _start.
59
+ // Para android: ainda é um binário standalone, mas com rótulo Delegua_main
60
+ // para deixar claro que não é o _start do CRT padrão.
61
+ const entryLabel = this.alvo === 'android' ? 'Delegua_main' : '_start';
62
+ this.text = `
63
+ .text
64
+ .global ${entryLabel}
65
+
66
+ ${entryLabel}:`;
67
+ }
68
+ gerarDigitoAleatorio() {
69
+ let result = '';
70
+ const digits = '0123456789';
71
+ for (let i = 0; i < 5; i++) {
72
+ const randomIndex = Math.floor(Math.random() * digits.length);
73
+ result += digits.charAt(randomIndex);
74
+ }
75
+ return result;
76
+ }
77
+ gerarLabel() {
78
+ return `.L${this.contadorLabels++}`;
79
+ }
80
+ obterRegistrador() {
81
+ if (this.registradoresDisponiveis.length > 0) {
82
+ const reg = this.registradoresDisponiveis.pop();
83
+ this.pilhaRegistradores.push(reg);
84
+ return reg;
85
+ }
86
+ return 'r0'; // fallback
87
+ }
88
+ liberarRegistrador(reg) {
89
+ const index = this.pilhaRegistradores.indexOf(reg);
90
+ if (index > -1) {
91
+ this.pilhaRegistradores.splice(index, 1);
92
+ this.registradoresDisponiveis.push(reg);
93
+ }
94
+ }
95
+ // Implementação dos Construtos
96
+ traduzirAcessoIndiceVariavel(construto) {
97
+ var _a;
98
+ let nomeVar;
99
+ if (construto.entidadeChamada instanceof construtos_1.Variavel) {
100
+ nomeVar = (_a = construto.entidadeChamada.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
101
+ }
102
+ if (!nomeVar) {
103
+ nomeVar = 'unknown';
104
+ }
105
+ const indice = this.dicionarioConstrutos[construto.indice.constructor.name](construto.indice);
106
+ const reg = this.obterRegistrador();
107
+ this.text += `
108
+ ldr ${reg}, =${nomeVar}
109
+ ldr r0, =${indice}
110
+ lsl r0, r0, #2 @ multiply index by 4 (word size)
111
+ add ${reg}, ${reg}, r0
112
+ ldr r0, [${reg}]`;
113
+ this.liberarRegistrador(reg);
114
+ return 'r0';
115
+ }
116
+ trazudirConstrutoAcessoMetodo(construto) {
117
+ const objeto = this.dicionarioConstrutos[construto.objeto.constructor.name](construto.objeto);
118
+ return `${objeto}_${construto.nomeMetodo}`;
119
+ }
120
+ traduzirConstrutoAgrupamento(construto) {
121
+ return this.dicionarioConstrutos[construto.expressao.constructor.name](construto.expressao);
122
+ }
123
+ traduzirConstrutoAtribuicaoPorIndice(construto) {
124
+ var _a;
125
+ let nomeVar;
126
+ if (construto.objeto instanceof construtos_1.Variavel) {
127
+ nomeVar = (_a = construto.objeto.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
128
+ }
129
+ if (!nomeVar) {
130
+ nomeVar = 'unknown';
131
+ }
132
+ const indice = this.dicionarioConstrutos[construto.indice.constructor.name](construto.indice);
133
+ const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
134
+ const reg = this.obterRegistrador();
135
+ this.text += `
136
+ ldr ${reg}, =${nomeVar}
137
+ ldr r1, =${indice}
138
+ lsl r1, r1, #2 @ multiply by 4
139
+ add ${reg}, ${reg}, r1
140
+ ldr r1, =${valor}
141
+ str r1, [${reg}]`;
142
+ this.liberarRegistrador(reg);
143
+ }
144
+ traduzirConstrutoAtribuir(construto) {
145
+ var _a;
146
+ let nomeVar;
147
+ if (construto.alvo instanceof construtos_1.Variavel) {
148
+ nomeVar = (_a = construto.alvo.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
149
+ }
150
+ if (!nomeVar) {
151
+ return;
152
+ }
153
+ const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
154
+ if (!this.variaveis.has(nomeVar)) {
155
+ const varLabel = `var_${nomeVar}`;
156
+ this.bss += ` ${varLabel}: .space 4\n`;
157
+ this.variaveis.set(nomeVar, varLabel);
158
+ }
159
+ this.text += `
160
+ ldr r0, =${valor}
161
+ ldr r1, =${this.variaveis.get(nomeVar)}
162
+ str r0, [r1]`;
163
+ }
164
+ traduzirConstrutoBinario(construto) {
165
+ const esquerda = this.dicionarioConstrutos[construto.esquerda.constructor.name](construto.esquerda);
166
+ const direita = this.dicionarioConstrutos[construto.direita.constructor.name](construto.direita);
167
+ const operador = construto.operador.lexema;
168
+ const reg = this.obterRegistrador();
169
+ // Load left operand into r0
170
+ if (esquerda !== 'r0') {
171
+ this.text += `
172
+ ldr r0, =${esquerda}`;
173
+ }
174
+ // Load right operand into reg
175
+ this.text += `
176
+ ldr ${reg}, =${direita}`;
177
+ switch (operador) {
178
+ case '+':
179
+ this.text += `
180
+ add r0, r0, ${reg}`;
181
+ break;
182
+ case '-':
183
+ this.text += `
184
+ sub r0, r0, ${reg}`;
185
+ break;
186
+ case '*':
187
+ this.text += `
188
+ mul r0, r0, ${reg}`;
189
+ break;
190
+ case '/':
191
+ this.text += `
192
+ sdiv r0, r0, ${reg}`;
193
+ break;
194
+ case '%':
195
+ this.text += `
196
+ sdiv r1, r0, ${reg}
197
+ mul r1, r1, ${reg}
198
+ sub r0, r0, r1 @ r0 = r0 - (r0/reg)*reg`;
199
+ break;
200
+ case '<':
201
+ this.text += `
202
+ cmp r0, ${reg}
203
+ movlt r0, #1
204
+ movge r0, #0`;
205
+ break;
206
+ case '>':
207
+ this.text += `
208
+ cmp r0, ${reg}
209
+ movgt r0, #1
210
+ movle r0, #0`;
211
+ break;
212
+ case '<=':
213
+ this.text += `
214
+ cmp r0, ${reg}
215
+ movle r0, #1
216
+ movgt r0, #0`;
217
+ break;
218
+ case '>=':
219
+ this.text += `
220
+ cmp r0, ${reg}
221
+ movge r0, #1
222
+ movlt r0, #0`;
223
+ break;
224
+ case '==':
225
+ case '===':
226
+ this.text += `
227
+ cmp r0, ${reg}
228
+ moveq r0, #1
229
+ movne r0, #0`;
230
+ break;
231
+ case '!=':
232
+ case '!==':
233
+ this.text += `
234
+ cmp r0, ${reg}
235
+ movne r0, #1
236
+ moveq r0, #0`;
237
+ break;
238
+ default:
239
+ this.text += `
240
+ @ Operador ${operador} não implementado`;
241
+ }
242
+ this.liberarRegistrador(reg);
243
+ return 'r0';
244
+ }
245
+ traduzirConstrutoChamada(construto) {
246
+ var _a;
247
+ let nomeFuncao = 'funcao';
248
+ if (construto.entidadeChamada instanceof construtos_1.Variavel) {
249
+ nomeFuncao = ((_a = construto.entidadeChamada.simbolo) === null || _a === void 0 ? void 0 : _a.lexema) || 'funcao';
250
+ }
251
+ // ARM calling convention: r0-r3 for first 4 args, rest on stack
252
+ const registrosArgs = ['r0', 'r1', 'r2', 'r3'];
253
+ construto.argumentos.forEach((arg, index) => {
254
+ if (index < registrosArgs.length) {
255
+ const valorArg = this.dicionarioConstrutos[arg.constructor.name](arg);
256
+ if (valorArg !== registrosArgs[index]) {
257
+ this.text += `
258
+ ldr ${registrosArgs[index]}, =${valorArg}`;
259
+ }
260
+ }
261
+ else {
262
+ // Push extra args on stack
263
+ const valorArg = this.dicionarioConstrutos[arg.constructor.name](arg);
264
+ this.text += `
265
+ ldr r0, =${valorArg}
266
+ push {r0}`;
267
+ }
268
+ });
269
+ this.text += `
270
+ bl ${nomeFuncao}`;
271
+ }
272
+ traduzirConstrutoDefinirValor(construto) {
273
+ const objeto = this.dicionarioConstrutos[construto.objeto.constructor.name](construto.objeto);
274
+ const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
275
+ this.text += `
276
+ ldr r0, =${valor}
277
+ ldr r1, =${objeto}
278
+ str r0, [r1]`;
279
+ }
280
+ traduzirFuncaoConstruto(construto) {
281
+ const labelFuncao = `func_${this.gerarDigitoAleatorio()}`;
282
+ this.text += `
283
+
284
+ ${labelFuncao}:
285
+ push {fp, lr}
286
+ mov fp, sp`;
287
+ // Traduzir corpo da função
288
+ if (construto.corpo && Array.isArray(construto.corpo)) {
289
+ construto.corpo.forEach((declaracao) => {
290
+ if (this.dicionarioDeclaracoes[declaracao.constructor.name]) {
291
+ this.dicionarioDeclaracoes[declaracao.constructor.name](declaracao);
292
+ }
293
+ });
294
+ }
295
+ this.text += `
296
+ mov sp, fp
297
+ pop {fp, pc}`;
298
+ }
299
+ traduzirConstrutoLiteral(construto) {
300
+ if (typeof construto.valor === 'string') {
301
+ return this.criaStringLiteral(construto);
302
+ }
303
+ return String(construto.valor);
304
+ }
305
+ traduzirConstrutoLogico(construto) {
306
+ const esquerda = this.dicionarioConstrutos[construto.esquerda.constructor.name](construto.esquerda);
307
+ const direita = this.dicionarioConstrutos[construto.direita.constructor.name](construto.direita);
308
+ const operador = construto.operador.lexema;
309
+ const labelVerdadeiro = this.gerarLabel();
310
+ const labelFim = this.gerarLabel();
311
+ this.text += `
312
+ ldr r0, =${esquerda}
313
+ cmp r0, #0`;
314
+ if (operador === 'e' || operador === '&&') {
315
+ this.text += `
316
+ beq ${labelFim}
317
+ ldr r0, =${direita}
318
+ cmp r0, #0
319
+ beq ${labelFim}
320
+ ${labelVerdadeiro}:
321
+ mov r0, #1
322
+ ${labelFim}:`;
323
+ }
324
+ else if (operador === 'ou' || operador === '||') {
325
+ this.text += `
326
+ bne ${labelVerdadeiro}
327
+ ldr r0, =${direita}
328
+ cmp r0, #0
329
+ bne ${labelVerdadeiro}
330
+ mov r0, #0
331
+ b ${labelFim}
332
+ ${labelVerdadeiro}:
333
+ mov r0, #1
334
+ ${labelFim}:`;
335
+ }
336
+ return 'r0';
337
+ }
338
+ traduzirConstrutoTipoDe(construto) {
339
+ const expressao = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
340
+ return expressao;
341
+ }
342
+ traduzirConstrutoUnario(construto) {
343
+ const operando = this.dicionarioConstrutos[construto.operando.constructor.name](construto.operando);
344
+ const operador = construto.operador.lexema;
345
+ this.text += `
346
+ ldr r0, =${operando}`;
347
+ if (operador === '-') {
348
+ this.text += `
349
+ neg r0, r0`;
350
+ }
351
+ else if (operador === '!' || operador === 'nao') {
352
+ this.text += `
353
+ cmp r0, #0
354
+ moveq r0, #1
355
+ movne r0, #0`;
356
+ }
357
+ return 'r0';
358
+ }
359
+ traduzirConstrutoVariavel(construto) {
360
+ var _a;
361
+ const nomeVar = (_a = construto.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
362
+ if (nomeVar && this.variaveis.has(nomeVar)) {
363
+ const varLabel = this.variaveis.get(nomeVar);
364
+ this.text += `
365
+ ldr r0, =${varLabel}
366
+ ldr r0, [r0]`;
367
+ return 'r0';
368
+ }
369
+ return nomeVar || 'unknown';
370
+ }
371
+ traduzirConstrutoVetor(construto) {
372
+ var _a;
373
+ const labelVetor = `vetor_${this.gerarDigitoAleatorio()}`;
374
+ const tamanho = ((_a = construto.valores) === null || _a === void 0 ? void 0 : _a.length) || 0;
375
+ this.bss += ` ${labelVetor}: .space ${tamanho * 4}\n`;
376
+ if (construto.valores && Array.isArray(construto.valores)) {
377
+ construto.valores.forEach((valor, index) => {
378
+ if (this.dicionarioConstrutos[valor.constructor.name]) {
379
+ const valorTraduzido = this.dicionarioConstrutos[valor.constructor.name](valor);
380
+ this.text += `
381
+ ldr r0, =${valorTraduzido}
382
+ ldr r1, =${labelVetor}
383
+ str r0, [r1, #${index * 4}]`;
384
+ }
385
+ });
386
+ }
387
+ return labelVetor;
388
+ }
389
+ // Implementação das Declarações
390
+ traduzirDeclaracaoBloco(declaracao) {
391
+ if (declaracao.declaracoes && Array.isArray(declaracao.declaracoes)) {
392
+ declaracao.declaracoes.forEach((decl) => {
393
+ if (this.dicionarioDeclaracoes[decl.constructor.name]) {
394
+ this.dicionarioDeclaracoes[decl.constructor.name](decl);
395
+ }
396
+ });
397
+ }
398
+ }
399
+ traduzirDeclaracaoEnquanto(declaracao) {
400
+ const labelInicio = this.gerarLabel();
401
+ const labelFim = this.gerarLabel();
402
+ this.text += `
403
+ ${labelInicio}:`;
404
+ const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
405
+ this.text += `
406
+ cmp ${condicao}, #0
407
+ beq ${labelFim}`;
408
+ if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
409
+ this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
410
+ }
411
+ this.text += `
412
+ b ${labelInicio}
413
+ ${labelFim}:`;
414
+ }
415
+ traduzirDeclaracaoEscolha(declaracao) {
416
+ const labelFim = this.gerarLabel();
417
+ const valorEscolha = this.dicionarioConstrutos[declaracao.identificadorOuLiteral.constructor.name](declaracao.identificadorOuLiteral);
418
+ if (declaracao.caminhos && Array.isArray(declaracao.caminhos)) {
419
+ declaracao.caminhos.forEach((caminho) => {
420
+ const labelProximo = this.gerarLabel();
421
+ if (caminho.condicoes && caminho.condicoes[0]) {
422
+ const valorCaso = this.dicionarioConstrutos[caminho.condicoes[0].constructor.name](caminho.condicoes[0]);
423
+ this.text += `
424
+ ldr r0, =${valorEscolha}
425
+ ldr r1, =${valorCaso}
426
+ cmp r0, r1
427
+ bne ${labelProximo}`;
428
+ if (caminho.declaracoes && Array.isArray(caminho.declaracoes)) {
429
+ caminho.declaracoes.forEach((decl) => {
430
+ if (this.dicionarioDeclaracoes[decl.constructor.name]) {
431
+ this.dicionarioDeclaracoes[decl.constructor.name](decl);
432
+ }
433
+ });
434
+ }
435
+ this.text += `
436
+ b ${labelFim}
437
+ ${labelProximo}:`;
438
+ }
439
+ });
440
+ }
441
+ this.text += `
442
+ ${labelFim}:`;
443
+ }
444
+ traduzirDeclaracaoExpressao(declaracao) {
445
+ if (declaracao.expressao && this.dicionarioConstrutos[declaracao.expressao.constructor.name]) {
446
+ this.dicionarioConstrutos[declaracao.expressao.constructor.name](declaracao.expressao);
447
+ }
448
+ }
449
+ traduzirDeclaracaoFazer(declaracao) {
450
+ const labelInicio = this.gerarLabel();
451
+ this.text += `
452
+ ${labelInicio}:`;
453
+ if (declaracao.caminhoFazer && declaracao.caminhoFazer.declaracoes) {
454
+ declaracao.caminhoFazer.declaracoes.forEach((decl) => {
455
+ if (this.dicionarioDeclaracoes[decl.constructor.name]) {
456
+ this.dicionarioDeclaracoes[decl.constructor.name](decl);
457
+ }
458
+ });
459
+ }
460
+ if (declaracao.condicaoEnquanto) {
461
+ const condicao = this.dicionarioConstrutos[declaracao.condicaoEnquanto.constructor.name](declaracao.condicaoEnquanto);
462
+ this.text += `
463
+ cmp ${condicao}, #0
464
+ bne ${labelInicio}`;
465
+ }
466
+ }
467
+ traduzirDeclaracaoFalhar(declaracao) {
468
+ let mensagem = '"Erro"';
469
+ if (declaracao.explicacao && typeof declaracao.explicacao === 'object' && 'constructor' in declaracao.explicacao) {
470
+ const explicacao = declaracao.explicacao;
471
+ if (explicacao.constructor && this.dicionarioConstrutos[explicacao.constructor.name]) {
472
+ mensagem = this.dicionarioConstrutos[explicacao.constructor.name](explicacao);
473
+ }
474
+ }
475
+ this.text += `
476
+ @ Falhar com mensagem: ${mensagem}
477
+ mov r0, #1
478
+ mov r7, #1 @ sys_exit
479
+ swi 0`;
480
+ }
481
+ traduzirDeclaracaoFuncao(declaracao) {
482
+ var _a;
483
+ const nomeFuncao = ((_a = declaracao.simbolo) === null || _a === void 0 ? void 0 : _a.lexema) || 'funcao';
484
+ this.text += `
485
+
486
+ ${nomeFuncao}:
487
+ push {fp, lr}
488
+ mov fp, sp`;
489
+ if (declaracao.funcao && declaracao.funcao.corpo && Array.isArray(declaracao.funcao.corpo)) {
490
+ declaracao.funcao.corpo.forEach((decl) => {
491
+ if (this.dicionarioDeclaracoes[decl.constructor.name]) {
492
+ this.dicionarioDeclaracoes[decl.constructor.name](decl);
493
+ }
494
+ });
495
+ }
496
+ this.text += `
497
+ mov sp, fp
498
+ pop {fp, pc}`;
499
+ }
500
+ traduzirDeclaracaoImportar(declaracao) {
501
+ this.text += `
502
+ @ Importar: ${declaracao.caminho || 'unknown'}`;
503
+ }
504
+ traduzirDeclaracaoLeia(declaracao) {
505
+ var _a;
506
+ let nomeVar;
507
+ if (declaracao.argumentos && declaracao.argumentos[0] && declaracao.argumentos[0] instanceof construtos_1.Variavel) {
508
+ nomeVar = (_a = declaracao.argumentos[0].simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
509
+ }
510
+ if (!nomeVar)
511
+ return;
512
+ if (!this.variaveis.has(nomeVar)) {
513
+ const varLabel = `var_${nomeVar}`;
514
+ this.bss += ` ${varLabel}: .space 256\n`;
515
+ this.variaveis.set(nomeVar, varLabel);
516
+ }
517
+ this.text += `
518
+ ldr r1, =${this.variaveis.get(nomeVar)}
519
+ mov r2, #256
520
+ mov r0, #0 @ stdin
521
+ mov r7, #3 @ sys_read
522
+ swi 0`;
523
+ }
524
+ traduzirDeclaracaoPara(declaracao) {
525
+ const labelInicio = this.gerarLabel();
526
+ const labelFim = this.gerarLabel();
527
+ if (declaracao.inicializador) {
528
+ const tipoInicializador = declaracao.inicializador.constructor.name;
529
+ if (this.dicionarioDeclaracoes[tipoInicializador]) {
530
+ this.dicionarioDeclaracoes[tipoInicializador](declaracao.inicializador);
531
+ }
532
+ else if (this.dicionarioConstrutos[tipoInicializador]) {
533
+ this.dicionarioConstrutos[tipoInicializador](declaracao.inicializador);
534
+ }
535
+ }
536
+ this.text += `
537
+ ${labelInicio}:`;
538
+ if (declaracao.condicao) {
539
+ const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
540
+ this.text += `
541
+ cmp ${condicao}, #0
542
+ beq ${labelFim}`;
543
+ }
544
+ if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
545
+ this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
546
+ }
547
+ if (declaracao.incrementar) {
548
+ if (this.dicionarioConstrutos[declaracao.incrementar.constructor.name]) {
549
+ this.dicionarioConstrutos[declaracao.incrementar.constructor.name](declaracao.incrementar);
550
+ }
551
+ }
552
+ this.text += `
553
+ b ${labelInicio}
554
+ ${labelFim}:`;
555
+ }
556
+ traduzirDeclaracaoParaCada(declaracao) {
557
+ var _a;
558
+ const labelInicio = this.gerarLabel();
559
+ const labelFim = this.gerarLabel();
560
+ let nomeVar;
561
+ if (declaracao.variavelIteracao instanceof construtos_1.Variavel) {
562
+ nomeVar = (_a = declaracao.variavelIteracao.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
563
+ }
564
+ const vetor = declaracao.vetorOuDicionario;
565
+ let tamanhoVetor = 0;
566
+ if (vetor instanceof construtos_1.Vetor) {
567
+ tamanhoVetor = vetor.tamanho || 0;
568
+ }
569
+ this.text += `
570
+ mov r4, #0 @ counter
571
+ ${labelInicio}:
572
+ cmp r4, #${tamanhoVetor}
573
+ bge ${labelFim}`;
574
+ if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
575
+ this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
576
+ }
577
+ this.text += `
578
+ add r4, r4, #1
579
+ b ${labelInicio}
580
+ ${labelFim}:`;
581
+ }
582
+ traduzirDeclaracaoRetorna(declaracao) {
583
+ if (declaracao.valor) {
584
+ const valor = this.dicionarioConstrutos[declaracao.valor.constructor.name](declaracao.valor);
585
+ this.text += `
586
+ ldr r0, =${valor}`;
587
+ }
588
+ this.text += `
589
+ mov sp, fp
590
+ pop {fp, pc}`;
591
+ }
592
+ traduzirDeclaracaoSe(declaracao) {
593
+ const labelSenao = this.gerarLabel();
594
+ const labelFim = this.gerarLabel();
595
+ const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
596
+ this.text += `
597
+ cmp ${condicao}, #0
598
+ beq ${labelSenao}`;
599
+ if (this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name]) {
600
+ this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name](declaracao.caminhoEntao);
601
+ }
602
+ this.text += `
603
+ b ${labelFim}
604
+ ${labelSenao}:`;
605
+ if (declaracao.caminhoSenao && this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name]) {
606
+ this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name](declaracao.caminhoSenao);
607
+ }
608
+ this.text += `
609
+ ${labelFim}:`;
610
+ }
611
+ traduzirDeclaracaoClasse(declaracao) {
612
+ var _a;
613
+ this.text += `
614
+ @ Classe: ${((_a = declaracao.simbolo) === null || _a === void 0 ? void 0 : _a.lexema) || 'unknown'}`;
615
+ }
616
+ traduzirDeclaracaoTente(declaracao) {
617
+ this.text += `
618
+ @ Tente-pegue`;
619
+ if (declaracao.caminhoTente && Array.isArray(declaracao.caminhoTente)) {
620
+ declaracao.caminhoTente.forEach((decl) => {
621
+ if (this.dicionarioDeclaracoes[decl.constructor.name]) {
622
+ this.dicionarioDeclaracoes[decl.constructor.name](decl);
623
+ }
624
+ });
625
+ }
626
+ }
627
+ traduzirDeclaracaoConst(declaracao) {
628
+ var _a;
629
+ const nomeVar = (_a = declaracao.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
630
+ if (!nomeVar)
631
+ return;
632
+ const valor = this.dicionarioConstrutos[declaracao.inicializador.constructor.name](declaracao.inicializador);
633
+ const varLabel = `const_${nomeVar}`;
634
+ this.data += ` ${varLabel}: .word ${valor}\n`;
635
+ this.variaveis.set(nomeVar, varLabel);
636
+ }
637
+ traduzirDeclaracaoVar(declaracao) {
638
+ var _a;
639
+ const nomeVar = (_a = declaracao.simbolo) === null || _a === void 0 ? void 0 : _a.lexema;
640
+ if (!nomeVar)
641
+ return;
642
+ const varLabel = `var_${nomeVar}`;
643
+ this.bss += ` ${varLabel}: .space 4\n`;
644
+ this.variaveis.set(nomeVar, varLabel);
645
+ if (declaracao.inicializador && declaracao.inicializador.valor !== null) {
646
+ const tipoInicializador = declaracao.inicializador.constructor.name;
647
+ // Verificar se é um vetor
648
+ if (declaracao.inicializador instanceof construtos_1.Vetor) {
649
+ // Vetor precisa de tratamento especial
650
+ const labelVetor = this.traduzirConstrutoVetor(declaracao.inicializador);
651
+ // Associar o nome da variável com o label do vetor
652
+ this.variaveis.set(nomeVar, labelVetor);
653
+ }
654
+ else if (this.dicionarioConstrutos[tipoInicializador]) {
655
+ const valor = this.dicionarioConstrutos[tipoInicializador](declaracao.inicializador);
656
+ this.text += `
657
+ ldr r0, =${valor}
658
+ ldr r1, =${varLabel}
659
+ str r0, [r1]`;
660
+ }
661
+ }
662
+ }
663
+ criaStringLiteral(literal) {
664
+ const varLiteral = `Delegua_${this.gerarDigitoAleatorio()}`;
665
+ this.data += ` ${varLiteral}: .asciz "${literal.valor}"\n`;
666
+ return varLiteral;
667
+ }
668
+ criaTamanhoNaMemoriaReferenteAVar(nomeStringLiteral) {
669
+ const varTamanho = `tam_${nomeStringLiteral}`;
670
+ // Em ARM, calculamos o tamanho de forma diferente
671
+ // Podemos usar uma diretiva ou calcular em tempo de execução
672
+ return varTamanho;
673
+ }
674
+ traduzirDeclaracaoEscreva(declaracaoEscreva) {
675
+ let tam_string_literal = '';
676
+ let nome_string_literal = '';
677
+ if (declaracaoEscreva.argumentos[0] instanceof construtos_1.Literal) {
678
+ nome_string_literal = this.criaStringLiteral(declaracaoEscreva.argumentos[0]);
679
+ const stringValue = declaracaoEscreva.argumentos[0].valor;
680
+ tam_string_literal = String(stringValue.length);
681
+ }
682
+ // Para ambos linux-arm e android, continuamos usando a convenção
683
+ // de syscall Linux ARM (write). Em Android típico, esse binário
684
+ // seria executado via adb/Termux, onde essa convenção ainda é válida.
685
+ this.text += `
686
+ ldr r1, =${nome_string_literal}
687
+ mov r2, #${tam_string_literal}
688
+ mov r0, #1 @ fd stdout
689
+ mov r7, #4 @ sys_write
690
+ swi 0`;
691
+ }
692
+ saidaSistema() {
693
+ // Mesmo comentário da função Escreva: usamos sys_exit Linux.
694
+ // Em um futuro modo Android "NDK/JNI", esta função deveria
695
+ // apenas retornar ao chamador em vez de fazer syscall direta.
696
+ this.text += `
697
+ mov r0, #1 @ exit status
698
+ mov r7, #1 @ sys_exit
699
+ swi 0`;
700
+ }
701
+ traduzir(declaracoes) {
702
+ let resultado = '';
703
+ this.declaracoesDeClasses = declaracoes.filter((declaracao) => declaracao instanceof declaracoes_1.Classe);
704
+ for (const declaracao of declaracoes) {
705
+ if (this.dicionarioDeclaracoes[declaracao.constructor.name]) {
706
+ this.dicionarioDeclaracoes[declaracao.constructor.name](declaracao);
707
+ }
708
+ }
709
+ this.saidaSistema();
710
+ resultado += this.bss + '\n' + this.data + '\n' + this.text;
711
+ return resultado;
712
+ }
713
+ }
714
+ exports.TradutorAssemblyARM = TradutorAssemblyARM;
715
+ //# sourceMappingURL=tradutor-assembly-arm.js.map