Class: Einstein::Expression
- Inherits:
-
Object
- Object
- Einstein::Expression
- Defined in:
- lib/einstein/expression.rb
Overview
Node representing an entire Einstein statement. This is the root-level node of the parser. This node’s exp
is the tree of expressions that make up a single logical statement.
Instance Attribute Summary collapse
-
#sexp ⇒ Object
(also: #to_sexp)
readonly
The s-expression given to this Expression’s constructor.
Instance Method Summary collapse
-
#evaluate(scope = {}) ⇒ Object
Evaluate this node against the given
scope
. -
#initialize(sexp) ⇒ Expression
constructor
Initialize a new expression wrapping the given
sexp
, which is expected to be an s-expression represented as a set of nested arrays. -
#to_s ⇒ Object
(also: #inspect)
Performs a “pretty print” of this expression.
Constructor Details
#initialize(sexp) ⇒ Expression
Initialize a new expression wrapping the given sexp
, which is expected to be an s-expression represented as a set of nested arrays.
Example:
Expression.new([:lit, 10]) # => 10
Expression.new([:multiply, [:lit, 10], [:lit, 5]]) # => (10 * 5)
15 16 17 |
# File 'lib/einstein/expression.rb', line 15 def initialize(sexp) @sexp = sexp end |
Instance Attribute Details
#sexp ⇒ Object (readonly) Also known as: to_sexp
The s-expression given to this Expression’s constructor.
20 21 22 |
# File 'lib/einstein/expression.rb', line 20 def sexp @sexp end |
Instance Method Details
#evaluate(scope = {}) ⇒ Object
Evaluate this node against the given scope
. Returns a numeric value calculated by walking the AST with an instance of EvaluateProcessor.
25 26 27 |
# File 'lib/einstein/expression.rb', line 25 def evaluate(scope = {}) Evaluator.new(scope).process(to_sexp) end |
#to_s ⇒ Object Also known as: inspect
Performs a “pretty print” of this expression.
30 31 32 |
# File 'lib/einstein/expression.rb', line 30 def to_s PrettyPrinter.new.process(to_sexp) end |