Skip to content

Commit

Permalink
refactor: remove function bind hack
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Mar 22, 2024
1 parent fe84874 commit f901d40
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
21 changes: 9 additions & 12 deletions cel-rs/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -125,6 +121,7 @@ impl Eval {
}
panic!("unknown attribute {}", &r)
},
Expression::FunctionCall(_) => todo!(),
}
}
}
4 changes: 3 additions & 1 deletion cel-rs/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum Expression {

Member(Box<Expression>, Box<Member>),

FunctionCall(Box<Member>),

List(Vec<Expression>),
Map(Vec<(Expression, Expression)>),

Expand All @@ -50,7 +52,7 @@ pub enum Expression {
#[derive(Debug, PartialEq, Clone)]
pub enum Member {
Attribute(Rc<String>),
FunctionCall(Vec<Expression>),
FunctionCall(Rc<String>, Vec<Expression>),
Index(Box<Expression>),
Fields(Vec<(Rc<String>, Expression)>),
}
Expand Down
6 changes: 2 additions & 4 deletions cel-rs/src/parser/cel.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ pub Expression: Expression = {
pub Member: Expression = {
<left:Member> "." <identifier:Ident> => Expression::Member(left.into(), Member::Attribute(identifier.into()).into()).into(),
<left:Member> "." <identifier:Ident> "(" <arguments:CommaSeparated<Expression>> ")" => {
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()
},
<left:Member> "[" <expression:Expression> "]" => Expression::Member(left.into(), Member::Index(expression.into()).into()).into(),
<left:Member> "{" <fields:CommaSeparated<FieldInits>> "}" => Expression::Member(left.into(), Member::Fields(fields.into()).into()).into(),
Expand All @@ -36,8 +35,7 @@ pub Member: Expression = {
pub Primary: Expression = {
"."? <Ident> => Expression::Ident(<>.into()).into(),
"."? <identifier:Ident> "(" <arguments:CommaSeparated<Expression>> ")" => {
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(),
"[" <members:CommaSeparated<Expression>> "]" => Expression::List(<>).into(),
Expand Down
6 changes: 3 additions & 3 deletions cel-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub mod tests {

use crate::{
program,
value::{FnValue, Overload},
value::{Function, Overload},
Value,
};

Expand Down Expand Up @@ -87,7 +87,7 @@ pub mod tests {

#[test]
fn fn_test() {
let func = FnValue {
let func = Function {
name: "calc",
overloads: &[Overload {
key: "calc_string",
Expand All @@ -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));
}
}
8 changes: 4 additions & 4 deletions cel-rs/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub enum Value {
String(Rc<String>),
Map(Rc<OrderedHashMap<Value, Value>>),
List(Rc<Vec<Value>>),
Function(Rc<FnValue>, Option<Rc<Value>>)
Function(Rc<Function>)
}

#[derive(PartialEq, Eq, Debug, Clone)]
pub struct FnValue {
pub struct Function {
pub name: &'static str,
pub overloads: &'static [Overload],
}
Expand All @@ -44,7 +44,7 @@ impl Into<bool> 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,
}
}
}
Expand Down Expand Up @@ -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),
}
}
}
Expand Down

0 comments on commit f901d40

Please sign in to comment.