add resolve
This commit is contained in:
parent
3b6180ad7d
commit
752a842cbf
1 changed files with 29 additions and 23 deletions
52
src/eval.rs
52
src/eval.rs
|
|
@ -23,6 +23,9 @@ pub enum EvalError {
|
||||||
#[error("Unknown function {0}")]
|
#[error("Unknown function {0}")]
|
||||||
UnknownFunction(String),
|
UnknownFunction(String),
|
||||||
|
|
||||||
|
#[error("Unknown variable {0}")]
|
||||||
|
UnknownVariable(String),
|
||||||
|
|
||||||
#[error("Unable to index into value {0}")]
|
#[error("Unable to index into value {0}")]
|
||||||
CannotIndex(String),
|
CannotIndex(String),
|
||||||
|
|
||||||
|
|
@ -53,34 +56,37 @@ impl SimplExpr {
|
||||||
BinOp(span, box a, op, box b) => BinOp(span, box f(a), op, box f(b)),
|
BinOp(span, box a, op, box b) => BinOp(span, box f(a), op, box f(b)),
|
||||||
UnaryOp(span, op, box a) => UnaryOp(span, op, box f(a)),
|
UnaryOp(span, op, box a) => UnaryOp(span, op, box f(a)),
|
||||||
IfElse(span, box a, box b, box c) => IfElse(span, box f(a), box f(b), box f(c)),
|
IfElse(span, box a, box b, box c) => IfElse(span, box f(a), box f(b), box f(c)),
|
||||||
|
JsonAccess(span, box a, box b) => JsonAccess(span, box f(a), box f(b)),
|
||||||
|
FunctionCall(span, name, args) => FunctionCall(span, name, args.into_iter().map(f).collect()),
|
||||||
other => f(other),
|
other => f(other),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// resolve variable references in the expression. Fails if a variable cannot be resolved.
|
/// resolve variable references in the expression. Fails if a variable cannot be resolved.
|
||||||
// pub fn resolve_refs(self, variables: &HashMap<VarName, DynVal>) -> Result<Self> {
|
pub fn resolve_refs(self, variables: &HashMap<VarName, DynVal>) -> Result<Self, EvalError> {
|
||||||
// use SimplExpr::*;
|
use SimplExpr::*;
|
||||||
// match self {
|
match self {
|
||||||
//// Literal(x) => Ok(Literal(AttrValue::from_primitive(x.resolve_fully(&variables)?))),
|
// Literal(x) => Ok(Literal(AttrValue::from_primitive(x.resolve_fully(&variables)?))),
|
||||||
// Literal(x) => Ok(Literal(x)),
|
Literal(span, x) => Ok(Literal(span, x)),
|
||||||
// VarRef(ref name) => Ok(Literal(AttrVal::from_primitive(
|
BinOp(span, box a, op, box b) => Ok(BinOp(span, box a.resolve_refs(variables)?, op, box b.resolve_refs(variables)?)),
|
||||||
// variables.get(name).with_context(|| format!("Unknown variable {} referenced in {:?}", &name, &self))?.clone(),
|
UnaryOp(span, op, box x) => Ok(UnaryOp(span, op, box x.resolve_refs(variables)?)),
|
||||||
//))),
|
IfElse(span, box a, box b, box c) => {
|
||||||
// BinOp(box a, op, box b) => {
|
Ok(IfElse(span, box a.resolve_refs(variables)?, box b.resolve_refs(variables)?, box c.resolve_refs(variables)?))
|
||||||
// Ok(BinOp(box a.resolve_refs(variables?), op, box b.resolve_refs(variables?)))
|
}
|
||||||
//}
|
JsonAccess(span, box a, box b) => {
|
||||||
// UnaryOp(op, box x) => Ok(UnaryOp(op, box x.resolve_refs(variables?))),
|
Ok(JsonAccess(span, box a.resolve_refs(variables)?, box b.resolve_refs(variables)?))
|
||||||
// IfElse(box a, box b, box c) => Ok(IfElse(
|
}
|
||||||
// box a.resolve_refs(variables?),
|
FunctionCall(span, function_name, args) => Ok(FunctionCall(
|
||||||
// box b.resolve_refs(variables?),
|
span,
|
||||||
// box c.resolve_refs(variables?),
|
function_name,
|
||||||
//)),
|
args.into_iter().map(|a| a.resolve_refs(variables)).collect::<Result<_, EvalError>>()?,
|
||||||
// JsonAccess(box a, box b) => {
|
)),
|
||||||
// Ok(JsonAccess(box a.resolve_refs(variables?), box b.resolve_refs(variables?)))
|
VarRef(span, ref name) => match variables.get(name) {
|
||||||
//}
|
Some(value) => Ok(Literal(span, value.clone())),
|
||||||
// FunctionCall(function_name, args) => {
|
None => Err(EvalError::UnknownVariable(name.to_string()).at(span)),
|
||||||
// Ok(FunctionCall(function_name, args.into_iter().map(|a| a.resolve_refs(variables)).collect::<Result<_>>()?))
|
},
|
||||||
//}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn var_refs(&self) -> Vec<&String> {
|
pub fn var_refs(&self) -> Vec<&String> {
|
||||||
use SimplExpr::*;
|
use SimplExpr::*;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue