Giter Club home page Giter Club logo

eval's Issues

Possible parser bug

With the following configuration, Eval is producing nil for the 4th expression. I'm not sure how to dig further into where the bug lies, but it's possibly confused by the nested function calls?

let number = DataType(type: Double.self, literals: [
    Literal<Double> { value, _ in Double(value) },
]) { arg, _ in "\(arg)" }

let addition = Function<Double>(Variable<Double>("lhs") + Keyword("+") +
    Variable<Double>("rhs")) { arguments, _, _  in
    guard let lhs = arguments["lhs"] as? Double,
        let rhs = arguments["rhs"] as? Double else { return nil }
    return lhs + rhs
}

let foo = Function<Double>(Keyword("foo") + Keyword("(") + Variable<Double>("lhs") + Keyword(",") + Variable<Double>("rhs") + Keyword(")")) { arguments, _, _  in
    guard let lhs = arguments["lhs"] as? Double,
        let rhs = arguments["rhs"] as? Double else { return nil }
    return lhs - rhs
}

let bar = Function<Double>(Keyword("bar") + Keyword("(") + Variable<Double>("value") + Keyword(")")) { arguments, _, _  in
    guard let value = arguments["value"] as? Double else { return nil }
    return value - 2
}

let interpreter = TypedInterpreter(dataTypes: [number], functions: [addition, foo, bar])

print(interpreter.evaluate("5 + 6") as? Double ?? "nil") // 11.0
print(interpreter.evaluate("foo(5, 6)") as? Double ?? "nil") // -1.0
print(interpreter.evaluate("bar(6)") as? Double ?? "nil") // 4.0
print(interpreter.evaluate("foo(5, 6 + bar(6))") as? Double ?? "nil") // nil

figure out the suffix error in this case

// TODO: figure out the suffix error in this case
XCTAssertEqual(evaluate("(toggle == true) and (url exists)", inputs: ["toggle": true, "url": 1]), true)
// XCTAssertEqual(evaluate("(toggle == true) and (url exists)", inputs: ["toggle": true]), false)
XCTAssertEqual(evaluate("(toggle == true) and (not(url exists))", inputs: ["toggle": true]), true)
}
func testComposition2() {


This issue was generated by todo based on a TODO comment in df80bd8. It's been assigned to @undefined because they committed the code.

Issue with suffix functions

I'm incorporating this library into a project where I want to let users create formulas that check against certain fields. This is mostly working, but I've hit a small roadblock when trying to create a x exists suffix operator that basically checks x != nil. This generally works, but fails in certain nesting conditions.

It might have to do something with operator precedence, as changing the order fixes one case but then causes another test case to fail. I'd appreciate some feedback on how to fix this or how to debug this.

The strange thing is that it works fine perfectly if it's a prefix operator (didset X in my example), though I'd prefer the suffix variant.

The full code sample is here, which is a minor modification of the code from the template example in this repo: https://gist.github.com/nighthawk/060475d0ded20788e9269054e152d76d

Any help would be much appreciated!

Curious performance issue when keyword matches potential variable

I built an expression library using Eval with about 50 operators (most of them taken from sample code – thanks a lot for that!) and noticed a curious drop in performance when defining a prefix function like not X and then having an expression like nothing == true where nothing could potentially be a variable. The performance drop is quite significant in my case, where by having adding that not prefix function slows down the overall evaluation by a factor of 5-6 compared to just having a ! prefix function.

I wonder if the evaluate method of the interpreter could be improved by introducing at the appropriate place a check that a function name shouldn't be evaluated against a substring. Though that might be done on purpose for some use case. In that case an alternative would be to provide a PatternOptions which allows telling the parser that this specific function keyword can't be a substring.

I can try to do those adjustments, though I'm not sure where to do them in the code. Any hints would be appreciated.

Some sample code which reproduces the slowdown (less than in my big library, but still by a factor of 3): https://gist.github.com/nighthawk/c7daa27285da406e5b2a71f8b789bf3f

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.