Class: Ifdef::LogicProcessor
- Inherits:
-
Parser::AST::Processor
- Object
- Parser::AST::Processor
- Ifdef::LogicProcessor
- Defined in:
- lib/ifdef/logic_processor.rb
Defined Under Namespace
Classes: LogicError
Instance Method Summary collapse
-
#initialize(truth) ⇒ LogicProcessor
constructor
A new instance of LogicProcessor.
- #node?(object) ⇒ Boolean
-
#on_and(node) ⇒ Object
Handle the ‘&&` statement.
-
#on_begin(node) ⇒ Object
Handle logic statements explicitly wrapped in parenthesis.
-
#on_false(node) ⇒ Object
Handle the ‘false` statement.
-
#on_nil(node) ⇒ Object
Handle the ‘nil` statement.
-
#on_or(node) ⇒ Object
Handle the ‘||` statement.
-
#on_send(node) ⇒ Object
Handles the ‘!` statement.
-
#on_true(node) ⇒ Object
Handle the ‘true` statement.
-
#result(ast) ⇒ Object
Evaluate a logical statement and reduce it to true/false if we know enough to do so.
Constructor Details
#initialize(truth) ⇒ LogicProcessor
Returns a new instance of LogicProcessor.
5 6 7 |
# File 'lib/ifdef/logic_processor.rb', line 5 def initialize(truth) @truth = truth end |
Instance Method Details
#node?(object) ⇒ Boolean
134 135 136 |
# File 'lib/ifdef/logic_processor.rb', line 134 def node?(object) object.is_a?(Parser::AST::Node) end |
#on_and(node) ⇒ Object
Handle the ‘&&` statement.
node - the node to evaluate.
Returns :true if both sides are known to be true, :false if either sides is known to be false, and nil otherwise.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ifdef/logic_processor.rb', line 51 def on_and(node) a, b = node.children.map { |c| @truth.fetch(c, process(c)) } if a == :true && b == :true :true elsif a == :false || b == :false :false else nil end end |
#on_begin(node) ⇒ Object
Handle logic statements explicitly wrapped in parenthesis.
node - the node to evaluate.
Returns the result of the statement within the parenthesis.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ifdef/logic_processor.rb', line 91 def on_begin(node) child, other_children = *node.children # Not sure if this can happen in an `if` statement raise LogicError if other_children case @truth.fetch(child, process(child)) when :true :true when :false :false else nil end end |
#on_false(node) ⇒ Object
Handle the ‘false` statement.
node - the node to evaluate.
Returns :false
121 122 123 |
# File 'lib/ifdef/logic_processor.rb', line 121 def on_false(node) :false end |
#on_nil(node) ⇒ Object
Handle the ‘nil` statement.
node - the node to evaluate.
Returns :false
130 131 132 |
# File 'lib/ifdef/logic_processor.rb', line 130 def on_nil(node) :false end |
#on_or(node) ⇒ Object
Handle the ‘||` statement.
node - the node to evaluate.
Returns :true if either side is known to be true, :false if both sides are known to be false, and nil otherwise.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ifdef/logic_processor.rb', line 33 def on_or(node) a, b = node.children.map { |c| @truth.fetch(c, process(c)) } if a == :true || b == :true :true elsif a == :false && b == :false :false else nil end end |
#on_send(node) ⇒ Object
Handles the ‘!` statement.
node - the node to evaluate.
Returns the inverse of the child expression.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ifdef/logic_processor.rb', line 69 def on_send(node) _target, _method, _args = node.children if _method == :! case @truth.fetch(_target, process(_target)) when :true :false when :false :true else nil end else nil end end |
#on_true(node) ⇒ Object
Handle the ‘true` statement.
node - the node to evaluate.
Returns :true
112 113 114 |
# File 'lib/ifdef/logic_processor.rb', line 112 def on_true(node) :true end |
#result(ast) ⇒ Object
Evaluate a logical statement and reduce it to true/false if we know enough to do so.
ast - the AST to evaluate.
Returns true if we know the statement to be :true, :false if we know the statement to be false, and nil otherwise.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ifdef/logic_processor.rb', line 16 def result(ast) case @truth.fetch(ast, process(ast)) when :true :true when :false :false else nil end end |