diff --git a/cel-rs/src/eval.rs b/cel-rs/src/eval.rs index 1991e48..6a9d663 100644 --- a/cel-rs/src/eval.rs +++ b/cel-rs/src/eval.rs @@ -28,29 +28,25 @@ impl Eval { return value.to_owned() } if let Some(val) = ctx.resolve(&attr) { - if let Value::Function(f, _) = val { - return Value::Function(f, Some(Rc::new(v))); - } return val } - panic!("unknown attribute {}", attr) }, - crate::parser::Member::FunctionCall(mut rargs) => { + crate::parser::Member::FunctionCall(name, mut rargs) => { let mut args = Vec::with_capacity(rargs.len()); rargs.reverse(); for arg in rargs { args.push(self.eval(arg, ctx).unpack()); } - - if let Value::Function(f, bound) = v { - if let Some(b) = bound { - args.push((*b).clone()); - args.reverse() + + if let Some(val) = ctx.resolve(&name) { + args.push(v.clone()); + args.reverse(); + if let Value::Function(f) = val { + return (f.overloads.first().unwrap().func)(args) } - return (f.overloads.first().unwrap().func)(args) } - + panic!("is not a func") }, crate::parser::Member::Index(i) => { @@ -125,6 +121,7 @@ impl Eval { } panic!("unknown attribute {}", &r) }, + Expression::FunctionCall(_) => todo!(), } } } diff --git a/cel-rs/src/parser/ast.rs b/cel-rs/src/parser/ast.rs index e442d6c..0ba30e8 100644 --- a/cel-rs/src/parser/ast.rs +++ b/cel-rs/src/parser/ast.rs @@ -40,6 +40,8 @@ pub enum Expression { Member(Box, Box), + FunctionCall(Box), + List(Vec), Map(Vec<(Expression, Expression)>), @@ -50,7 +52,7 @@ pub enum Expression { #[derive(Debug, PartialEq, Clone)] pub enum Member { Attribute(Rc), - FunctionCall(Vec), + FunctionCall(Rc, Vec), Index(Box), Fields(Vec<(Rc, Expression)>), } diff --git a/cel-rs/src/parser/cel.lalrpop b/cel-rs/src/parser/cel.lalrpop index 5c3f0b0..1a3c04e 100644 --- a/cel-rs/src/parser/cel.lalrpop +++ b/cel-rs/src/parser/cel.lalrpop @@ -25,8 +25,7 @@ pub Expression: Expression = { pub Member: Expression = { "." => Expression::Member(left.into(), Member::Attribute(identifier.into()).into()).into(), "." "(" > ")" => { - let inner = Expression::Member(left.into(), Member::Attribute(identifier.into()).into()).into(); - Expression::Member(inner, Member::FunctionCall(arguments).into()).into() + Expression::Member(left.into(), Member::FunctionCall(identifier.into(), arguments).into()).into() }, "[" "]" => Expression::Member(left.into(), Member::Index(expression.into()).into()).into(), "{" > "}" => Expression::Member(left.into(), Member::Fields(fields.into()).into()).into(), @@ -36,8 +35,7 @@ pub Member: Expression = { pub Primary: Expression = { "."? => Expression::Ident(<>.into()).into(), "."? "(" > ")" => { - let inner = Expression::Ident(identifier.into()).into(); - Expression::Member(inner, Member::FunctionCall(arguments).into()).into() + Expression::FunctionCall(Member::FunctionCall(identifier.into(), arguments).into()).into() }, Atom => Expression::Atom(<>).into(), "[" > "]" => Expression::List(<>).into(), diff --git a/cel-rs/src/program.rs b/cel-rs/src/program.rs index 2734d92..9cb4348 100644 --- a/cel-rs/src/program.rs +++ b/cel-rs/src/program.rs @@ -47,7 +47,7 @@ pub mod tests { use crate::{ program, - value::{FnValue, Overload}, + value::{Function, Overload}, Value, }; @@ -87,7 +87,7 @@ pub mod tests { #[test] fn fn_test() { - let func = FnValue { + let func = Function { name: "calc", overloads: &[Overload { key: "calc_string", @@ -97,7 +97,7 @@ pub mod tests { let mut ctx = program::Context::default() .add_variable("a", Value::Int(10)) .add_variable("b", Value::Int(10)) - .add_variable("calc", crate::Value::Function(Rc::new(func), None)); + .add_variable("calc", crate::Value::Function(func.into())); assert_eq!(eval_program!(r#"b.calc(a)"#, &mut ctx), Value::Int(20)); } } diff --git a/cel-rs/src/value.rs b/cel-rs/src/value.rs index 5c56de6..de90b92 100644 --- a/cel-rs/src/value.rs +++ b/cel-rs/src/value.rs @@ -15,11 +15,11 @@ pub enum Value { String(Rc), Map(Rc>), List(Rc>), - Function(Rc, Option>) + Function(Rc) } #[derive(PartialEq, Eq, Debug, Clone)] -pub struct FnValue { +pub struct Function { pub name: &'static str, pub overloads: &'static [Overload], } @@ -44,7 +44,7 @@ impl Into for Value { Value::String(v) => v.len() > 0, Value::Map(v) => v.len() > 0, Value::List(v) => v.len() > 0, - Value::Function(_, _) => true, + Value::Function(v) => true, } } } @@ -82,7 +82,7 @@ impl std::fmt::Display for Value { Value::String(v) => write!(f, "string({})", v), Value::Map(v) => write!(f, "map(len = {})", v.len()), Value::List(v) => write!(f, "list(len = {})", v.len()), - Value::Function(v, bound) => write!(f, "function(name = {}, bound = {:?})", v.name, bound), + Value::Function(v) => write!(f, "function(name = {})", v.name), } } }