Add parsing for conditionals (cont.)

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-08 15:06:29 +01:00
parent 974086a877
commit 8afc2d1bde
29 changed files with 994 additions and 746 deletions

View file

@ -13,6 +13,11 @@ pub enum EvaluationError {
UnknownVariable(NomoInput),
/// An explicit abort was requested
ExplicitAbort,
/** The instruction pointer overflowed
**
** This is an internal error and is a bug that should be reported
*/
InstructionPointerOverflow,
}
pub fn execute(
@ -23,7 +28,14 @@ pub fn execute(
let mut scopes: HashMap<crate::emit::VariableSlot, serde_json::Value> = HashMap::new();
for instr in instructions {
let mut ip = 0;
loop {
if ip >= instructions.len() {
break;
}
let instr = instructions.get(ip).unwrap();
match instr {
Instruction::AppendContent { content } => output.push_str(content),
Instruction::LoadFromContextToSlot { name, slot } => {
@ -40,7 +52,24 @@ pub fn execute(
}
Instruction::PushScope { inherit_parent: _ } => todo!(),
Instruction::Abort => return Err(EvaluationError::ExplicitAbort),
Instruction::JumpIfNotTrue { emit_slot, jump } => {
let dont_jump = scopes.get(emit_slot).unwrap().as_bool().unwrap();
if dont_jump {
// We are done
} else {
let (new_ip, overflow) = ip.overflowing_add_signed(*jump);
if overflow {
return Err(EvaluationError::InstructionPointerOverflow);
} else {
ip = new_ip;
continue;
}
}
}
}
ip += 1;
}
Ok(output)