Class: Einstein::Expression

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#sexpObject (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_sObject 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