Class: Logicality::Interpreter::SimpleInterpreter
- Inherits:
-
NodeVisitor
- Object
- NodeVisitor
- Logicality::Interpreter::SimpleInterpreter
- Defined in:
- lib/logicality/interpreter/simple_interpreter.rb
Overview
This class implements NodeVisitor and gives each type of node the proper type of visitor implementation.
Instance Attribute Summary collapse
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
Instance Method Summary collapse
- #error(node) ⇒ Object
-
#initialize(resolver) ⇒ SimpleInterpreter
constructor
A new instance of SimpleInterpreter.
- #visit_binary_operator_node(node) ⇒ Object
- #visit_unary_operator_node(node) ⇒ Object
- #visit_value_operand_node(node) ⇒ Object
Methods inherited from NodeVisitor
Constructor Details
#initialize(resolver) ⇒ SimpleInterpreter
Returns a new instance of SimpleInterpreter.
17 18 19 20 21 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 17 def initialize(resolver) raise ArgumentError, 'Resolver is required' unless resolver @resolver = resolver end |
Instance Attribute Details
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
15 16 17 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 15 def resolver @resolver end |
Instance Method Details
#error(node) ⇒ Object
23 24 25 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 23 def error(node) raise ArgumentError, "Visitor cant process node token type: #{node.token.type}" end |
#visit_binary_operator_node(node) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 27 def visit_binary_operator_node(node) if node.token.type == Lexer::Token::Type::AND_OP visit(node.left) && visit(node.right) elsif node.token.type == Lexer::Token::Type::OR_OP visit(node.left) || visit(node.right) else error(node) end end |
#visit_unary_operator_node(node) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 37 def visit_unary_operator_node(node) if node.token.type == Lexer::Token::Type::NOT_OP !visit(node.child) else error(node) end end |
#visit_value_operand_node(node) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 45 def visit_value_operand_node(node) if node.value == 'true' true elsif node.value == 'false' || node.value == 'null' false else resolve_value(node.value) end end |