easy_expression_eval v1.0.0
Easy Expression Eval (eeeval)
eeeval is a lightweight and efficient expression evaluator for Crystal. It supports mathematical calculations, user-defined variables, pre-compiled ASTs for performance, and conditional expressions.
Installation
-
Add the dependency to your
shard.yml:dependencies: eeeval: github: eltony81/easy_expression_eval -
Run
shards install
Features
Mathematical Evaluation
You can evaluate complex mathematical expressions containing numbers, functions, and operators:
require "eeeval"
# Simple evaluation
result = EEEval::CalcFuncParser.evaluate("sin(pi/2) + e")
puts result # => 3.718281828459045
Variables Support (Native & Efficient)
Pass a hash of variables to the evaluator. No string replacement is performed; variables are resolved during AST evaluation:
require "eeeval"
vars = {"x" => 3.0, "y" => 1.5}
result = EEEval::CalcFuncParser.evaluate("x^2 + sin(y)", vars)
puts result
Pre-compilation (AST)
For performance-critical code (such as evaluations inside loops), compile the expression once into an AST, then evaluate it repeatedly with different variables:
require "eeeval"
# Compile the expression once
ast = EEEval::CalcFuncParser.compile("sin(x) * phi")
# Evaluate multiple times without re-parsing
(0..100).each do |i|
res = EEEval::CalcFuncParser.evaluate(ast, {"x" => i.to_f64})
puts "f(#{i}) = #{res}"
end
Conditional Expressions
The library includes a CondParser for boolean logic:
require "eeeval"
# Numeric comparisons
EEEval::CondParser.evaluate("10 == 10") # => true
EEEval::CondParser.evaluate("5 != 3") # => true
# String comparisons
EEEval::CondParser.evaluate("'hello' == 'hello'") # => true
# Logical operators
EEEval::CondParser.evaluate("(1 == 1) && (2 != 3)") # => true
EEEval::CondParser.evaluate("1 == 0 || 1 == 1") # => true
Built-in Support
- Constants:
pi,e,tau,sqrt2,phi(available inside environments by default). - Functions:
sin,cos,tan,asin,acos,atan,log,log2,log10,exp,exp2,sqrt,abs. - Operators:
+,-,*,/,^(power).
CLI Usage
The library includes a CLI tool for quick evaluations and range calculations.
Parameters:
-v, --var VAR: Specify the variable name to use in the expression (e.g.,x,t).-s, --start VAL: Set the starting value for the range evaluation.-e, --end VAL: Set the ending value for the range evaluation.-d, --step VAL: Set the increment step size (defaults to1.0).
Examples:
Single expression evaluation:
crystal run src/cli.cr -- "sin(pi/2) + e"
Range evaluation (vector calculation): This will evaluate the expression for x from 0 to 10 with a step of 0.5, producing a vector of results.
crystal run src/cli.cr -- -v x -s 0 -e 10 -d 0.5 "x^2 + sin(x)"
Contributing
- Fork it (https://github.com/eltony81/easy_expression_eval/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- eltony81 - creator and maintainer
easy_expression_eval
- 2
- 0
- 0
- 0
- 1
- 10 days ago
- November 28, 2022
MIT License
Tue, 02 Jun 2026 06:43:18 GMT