Class: Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/quecto_calc/evaluator.rb

Overview

Evaluates expression from the abstract syntax tree.

Instance Method Summary collapse

Constructor Details

#initialize(consts = {}) ⇒ Evaluator

Initialize an evaluator instance.

Parameters:

  • consts (Hash{ String => Numeric }) (defaults to: {})

    List of constants and their values to put in the expression.



15
16
17
# File 'lib/quecto_calc/evaluator.rb', line 15

def initialize(consts = {})
  @consts = consts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(_method_name, node) ⇒ Object

Parameters:

  • _method_name (String)
  • node (Object)

Raises:

  • (NoMethodError)


34
35
36
37
# File 'lib/quecto_calc/evaluator.rb', line 34

def method_missing(_method_name, node)
  error_msg = "unsupported node type: #{node.class}. Plase check parser's output for correct node types."
  raise NoMethodError, error_msg
end

Instance Method Details

#respond_to_missing?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/quecto_calc/evaluator.rb', line 39

def respond_to_missing?(*)
  true
end

#visit(node) ⇒ Object

Parameters:

  • node (QuectoParser::BinOpNode, QuectoParser::NumberNode)


22
23
24
25
# File 'lib/quecto_calc/evaluator.rb', line 22

def visit(node)
  method_name = "_visit_#{node.class}"
  send(method_name, node)
end