Skip to content

Commit 91ea9c8

Browse files
committed
Implementa función alert_float y actualiza el manejo de tipos en GeneradorDeCodigo para soportar flotantes CORRECIONES FINALES
1 parent e11edaf commit 91ea9c8

8 files changed

Lines changed: 1441 additions & 1604 deletions

File tree

resources/templates/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ <h3>Resultados ejecución WASM</h3>
1313
alert("Valor recibido --> " + x);
1414
}
1515

16+
function alert_float(x) {
17+
alert("Valor recibido --> " + x);
18+
}
19+
1620
function alert_str(ptr, len) {
1721
if (!wasmInstance) { console.error('wasmInstance no inicializada'); return; }
1822
const memoryBuffer = wasmInstance.exports.memory.buffer;
@@ -29,7 +33,8 @@ <h3>Resultados ejecución WASM</h3>
2933
const importObject = {
3034
env: {
3135
alert_i32,
32-
alert_str
36+
alert_str,
37+
alert_float
3338
}
3439
};
3540

src/Compilador/ModuloGC/GeneradorDeCodigo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public void generarCodigo() {
7575
// 2. Encabezado
7676
codigoWAT.append("(module\n");
7777
codigoWAT.append(" (import \"env\" \"alert_i32\" (func $alert_i32 (param i32)))\n");
78+
codigoWAT.append(" (import \"env\" \"alert_float\" (func $alert_float (param f64)))\n");
7879
codigoWAT.append(" (import \"env\" \"alert_str\" (func $alert_str (param i32 i32)))\n");
7980
codigoWAT.append(" (memory (export \"memory\") 1)\n");
8081

@@ -394,16 +395,26 @@ private void procesarAsignacion(Terceto terceto, StringBuilder codigo) {
394395
private void procesarPrint(Terceto terceto, StringBuilder codigo) {
395396
String original = terceto.getOp1();
396397
String valor = obtenerValor(original);
398+
397399
if (original != null && esCadena(original)) {
400+
// 1. Es Cadena
398401
int pos = registrarCadena(original);
399402
int len = largoCadena.get(original);
400403
codigo.append(Identador.obtenerIndentacion()).append(" i32.const ").append(pos).append("\n");
401404
codigo.append(Identador.obtenerIndentacion()).append(" i32.const ").append(len).append("\n");
402405
codigo.append(Identador.obtenerIndentacion()).append(" call $alert_str\n");
406+
407+
} else if (esFlotante(valor)) {
408+
// 2. NUEVO: Es Flotante (Literal float o Variable f64)
409+
ponerFlotanteEnPila(valor, codigo);
410+
codigo.append(Identador.obtenerIndentacion()).append(" call $alert_float\n");
411+
403412
} else {
413+
// 3. Es Entero (Literal ulong o Variable i32)
404414
ponerEnteroEnPila(valor, codigo);
405415
codigo.append(Identador.obtenerIndentacion()).append(" call $alert_i32\n");
406416
}
417+
407418
terceto.setResultado("");
408419
}
409420
private void procesarTrunc(Terceto terceto, StringBuilder codigo) {
@@ -633,7 +644,7 @@ private String limpiarNombre(String nombre) {
633644
return limpio;
634645
}
635646
private boolean esNumeroULONG(String valor) { return valor != null && valor.trim().matches("\\d+UL"); }
636-
private boolean esNumeroFlotante(String valor) { return valor != null && valor.trim().matches("[+\\-]?\\d+[,.]\\d+[dDeE][+\\-]?\\d+"); }
647+
private boolean esNumeroFlotante(String valor) { return valor != null && valor.trim().matches("[+\\-]?\\d+[,.]\\d+([dDeE][+\\-]?\\d+)?");}
637648
private boolean esCadena(String valor) { return valor != null && valor.startsWith("\"") && valor.endsWith("\""); }
638649
private boolean esFlotante(String valor) {
639650
if (esNumeroFlotante(valor)) return true;

src/Compilador/ModuloGC/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ <h3>Resultados ejecución WASM</h3>
1313
alert("Valor recibido --> " + x);
1414
}
1515

16+
function alert_float(x) {
17+
alert("Valor recibido --> " + x);
18+
}
19+
1620
function alert_str(ptr, len) {
1721
if (!wasmInstance) { console.error('wasmInstance no inicializada'); return; }
1822
const memoryBuffer = wasmInstance.exports.memory.buffer;
@@ -29,7 +33,8 @@ <h3>Resultados ejecución WASM</h3>
2933
const importObject = {
3034
env: {
3135
alert_i32,
32-
alert_str
36+
alert_str,
37+
alert_float
3338
}
3439
};
3540

Lines changed: 21 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -1,246 +1,35 @@
11
(module
22
(import "env" "alert_i32" (func $alert_i32 (param i32)))
3+
(import "env" "alert_float" (func $alert_float (param f64)))
34
(import "env" "alert_str" (func $alert_str (param i32 i32)))
45
(memory (export "memory") 1)
5-
(global $BASEDOS_PROGRAMAMAE (mut i32) (i32.const 0))
6-
(global $_t1_PROGRAMAMAE (mut i32) (i32.const 0))
7-
(global $SALIDA_PROGRAMAMAE_PROCESAR (mut i32) (i32.const 0))
8-
(global $A_PROGRAMAMAE_SUMAR (mut i32) (i32.const 0))
9-
(global $TOPE_PROGRAMAMAE (mut i32) (i32.const 0))
10-
(global $BASEUNA_PROGRAMAMAE (mut i32) (i32.const 0))
11-
(global $_t2_PROGRAMAMAE (mut i32) (i32.const 0))
12-
(global $_t_t1_PROGRAMAMAE_PROGRAMAMAE (mut i32) (i32.const 0))
13-
(global $LIMITE_PROGRAMAMAE_PROCESAR (mut i32) (i32.const 0))
14-
(global $_t_t2_PROGRAMAMAE_PROGRAMAMAE (mut i32) (i32.const 0))
15-
(global $_t_t3_PROGRAMAMAE_PROGRAMAMAE (mut i32) (i32.const 0))
16-
(global $_t_t4_PROGRAMAMAE_PROGRAMAMAE (mut i32) (i32.const 0))
17-
(global $BOLSAGLOBAL_PROGRAMAMAE (mut i32) (i32.const 0))
18-
(global $_t3_PROGRAMAMAE (mut i32) (i32.const 0))
19-
(global $CONTADOR_PROGRAMAMAE_PROCESAR (mut i32) (i32.const 0))
20-
(global $DUMMY_PROGRAMAMAE (mut i32) (i32.const 0))
21-
(global $_t4_PROGRAMAMAE (mut i32) (i32.const 0))
22-
(global $N_PROGRAMAMAE_CUADRADO (mut i32) (i32.const 0))
23-
(global $RESFINAL_PROGRAMAMAE (mut i32) (i32.const 0))
24-
(global $B_PROGRAMAMAE_SUMAR (mut i32) (i32.const 0))
25-
(global $VAL_PROGRAMAMAE_PROCESAR__lambda1 (mut i32) (i32.const 0))
26-
(global $_lock_PROGRAMAMAE_CUADRADO (mut i32) (i32.const 0)) ;; Candado de Recursion
6+
(global $X_TRUNC (mut i32) (i32.const 0))
7+
(global $Y_TRUNC (mut i32) (i32.const 0))
278
(global $_t0 (mut i32) (i32.const 0))
28-
(global $_lock_PROGRAMAMAE_SUMAR (mut i32) (i32.const 0)) ;; Candado de Recursion
299
(global $_t1 (mut i32) (i32.const 0))
30-
(global $_lock_PROGRAMAMAE_PROCESAR (mut i32) (i32.const 0)) ;; Candado de Recursion
31-
(global $_t2 (mut i32) (i32.const 0))
32-
(global $_t3 (mut i32) (i32.const 0))
33-
(global $_t4 (mut i32) (i32.const 0))
34-
(global $_t5 (mut i32) (i32.const 0))
35-
(global $_t6 (mut i32) (i32.const 0))
36-
(global $_t7 (mut i32) (i32.const 0))
37-
(global $_t8 (mut i32) (i32.const 0))
38-
(global $_t_t1_PROGRAMAMAE (mut i32) (i32.const 0))
39-
(global $_t9 (mut i32) (i32.const 0))
40-
(global $_t_t2_PROGRAMAMAE (mut i32) (i32.const 0))
41-
(global $_t10 (mut i32) (i32.const 0))
42-
(global $_t_t3_PROGRAMAMAE (mut i32) (i32.const 0))
43-
(global $_t11 (mut i32) (i32.const 0))
44-
(global $_t_t4_PROGRAMAMAE (mut i32) (i32.const 0))
4510
(data (i32.const 0) "Error: Division por cero.")
4611
(data (i32.const 25) "Error: Resultado de resta negativo (underflow).")
4712
(data (i32.const 72) "Error: Una funci\c3\b3n no puede llamarse a si misma")
13+
(data (i32.const 120) "inicio programa")
4814

4915
(func $main (export "main")
50-
i32.const 0
51-
global.set $BOLSAGLOBAL_PROGRAMAMAE
52-
i32.const 3
53-
global.set $BASEUNA_PROGRAMAMAE
54-
i32.const 4
55-
global.set $BASEDOS_PROGRAMAMAE
56-
i32.const 0
57-
global.set $RESFINAL_PROGRAMAMAE
58-
i32.const 1111
16+
i32.const 120
17+
i32.const 15
18+
call $alert_str
19+
f64.const 1.100000e+00
20+
call $alert_float
21+
i32.const 1
22+
global.set $X_TRUNC
23+
f64.const 1.200000e+00
24+
i32.trunc_f64_u
25+
global.set $_t0
26+
global.get $_t0
27+
global.get $X_TRUNC
28+
i32.add
29+
global.set $_t1
30+
global.get $_t1
31+
global.set $Y_TRUNC
32+
global.get $Y_TRUNC
5933
call $alert_i32
60-
global.get $BASEUNA_PROGRAMAMAE
61-
global.set $N_PROGRAMAMAE_CUADRADO
62-
call $PROGRAMAMAE_CUADRADO
63-
global.set $_t8
64-
global.get $_t8
65-
global.set $_t_t1_PROGRAMAMAE
66-
global.get $_t_t1_PROGRAMAMAE
67-
global.set $A_PROGRAMAMAE_SUMAR
68-
global.get $BASEDOS_PROGRAMAMAE
69-
global.set $N_PROGRAMAMAE_CUADRADO
70-
call $PROGRAMAMAE_CUADRADO
71-
global.set $_t9
72-
global.get $_t9
73-
global.set $_t_t2_PROGRAMAMAE
74-
global.get $_t_t2_PROGRAMAMAE
75-
global.set $B_PROGRAMAMAE_SUMAR
76-
call $PROGRAMAMAE_SUMAR
77-
global.set $_t10
78-
global.get $_t10
79-
global.set $_t_t3_PROGRAMAMAE
80-
global.get $_t_t3_PROGRAMAMAE
81-
global.set $TOPE_PROGRAMAMAE
82-
global.get $TOPE_PROGRAMAMAE
83-
call $alert_i32
84-
global.get $TOPE_PROGRAMAMAE
85-
global.set $LIMITE_PROGRAMAMAE_PROCESAR
86-
call $PROGRAMAMAE_PROCESAR
87-
global.set $_t11
88-
global.get $_t11
89-
global.set $_t_t4_PROGRAMAMAE
90-
global.get $SALIDA_PROGRAMAMAE_PROCESAR
91-
global.set $RESFINAL_PROGRAMAMAE
92-
global.get $_t_t4_PROGRAMAMAE
93-
global.set $DUMMY_PROGRAMAMAE
94-
i32.const 9999
95-
call $alert_i32
96-
global.get $RESFINAL_PROGRAMAMAE
97-
call $alert_i32
98-
)
99-
100-
(func $PROGRAMAMAE_CUADRADO (result i32)
101-
;; Bloqueo de Recursion Directa (Runtime Check)
102-
(block
103-
global.get $_lock_PROGRAMAMAE_CUADRADO
104-
i32.const 1
105-
i32.ne ;; Compara candado != 1
106-
br_if 0 ;; Si candado != 1, saltar (función inactiva, OK)
107-
i32.const 72
108-
i32.const 48
109-
call $alert_str ;; Muestra el error
110-
unreachable ;; Termina el programa
111-
)
112-
i32.const 1
113-
global.set $_lock_PROGRAMAMAE_CUADRADO ;; Set Candado a 1
114-
global.get $N_PROGRAMAMAE_CUADRADO
115-
global.get $N_PROGRAMAMAE_CUADRADO
116-
i32.mul
117-
global.set $_t0
118-
global.get $_t0
119-
;; Liberacion de Candado de Recursion
120-
i32.const 0
121-
global.set $_lock_PROGRAMAMAE_CUADRADO ;; Set Candado a 0
122-
return
123-
)
124-
125-
(func $PROGRAMAMAE_SUMAR (result i32)
126-
;; Bloqueo de Recursion Directa (Runtime Check)
127-
(block
128-
global.get $_lock_PROGRAMAMAE_SUMAR
129-
i32.const 1
130-
i32.ne ;; Compara candado != 1
131-
br_if 0 ;; Si candado != 1, saltar (función inactiva, OK)
132-
i32.const 72
133-
i32.const 48
134-
call $alert_str ;; Muestra el error
135-
unreachable ;; Termina el programa
136-
)
137-
i32.const 1
138-
global.set $_lock_PROGRAMAMAE_SUMAR ;; Set Candado a 1
139-
global.get $A_PROGRAMAMAE_SUMAR
140-
global.get $B_PROGRAMAMAE_SUMAR
141-
i32.add
142-
global.set $_t1
143-
global.get $_t1
144-
;; Liberacion de Candado de Recursion
145-
i32.const 0
146-
global.set $_lock_PROGRAMAMAE_SUMAR ;; Set Candado a 0
147-
return
148-
)
149-
150-
(func $PROGRAMAMAE_PROCESAR (result i32)
151-
;; Bloqueo de Recursion Directa (Runtime Check)
152-
(block
153-
global.get $_lock_PROGRAMAMAE_PROCESAR
154-
i32.const 1
155-
i32.ne ;; Compara candado != 1
156-
br_if 0 ;; Si candado != 1, saltar (función inactiva, OK)
157-
i32.const 72
158-
i32.const 48
159-
call $alert_str ;; Muestra el error
160-
unreachable ;; Termina el programa
161-
)
162-
i32.const 1
163-
global.set $_lock_PROGRAMAMAE_PROCESAR ;; Set Candado a 1
164-
global.get $LIMITE_PROGRAMAMAE_PROCESAR
165-
global.set $CONTADOR_PROGRAMAMAE_PROCESAR
166-
(block $break_W0
167-
(loop $loop_W0
168-
global.get $CONTADOR_PROGRAMAMAE_PROCESAR
169-
i32.const 0
170-
i32.gt_u
171-
global.set $_t2
172-
global.get $_t2
173-
i32.eqz
174-
br_if $break_W0
175-
global.get $CONTADOR_PROGRAMAMAE_PROCESAR
176-
global.set $VAL_PROGRAMAMAE_PROCESAR__lambda1
177-
call $_lambda1_PROGRAMAMAE_PROCESAR
178-
(block
179-
global.get $CONTADOR_PROGRAMAMAE_PROCESAR
180-
i32.const 1
181-
i32.lt_u ;; Verifica si A < B (Underflow)
182-
i32.eqz
183-
br_if 0
184-
i32.const 25
185-
i32.const 47
186-
call $alert_str ;; Muestra el mensaje de error
187-
unreachable ;; Termina el programa
188-
)
189-
global.get $CONTADOR_PROGRAMAMAE_PROCESAR
190-
i32.const 1
191-
i32.sub
192-
global.set $_t7
193-
global.get $_t7
194-
global.set $CONTADOR_PROGRAMAMAE_PROCESAR
195-
br $loop_W0
196-
) ;; fin loop
197-
) ;; fin block while
198-
global.get $BOLSAGLOBAL_PROGRAMAMAE
199-
global.set $SALIDA_PROGRAMAMAE_PROCESAR
200-
i32.const 0
201-
;; Liberacion de Candado de Recursion
202-
i32.const 0
203-
global.set $_lock_PROGRAMAMAE_PROCESAR ;; Set Candado a 0
204-
return
205-
)
206-
207-
(func $_lambda1_PROGRAMAMAE_PROCESAR
208-
(block
209-
(block
210-
i32.const 5
211-
i32.const 0
212-
i32.eq ;; Pila: [B == 0] (0 o 1)
213-
i32.eqz ;;Invertir condicion
214-
br_if 0 ;; Si B == 0, salta al IF
215-
i32.const 0
216-
i32.const 25
217-
call $alert_str ;; Muestra el mensaje
218-
unreachable ;; Termina el programa
219-
)
220-
global.get $VAL_PROGRAMAMAE_PROCESAR__lambda1
221-
i32.const 5
222-
i32.div_u ;; Ejecutar A / B
223-
global.set $_t3
224-
global.get $_t3
225-
i32.const 5
226-
i32.mul
227-
global.set $_t4
228-
global.get $_t4
229-
global.get $VAL_PROGRAMAMAE_PROCESAR__lambda1
230-
i32.eq
231-
global.set $_t5
232-
global.get $_t5
233-
i32.eqz
234-
br_if 0
235-
global.get $BOLSAGLOBAL_PROGRAMAMAE
236-
global.get $VAL_PROGRAMAMAE_PROCESAR__lambda1
237-
i32.add
238-
global.set $_t6
239-
global.get $_t6
240-
global.set $BOLSAGLOBAL_PROGRAMAMAE
241-
global.get $VAL_PROGRAMAMAE_PROCESAR__lambda1
242-
call $alert_i32
243-
br 0
244-
) ;; fin block
24534
)
24635
)

0 commit comments

Comments
 (0)