class EEEval::CalcFuncParser

Defined in:

eeeval.cr

Class Method Summary

Class Method Detail

def self.build_ast(tokens : Array(Token)) : AST::Node #

build_ast(tokens) — public access to the AST builder. Converts a pre-tokenized Array(Token) into an AST::Node tree.


[View source]
def self.compile(expression : String) : AST::Node #

compile(expression) — parses the expression once and returns the AST. Use this when the same expression will be evaluated many times with different variable values (e.g. CLI range, plotting, benchmarks).

Example: ast = EEEval::CalcFuncParser.compile("sin(x) * phi") (0..100).each do |i| EEEval::CalcFuncParser.evaluate(ast, {"x" => i.to_f}) end


[View source]
def self.evaluate(expression : String, vars : Hash(String, Float64)) : Float64 #

evaluate(expression, vars) — evaluates with user-defined variables. User vars are merged on top of the built-in constants, so user can override a constant if needed (e.g. redefine "pi" for testing).


[View source]
def self.evaluate(ast : AST::Node, vars : Hash(String, Float64)) : Float64 #

evaluate(ast, vars) — evaluates a pre-compiled AST with an environment. Most efficient form: use this inside loops / range evaluations.


[View source]
def self.evaluate(ast : AST::Node, vars : Hash(String, Tensor(Float64, CPU(Float64)))) : Tensor(Float64, CPU(Float64)) #

evaluate(ast, vars) — evaluates a pre-compiled AST with a Tensor environment.


[View source]
def self.evaluate(expression : String) : Float64 #

evaluate(expression) — backward-compatible single-argument form. Built-in constants (pi, e, tau, sqrt2, phi) are always available.


[View source]
def self.tokenize(expression : String) : Array(Token) #

tokenize(expression) — public access to the tokenizer. Returns the flat Array(Token) produced by lexical analysis. Useful for debugging or building custom evaluators.


[View source]