Module: FastRuby::BlockTranslator

Defined in:
lib/fastruby/translator/modules/block.rb,
lib/fastruby/translator/modules/method_group.rb

Instance Method Summary collapse

Instance Method Details

#to_c_block(tree, result_variable = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fastruby/translator/modules/block.rb', line 97

def to_c_block(tree, result_variable = nil)
  if tree.size == 1
    return inline_block("return Qnil;")
  end
  
  str = ""
  str = tree[1..-2].map{ |subtree|
    to_c(subtree,"last_expression")
  }.join(";")

  if tree[-1]
      str = str + ";#{to_c(tree[-1],"last_expression")};"
  end

  if result_variable
    str << "#{result_variable} = last_expression;"
    str
  else
    str << "return last_expression;"
    inline_block str
  end
end

#to_c_class(tree, result_var = nil) ⇒ Object



25
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
55
56
57
58
59
60
61
# File 'lib/fastruby/translator/modules/method_group.rb', line 25

def to_c_class(tree, result_var = nil)
  str_class_name = get_class_name(tree[1])
  container_tree = get_container_tree(tree[1])

  if container_tree == s(:self)
    method_group("
                VALUE superklass = rb_cObject;
                #{
                if tree[2] 
                  to_c tree[2], "superklass"
                end
                };
                VALUE tmpklass = rb_define_class(
                  #{str_class_name.inspect},
                  superklass
              );
    ", tree[3], result_var)
  else
    method_group("
                VALUE container_klass = Qnil;
                VALUE superklass = rb_cObject;
                
                #{
                if tree[2] 
                  to_c tree[2], "superklass"
                end
                };
                
                #{to_c(container_tree, "container_klass")};
                VALUE tmpklass = rb_define_class_under(
                  container_klass,
                  #{str_class_name.inspect},
                  superklass
              );
    ", tree[3], result_var)
  end
end

#to_c_module(tree, result_var = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fastruby/translator/modules/method_group.rb', line 63

def to_c_module(tree, result_var = nil)
  str_class_name = get_class_name(tree[1])
  container_tree = get_container_tree(tree[1])

  if container_tree == s(:self)
    method_group("
                  VALUE tmpklass = rb_define_module(#{str_class_name.inspect});
    ", tree[2], result_var)
  else
    method_group("
                  VALUE container_klass = Qnil;
                  
                  #{to_c(container_tree, "container_klass")};
                  VALUE tmpklass = rb_define_module_under(container_klass,#{str_class_name.inspect});
    ", tree[2], result_var)
  end
end

#to_c_yield(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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fastruby/translator/modules/block.rb', line 26

def to_c_yield(tree)

  block_code = proc { |name| "
    static VALUE #{name}(VALUE frame_param, VALUE* block_args, int size) {

      #{@locals_struct} *plocals;
      #{@frame_struct} *pframe;
      pframe = (void*)frame_param;
      plocals = (void*)pframe->plocals;

      if ((plocals->block_function_address) == 0) {
        #{_raise("rb_eLocalJumpError", "no block given")};
      } else {
        return ((VALUE(*)(int,VALUE*,VALUE,VALUE))(plocals->block_function_address))(size, block_args, (VALUE)(plocals->block_function_param), (VALUE)pframe);
      }
    }
  "
  }

  splat_arg = tree.find{|x| x == :yield ? false : x[0] == :splat}
  ret = nil      
  if splat_arg
    ret = "
      VALUE splat_array = Qnil;
      VALUE block_aux = Qnil;
       #{to_c(splat_arg[1], "splat_array")};
      
      if (CLASS_OF(splat_array) == rb_cArray) {
        VALUE block_args[_RARRAY_LEN(splat_array) + #{tree.size}];
        int i;
        #{ 
          (0..tree.size-3).map{|i|
            "
            #{to_c(tree[i+1], "block_aux")};
            block_args[#{i}] = block_aux;
            "
          }.join(";\n")
        };
        
        for (i=0; i<_RARRAY_LEN(splat_array); i++) {
          block_args[i+#{tree.size-2}] = rb_ary_entry(splat_array,i);
        }
        
        last_expression = #{anonymous_function(&block_code)}((VALUE)pframe, block_args, _RARRAY_LEN(splat_array) + #{tree.size-2});
      } else {
        VALUE block_args[1+#{tree.size}];
        #{ 
          (0..tree.size-3).map{|i|
            "
            #{to_c(tree[i+1], "block_aux")};
            block_args[#{i}] = block_aux;
            "
          }.join(";\n")
        };
        
        block_args[#{tree.size-2}] = splat_array;
        last_expression = #{anonymous_function(&block_code)}((VALUE)pframe, block_args, #{tree.size-1});
      }
      
    "
  else
    ret = if tree.size > 1
        "last_expression = " + anonymous_function(&block_code)+"((VALUE)pframe, (VALUE[]){#{tree[1..-1].map{|subtree| to_c subtree}.join(",")}},#{tree.size-1})"
      else
        "last_expression = " + anonymous_function(&block_code)+"((VALUE)pframe, (VALUE[]){}, #{tree.size-1})"
      end
  end
  
  protected_block(ret, false)
end