From e0e84ede1c9a3e17ef533362011a399c272ede39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Mon, 9 Mar 2026 13:33:52 +0100 Subject: [PATCH] Add a noop if no whitespace is emitted to have something to jump to MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- src/emit/mod.rs | 3 +++ .../snapshots/nomo__emit__tests__check_if_else_if.snap | 7 ++++--- src/eval/mod.rs | 7 +++---- tests/cases/3-instructions@if_else_if.snap | 5 +++-- tests/cases/3-instructions@trim_whitespace.snap | 3 ++- tests/cases/4-output@if_else_if.snap | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/emit/mod.rs b/src/emit/mod.rs index 95a92e6..11985e8 100644 --- a/src/emit/mod.rs +++ b/src/emit/mod.rs @@ -45,6 +45,7 @@ pub enum Instruction { Jump { jump: isize, }, + NoOp, } pub fn emit_machine(input: crate::ast::TemplateAst<'_>) -> Vec { @@ -185,6 +186,8 @@ fn emit_ast_expr( eval.push(Instruction::AppendContent { content: ws.source().clone(), }); + } else { + eval.push(Instruction::NoOp); } for index in end_indices { diff --git a/src/emit/snapshots/nomo__emit__tests__check_if_else_if.snap b/src/emit/snapshots/nomo__emit__tests__check_if_else_if.snap index ee52b87..469aa47 100644 --- a/src/emit/snapshots/nomo__emit__tests__check_if_else_if.snap +++ b/src/emit/snapshots/nomo__emit__tests__check_if_else_if.snap @@ -25,7 +25,7 @@ expression: emit content: " " (16..17), }, Jump { - jump: 13, + jump: 14, }, AppendContent { content: " " (12..13), @@ -52,7 +52,7 @@ expression: emit content: " " (38..39), }, Jump { - jump: 6, + jump: 7, }, AppendContent { content: " " (34..35), @@ -67,9 +67,10 @@ expression: emit content: " " (56..57), }, Jump { - jump: 1, + jump: 2, }, AppendContent { content: " " (49..50), }, + NoOp, ] diff --git a/src/eval/mod.rs b/src/eval/mod.rs index b4202f1..10250f1 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -37,6 +37,7 @@ pub fn execute( let instr = instructions.get(ip).unwrap(); match instr { + Instruction::NoOp => (), Instruction::AppendContent { content } => output.push_str(content), Instruction::LoadFromContextToSlot { name, slot } => { let value = global_context @@ -53,12 +54,11 @@ pub fn execute( Instruction::PushScope { inherit_parent: _ } => todo!(), Instruction::Abort => return Err(EvaluationError::ExplicitAbort), Instruction::JumpIfNotTrue { emit_slot, jump } => { - let jump = if *jump == 0 { 1 } else { *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); + let (new_ip, overflow) = ip.overflowing_add_signed(*jump); if overflow { return Err(EvaluationError::InstructionPointerOverflow); @@ -69,8 +69,7 @@ pub fn execute( } } 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 { return Err(EvaluationError::InstructionPointerOverflow); diff --git a/tests/cases/3-instructions@if_else_if.snap b/tests/cases/3-instructions@if_else_if.snap index d19a4ed..c9ab17d 100644 --- a/tests/cases/3-instructions@if_else_if.snap +++ b/tests/cases/3-instructions@if_else_if.snap @@ -26,7 +26,7 @@ input_file: tests/cases/if_else_if.nomo content: "\n" (37..38), }, Jump { - jump: 8, + jump: 9, }, AppendContent { content: "\n " (13..18), @@ -53,9 +53,10 @@ input_file: tests/cases/if_else_if.nomo content: "\n" (81..82), }, Jump { - jump: 1, + jump: 2, }, AppendContent { content: "\n " (64..69), }, + NoOp, ] diff --git a/tests/cases/3-instructions@trim_whitespace.snap b/tests/cases/3-instructions@trim_whitespace.snap index 09c074c..754e5e9 100644 --- a/tests/cases/3-instructions@trim_whitespace.snap +++ b/tests/cases/3-instructions@trim_whitespace.snap @@ -34,6 +34,7 @@ input_file: tests/cases/trim_whitespace.nomo }, }, Jump { - jump: 0, + jump: 1, }, + NoOp, ] diff --git a/tests/cases/4-output@if_else_if.snap b/tests/cases/4-output@if_else_if.snap index efb547b..8908bd0 100644 --- a/tests/cases/4-output@if_else_if.snap +++ b/tests/cases/4-output@if_else_if.snap @@ -3,4 +3,4 @@ source: tests/file_tests.rs expression: output input_file: tests/cases/if_else_if.nomo --- -"\n \n Hello World!\n\n " +"\n \n Hello World!\n"