Add emitting of function calls

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-14 10:52:29 +01:00
parent cb55c00739
commit 52a63a7066
5 changed files with 116 additions and 11 deletions

View file

@ -102,6 +102,11 @@ pub enum Instruction {
right_slot: VariableSlot,
result_slot: VariableSlot,
},
FunctionCall {
name: NomoInput,
args: Vec<VariableSlot>,
slot: VariableSlot,
},
}
#[derive(Debug, Clone)]
@ -456,11 +461,24 @@ fn emit_expr_load(
result_slot: emit_slot,
});
}
TemplateAstExpr::FunctionCall { name, args } => {
let mut arg_slots = vec![];
for arg in args {
let slot = machine.reserve_slot();
emit_expr_load(machine, eval, slot, arg);
arg_slots.push(slot);
}
eval.push(Instruction::FunctionCall {
name: name.source(),
args: arg_slots,
slot: emit_slot,
});
}
TemplateAstExpr::Invalid { .. } => eval.push(Instruction::Abort),
TemplateAstExpr::StaticContent { .. } | TemplateAstExpr::Interpolation { .. } => {
unreachable!("Invalid AST here")
}
TemplateAstExpr::FunctionCall { .. } => todo!(),
TemplateAstExpr::ConditionalChain { .. } => todo!(),
TemplateAstExpr::ElseConditional { .. } => todo!(),
TemplateAstExpr::EndBlock => todo!(),
@ -526,4 +544,17 @@ mod tests {
insta::assert_debug_snapshot!(emit);
}
#[test]
fn check_function_call() {
let input = "{{ if foo(23) }} bar {{ else }} foobar {{ end }}";
let parsed = crate::parser::parse(input.into()).unwrap();
let ast = crate::ast::parse(parsed.tokens()).unwrap();
let emit = emit_machine(ast);
insta::assert_debug_snapshot!(emit);
}
}