feat(charka): EVALUATE — el case de COBOL

EVALUATE atraviesa el pipeline entero — antes el parser lo guardaba
crudo como Stmt::Unknown.

- IR: Stmt::Evaluate { subject, whens, other } con
  WhenBranch { values, body }. Varios WHEN apilados comparten cuerpo;
  WHEN OTHER es el caso por defecto.
- Parser: EVALUATE subject WHEN v1 WHEN v2 ... [WHEN OTHER ...]
  END-EVALUATE.
- Codegen: lo baja a una cadena if / else if / else — una rama se
  elige si el sujeto es igual a alguno de sus valores, sin caída.
- Shadow: el intérprete evalúa el sujeto y ejecuta la primera rama
  cuyos valores casen, o el WHEN OTHER.
- Corpus: programa nuevo 09-evaluar (EVALUATE por valor anidado en un
  PERFORM VARYING, con WHEN apilados y WHEN OTHER). Verificado: el
  intérprete sombra y el crate compilado por scaffold dan la misma
  salida.

Alcance v1: EVALUATE por igualdad de valor; no la forma EVALUATE TRUE
con condiciones ni los rangos THRU.

Tests: charka-ir 19, charka-codegen 16, charka-shadow 14. fmt +
clippy limpios.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
sergio
2026-05-21 21:37:28 +00:00
parent 321e6f8e27
commit 4df7478b71
13 changed files with 259 additions and 8 deletions
+36 -1
View File
@@ -5,7 +5,7 @@
use charka_parser::TokenKind;
use crate::ast::{Operand, Perform, PerformControl, PerformTarget, Stmt};
use crate::ast::{Operand, Perform, PerformControl, PerformTarget, Stmt, WhenBranch};
use crate::cursor::{parse_operand, Cursor};
use crate::expr::{parse_cond, parse_expr};
use crate::kw::{is_boundary, is_terminator, is_verb};
@@ -38,6 +38,7 @@ fn parse_one_stmt(c: &mut Cursor, stops: &[&str]) -> Stmt {
"MULTIPLY" => parse_multiply(c),
"DIVIDE" => parse_divide(c),
"IF" => parse_if(c),
"EVALUATE" => parse_evaluate(c),
"PERFORM" => parse_perform(c),
"GO" => parse_goto(c),
"STOP" => parse_stop(c),
@@ -285,6 +286,40 @@ fn parse_if(c: &mut Cursor) -> Stmt {
}
}
fn parse_evaluate(c: &mut Cursor) -> Stmt {
c.bump(); // EVALUATE
let subject = parse_operand(c);
let mut whens = Vec::new();
let mut other = Vec::new();
while !c.done() && !c.at_word("END-EVALUATE") {
if !c.at_word("WHEN") {
break; // algo inesperado dentro del EVALUATE: se corta
}
// Varios `WHEN` apilados comparten el mismo cuerpo.
let mut values = Vec::new();
let mut is_other = false;
while c.eat_word("WHEN") {
if c.eat_word("OTHER") {
is_other = true;
} else {
values.push(parse_operand(c));
}
}
let body = parse_statements(c, &["WHEN", "END-EVALUATE"]);
if is_other {
other = body;
} else {
whens.push(WhenBranch { values, body });
}
}
c.eat_word("END-EVALUATE");
Stmt::Evaluate {
subject,
whens,
other,
}
}
fn parse_perform(c: &mut Cursor) -> Stmt {
c.bump(); // PERFORM