Add using literal loading
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
8c02dbd672
commit
05c095ccfe
7 changed files with 162 additions and 1 deletions
|
|
@ -2,6 +2,8 @@ use std::collections::BTreeMap;
|
||||||
|
|
||||||
use crate::ast::TemplateAstExpr;
|
use crate::ast::TemplateAstExpr;
|
||||||
use crate::input::NomoInput;
|
use crate::input::NomoInput;
|
||||||
|
use crate::parser::TemplateToken;
|
||||||
|
use crate::value::NomoValue;
|
||||||
|
|
||||||
pub struct EmitMachine {
|
pub struct EmitMachine {
|
||||||
current_index: usize,
|
current_index: usize,
|
||||||
|
|
@ -88,6 +90,11 @@ pub enum Instruction {
|
||||||
value_ident: NomoInput,
|
value_ident: NomoInput,
|
||||||
value_slot: VariableSlot,
|
value_slot: VariableSlot,
|
||||||
},
|
},
|
||||||
|
LoadLiteralToSlot {
|
||||||
|
source: TemplateToken,
|
||||||
|
value: NomoValue,
|
||||||
|
slot: VariableSlot,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -421,6 +428,13 @@ fn emit_expr_load(
|
||||||
slot: emit_slot,
|
slot: emit_slot,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
TemplateAstExpr::Literal { source, value } => {
|
||||||
|
eval.push(Instruction::LoadLiteralToSlot {
|
||||||
|
source: source.clone(),
|
||||||
|
value: value.clone(),
|
||||||
|
slot: emit_slot,
|
||||||
|
});
|
||||||
|
}
|
||||||
TemplateAstExpr::Invalid { .. } => eval.push(Instruction::Abort),
|
TemplateAstExpr::Invalid { .. } => eval.push(Instruction::Abort),
|
||||||
TemplateAstExpr::StaticContent { .. } | TemplateAstExpr::Interpolation { .. } => {
|
TemplateAstExpr::StaticContent { .. } | TemplateAstExpr::Interpolation { .. } => {
|
||||||
unreachable!("Invalid AST here")
|
unreachable!("Invalid AST here")
|
||||||
|
|
@ -433,7 +447,6 @@ fn emit_expr_load(
|
||||||
TemplateAstExpr::For { .. } => todo!(),
|
TemplateAstExpr::For { .. } => todo!(),
|
||||||
TemplateAstExpr::ForElse => todo!(),
|
TemplateAstExpr::ForElse => todo!(),
|
||||||
TemplateAstExpr::Operation { .. } => todo!(),
|
TemplateAstExpr::Operation { .. } => todo!(),
|
||||||
TemplateAstExpr::Literal { .. } => todo!(),
|
|
||||||
|
|
||||||
TemplateAstExpr::IfConditional { .. } => todo!(),
|
TemplateAstExpr::IfConditional { .. } => todo!(),
|
||||||
TemplateAstExpr::ConditionalContent { .. } => todo!(),
|
TemplateAstExpr::ConditionalContent { .. } => todo!(),
|
||||||
|
|
|
||||||
|
|
@ -179,6 +179,13 @@ pub fn execute(vm: &VMInstructions, global_context: &Context) -> Result<String,
|
||||||
|
|
||||||
scopes.insert_into_scope(value_ident, value);
|
scopes.insert_into_scope(value_ident, value);
|
||||||
}
|
}
|
||||||
|
Instruction::LoadLiteralToSlot {
|
||||||
|
source: _,
|
||||||
|
value,
|
||||||
|
slot,
|
||||||
|
} => {
|
||||||
|
scopes.insert_into_slot(*slot, value.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ip += 1;
|
ip += 1;
|
||||||
|
|
|
||||||
27
tests/cases/1-parsed@literals.snap
Normal file
27
tests/cases/1-parsed@literals.snap
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: parsed
|
||||||
|
info:
|
||||||
|
input: "{{ if true }}\n Hello World!\n{{ end }}"
|
||||||
|
context: {}
|
||||||
|
input_file: tests/cases/literals.nomo
|
||||||
|
---
|
||||||
|
ParsedTemplate {
|
||||||
|
tokens: [
|
||||||
|
[LeftDelim]"{{" (0..2),
|
||||||
|
[Whitespace]" " (2..3),
|
||||||
|
[ConditionalIf]"if" (3..5),
|
||||||
|
[Whitespace]" " (5..6),
|
||||||
|
[Literal(Bool(true))]"true" (6..10),
|
||||||
|
[Whitespace]" " (10..11),
|
||||||
|
[RightDelim]"}}" (11..13),
|
||||||
|
[Whitespace]"\n " (13..18),
|
||||||
|
[Content]"Hello World!" (18..30),
|
||||||
|
[Whitespace]"\n" (30..31),
|
||||||
|
[LeftDelim]"{{" (31..33),
|
||||||
|
[Whitespace]" " (33..34),
|
||||||
|
[End]"end" (34..37),
|
||||||
|
[Whitespace]" " (37..38),
|
||||||
|
[RightDelim]"}}" (38..40),
|
||||||
|
],
|
||||||
|
}
|
||||||
44
tests/cases/2-ast@literals.snap
Normal file
44
tests/cases/2-ast@literals.snap
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: ast
|
||||||
|
info:
|
||||||
|
input: "{{ if true }}\n Hello World!\n{{ end }}"
|
||||||
|
context: {}
|
||||||
|
input_file: tests/cases/literals.nomo
|
||||||
|
---
|
||||||
|
TemplateAst {
|
||||||
|
root: [
|
||||||
|
ConditionalChain {
|
||||||
|
chain: [
|
||||||
|
Block {
|
||||||
|
prev_whitespace_content: None,
|
||||||
|
expression: IfConditional {
|
||||||
|
expression: Literal {
|
||||||
|
source: [Literal(Bool(true))]"true" (6..10),
|
||||||
|
value: Bool {
|
||||||
|
value: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
post_whitespace_content: Some(
|
||||||
|
[Whitespace]"\n " (13..18),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
ConditionalContent {
|
||||||
|
content: [
|
||||||
|
StaticContent(
|
||||||
|
[Content]"Hello World!" (18..30),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Block {
|
||||||
|
prev_whitespace_content: Some(
|
||||||
|
[Whitespace]"\n" (30..31),
|
||||||
|
),
|
||||||
|
expression: EndBlock,
|
||||||
|
post_whitespace_content: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
55
tests/cases/3-instructions@literals.snap
Normal file
55
tests/cases/3-instructions@literals.snap
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: emit
|
||||||
|
info:
|
||||||
|
input: "{{ if true }}\n Hello World!\n{{ end }}"
|
||||||
|
context: {}
|
||||||
|
input_file: tests/cases/literals.nomo
|
||||||
|
---
|
||||||
|
VMInstructions {
|
||||||
|
labels: {
|
||||||
|
LabelSlot {
|
||||||
|
index: 0,
|
||||||
|
}: 7,
|
||||||
|
LabelSlot {
|
||||||
|
index: 2,
|
||||||
|
}: 7,
|
||||||
|
},
|
||||||
|
instructions: [
|
||||||
|
LoadLiteralToSlot {
|
||||||
|
source: [Literal(Bool(true))]"true" (6..10),
|
||||||
|
value: Bool {
|
||||||
|
value: true,
|
||||||
|
},
|
||||||
|
slot: VariableSlot {
|
||||||
|
index: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
JumpIfNotTrue {
|
||||||
|
emit_slot: VariableSlot {
|
||||||
|
index: 1,
|
||||||
|
},
|
||||||
|
jump: LabelSlot {
|
||||||
|
index: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n " (13..18),
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "Hello World!" (18..30),
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n" (30..31),
|
||||||
|
},
|
||||||
|
Jump {
|
||||||
|
jump: LabelSlot {
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n " (13..18),
|
||||||
|
},
|
||||||
|
NoOp,
|
||||||
|
],
|
||||||
|
}
|
||||||
9
tests/cases/4-output@literals.snap
Normal file
9
tests/cases/4-output@literals.snap
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: output
|
||||||
|
info:
|
||||||
|
input: "{{ if true }}\n Hello World!\n{{ end }}"
|
||||||
|
context: {}
|
||||||
|
input_file: tests/cases/literals.nomo
|
||||||
|
---
|
||||||
|
"\n Hello World!\n"
|
||||||
6
tests/cases/literals.nomo
Normal file
6
tests/cases/literals.nomo
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
}
|
||||||
|
---
|
||||||
|
{{ if true }}
|
||||||
|
Hello World!
|
||||||
|
{{ end }}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue