Class: Logicality::Interpreter::SimpleInterpreter
- Inherits:
-
NodeVisitor
- Object
- NodeVisitor
- Logicality::Interpreter::SimpleInterpreter
- Defined in:
- lib/logicality/interpreter/simple_interpreter.rb
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.
14 15 16 17 18 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 14 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.
12 13 14 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 12 def resolver @resolver end |
Instance Method Details
#error(node) ⇒ Object
20 21 22 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 20 def error(node) raise ArgumentError, "Visitor cant process node token type: #{node.token.type}" end |
#visit_binary_operator_node(node) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 24 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
34 35 36 37 38 39 40 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 34 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
42 43 44 45 46 47 48 49 50 |
# File 'lib/logicality/interpreter/simple_interpreter.rb', line 42 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 |