Skip to content

Commit 752a842

Browse files
committed
add resolve
1 parent 3b6180a commit 752a842

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/eval.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub enum EvalError {
2323
#[error("Unknown function {0}")]
2424
UnknownFunction(String),
2525

26+
#[error("Unknown variable {0}")]
27+
UnknownVariable(String),
28+
2629
#[error("Unable to index into value {0}")]
2730
CannotIndex(String),
2831

@@ -53,34 +56,37 @@ impl SimplExpr {
5356
BinOp(span, box a, op, box b) => BinOp(span, box f(a), op, box f(b)),
5457
UnaryOp(span, op, box a) => UnaryOp(span, op, box f(a)),
5558
IfElse(span, box a, box b, box c) => IfElse(span, box f(a), box f(b), box f(c)),
59+
JsonAccess(span, box a, box b) => JsonAccess(span, box f(a), box f(b)),
60+
FunctionCall(span, name, args) => FunctionCall(span, name, args.into_iter().map(f).collect()),
5661
other => f(other),
5762
}
5863
}
5964

6065
/// resolve variable references in the expression. Fails if a variable cannot be resolved.
61-
// pub fn resolve_refs(self, variables: &HashMap<VarName, DynVal>) -> Result<Self> {
62-
// use SimplExpr::*;
63-
// match self {
64-
//// Literal(x) => Ok(Literal(AttrValue::from_primitive(x.resolve_fully(&variables)?))),
65-
// Literal(x) => Ok(Literal(x)),
66-
// VarRef(ref name) => Ok(Literal(AttrVal::from_primitive(
67-
// variables.get(name).with_context(|| format!("Unknown variable {} referenced in {:?}", &name, &self))?.clone(),
68-
//))),
69-
// BinOp(box a, op, box b) => {
70-
// Ok(BinOp(box a.resolve_refs(variables?), op, box b.resolve_refs(variables?)))
71-
//}
72-
// UnaryOp(op, box x) => Ok(UnaryOp(op, box x.resolve_refs(variables?))),
73-
// IfElse(box a, box b, box c) => Ok(IfElse(
74-
// box a.resolve_refs(variables?),
75-
// box b.resolve_refs(variables?),
76-
// box c.resolve_refs(variables?),
77-
//)),
78-
// JsonAccess(box a, box b) => {
79-
// Ok(JsonAccess(box a.resolve_refs(variables?), box b.resolve_refs(variables?)))
80-
//}
81-
// FunctionCall(function_name, args) => {
82-
// Ok(FunctionCall(function_name, args.into_iter().map(|a| a.resolve_refs(variables)).collect::<Result<_>>()?))
83-
//}
66+
pub fn resolve_refs(self, variables: &HashMap<VarName, DynVal>) -> Result<Self, EvalError> {
67+
use SimplExpr::*;
68+
match self {
69+
// Literal(x) => Ok(Literal(AttrValue::from_primitive(x.resolve_fully(&variables)?))),
70+
Literal(span, x) => Ok(Literal(span, x)),
71+
BinOp(span, box a, op, box b) => Ok(BinOp(span, box a.resolve_refs(variables)?, op, box b.resolve_refs(variables)?)),
72+
UnaryOp(span, op, box x) => Ok(UnaryOp(span, op, box x.resolve_refs(variables)?)),
73+
IfElse(span, box a, box b, box c) => {
74+
Ok(IfElse(span, box a.resolve_refs(variables)?, box b.resolve_refs(variables)?, box c.resolve_refs(variables)?))
75+
}
76+
JsonAccess(span, box a, box b) => {
77+
Ok(JsonAccess(span, box a.resolve_refs(variables)?, box b.resolve_refs(variables)?))
78+
}
79+
FunctionCall(span, function_name, args) => Ok(FunctionCall(
80+
span,
81+
function_name,
82+
args.into_iter().map(|a| a.resolve_refs(variables)).collect::<Result<_, EvalError>>()?,
83+
)),
84+
VarRef(span, ref name) => match variables.get(name) {
85+
Some(value) => Ok(Literal(span, value.clone())),
86+
None => Err(EvalError::UnknownVariable(name.to_string()).at(span)),
87+
},
88+
}
89+
}
8490

8591
pub fn var_refs(&self) -> Vec<&String> {
8692
use SimplExpr::*;

0 commit comments

Comments
 (0)