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:
@@ -197,4 +197,13 @@ pub enum PerformControl {
|
||||
Times(Operand),
|
||||
/// `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`,
|
||||
//! `SUBTRACT`, `MULTIPLY`, `DIVIDE`, `IF`/`ELSE`/`END-IF` (con
|
||||
//! condiciones `AND`/`OR`/`NOT`), `PERFORM` (fuera de línea, en línea,
|
||||
//! `TIMES`, `UNTIL`), `GO TO`, `STOP RUN`, `GOBACK`, `EXIT`,
|
||||
//! `CONTINUE`. Fuera de alcance: `EVALUATE`, `STRING`/`UNSTRING`, E/S
|
||||
//! de ficheros, `PERFORM VARYING`, CICS y SQL embebido.
|
||||
//! `TIMES`, `UNTIL`, `VARYING`), `GO TO`, `STOP RUN`, `GOBACK`,
|
||||
//! `EXIT`, `CONTINUE`. Fuera de alcance: `EVALUATE`,
|
||||
//! `STRING`/`UNSTRING`, E/S de ficheros, CICS y SQL embebido.
|
||||
|
||||
#![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]
|
||||
fn several_statements_in_one_sentence() {
|
||||
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 {
|
||||
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.
|
||||
if c.eat_word("UNTIL") {
|
||||
let cond = parse_cond(c);
|
||||
@@ -312,9 +320,9 @@ fn parse_perform(c: &mut Cursor) -> Stmt {
|
||||
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 {
|
||||
// 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);
|
||||
};
|
||||
let thru = if c.eat_word("THRU") || c.eat_word("THROUGH") {
|
||||
@@ -322,7 +330,9 @@ fn parse_perform(c: &mut Cursor) -> Stmt {
|
||||
} else {
|
||||
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))
|
||||
} else if at_count(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`?
|
||||
fn at_count(c: &Cursor) -> bool {
|
||||
match c.peek().map(|t| t.kind) {
|
||||
|
||||
Reference in New Issue
Block a user