feat(charka): PERFORM VARYING — el bucle con variable de control
El bucle más usado de COBOL, que antes el parser degradaba a un
PERFORM vacío (un hueco de corrección real). Ahora atraviesa el
pipeline entero como una rebanada vertical.
- IR: PerformControl::Varying { var, from, by, until }.
- Parser: reconoce PERFORM VARYING var FROM x BY y UNTIL cond en
línea (END-PERFORM) y fuera de línea (PERFORM párrafo VARYING ...).
- Codegen: emite var = from; while !(until) { cuerpo; var += by; }.
- Shadow: el intérprete inicializa la variable, evalúa la condición
antes de cada vuelta e incrementa al final.
- Corpus: programa nuevo 08-varying (suma 1..10). Verificado: el
intérprete sombra y el crate compilado por scaffold dan ambos
SUMA 1 A 10 = 00055 — las dos rutas concuerdan.
Tests: charka-ir 18, charka-codegen 15, charka-shadow 13. fmt +
clippy limpios.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -89,8 +89,10 @@ Tercera etapa: `Program` → `Ir`. Aquí se parsea cada `Sentence` cruda
|
|||||||
empieza el verbo del siguiente. El parser usa palabras "frontera"
|
empieza el verbo del siguiente. El parser usa palabras "frontera"
|
||||||
(verbos + terminadores `END-*`/`ELSE` + conectores `TO`/`GIVING`...)
|
(verbos + terminadores `END-*`/`ELSE` + conectores `TO`/`GIVING`...)
|
||||||
para delimitar listas de operandos.
|
para delimitar listas de operandos.
|
||||||
|
- `PERFORM` cubre las cuatro formas: párrafo / en línea, `n TIMES`,
|
||||||
|
`UNTIL cond` y `VARYING var FROM x BY y UNTIL cond`.
|
||||||
- Fuera de alcance v1: `EVALUATE`, `STRING`/`UNSTRING`, E/S de
|
- Fuera de alcance v1: `EVALUATE`, `STRING`/`UNSTRING`, E/S de
|
||||||
ficheros, `PERFORM VARYING`, CICS, SQL embebido.
|
ficheros, CICS, SQL embebido.
|
||||||
|
|
||||||
## charka-runtime
|
## charka-runtime
|
||||||
|
|
||||||
|
|||||||
@@ -334,6 +334,22 @@ mod tests {
|
|||||||
assert!(out.contains("for _ in 0..3usize {"));
|
assert!(out.contains("for _ in 0..3usize {"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn perform_varying_emits_init_loop_and_increment() {
|
||||||
|
let out = gen("DATA DIVISION.\n\
|
||||||
|
WORKING-STORAGE SECTION.\n\
|
||||||
|
01 WS-I PIC 9(2).\n\
|
||||||
|
01 WS-N PIC 9(3).\n\
|
||||||
|
PROCEDURE DIVISION.\n\
|
||||||
|
MAIN.\n\
|
||||||
|
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5\n\
|
||||||
|
ADD 1 TO WS-N\n\
|
||||||
|
END-PERFORM.\n");
|
||||||
|
assert!(out.contains("self.ws_i.store(dec(\"1\"));"));
|
||||||
|
assert!(out.contains("while !((self.ws_i.value()) > (dec(\"5\"))) {"));
|
||||||
|
assert!(out.contains("self.ws_i.store(self.ws_i.value().add(&(dec(\"1\"))));"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_program_still_compiles_shape() {
|
fn empty_program_still_compiles_shape() {
|
||||||
let out = gen("");
|
let out = gen("");
|
||||||
|
|||||||
@@ -360,5 +360,20 @@ fn emit_perform(em: &mut Emitter, sym: &Symbols, p: &Perform) {
|
|||||||
em.dedent();
|
em.dedent();
|
||||||
em.line("}");
|
em.line("}");
|
||||||
}
|
}
|
||||||
|
PerformControl::Varying {
|
||||||
|
var,
|
||||||
|
from,
|
||||||
|
by,
|
||||||
|
until,
|
||||||
|
} => {
|
||||||
|
// var = from; mientras no se cumpla `until`: cuerpo; var += by.
|
||||||
|
emit_store(em, sym, var, &operand_decimal(sym, from), false);
|
||||||
|
em.line(&format!("while !({}) {{", emit_cond(sym, until)));
|
||||||
|
em.indent();
|
||||||
|
emit_body(em, sym);
|
||||||
|
emit_inplace(em, sym, var, "add", &operand_decimal(sym, by), false);
|
||||||
|
em.dedent();
|
||||||
|
em.line("}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -197,4 +197,13 @@ pub enum PerformControl {
|
|||||||
Times(Operand),
|
Times(Operand),
|
||||||
/// `UNTIL cond`.
|
/// `UNTIL cond`.
|
||||||
Until(Cond),
|
Until(Cond),
|
||||||
|
/// `VARYING var FROM from BY by UNTIL until` — el bucle con
|
||||||
|
/// variable de control: `var = from`; mientras no se cumpla
|
||||||
|
/// `until`, ejecuta el cuerpo y hace `var = var + by`.
|
||||||
|
Varying {
|
||||||
|
var: String,
|
||||||
|
from: Operand,
|
||||||
|
by: Operand,
|
||||||
|
until: Cond,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,9 @@
|
|||||||
//! `ACCEPT`, `COMPUTE` (con expresiones con precedencia), `ADD`,
|
//! `ACCEPT`, `COMPUTE` (con expresiones con precedencia), `ADD`,
|
||||||
//! `SUBTRACT`, `MULTIPLY`, `DIVIDE`, `IF`/`ELSE`/`END-IF` (con
|
//! `SUBTRACT`, `MULTIPLY`, `DIVIDE`, `IF`/`ELSE`/`END-IF` (con
|
||||||
//! condiciones `AND`/`OR`/`NOT`), `PERFORM` (fuera de línea, en línea,
|
//! condiciones `AND`/`OR`/`NOT`), `PERFORM` (fuera de línea, en línea,
|
||||||
//! `TIMES`, `UNTIL`), `GO TO`, `STOP RUN`, `GOBACK`, `EXIT`,
|
//! `TIMES`, `UNTIL`, `VARYING`), `GO TO`, `STOP RUN`, `GOBACK`,
|
||||||
//! `CONTINUE`. Fuera de alcance: `EVALUATE`, `STRING`/`UNSTRING`, E/S
|
//! `EXIT`, `CONTINUE`. Fuera de alcance: `EVALUATE`,
|
||||||
//! de ficheros, `PERFORM VARYING`, CICS y SQL embebido.
|
//! `STRING`/`UNSTRING`, E/S de ficheros, CICS y SQL embebido.
|
||||||
|
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
@@ -286,6 +286,31 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn perform_varying_inline() {
|
||||||
|
let b = body(
|
||||||
|
"PERFORM VARYING WS-I FROM 1 BY 2 UNTIL WS-I > 9 \
|
||||||
|
CONTINUE END-PERFORM.",
|
||||||
|
);
|
||||||
|
match &b[0] {
|
||||||
|
Stmt::Perform(p) => match &p.control {
|
||||||
|
PerformControl::Varying {
|
||||||
|
var,
|
||||||
|
from,
|
||||||
|
by,
|
||||||
|
until,
|
||||||
|
} => {
|
||||||
|
assert_eq!(var, "WS-I");
|
||||||
|
assert_eq!(from, &Operand::Num("1".into()));
|
||||||
|
assert_eq!(by, &Operand::Num("2".into()));
|
||||||
|
assert!(matches!(until, Cond::Compare { .. }));
|
||||||
|
}
|
||||||
|
other => panic!("se esperaba Varying, vino {other:?}"),
|
||||||
|
},
|
||||||
|
other => panic!("se esperaba PERFORM, vino {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn several_statements_in_one_sentence() {
|
fn several_statements_in_one_sentence() {
|
||||||
let b = body("MOVE 1 TO X DISPLAY X STOP RUN.");
|
let b = body("MOVE 1 TO X DISPLAY X STOP RUN.");
|
||||||
|
|||||||
@@ -288,6 +288,14 @@ fn parse_if(c: &mut Cursor) -> Stmt {
|
|||||||
fn parse_perform(c: &mut Cursor) -> Stmt {
|
fn parse_perform(c: &mut Cursor) -> Stmt {
|
||||||
c.bump(); // PERFORM
|
c.bump(); // PERFORM
|
||||||
|
|
||||||
|
// `PERFORM VARYING ... ... END-PERFORM` — cuerpo en línea.
|
||||||
|
if c.eat_word("VARYING") {
|
||||||
|
let control = parse_varying(c);
|
||||||
|
let body = parse_statements(c, &["END-PERFORM"]);
|
||||||
|
c.eat_word("END-PERFORM");
|
||||||
|
return inline_perform(body, control);
|
||||||
|
}
|
||||||
|
|
||||||
// `PERFORM UNTIL cond ... END-PERFORM` — cuerpo en línea.
|
// `PERFORM UNTIL cond ... END-PERFORM` — cuerpo en línea.
|
||||||
if c.eat_word("UNTIL") {
|
if c.eat_word("UNTIL") {
|
||||||
let cond = parse_cond(c);
|
let cond = parse_cond(c);
|
||||||
@@ -312,9 +320,9 @@ fn parse_perform(c: &mut Cursor) -> Stmt {
|
|||||||
return inline_perform(body, PerformControl::Once);
|
return inline_perform(body, PerformControl::Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
// `PERFORM PARA [THRU PARA2] [n TIMES | UNTIL cond]` — fuera de línea.
|
// `PERFORM PARA [THRU PARA2] [VARYING ... | n TIMES | UNTIL cond]`.
|
||||||
let Some(name) = parse_one_name(c) else {
|
let Some(name) = parse_one_name(c) else {
|
||||||
// Forma no soportada (p. ej. `PERFORM VARYING`): perform vacío.
|
// Forma no reconocida tras `PERFORM`: perform vacío.
|
||||||
return inline_perform(Vec::new(), PerformControl::Once);
|
return inline_perform(Vec::new(), PerformControl::Once);
|
||||||
};
|
};
|
||||||
let thru = if c.eat_word("THRU") || c.eat_word("THROUGH") {
|
let thru = if c.eat_word("THRU") || c.eat_word("THROUGH") {
|
||||||
@@ -322,7 +330,9 @@ fn parse_perform(c: &mut Cursor) -> Stmt {
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
let control = if c.eat_word("UNTIL") {
|
let control = if c.eat_word("VARYING") {
|
||||||
|
parse_varying(c)
|
||||||
|
} else if c.eat_word("UNTIL") {
|
||||||
PerformControl::Until(parse_cond(c))
|
PerformControl::Until(parse_cond(c))
|
||||||
} else if at_count(c) {
|
} else if at_count(c) {
|
||||||
let n = parse_operand(c);
|
let n = parse_operand(c);
|
||||||
@@ -345,6 +355,24 @@ fn inline_perform(body: Vec<Stmt>, control: PerformControl) -> Stmt {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parsea la cláusula `VARYING var FROM x BY y UNTIL cond`, ya
|
||||||
|
/// consumida la palabra `VARYING`.
|
||||||
|
fn parse_varying(c: &mut Cursor) -> PerformControl {
|
||||||
|
let var = parse_one_name(c).unwrap_or_default();
|
||||||
|
c.eat_word("FROM");
|
||||||
|
let from = parse_operand(c);
|
||||||
|
c.eat_word("BY");
|
||||||
|
let by = parse_operand(c);
|
||||||
|
c.eat_word("UNTIL");
|
||||||
|
let until = parse_cond(c);
|
||||||
|
PerformControl::Varying {
|
||||||
|
var,
|
||||||
|
from,
|
||||||
|
by,
|
||||||
|
until,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// ¿El cursor está sobre `<operando> TIMES`?
|
/// ¿El cursor está sobre `<operando> TIMES`?
|
||||||
fn at_count(c: &Cursor) -> bool {
|
fn at_count(c: &Cursor) -> bool {
|
||||||
match c.peek().map(|t| t.kind) {
|
match c.peek().map(|t| t.kind) {
|
||||||
|
|||||||
@@ -265,6 +265,28 @@ impl<'a> Machine<'a> {
|
|||||||
return Flow::Stop;
|
return Flow::Stop;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
PerformControl::Varying {
|
||||||
|
var,
|
||||||
|
from,
|
||||||
|
by,
|
||||||
|
until,
|
||||||
|
} => {
|
||||||
|
let start = self.eval_decimal(from);
|
||||||
|
self.store(var, start, false);
|
||||||
|
loop {
|
||||||
|
if self.tick() {
|
||||||
|
return Flow::Stop;
|
||||||
|
}
|
||||||
|
if self.eval_cond(until) {
|
||||||
|
return Flow::Normal;
|
||||||
|
}
|
||||||
|
if let Flow::Stop = self.run_target(&p.target) {
|
||||||
|
return Flow::Stop;
|
||||||
|
}
|
||||||
|
let next = self.field_value(var).add(&self.eval_decimal(by));
|
||||||
|
self.store(var, next, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ mod tests {
|
|||||||
corpus_test!(corpus_05_factorial, "05-factorial");
|
corpus_test!(corpus_05_factorial, "05-factorial");
|
||||||
corpus_test!(corpus_06_nomina, "06-nomina");
|
corpus_test!(corpus_06_nomina, "06-nomina");
|
||||||
corpus_test!(corpus_07_clasificar, "07-clasificar");
|
corpus_test!(corpus_07_clasificar, "07-clasificar");
|
||||||
|
corpus_test!(corpus_08_varying, "08-varying");
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_source_runs_clean() {
|
fn empty_source_runs_clean() {
|
||||||
@@ -146,6 +147,27 @@ mod tests {
|
|||||||
assert_eq!(outcome.halt, Halt::StepLimit);
|
assert_eq!(outcome.halt, Halt::StepLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn perform_varying_out_of_line() {
|
||||||
|
// `PERFORM CONTAR VARYING ...` — el párrafo es el cuerpo del bucle.
|
||||||
|
// WS-I = 1, 3, 5, 7, 9 (FROM 1 BY 2 UNTIL > 9) → 5 iteraciones.
|
||||||
|
let outcome = run_source(
|
||||||
|
"DATA DIVISION.\n\
|
||||||
|
WORKING-STORAGE SECTION.\n\
|
||||||
|
01 WS-I PIC 9(2) VALUE 0.\n\
|
||||||
|
01 WS-N PIC 9(3) VALUE 0.\n\
|
||||||
|
PROCEDURE DIVISION.\n\
|
||||||
|
MAIN.\n\
|
||||||
|
PERFORM CONTAR VARYING WS-I FROM 1 BY 2 UNTIL WS-I > 9.\n\
|
||||||
|
DISPLAY WS-N.\n\
|
||||||
|
STOP RUN.\n\
|
||||||
|
CONTAR.\n\
|
||||||
|
ADD 1 TO WS-N.\n",
|
||||||
|
)
|
||||||
|
.expect("pipeline OK");
|
||||||
|
assert_eq!(outcome.lines, vec!["005".to_string()]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lex_error_surfaces() {
|
fn lex_error_surfaces() {
|
||||||
let err = run_source("PROCEDURE DIVISION.\nMAIN.\n DISPLAY 'sin cerrar.\n").unwrap_err();
|
let err = run_source("PROCEDURE DIVISION.\nMAIN.\n DISPLAY 'sin cerrar.\n").unwrap_err();
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
* corpus charka — nivel 4: PERFORM VARYING, el bucle clásico de COBOL
|
||||||
|
IDENTIFICATION DIVISION.
|
||||||
|
PROGRAM-ID. SUMAR.
|
||||||
|
DATA DIVISION.
|
||||||
|
WORKING-STORAGE SECTION.
|
||||||
|
01 WS-I PIC 9(2) VALUE 0.
|
||||||
|
01 WS-SUMA PIC 9(5) VALUE 0.
|
||||||
|
PROCEDURE DIVISION.
|
||||||
|
MAIN.
|
||||||
|
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10
|
||||||
|
ADD WS-I TO WS-SUMA
|
||||||
|
END-PERFORM.
|
||||||
|
DISPLAY 'SUMA 1 A 10 = ' WS-SUMA.
|
||||||
|
STOP RUN.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
SUMA 1 A 10 = 00055
|
||||||
@@ -16,6 +16,7 @@ salida correcta, una línea por `DISPLAY`.
|
|||||||
| `05-factorial` | 4 | `PERFORM UNTIL` en línea, `MULTIPLY` in situ |
|
| `05-factorial` | 4 | `PERFORM UNTIL` en línea, `MULTIPLY` in situ |
|
||||||
| `06-nomina` | 5 | grupos, `COMPUTE` con paréntesis, `ROUNDED`, V99 |
|
| `06-nomina` | 5 | grupos, `COMPUTE` con paréntesis, `ROUNDED`, V99 |
|
||||||
| `07-clasificar` | 5 | `IF` anidado, condiciones con `AND` |
|
| `07-clasificar` | 5 | `IF` anidado, condiciones con `AND` |
|
||||||
|
| `08-varying` | 4 | `PERFORM VARYING` — el bucle con variable de control|
|
||||||
|
|
||||||
## Formato
|
## Formato
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,21 @@
|
|||||||
Transpilador COBOL → Rust. El módulo más grande del ecosistema (Fase D
|
Transpilador COBOL → Rust. El módulo más grande del ecosistema (Fase D
|
||||||
del plan macro) — el parser COBOL completo es un esfuerzo multi-mes.
|
del plan macro) — el parser COBOL completo es un esfuerzo multi-mes.
|
||||||
|
|
||||||
|
### feat(charka): PERFORM VARYING — el bucle con variable de control
|
||||||
|
|
||||||
|
El bucle más usado de COBOL, que antes el parser degradaba a un
|
||||||
|
`PERFORM` vacío. Ahora atraviesa el pipeline entero.
|
||||||
|
|
||||||
|
- IR: `PerformControl::Varying { var, from, by, until }`.
|
||||||
|
- Parser: reconoce `PERFORM VARYING var FROM x BY y UNTIL cond` en
|
||||||
|
línea (`END-PERFORM`) y fuera de línea (`PERFORM párrafo VARYING …`).
|
||||||
|
- Codegen: emite `var = from; while !(until) { cuerpo; var += by; }`.
|
||||||
|
- Shadow: el intérprete inicializa la variable, evalúa la condición
|
||||||
|
antes de cada vuelta e incrementa al final.
|
||||||
|
- Corpus: programa nuevo `08-varying` (suma 1..10 con `PERFORM
|
||||||
|
VARYING`). Verificado: el intérprete sombra y el crate compilado por
|
||||||
|
`scaffold` dan ambos `SUMA 1 A 10 = 00055`.
|
||||||
|
|
||||||
### feat(charka): CLI del transpilador — transpile / scaffold / run / check
|
### feat(charka): CLI del transpilador — transpile / scaffold / run / check
|
||||||
|
|
||||||
App nueva `crates/apps/charka` — el binario `charka`, que vuelve usable
|
App nueva `crates/apps/charka` — el binario `charka`, que vuelve usable
|
||||||
|
|||||||
Reference in New Issue
Block a user