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(" \#{node.source(1)}.each do |\#{node.source(0)}|\n \#{node.source(2)}\n end\n CODE\n\n node.update(:stmts_add, ast[0].body)\nend\n") |
#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(" loop do\n \#{node.source(1)}\n end\n CODE\n\n node.update(:stmts_add, ast[0].body)\n end\n\n # ignore `until true`\n if node[0].is?(:var_ref) &&\n # the predicate to the until is a variable reference\n node[0, 0].is?(:@kw) &&\n # the variable reference is a keyword\n node[0, 0].body == 'true'\n # the kwyword is \"true\"\n\n node.update(:void_stmt, [])\n end\nend\n") |
#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(" loop do\n \#{node.source(1)}\n end\n CODE\n\n node.update(:stmts_add, ast[0].body)\n end\n\n # ignore `while false`\n if node[0].is?(:var_ref) &&\n # the predicate to the while is a variable reference\n node[0, 0].is?(:@kw) &&\n # the variable reference is a keyword\n node[0, 0].body == 'false'\n # the kwyword is \"false\"\n\n node.update(:void_stmt, [])\n end\nend\n") |