Add deep access

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-15 14:31:46 +01:00
parent 4f770c1f24
commit ffd9baf90f
9 changed files with 669 additions and 5 deletions

View file

@ -108,6 +108,20 @@ pub enum Instruction {
args: Vec<VariableSlot>,
slot: VariableSlot,
},
LoadFromSlotToSlot {
from_slot: VariableSlot,
to_slot: VariableSlot,
},
JumpIfUndefined {
slot: VariableSlot,
jump: LabelSlot,
},
IndexSlotToSlot {
name: NomoInput,
from_slot: VariableSlot,
to_slot: VariableSlot,
fail_on_not_found: bool,
},
}
#[derive(Debug, Clone)]
@ -452,6 +466,63 @@ fn emit_expr_load(
fail_on_not_found: false,
});
}
TemplateAstExpr::AccessOperation { op, lhs, rhs } => {
assert_eq!(
op,
&TokenOperator::Dot,
"Only dot can be used to access variables, this is a bug"
);
fn inner_access_op(
eval: &mut Vec<Instruction>,
end_label: LabelSlot,
current_slot: VariableSlot,
next: &TemplateAstExpr<'_>,
) {
match next {
TemplateAstExpr::ConditionalAccess(template_token)
| TemplateAstExpr::VariableAccess(template_token) => {
eval.push(Instruction::IndexSlotToSlot {
name: template_token.source().clone(),
from_slot: current_slot,
to_slot: current_slot,
fail_on_not_found: matches!(
next,
TemplateAstExpr::VariableAccess { .. }
),
});
eval.push(Instruction::JumpIfUndefined {
slot: current_slot,
jump: end_label,
});
}
TemplateAstExpr::AccessOperation { op, lhs, rhs } => {
assert_eq!(
op,
&TokenOperator::Dot,
"Only dot can be used to access variables, this is a bug"
);
inner_access_op(eval, end_label, current_slot, lhs);
inner_access_op(eval, end_label, current_slot, rhs);
}
_ => unreachable!(),
}
}
let end_label = machine.reserve_label();
let start_slot = machine.reserve_slot();
emit_expr_load(machine, eval, start_slot, lhs);
eval.push(Instruction::JumpIfUndefined {
slot: start_slot,
jump: end_label,
});
inner_access_op(eval, end_label, start_slot, rhs);
machine.assign_label(end_label, eval.len());
eval.push(Instruction::LoadFromSlotToSlot {
from_slot: start_slot,
to_slot: emit_slot,
});
}
TemplateAstExpr::Literal { source, value } => {
eval.push(Instruction::LoadLiteralToSlot {
source: source.clone(),
@ -492,7 +563,6 @@ fn emit_expr_load(
}
TemplateAstExpr::ConditionalChain { .. } => todo!(),
TemplateAstExpr::ElseConditional { .. } => todo!(),
TemplateAstExpr::AccessOperation { .. } => todo!(),
TemplateAstExpr::EndBlock => todo!(),
TemplateAstExpr::Block { .. } => todo!(),
TemplateAstExpr::ForChain { .. } => todo!(),

View file

@ -119,7 +119,7 @@ pub fn execute(
} else if matches!(value, NomoValue::Undefined) {
String::new()
} else {
panic!("Unknown variable");
panic!("Could not print out value {value:?}");
};
output.push_str(&value);
@ -243,6 +243,46 @@ pub fn execute(
scopes.insert_into_slot(*slot, value);
}
Instruction::LoadFromSlotToSlot { from_slot, to_slot } => {
let value = scopes.get(from_slot);
scopes.insert_into_slot(*to_slot, value.clone());
}
Instruction::JumpIfUndefined { slot, jump } => {
let is_undefined = matches!(scopes.get(slot), NomoValue::Undefined);
if is_undefined {
let Some(new_ip) = vm.labels.get(jump) else {
return Err(EvaluationError::LabelNotFound);
};
ip = *new_ip;
continue;
}
}
Instruction::IndexSlotToSlot {
name,
from_slot,
to_slot,
fail_on_not_found,
} => {
let value = scopes.get(from_slot);
match value {
NomoValue::Object { value } => {
let value = value.get(name.as_str());
let value = if let Some(value) = value {
value.clone()
} else if *fail_on_not_found {
panic!("Could not index");
} else {
NomoValue::Undefined
};
scopes.insert_into_slot(dbg!(*to_slot), dbg!(value));
}
_ => panic!("Invalid indexing"),
}
}
}
ip += 1;

View file

@ -715,10 +715,10 @@ fn parse_expression<'input>(
Left MathOperation GreaterOrEqual => 15,
Left MathOperation Lesser => 15,
Left MathOperation LesserOrEqual => 15,
Left AccessOperation Dot => 23,
Left AccessOperation Dot => 22,
]
}).postfix(dispatch! { surrounded(ws, parse_operator);
TokenOperator::QuestionMark => Postfix(22, |input, rhs| {
TokenOperator::QuestionMark => Postfix(23, |input, rhs| {
match rhs {
TemplateAstExpr::VariableAccess(access) => Ok(TemplateAstExpr::ConditionalAccess(access)),
_ => Err(AstError::from_input(input)),

View file

@ -54,6 +54,8 @@ impl NomoValue {
pub fn as_bool(&self) -> Option<bool> {
if let Self::Bool { value } = self {
Some(*value)
} else if let Self::Undefined = self {
Some(false)
} else {
None
}
@ -438,7 +440,14 @@ impl TryFrom<serde_json::Value> for NomoValue {
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
}),
serde_json::Value::Object(_map) => todo!(),
serde_json::Value::Object(map) => {
let value = map
.into_iter()
.map(|(key, value)| Ok((key, NomoValue::try_from(value)?)))
.collect::<Result<_, _>>()?;
Ok(NomoValue::Object { value })
}
}
}
}