Class: Einstein::Evaluator
- Defined in:
- lib/einstein/evaluator.rb
Overview
This processor walks the AST and evaluates the values of the nodes.
Instance Method Summary collapse
-
#initialize(scope = {}) ⇒ Evaluator
constructor
Initialize a new instance of this processor with the given
scope
, as a hash. -
#process_add(exp) ⇒ Object
Adds the left and right values of
exp
. -
#process_bitwise_and(exp) ⇒ Object
Performs a bitwise AND with the left and right values of
exp
. -
#process_bitwise_not(exp) ⇒ Object
Performs a bitwise NOT operation on the value of
exp
. -
#process_bitwise_or(exp) ⇒ Object
Performs a bitwise OR with the left and right values of
exp
. -
#process_bitwise_xor(exp) ⇒ Object
Performs a bitwise XOR with the left and right values of
exp
. -
#process_divide(exp) ⇒ Object
Divides the left value of
exp
by the right value ofexp
. -
#process_exponent(exp) ⇒ Object
Raises the left value of
exp
by the right value ofexp
. -
#process_lit(exp) ⇒ Object
Returns the value of
exp
. -
#process_lshift(exp) ⇒ Object
Performs a bitwise left shift of the left value of
exp
by the number of bits specified in the right value ofexp
. -
#process_modulus(exp) ⇒ Object
Performs a modulus operation for the left and right values of
exp
. -
#process_multiply(exp) ⇒ Object
Multiplies the left and right values of
exp
. -
#process_resolve(exp) ⇒ Object
Performs a lookup for the value of
exp
inside this processor’s scope. -
#process_rshift(exp) ⇒ Object
Performs a bitwise right shift of the left value of
exp
by the number of bits specified in the right value ofexp
. -
#process_subtract(exp) ⇒ Object
Subtracts the right value of
exp
by the left value ofexp
. -
#process_unary_minus(exp) ⇒ Object
Returns the negated value of
exp
. -
#process_unary_plus(exp) ⇒ Object
Returns the value of
exp
.
Methods inherited from Processor
Constructor Details
#initialize(scope = {}) ⇒ Evaluator
Initialize a new instance of this processor with the given scope
, as a hash. This scope
should provide a mapping of variable names to values.
9 10 11 12 13 14 15 |
# File 'lib/einstein/evaluator.rb', line 9 def initialize(scope = {}) # Convert symbol keys into strings. @scope = scope.inject({}) do |hash, (key, value)| hash[key.to_s] = value hash end end |
Instance Method Details
#process_add(exp) ⇒ Object
Adds the left and right values of exp
.
59 60 61 |
# File 'lib/einstein/evaluator.rb', line 59 def process_add(exp) process(exp[1]) + process(exp[2]) end |
#process_bitwise_and(exp) ⇒ Object
Performs a bitwise AND with the left and right values of exp
.
81 82 83 |
# File 'lib/einstein/evaluator.rb', line 81 def process_bitwise_and(exp) process(exp[1]) & process(exp[2]) end |
#process_bitwise_not(exp) ⇒ Object
Performs a bitwise NOT operation on the value of exp
.
33 34 35 |
# File 'lib/einstein/evaluator.rb', line 33 def process_bitwise_not(exp) ~process(exp[1]) end |
#process_bitwise_or(exp) ⇒ Object
Performs a bitwise OR with the left and right values of exp
.
91 92 93 |
# File 'lib/einstein/evaluator.rb', line 91 def process_bitwise_or(exp) process(exp[1]) | process(exp[2]) end |
#process_bitwise_xor(exp) ⇒ Object
Performs a bitwise XOR with the left and right values of exp
.
86 87 88 |
# File 'lib/einstein/evaluator.rb', line 86 def process_bitwise_xor(exp) process(exp[1]) ^ process(exp[2]) end |
#process_divide(exp) ⇒ Object
Divides the left value of exp
by the right value of exp
. Will raise ZeroDivisionError if the right value of exp
evaluates to 0.
49 50 51 |
# File 'lib/einstein/evaluator.rb', line 49 def process_divide(exp) process(exp[1]) / process(exp[2]) end |
#process_exponent(exp) ⇒ Object
Raises the left value of exp
by the right value of exp
.
38 39 40 |
# File 'lib/einstein/evaluator.rb', line 38 def process_exponent(exp) process(exp[1]) ** process(exp[2]) end |
#process_lit(exp) ⇒ Object
Returns the value of exp
.
18 19 20 |
# File 'lib/einstein/evaluator.rb', line 18 def process_lit(exp) exp[1] end |
#process_lshift(exp) ⇒ Object
Performs a bitwise left shift of the left value of exp
by the number of bits specified in the right value of exp
.
70 71 72 |
# File 'lib/einstein/evaluator.rb', line 70 def process_lshift(exp) process(exp[1]) << process(exp[2]) end |
#process_modulus(exp) ⇒ Object
Performs a modulus operation for the left and right values of exp
.
54 55 56 |
# File 'lib/einstein/evaluator.rb', line 54 def process_modulus(exp) process(exp[1]) % process(exp[2]) end |
#process_multiply(exp) ⇒ Object
Multiplies the left and right values of exp
.
43 44 45 |
# File 'lib/einstein/evaluator.rb', line 43 def process_multiply(exp) process(exp[1]) * process(exp[2]) end |
#process_resolve(exp) ⇒ Object
Performs a lookup for the value of exp
inside this processor’s scope. Raises ResolveError if the variable is not in scope.
97 98 99 100 |
# File 'lib/einstein/evaluator.rb', line 97 def process_resolve(exp) raise ResolveError, "Undefined variable: #{exp[1]}" unless @scope.has_key?(exp[1]) @scope[exp[1]] end |
#process_rshift(exp) ⇒ Object
Performs a bitwise right shift of the left value of exp
by the number of bits specified in the right value of exp
.
76 77 78 |
# File 'lib/einstein/evaluator.rb', line 76 def process_rshift(exp) process(exp[1]) >> process(exp[2]) end |
#process_subtract(exp) ⇒ Object
Subtracts the right value of exp
by the left value of exp
.
64 65 66 |
# File 'lib/einstein/evaluator.rb', line 64 def process_subtract(exp) process(exp[1]) - process(exp[2]) end |
#process_unary_minus(exp) ⇒ Object
Returns the negated value of exp
.
28 29 30 |
# File 'lib/einstein/evaluator.rb', line 28 def process_unary_minus(exp) -process(exp[1]) end |
#process_unary_plus(exp) ⇒ Object
Returns the value of exp
.
23 24 25 |
# File 'lib/einstein/evaluator.rb', line 23 def process_unary_plus(exp) process(exp[1]) end |