API reference
NumExpr.ExprNode
— TypeExprNode
Represents a transitional nested obejct obtained after parsing by parse_expr
function. Used by eval_expr
function for evaluations an expression.
Fields
head::AbstractExpr
: Leading variable or operation of current expression layer.args::Vector{Union{AbstractExpr,ExprNode}}
: Elements of current expression node such as variables or another operations.
NumExpr.Variable
— TypeVariable{S<:AbstractScope}
Represents custom user defined variable in expression after parsing. Type S
must be one of LocalScope
or GlobalScope
.
Full name of the variable can be obtain by empty operator of getting index e.g []
. It may be usefull in some function overloadings.
For more information see section Variables.
Fields
val::String
: Full name of the variable including its tags.name::String
: The variable name without tagstags::Dict{String,String}
: Tags of the variable.
Examples
julia> const vars = Dict{String,Float64}(
"var" => 1,
"var{tag='value'}" => 2,
);
julia> NumExpr.eval_expr(var::NumExpr.Variable) = get(vars, var[], NaN)
julia> var = parse_expr("var{tag='value'}")
var{tag='value'}
julia> typeof(var)
NumExpr.Variable{NumExpr.LocalScope}
julia> var_name(var)
"var"
julia> var_tags(var)
Dict{String, String} with 1 entry:
"tag" => "value"
julia> var[]
"var{tag='value'}"
NumExpr.parse_expr
— Functionparse_expr(str::AbstractString) -> ExprNode
Parse the string expression x
and turn it into nested ExprNode
that can be evaluated by eval_expr
.
For more information see syntax guide.
Examples
julia> colors = Dict{String,UInt32}(
"color[name='red']" => 0xff0000,
"color[name='green']" => 0x00ff00,
"color[name='blue']" => 0x0000ff,
);
julia> NumExpr.eval_expr(var::NumExpr.Variable) = get(colors, var[], NaN)
julia> expr = parse_expr("color[name='red'] + color[name='blue']")
NumExpr.ExprNode(
+,
Union{NumExpr.AbstractExpr, NumExpr.ExprNode}[
color[name='red'],
color[name='blue'],
],
)
NumExpr.eval_expr
— Methodeval_expr(expr::ExprNode)
Evaluate the expression object expr
obtained after parsing by the function parse_expr
.
For more information see syntax guide.
Examples
julia> expr = parse_expr("1 + 2^3 + 4");
julia> eval_expr(expr)
13.0
julia> expr = parse_expr("sin(10)^2 + cos(10)^2");
julia> eval_expr(expr)
1.0
julia> expr = parse_expr("'a' * 'b'");
julia> eval_expr(expr)
"ab"
NumExpr.eval_expr
— Methodeval_expr(x::Variable)
This function defines the behavior when evaluating the values of variables in a parsed expression.
Initially, this function simply returns the value x
, but can be overloaded to define new behavior for retrieving variable data from a new source.
For more information see variables.
Examples
julia> colors = Dict{String,UInt32}(
"color[name='red']" => 0xff0000,
"color[name='green']" => 0x00ff00,
"color[name='blue']" => 0x0000ff,
);
julia> NumExpr.eval_expr(var::NumExpr.Variable) = get(colors, var[], NaN)
julia> expr = parse_expr("color[name='red'] + color[name='blue']");
julia> eval_expr(expr)
0x00ff00ff