Giter VIP home page Giter VIP logo

keval's Introduction

keval's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

keval's Issues

Incorrect behavior when mixing functions and operators

Hi!
I have the following code snippet:

val evaluator = Keval {
  includeDefault()
  function {
    name = "if"
    arity = 3
    implementation = { args -> if (args[2]==1.0) args[1] else args[0] }
  }
  println(evaluator.eval("if((1*1),4,5))
  println(evaluator.eval("if(1*1,4,5))
}

The first expression evaluates to 4 (as expected), the second one evaluates to 1 (not expected).
Do you agree that this is a bug?

Another thought: the argument order confused me a bit, I was expecting args[0] to be the leftmost argument of the function, but it is the other way around..

No support for negative numbers

Running keval() on single negative number results in exception KevalInvalidExpressionException

The neg() function seems to work in those situations but it would be nice to have support for negation via minus operator.

Tested on: Android
keval version: 0.9.0
minimal reproducible example: "-1".keval()

Can not recognize "log10" as a token.

When using log10(1.6) as the expression, the tokens will be like this:
Screenshot 2024-03-24 at 10 49 01

Seems the regex is not correct, that splits the token into log and 10.

And I can not use log(1.6) as the expression, because the default operators don't contain log.
Screenshot 2024-03-24 at 10 51 37

So could you please either change DEFAULT_RESOURCES to log or modify the regex to match log10?

Unary minus operator (negation)

Hello! So I use ur library in one of my app cuz its very cool and simple, are you still working on negative numbers within the .keval() method ?

String tokenizer doesn't handle appearance of subnames

If we have 2 operators/functions such that the name of one of them is subset of the other, it can lead to both false exceptions, and unexpected behavior

false exception

val resource = Keval {
    function {
        name = "a"
        arity = 1
        implementation = { it[0] }
    }

    function {
        name = "ab"
        arity = 1
        implementation = { it[0] + 1 }
    }
}

assertFailsWith(KevalInvalidOperatorException::class) {
    resource.eval("ab(1)")
}

The expected result is 2

unexpected behavior

val eval = Keval {
    operator {
        symbol = "a"
        precedence = 1
        isLeftAssociative = false
        implementation = { a, b -> a - b }
    }

    function {
        name = "a1"
        arity = 1
        implementation = { it[0] + 1 }
    }
}.eval("1a1(1)")

assertNotEquals(1.0*(1.0+1.0), eval)
assertEquals((1.0-1.0)*(1.0), eval)

The expected result is either 1*(1+1) or an exception

Cannot overwrite operators

When try to add a new binary operator, an exception throws like:
"java.lang.ClassCastException: class com.notkamui.keval.KevalBinaryOperator cannot be cast to class com.notkamui.keval.KevalUnaryOperator."

The bug is that in KevalBuilder.addOperator, all the convertions are wrong:

    private fun addOperator(symbol: Char, operator: KevalOperator, isUnary: Boolean) {
        when (val resource = resources[symbol.toString()]) {
            is KevalUnaryOperator -> resources[symbol.toString()] =
                KevalBothOperator(operator as KevalBinaryOperator, resource)     // should be KevalUnaryOperator

            is KevalBinaryOperator -> resources[symbol.toString()] =
                KevalBothOperator(resource, operator as KevalUnaryOperator)      // should be KevalBinaryOperator

            is KevalBothOperator -> resources[symbol.toString()] =
                if (isUnary) KevalBothOperator(operator as KevalBinaryOperator, resource.unary)   // should be KevelUnaryOperator
                else KevalBothOperator(resource.binary, operator as KevalUnaryOperator)    // should be KevalBinaryOperator

            else -> resources[symbol.toString()] = operator
        }
    }

Could you correct this and publish a new version?

BTW, this is a really nice repository, saves my day when creating a calculator app~

NoSuchElementException when evaluating empty expression

Hi,

When passing an empty string to eval(), a NoSuchElementException is thrown by Keval.

Here is complete stack trace :

Caused by: java.util.NoSuchElementException: List is empty.
    at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:386)
    at com.notkamui.keval.ShuntingYardKt.toAbstractSyntaxTree(ShuntingYard.kt:168)
    at com.notkamui.keval.Keval.eval(Keval.kt:120)

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.