@@ -23,6 +23,9 @@ pub enum EvalError {
23
23
#[ error( "Unknown function {0}" ) ]
24
24
UnknownFunction ( String ) ,
25
25
26
+ #[ error( "Unknown variable {0}" ) ]
27
+ UnknownVariable ( String ) ,
28
+
26
29
#[ error( "Unable to index into value {0}" ) ]
27
30
CannotIndex ( String ) ,
28
31
@@ -53,34 +56,37 @@ impl SimplExpr {
53
56
BinOp ( span, box a, op, box b) => BinOp ( span, box f( a) , op, box f( b) ) ,
54
57
UnaryOp ( span, op, box a) => UnaryOp ( span, op, box f( a) ) ,
55
58
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 ( ) ) ,
56
61
other => f ( other) ,
57
62
}
58
63
}
59
64
60
65
/// 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
+ }
84
90
85
91
pub fn var_refs ( & self ) -> Vec < & String > {
86
92
use SimplExpr :: * ;
0 commit comments