Rename emit to compiler

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-15 11:45:32 +01:00
parent 705c6a8818
commit d72f888849
9 changed files with 221 additions and 15 deletions

206
src/compiler/lib.rs Normal file
View file

@ -0,0 +1,206 @@
use std::collections::HashMap;
use displaydoc::Display;
use thiserror::Error;
use crate::compiler::VMInstructions;
use crate::functions::FunctionMap;
use crate::input::NomoInput;
use crate::value::NomoValue;
use crate::value::NomoValueError;
pub mod parser;
pub mod compiler;
pub mod eval;
pub mod functions;
pub mod input;
pub mod lexer;
pub mod value;
#[derive(Debug, Error, Display)]
pub enum NomoError {
/// Could not parse the given template
ParseError {
#[from]
source: lexer::ParseFailure,
},
/// Invalid Template
AstError {
#[from]
source: parser::AstFailure,
},
/// An error occurred while evaluating
EvaluationError {
#[from]
source: eval::EvaluationError,
},
/// The template '{0}' could not be found
UnknownTemplate(String),
}
pub struct Nomo {
templates: HashMap<String, Template>,
function_map: FunctionMap,
}
impl Default for Nomo {
fn default() -> Self {
Self::new()
}
}
impl Nomo {
pub fn new() -> Nomo {
Nomo {
templates: HashMap::new(),
function_map: FunctionMap::default(),
}
}
pub fn add_template(
&mut self,
name: impl Into<String>,
value: impl Into<NomoInput>,
) -> Result<(), NomoError> {
let source = value.into();
let parse = lexer::parse(source.clone())?;
let ast = parser::parse(parse.tokens())?;
let instructions = compiler::emit_machine(ast);
self.templates
.insert(name.into(), Template { instructions });
Ok(())
}
pub fn render(&self, name: &str, ctx: &Context) -> Result<String, NomoError> {
let template = self
.templates
.get(name)
.ok_or_else(|| NomoError::UnknownTemplate(name.to_string()))?;
let res = eval::execute(&self.function_map, &template.instructions, ctx)?;
Ok(res)
}
}
struct Template {
instructions: VMInstructions,
}
pub struct Context {
values: HashMap<String, NomoValue>,
}
impl Default for Context {
fn default() -> Self {
Context::new()
}
}
impl Context {
pub fn new() -> Context {
Context {
values: HashMap::new(),
}
}
pub fn try_insert(
&mut self,
key: impl Into<String>,
value: impl TryInto<NomoValue, Error = NomoValueError>,
) -> Result<(), NomoValueError> {
self.values.insert(key.into(), value.try_into()?);
Ok(())
}
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<NomoValue>) {
self.values.insert(key.into(), value.into());
}
pub fn values(&self) -> &HashMap<String, NomoValue> {
&self.values
}
}
#[derive(Debug, Clone)]
pub struct SourceSpan {
pub range: std::ops::Range<usize>,
}
// This is just like the standard .resume_after(), except we only resume on Cut errors.
fn resume_after_cut<Input, Output, Error, ParseNext, ParseRecover>(
mut parser: ParseNext,
mut recover: ParseRecover,
) -> impl winnow::Parser<Input, Option<Output>, Error>
where
Input: winnow::stream::Stream + winnow::stream::Recover<Error>,
Error: winnow::error::ParserError<Input> + winnow::error::FromRecoverableError<Input, Error>,
ParseNext: winnow::Parser<Input, Output, Error>,
ParseRecover: winnow::Parser<Input, (), Error>,
{
winnow::combinator::trace("resume_after_cut", move |input: &mut Input| {
resume_after_cut_inner(&mut parser, &mut recover, input)
})
}
fn resume_after_cut_inner<P, R, I, O, E>(
parser: &mut P,
recover: &mut R,
i: &mut I,
) -> winnow::Result<Option<O>, E>
where
P: winnow::Parser<I, O, E>,
R: winnow::Parser<I, (), E>,
I: winnow::stream::Stream,
I: winnow::stream::Recover<E>,
E: winnow::error::ParserError<I> + winnow::error::FromRecoverableError<I, E>,
{
let token_start = i.checkpoint();
let mut err = match parser.parse_next(i) {
Ok(o) => {
return Ok(Some(o));
}
Err(e) if e.is_incomplete() || e.is_backtrack() => {
return Err(e);
}
Err(err) => err,
};
let err_start = i.checkpoint();
if recover.parse_next(i).is_ok() {
if let Err(err_) = i.record_err(&token_start, &err_start, err) {
err = err_;
} else {
return Ok(None);
}
}
i.reset(&err_start);
err = E::from_recoverable_error(&token_start, &err_start, i, err);
Err(err)
}
#[cfg(test)]
mod tests {
use crate::Context;
use crate::Nomo;
#[test]
fn check_simple_template() {
let mut temp = Nomo::new();
temp.add_template("base", "Hello {{= name }}").unwrap();
let mut ctx = Context::new();
ctx.insert("name", "World");
let rendered = temp.render("base", &ctx).unwrap();
insta::assert_snapshot!(rendered, @"Hello World")
}
}

560
src/compiler/mod.rs Normal file
View file

@ -0,0 +1,560 @@
use std::collections::BTreeMap;
use crate::parser::TemplateAstExpr;
use crate::input::NomoInput;
use crate::lexer::TemplateToken;
use crate::lexer::TokenOperator;
use crate::value::NomoValue;
pub struct EmitMachine {
current_index: usize,
labels: BTreeMap<LabelSlot, usize>,
}
impl EmitMachine {
fn reserve_slot(&mut self) -> VariableSlot {
VariableSlot {
index: {
let val = self.current_index;
self.current_index += 1;
val
},
}
}
fn reserve_label(&mut self) -> LabelSlot {
LabelSlot {
index: {
let val = self.current_index;
self.current_index += 1;
val
},
}
}
fn assign_label(&mut self, slot: LabelSlot, idx: usize) {
let no_prev = self.labels.insert(slot, idx).is_none();
assert!(no_prev, "A label slot was already assigned")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct VariableSlot {
index: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LabelSlot {
index: usize,
}
#[derive(Debug, Clone)]
pub enum Instruction {
AppendContent {
content: NomoInput,
},
LoadFromContextToSlot {
name: NomoInput,
slot: VariableSlot,
},
EmitFromSlot {
slot: VariableSlot,
},
PushScope {
inherit_parent: bool,
},
Abort,
JumpIfNotTrue {
emit_slot: VariableSlot,
jump: LabelSlot,
},
Jump {
jump: LabelSlot,
},
NoOp,
CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot,
iterator_source_slot: VariableSlot,
},
AdvanceIteratorOrJump {
iterator_slot: VariableSlot,
value_slot: VariableSlot,
jump: LabelSlot,
},
GetIteratorEmptyOrJump {
iterator_slot: VariableSlot,
jump: LabelSlot,
},
PopScope,
LoadFromSlotToContext {
value_ident: NomoInput,
value_slot: VariableSlot,
},
LoadLiteralToSlot {
source: TemplateToken,
value: NomoValue,
slot: VariableSlot,
},
MathOperate {
op: TokenOperator,
left_slot: VariableSlot,
right_slot: VariableSlot,
result_slot: VariableSlot,
},
FunctionCall {
name: NomoInput,
args: Vec<VariableSlot>,
slot: VariableSlot,
},
}
#[derive(Debug, Clone)]
pub struct VMInstructions {
pub labels: BTreeMap<LabelSlot, usize>,
pub instructions: Vec<Instruction>,
}
pub fn emit_machine(input: crate::parser::TemplateAst<'_>) -> VMInstructions {
let mut eval = vec![];
let mut machine = EmitMachine {
current_index: 0,
labels: BTreeMap::new(),
};
for ast in input.root() {
emit_ast_expr(&mut machine, &mut eval, ast);
}
VMInstructions {
labels: machine.labels,
instructions: eval,
}
}
fn emit_ast_expr(
machine: &mut EmitMachine,
eval: &mut Vec<Instruction>,
ast: &TemplateAstExpr<'_>,
) {
match ast {
TemplateAstExpr::StaticContent(template_token) => {
eval.push(Instruction::AppendContent {
content: template_token.source().clone(),
});
}
TemplateAstExpr::Interpolation {
prev_whitespace_content,
expression,
post_whitespace_content,
} => {
if let Some(ws) = prev_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
let emit_slot = machine.reserve_slot();
emit_expr_load(machine, eval, emit_slot, expression);
eval.push(Instruction::EmitFromSlot { slot: emit_slot });
if let Some(ws) = post_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
}
TemplateAstExpr::ConditionalChain { chain } => {
let mut chain = chain.iter();
let end_label = machine.reserve_label();
let mut end_indices = vec![];
let mut previous_post_whitespace_content: &Option<TemplateToken> = &None;
let mut previous_jump: Option<LabelSlot> = None;
loop {
let next = chain.next().unwrap();
if let Some(ws) = previous_post_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
if let TemplateAstExpr::ConditionalContent { content } = &next {
for ast in content {
emit_ast_expr(machine, eval, ast);
}
end_indices.push(eval.len());
eval.push(Instruction::Jump { jump: end_label });
} else if let TemplateAstExpr::Block {
prev_whitespace_content,
post_whitespace_content,
expression,
} = &next
{
previous_post_whitespace_content = post_whitespace_content;
if let Some(ws) = prev_whitespace_content {
let idx = end_indices.last().copied();
eval.insert(
idx.unwrap_or(eval.len()),
Instruction::AppendContent {
content: ws.source().clone(),
},
);
if let Some(idx) = end_indices.last_mut() {
*idx += 1;
}
}
if let TemplateAstExpr::IfConditional { expression } = &**expression {
let emit_slot = machine.reserve_slot();
emit_expr_load(machine, eval, emit_slot, expression);
let jmp_label = machine.reserve_label();
previous_jump = Some(jmp_label);
eval.push(Instruction::JumpIfNotTrue {
emit_slot,
jump: jmp_label,
});
} else if let TemplateAstExpr::ElseConditional { expression } = &**expression {
if let Some(previous_jump) = previous_jump.take() {
machine.assign_label(previous_jump, eval.len());
} else {
panic!("Got an else without a previous if?");
}
if let Some(expression) = expression {
let emit_slot = machine.reserve_slot();
emit_expr_load(machine, eval, emit_slot, expression);
let jmp_label = machine.reserve_label();
previous_jump = Some(jmp_label);
eval.push(Instruction::JumpIfNotTrue {
emit_slot,
jump: jmp_label,
});
} else {
// We don't have to do anything in the else case
}
} else if let TemplateAstExpr::EndBlock = &**expression {
break;
}
}
}
if let Some(previous_jump) = previous_jump.take() {
machine.assign_label(previous_jump, eval.len());
}
machine.assign_label(end_label, eval.len());
if let Some(ws) = previous_post_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
} else {
eval.push(Instruction::NoOp);
}
}
TemplateAstExpr::ForChain {
for_block,
content,
else_block,
else_content,
end_block,
} => {
let post_for_whitespace_content;
let label_to_else_or_empty_index = machine.reserve_label();
let label_to_end_index = machine.reserve_label();
let label_start_loop = machine.reserve_label();
if let TemplateAstExpr::Block {
prev_whitespace_content,
expression,
post_whitespace_content,
} = &**for_block
&& let TemplateAstExpr::For {
value_ident,
value_expression,
} = &**expression
{
if let Some(ws) = prev_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
post_for_whitespace_content = post_whitespace_content;
eval.push(Instruction::PushScope {
inherit_parent: true,
});
let value_slot = machine.reserve_slot();
let iterator_source_slot = machine.reserve_slot();
let iterator_slot = machine.reserve_slot();
emit_expr_load(machine, eval, iterator_source_slot, value_expression);
eval.push(Instruction::CreateIteratorFromSlotToSlot {
iterator_source_slot,
iterator_slot,
});
eval.push(Instruction::GetIteratorEmptyOrJump {
iterator_slot,
jump: label_to_else_or_empty_index,
});
machine.assign_label(label_start_loop, eval.len());
eval.push(Instruction::AdvanceIteratorOrJump {
iterator_slot,
value_slot,
jump: label_to_end_index,
});
eval.push(Instruction::LoadFromSlotToContext {
value_slot,
value_ident: value_ident.source(),
});
} else {
panic!("For block should be a for block");
};
if let Some(ws) = post_for_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
for content in content {
emit_ast_expr(machine, eval, content);
}
let end_of_content_jump = eval.len();
eval.push(Instruction::Jump {
jump: label_start_loop,
});
let has_else = else_block.is_some();
if let Some(TemplateAstExpr::Block {
prev_whitespace_content,
expression,
post_whitespace_content,
}) = else_block.as_deref()
&& let TemplateAstExpr::ForElse = &**expression
{
if let Some(ws) = prev_whitespace_content {
eval.insert(
end_of_content_jump.saturating_sub(1),
Instruction::AppendContent {
content: ws.source().clone(),
},
);
}
machine.assign_label(label_to_else_or_empty_index, eval.len());
if let Some(ws) = post_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
for content in else_content
.as_ref()
.expect("If there is a for block, there should be for content (even if empty)")
{
emit_ast_expr(machine, eval, content);
}
}
let post_end_whitespace_content;
if let TemplateAstExpr::Block {
prev_whitespace_content,
expression,
post_whitespace_content,
} = &**end_block
&& let TemplateAstExpr::EndBlock = &**expression
{
post_end_whitespace_content = post_whitespace_content;
if let Some(ws) = prev_whitespace_content {
if has_else {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
} else {
eval.insert(
end_of_content_jump.saturating_sub(1),
Instruction::AppendContent {
content: ws.source().clone(),
},
);
}
}
if !has_else {
machine.assign_label(label_to_else_or_empty_index, eval.len());
}
machine.assign_label(label_to_end_index, eval.len());
eval.push(Instruction::PopScope);
if let Some(ws) = post_end_whitespace_content {
eval.push(Instruction::AppendContent {
content: ws.source().clone(),
});
}
} else {
panic!("End block should be an endblock");
}
}
TemplateAstExpr::Block { .. }
| TemplateAstExpr::EndBlock
| TemplateAstExpr::IfConditional { .. }
| TemplateAstExpr::ConditionalContent { .. }
| TemplateAstExpr::ElseConditional { .. }
| TemplateAstExpr::For { .. }
| TemplateAstExpr::ForElse
| TemplateAstExpr::Invalid { .. }
| TemplateAstExpr::Literal { .. }
| TemplateAstExpr::FunctionCall { .. }
| TemplateAstExpr::Operation { .. }
| TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort),
}
}
fn emit_expr_load(
machine: &mut EmitMachine,
eval: &mut Vec<Instruction>,
emit_slot: VariableSlot,
expression: &TemplateAstExpr<'_>,
) {
match expression {
TemplateAstExpr::VariableAccess(template_token) => {
eval.push(Instruction::LoadFromContextToSlot {
name: template_token.source().clone(),
slot: emit_slot,
});
}
TemplateAstExpr::Literal { source, value } => {
eval.push(Instruction::LoadLiteralToSlot {
source: source.clone(),
value: value.clone(),
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::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::ConditionalChain { .. } => todo!(),
TemplateAstExpr::ElseConditional { .. } => todo!(),
TemplateAstExpr::EndBlock => todo!(),
TemplateAstExpr::Block { .. } => todo!(),
TemplateAstExpr::ForChain { .. } => todo!(),
TemplateAstExpr::For { .. } => todo!(),
TemplateAstExpr::ForElse => todo!(),
TemplateAstExpr::IfConditional { .. } => todo!(),
TemplateAstExpr::ConditionalContent { .. } => todo!(),
}
}
#[cfg(test)]
mod tests {
use crate::compiler::emit_machine;
#[test]
fn check_simple_variable_interpolation() {
let input = "Hello {{= world }}";
let parsed = crate::lexer::parse(input.into()).unwrap();
let ast = crate::parser::parse(parsed.tokens()).unwrap();
let emit = emit_machine(ast);
insta::assert_debug_snapshot!(emit, @r#"
VMInstructions {
labels: {},
instructions: [
AppendContent {
content: "Hello" (0..5),
},
AppendContent {
content: " " (5..6),
},
LoadFromContextToSlot {
name: "world" (10..15),
slot: VariableSlot {
index: 0,
},
},
EmitFromSlot {
slot: VariableSlot {
index: 0,
},
},
],
}
"#);
}
#[test]
fn check_if_else_if() {
let input = "{{ if foo }} foo {{ else if bar }} bar {{ else }} foobar {{ end }}";
let parsed = crate::lexer::parse(input.into()).unwrap();
let ast = crate::parser::parse(parsed.tokens()).unwrap();
let emit = emit_machine(ast);
insta::assert_debug_snapshot!(emit);
}
#[test]
fn check_function_call() {
let input = "{{ if foo(23) }} bar {{ else }} foobar {{ end }}";
let parsed = crate::lexer::parse(input.into()).unwrap();
let ast = crate::parser::parse(parsed.tokens()).unwrap();
let emit = emit_machine(ast);
insta::assert_debug_snapshot!(emit);
}
}

View file

@ -0,0 +1,79 @@
---
source: src/compiler/mod.rs
expression: emit
---
VMInstructions {
labels: {
LabelSlot {
index: 0,
}: 13,
LabelSlot {
index: 3,
}: 8,
},
instructions: [
LoadLiteralToSlot {
source: [Literal(Integer(23))]"23" (10..12),
value: Integer {
value: 23,
},
slot: VariableSlot {
index: 2,
},
},
FunctionCall {
name: "foo" (6..9),
args: [
VariableSlot {
index: 2,
},
],
slot: VariableSlot {
index: 1,
},
},
JumpIfNotTrue {
emit_slot: VariableSlot {
index: 1,
},
jump: LabelSlot {
index: 3,
},
},
AppendContent {
content: " " (16..17),
},
AppendContent {
content: "bar" (17..20),
},
AppendContent {
content: " " (20..21),
},
Jump {
jump: LabelSlot {
index: 0,
},
},
AppendContent {
content: " " (16..17),
},
AppendContent {
content: " " (31..32),
},
AppendContent {
content: "foobar" (32..38),
},
AppendContent {
content: " " (38..39),
},
Jump {
jump: LabelSlot {
index: 0,
},
},
AppendContent {
content: " " (31..32),
},
NoOp,
],
}

View file

@ -0,0 +1,99 @@
---
source: src/compiler/mod.rs
expression: emit
---
VMInstructions {
labels: {
LabelSlot {
index: 0,
}: 19,
LabelSlot {
index: 2,
}: 7,
LabelSlot {
index: 4,
}: 14,
},
instructions: [
LoadFromContextToSlot {
name: "foo" (6..9),
slot: VariableSlot {
index: 1,
},
},
JumpIfNotTrue {
emit_slot: VariableSlot {
index: 1,
},
jump: LabelSlot {
index: 2,
},
},
AppendContent {
content: " " (12..13),
},
AppendContent {
content: "foo" (13..16),
},
AppendContent {
content: " " (16..17),
},
Jump {
jump: LabelSlot {
index: 0,
},
},
AppendContent {
content: " " (12..13),
},
LoadFromContextToSlot {
name: "bar" (28..31),
slot: VariableSlot {
index: 3,
},
},
JumpIfNotTrue {
emit_slot: VariableSlot {
index: 3,
},
jump: LabelSlot {
index: 4,
},
},
AppendContent {
content: " " (34..35),
},
AppendContent {
content: "bar" (35..38),
},
AppendContent {
content: " " (38..39),
},
Jump {
jump: LabelSlot {
index: 0,
},
},
AppendContent {
content: " " (34..35),
},
AppendContent {
content: " " (49..50),
},
AppendContent {
content: "foobar" (50..56),
},
AppendContent {
content: " " (56..57),
},
Jump {
jump: LabelSlot {
index: 0,
},
},
AppendContent {
content: " " (49..50),
},
NoOp,
],
}