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