CodeTree - Evaluation

synopsis

Expressions parsed using CodeTree::parse are functional expressions. However, there is two ways to make their evaluation in Ruby: the object-way and the functional-way. Both are proposed as methods installed on AstNode.

object evaluation

In object evaluation (object_eval method, aliased as eval on AstNode), the expression

(function arg0, arg1, ..., argn)

is recursively executed as

arg0.function(arg1, ..., argn)

For instance,

expr = CodeTree::parse{ (x + y).to_s }   
# => (to_s (+ x, y))

expr.eval(:x => 3, :y => 25)             
# => "28", executed as (x.+(y)).to_s()

functional evaluation

In functional evaluation (functional_eval method, aliased as apply on AstNode), the expression

(function arg0, arg1, ..., argn)

is recursively executed in the context of a receiver object as

receiver.function(arg0, arg1, ..., argn)

For instance,

expr = CodeTree::parse{ (display (concat x, y)) }    
# => (display (concat x, y))

expr.apply(receiver, :x => 3, :y => 25)              
# => "325", executed as receiver.display(receiver.concat(x, y))