Add conditional value emitting
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
662e574588
commit
9940881e46
10 changed files with 82 additions and 11 deletions
|
|
@ -93,15 +93,34 @@ pub fn execute(
|
|||
match instr {
|
||||
Instruction::NoOp => (),
|
||||
Instruction::AppendContent { content } => output.push_str(content),
|
||||
Instruction::LoadFromContextToSlot { name, slot } => {
|
||||
let value = scopes
|
||||
.get_scoped(name)
|
||||
.ok_or(EvaluationError::UnknownVariable(name.clone()))?;
|
||||
Instruction::LoadFromContextToSlot {
|
||||
name,
|
||||
slot,
|
||||
fail_on_not_found,
|
||||
} => {
|
||||
let value = scopes.get_scoped(name);
|
||||
|
||||
let value = if let Some(val) = value {
|
||||
val
|
||||
} else {
|
||||
if *fail_on_not_found {
|
||||
return Err(EvaluationError::UnknownVariable(name.clone()));
|
||||
} else {
|
||||
&NomoValue::Undefined
|
||||
}
|
||||
};
|
||||
|
||||
scopes.insert_into_slot(*slot, value.clone());
|
||||
}
|
||||
Instruction::EmitFromSlot { slot } => {
|
||||
let value = scopes.get(slot).try_to_string().unwrap();
|
||||
let value = scopes.get(slot);
|
||||
let value = if let Some(value) = value.try_to_string() {
|
||||
value
|
||||
} else if matches!(value, NomoValue::Undefined) {
|
||||
String::new()
|
||||
} else {
|
||||
panic!("Unknown variable");
|
||||
};
|
||||
|
||||
output.push_str(&value);
|
||||
}
|
||||
|
|
@ -286,4 +305,26 @@ mod tests {
|
|||
)
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_conditional_access() {
|
||||
let input = "Hello {{= unknown? }}";
|
||||
|
||||
let parsed = crate::lexer::parse(input.into()).unwrap();
|
||||
|
||||
let ast = crate::parser::parse(parsed.tokens()).unwrap();
|
||||
|
||||
let emit = crate::compiler::emit_machine(ast);
|
||||
|
||||
let context = Context::new();
|
||||
let function_map = FunctionMap::default();
|
||||
|
||||
let output = execute(&function_map, &emit, &context);
|
||||
|
||||
insta::assert_debug_snapshot!(output, @r#"
|
||||
Ok(
|
||||
"Hello ",
|
||||
)
|
||||
"#);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue