Add for loop

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-11 18:09:58 +01:00
parent 7182024342
commit 42e0056374
16 changed files with 775 additions and 44 deletions

View file

@ -0,0 +1,41 @@
---
source: tests/file_tests.rs
expression: parsed
info:
context:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
ParsedTemplate {
tokens: [
[LeftDelim]"{{" (0..2),
[Whitespace]" " (2..3),
[For]"for" (3..6),
[Whitespace]" " (6..7),
[Ident]"value" (7..12),
[Whitespace]" " (12..13),
[In]"in" (13..15),
[Whitespace]" " (15..16),
[Ident]"values" (16..22),
[Whitespace]" " (22..23),
[TrimWhitespace]"-" (23..24),
[RightDelim]"}}" (24..26),
[Whitespace]"\n " (26..31),
[LeftDelim]"{{" (31..33),
[TrimWhitespace]"-" (33..34),
[WantsOutput]"=" (34..35),
[Whitespace]" " (35..36),
[Ident]"value" (36..41),
[Whitespace]" " (41..42),
[RightDelim]"}}" (42..44),
[Whitespace]"\n" (44..45),
[LeftDelim]"{{" (45..47),
[TrimWhitespace]"-" (47..48),
[Whitespace]" " (48..49),
[End]"end" (49..52),
[Whitespace]" " (52..53),
[RightDelim]"}}" (53..55),
],
}

View file

@ -0,0 +1,44 @@
---
source: tests/file_tests.rs
expression: ast
info:
context:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
TemplateAst {
root: [
ForChain {
for_block: Block {
prev_whitespace_content: None,
expression: For {
value_ident: [Ident]"value" (7..12),
value_expression: VariableAccess(
[Ident]"values" (16..22),
),
},
post_whitespace_content: None,
},
content: [
Interpolation {
prev_whitespace_content: None,
expression: VariableAccess(
[Ident]"value" (36..41),
),
post_whitespace_content: Some(
[Whitespace]"\n" (44..45),
),
},
],
else_block: None,
else_content: None,
end_block: Block {
prev_whitespace_content: None,
expression: EndBlock,
post_whitespace_content: None,
},
},
],
}

View file

@ -6,10 +6,10 @@ input_file: tests/cases/condition.nomo
VMInstructions {
labels: {
LabelSlot {
index: 2,
index: 0,
}: 7,
LabelSlot {
index: 0,
index: 2,
}: 7,
},
instructions: [

View file

@ -5,15 +5,15 @@ input_file: tests/cases/if_else_if.nomo
---
VMInstructions {
labels: {
LabelSlot {
index: 0,
}: 14,
LabelSlot {
index: 2,
}: 7,
LabelSlot {
index: 4,
}: 14,
LabelSlot {
index: 0,
}: 14,
},
instructions: [
LoadFromContextToSlot {

View file

@ -0,0 +1,87 @@
---
source: tests/file_tests.rs
expression: emit
info:
context:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
VMInstructions {
labels: {
LabelSlot {
index: 0,
}: 10,
LabelSlot {
index: 1,
}: 10,
LabelSlot {
index: 2,
}: 4,
},
instructions: [
PushScope {
inherit_parent: true,
},
LoadFromContextToSlot {
name: "values" (16..22),
slot: VariableSlot {
index: 4,
},
},
CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot {
index: 5,
},
iterator_source_slot: VariableSlot {
index: 4,
},
},
GetIteratorEmptyOrJump {
iterator_slot: VariableSlot {
index: 5,
},
jump: LabelSlot {
index: 0,
},
},
AdvanceIteratorOrJump {
iterator_slot: VariableSlot {
index: 5,
},
value_slot: VariableSlot {
index: 3,
},
jump: LabelSlot {
index: 1,
},
},
LoadFromSlotToContext {
value_ident: "value" (7..12),
value_slot: VariableSlot {
index: 3,
},
},
LoadFromContextToSlot {
name: "value" (36..41),
slot: VariableSlot {
index: 6,
},
},
EmitFromSlot {
slot: VariableSlot {
index: 6,
},
},
AppendContent {
content: "\n" (44..45),
},
Jump {
jump: LabelSlot {
index: 2,
},
},
PopScope,
],
}

View file

@ -6,10 +6,10 @@ input_file: tests/cases/trim_whitespace.nomo
VMInstructions {
labels: {
LabelSlot {
index: 2,
index: 0,
}: 7,
LabelSlot {
index: 0,
index: 2,
}: 7,
},
instructions: [

View file

@ -0,0 +1,11 @@
---
source: tests/file_tests.rs
expression: output
info:
context:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
"one\ntwo\n"

View file

@ -0,0 +1,7 @@
{
"values": [ "one", "two" ]
}
---
{{ for value in values -}}
{{-= value }}
{{- end }}

View file

@ -2,6 +2,11 @@ use std::collections::HashMap;
use nomo::Context;
#[derive(serde::Serialize)]
struct Info {
context: HashMap<String, serde_json::Value>,
}
#[test]
fn check_cases() {
insta::glob!("cases/*.nomo", |path| {
@ -9,7 +14,6 @@ fn check_cases() {
settings.set_snapshot_path("cases");
settings.set_snapshot_suffix(path.file_stem().unwrap().display().to_string());
settings.set_prepend_module_to_snapshot(false);
let _guard = settings.bind_to_scope();
let input = std::fs::read_to_string(path).unwrap();
@ -21,14 +25,20 @@ fn check_cases() {
HashMap::new()
};
settings.set_info(&Info {
context: map.clone(),
});
let mut context = Context::new();
for (k, v) in map {
context.insert(k, v);
context.try_insert(k, v).unwrap();
}
let parsed = nomo::parser::parse(input.into()).unwrap();
let _guard = settings.bind_to_scope();
insta::assert_debug_snapshot!("1-parsed", parsed);
let ast = match nomo::ast::parse(parsed.tokens()) {