Easily evaluate simple expressions on the go...
This is a port of the BigEval.js/Eval.net library
Features:
5 * (4 / 3)
)x * 27 / 4
)30 * pow(24, 6) / cos(20)
)26 * 3 < 100
- returns a bool
value)(1 << 2) == 4
)"test" + 5 == "test5"
)Just add the repo in Swift Package Manager
import Eval
let config = DoubleEvalConfiguration()
let result1 = try Evaluator.execute("12+45*10", config) as? Double
let result2 = try Evaluator.execute("30 * pow(24, 6) / cos(20)", config) as? Double
let compiled = try Evaluator.compile("5 * n", config.clone())
compiled.setConstant(8, forName: "n")
let result3 = try compiled.execute() as? Double
compiled.setConstant(9, forName: "n")
let result4 = try compiled.execute() as? Double
The operators currently supported in order of precedence are -
[
['!'], // Factorial
['**'], // power
['/', '*', '%'],
['+', '-'],
['<<', '>>'], // bit shifts
['<', '<=', '>', '>='],
['==', '=', '!='], // equality comparisons
['&'], ['^'], ['|'], // bitwise operations
['&&'], ['||'] // logical operations
]
If you want to buy me a beer, you are very welcome to
Thanks :-)
This library is under the Apache License 2.0.
This library is free and can be used in commercial applications without royalty.
link |
Stars: 3 |
Last commit: 2 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics