Add a noop if no whitespace is emitted to have something to jump to

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-09 13:33:52 +01:00
parent 59f92e31fe
commit e0e84ede1c
6 changed files with 16 additions and 11 deletions

View file

@ -45,6 +45,7 @@ pub enum Instruction {
Jump { Jump {
jump: isize, jump: isize,
}, },
NoOp,
} }
pub fn emit_machine(input: crate::ast::TemplateAst<'_>) -> Vec<Instruction> { pub fn emit_machine(input: crate::ast::TemplateAst<'_>) -> Vec<Instruction> {
@ -185,6 +186,8 @@ fn emit_ast_expr(
eval.push(Instruction::AppendContent { eval.push(Instruction::AppendContent {
content: ws.source().clone(), content: ws.source().clone(),
}); });
} else {
eval.push(Instruction::NoOp);
} }
for index in end_indices { for index in end_indices {

View file

@ -25,7 +25,7 @@ expression: emit
content: " " (16..17), content: " " (16..17),
}, },
Jump { Jump {
jump: 13, jump: 14,
}, },
AppendContent { AppendContent {
content: " " (12..13), content: " " (12..13),
@ -52,7 +52,7 @@ expression: emit
content: " " (38..39), content: " " (38..39),
}, },
Jump { Jump {
jump: 6, jump: 7,
}, },
AppendContent { AppendContent {
content: " " (34..35), content: " " (34..35),
@ -67,9 +67,10 @@ expression: emit
content: " " (56..57), content: " " (56..57),
}, },
Jump { Jump {
jump: 1, jump: 2,
}, },
AppendContent { AppendContent {
content: " " (49..50), content: " " (49..50),
}, },
NoOp,
] ]

View file

@ -37,6 +37,7 @@ pub fn execute(
let instr = instructions.get(ip).unwrap(); let instr = instructions.get(ip).unwrap();
match instr { match instr {
Instruction::NoOp => (),
Instruction::AppendContent { content } => output.push_str(content), Instruction::AppendContent { content } => output.push_str(content),
Instruction::LoadFromContextToSlot { name, slot } => { Instruction::LoadFromContextToSlot { name, slot } => {
let value = global_context let value = global_context
@ -53,12 +54,11 @@ pub fn execute(
Instruction::PushScope { inherit_parent: _ } => todo!(), Instruction::PushScope { inherit_parent: _ } => todo!(),
Instruction::Abort => return Err(EvaluationError::ExplicitAbort), Instruction::Abort => return Err(EvaluationError::ExplicitAbort),
Instruction::JumpIfNotTrue { emit_slot, jump } => { Instruction::JumpIfNotTrue { emit_slot, jump } => {
let jump = if *jump == 0 { 1 } else { *jump };
let dont_jump = scopes.get(emit_slot).unwrap().as_bool().unwrap(); let dont_jump = scopes.get(emit_slot).unwrap().as_bool().unwrap();
if dont_jump { if dont_jump {
// We are done // We are done
} else { } else {
let (new_ip, overflow) = ip.overflowing_add_signed(jump); let (new_ip, overflow) = ip.overflowing_add_signed(*jump);
if overflow { if overflow {
return Err(EvaluationError::InstructionPointerOverflow); return Err(EvaluationError::InstructionPointerOverflow);
@ -69,8 +69,7 @@ pub fn execute(
} }
} }
Instruction::Jump { jump } => { Instruction::Jump { jump } => {
let jump = if *jump == 0 { 1 } else { *jump }; let (new_ip, overflow) = ip.overflowing_add_signed(*jump);
let (new_ip, overflow) = ip.overflowing_add_signed(jump);
if overflow { if overflow {
return Err(EvaluationError::InstructionPointerOverflow); return Err(EvaluationError::InstructionPointerOverflow);

View file

@ -26,7 +26,7 @@ input_file: tests/cases/if_else_if.nomo
content: "\n" (37..38), content: "\n" (37..38),
}, },
Jump { Jump {
jump: 8, jump: 9,
}, },
AppendContent { AppendContent {
content: "\n " (13..18), content: "\n " (13..18),
@ -53,9 +53,10 @@ input_file: tests/cases/if_else_if.nomo
content: "\n" (81..82), content: "\n" (81..82),
}, },
Jump { Jump {
jump: 1, jump: 2,
}, },
AppendContent { AppendContent {
content: "\n " (64..69), content: "\n " (64..69),
}, },
NoOp,
] ]

View file

@ -34,6 +34,7 @@ input_file: tests/cases/trim_whitespace.nomo
}, },
}, },
Jump { Jump {
jump: 0, jump: 1,
}, },
NoOp,
] ]

View file

@ -3,4 +3,4 @@ source: tests/file_tests.rs
expression: output expression: output
input_file: tests/cases/if_else_if.nomo input_file: tests/cases/if_else_if.nomo
--- ---
"\n \n Hello World!\n\n " "\n \n Hello World!\n"