Class: Preval::Visitors::Loops
- Inherits:
-
Preval::Visitor
- Object
- Preval::Visitor
- Preval::Visitors::Loops
- Defined in:
- lib/preval/visitors/loops.rb
Instance Method Summary collapse
Methods inherited from Preval::Visitor
Instance Method Details
#on_for(node) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/preval/visitors/loops.rb', line 6 def on_for(node) ast = Parser.parse(<<~CODE) #{node.source(1)}.each do |#{node.source(0)}| #{node.source(2)} end CODE node.update(:stmts_add, ast[0].body) end |
#on_until(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/preval/visitors/loops.rb', line 46 def on_until(node) # auto replace `until false` with `loop do` if node[0].is?(:var_ref) && # the predicate to the while is a variable reference node[0, 0].is?(:@kw) && # the variable reference is a keyword node[0, 0].body == 'false' # the keyword is "false" ast = Parser.parse(<<~CODE) loop do #{node.source(1)} end CODE node.update(:stmts_add, ast[0].body) end # ignore `until true` if node[0].is?(:var_ref) && # the predicate to the until is a variable reference node[0, 0].is?(:@kw) && # the variable reference is a keyword node[0, 0].body == 'true' # the kwyword is "true" node.update(:void_stmt, []) end end |
#on_while(node) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/preval/visitors/loops.rb', line 16 def on_while(node) # auto replace `while true` with `loop do` if node[0].is?(:var_ref) && # the predicate to the while is a variable reference node[0, 0].is?(:@kw) && # the variable reference is a keyword node[0, 0].body == 'true' # the keyword is "true" ast = Parser.parse(<<~CODE) loop do #{node.source(1)} end CODE node.update(:stmts_add, ast[0].body) end # ignore `while false` if node[0].is?(:var_ref) && # the predicate to the while is a variable reference node[0, 0].is?(:@kw) && # the variable reference is a keyword node[0, 0].body == 'false' # the kwyword is "false" node.update(:void_stmt, []) end end |