Module: FastRuby::FlowControlTranslator

Defined in:
lib/fastruby/translator/modules/flow.rb

Instance Method Summary collapse

Instance Method Details

#to_c_case(tree) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fastruby/translator/modules/flow.rb', line 26

def to_c_case(tree)

  tmpvarname = "tmp" + rand(1000000).to_s;

  code = tree[2..-2].map{|subtree|

    # this subtree is a when
    subtree[1][1..-1].map{|subsubtree|
      c_calltree = s(:call, nil, :inline_c, s(:arglist, s(:str, tmpvarname), s(:false)))
      calltree = s(:call, subsubtree, :===, s(:arglist, c_calltree))
          "
            if (RTEST(#{to_c_call(calltree, tmpvarname)})) {
               return #{to_c(subtree[2])};
            }

          "
    }.join("\n")

  }.join("\n")

  inline_block "

    VALUE #{tmpvarname} = #{to_c tree[1]};

    #{code};

    return #{to_c tree[-1]};
  "
end

#to_c_for(tree) ⇒ Object



75
76
77
78
79
80
# File 'lib/fastruby/translator/modules/flow.rb', line 75

def to_c_for(tree)
  alter_tree = tree.dup
  alter_tree[0] = :iter
  alter_tree[1] = [:call, alter_tree[1], :each, [:arglist]]
  to_c alter_tree
end

#to_c_if(tree) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fastruby/translator/modules/flow.rb', line 56

def to_c_if(tree)
  condition_tree = tree[1]
  impl_tree = tree[2]
  else_tree = tree[3]

  inline_block "
      if (RTEST(#{to_c condition_tree})) {
        last_expression = #{to_c impl_tree};
      }#{else_tree ?
        " else {
        last_expression = #{to_c else_tree};
        }
        " : ""
      }

      return last_expression;
  "
end

#to_c_while(tree) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/fastruby/translator/modules/flow.rb', line 82

def to_c_while(tree)
  inline_block("
      while (#{to_c tree[1]}) {
        #{to_c tree[2]};
      }
      return Qnil;
  ")
end