Add evaluating of simple maths

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-12 17:29:45 +01:00
parent 05c095ccfe
commit d222573a3a
8 changed files with 543 additions and 4 deletions

View file

@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use crate::ast::TemplateAstExpr;
use crate::input::NomoInput;
use crate::parser::TemplateToken;
use crate::parser::TokenOperator;
use crate::value::NomoValue;
pub struct EmitMachine {
@ -95,6 +96,12 @@ pub enum Instruction {
value: NomoValue,
slot: VariableSlot,
},
MathOperate {
op: TokenOperator,
left_slot: VariableSlot,
right_slot: VariableSlot,
result_slot: VariableSlot,
},
}
#[derive(Debug, Clone)]
@ -416,7 +423,7 @@ fn emit_ast_expr(
}
fn emit_expr_load(
_machine: &mut EmitMachine,
machine: &mut EmitMachine,
eval: &mut Vec<Instruction>,
emit_slot: VariableSlot,
expression: &TemplateAstExpr<'_>,
@ -435,6 +442,19 @@ fn emit_expr_load(
slot: emit_slot,
});
}
TemplateAstExpr::Operation { op, lhs, rhs } => {
let left_slot = machine.reserve_slot();
emit_expr_load(machine, eval, left_slot, lhs);
let right_slot = machine.reserve_slot();
emit_expr_load(machine, eval, right_slot, rhs);
eval.push(Instruction::MathOperate {
op: *op,
left_slot,
right_slot,
result_slot: emit_slot,
});
}
TemplateAstExpr::Invalid { .. } => eval.push(Instruction::Abort),
TemplateAstExpr::StaticContent { .. } | TemplateAstExpr::Interpolation { .. } => {
unreachable!("Invalid AST here")
@ -446,7 +466,6 @@ fn emit_expr_load(
TemplateAstExpr::ForChain { .. } => todo!(),
TemplateAstExpr::For { .. } => todo!(),
TemplateAstExpr::ForElse => todo!(),
TemplateAstExpr::Operation { .. } => todo!(),
TemplateAstExpr::IfConditional { .. } => todo!(),
TemplateAstExpr::ConditionalContent { .. } => todo!(),