Module: FastRuby::VariabeTranslator

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

Instance Method Summary collapse

Instance Method Details

#to_c_cdecl(tree) ⇒ Object



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
# File 'lib/fastruby/translator/modules/variable.rb', line 58

def to_c_cdecl(tree)
  if tree[1].instance_of? Symbol
    inline_block "
      // set constant #{tree[1].to_s}
      VALUE val = #{to_c tree[2]};
      rb_const_set(rb_cObject, #{intern_num tree[1]}, val);
      return val;
      "
  elsif tree[1].instance_of? FastRuby::FastRubySexp

    if tree[1].node_type == :colon2
      inline_block "
        // set constant #{tree[1].to_s}
        VALUE val = #{to_c tree[2]};
        VALUE klass = #{to_c tree[1][1]};
        rb_const_set(klass, #{intern_num tree[1][2]}, val);
        return val;
        "
    elsif tree[1].node_type == :colon3
      inline_block "
        // set constant #{tree[1].to_s}
        VALUE val = #{to_c tree[2]};
        rb_const_set(rb_cObject, #{intern_num tree[1][1]}, val);
        return val;
        "
    end
  end
end

#to_c_colon2(tree) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fastruby/translator/modules/variable.rb', line 91

def to_c_colon2(tree)
  inline_block "
    VALUE klass = #{to_c tree[1]};

  if (rb_is_const_id(#{intern_num tree[2]})) {
    switch (TYPE(klass)) {
      case T_CLASS:
      case T_MODULE:
        return rb_const_get_from(klass, #{intern_num tree[2]});
        break;
      default:
        #{_raise("rb_eTypeError","not a class/module")};
        break;
    }
  }
  else {
    return rb_funcall(klass, #{intern_num tree[2]}, 0, 0);
  }

    return Qnil;
  "
end

#to_c_colon3(tree) ⇒ Object



87
88
89
# File 'lib/fastruby/translator/modules/variable.rb', line 87

def to_c_colon3(tree)
  "rb_const_get_from(rb_cObject, #{intern_num tree[1]})"
end

#to_c_const(tree) ⇒ Object



54
55
56
# File 'lib/fastruby/translator/modules/variable.rb', line 54

def to_c_const(tree)
  "rb_const_get(CLASS_OF(plocals->self), #{intern_num(tree[1])})"
end

#to_c_cvar(tree) ⇒ Object



26
27
28
# File 'lib/fastruby/translator/modules/variable.rb', line 26

def to_c_cvar(tree)
  "rb_cvar_get(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]})"
end

#to_c_cvasgn(tree) ⇒ Object



30
31
32
# File 'lib/fastruby/translator/modules/variable.rb', line 30

def to_c_cvasgn(tree)
  "__rb_cvar_set(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]},#{to_c tree[2]},Qfalse)"
end

#to_c_defined(tree) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fastruby/translator/modules/variable.rb', line 144

def to_c_defined(tree)
  nt = tree[1].node_type

  if nt == :self
  'rb_str_new2("self")'
  elsif nt == :true
  'rb_str_new2("true")'
  elsif nt == :false
  'rb_str_new2("false")'
  elsif nt == :nil
  'rb_str_new2("nil")'
  elsif nt == :lvar
  'rb_str_new2("local-variable")'
  elsif nt == :gvar
  "rb_gvar_defined((struct global_entry*)#{global_entry(tree[1][1])}) ? #{literal_value "global-variable"} : Qnil"
  elsif nt == :const
  "rb_const_defined(rb_cObject, #{intern_num tree[1][1]}) ? #{literal_value "constant"} : Qnil"
  elsif nt == :call
  "rb_method_node(CLASS_OF(#{to_c tree[1][1]}), #{intern_num tree[1][2]}) ? #{literal_value "method"} : Qnil"
  elsif nt == :yield
    "rb_block_given_p() ? #{literal_value "yield"} : Qnil"
  elsif nt == :ivar
  "rb_ivar_defined(plocals->self,#{intern_num tree[1][1]}) ? #{literal_value "instance-variable"} : Qnil"
  elsif nt == :attrset or
        nt == :op_asgn1 or
        nt == :op_asgn2 or
        nt == :op_asgn_or or
        nt == :op_asgn_and or
        nt == :op_asgn_masgn or
        nt == :masgn or
        nt == :lasgn or
        nt == :dasgn or
        nt == :dasgn_curr or
        nt == :gasgn or
        nt == :iasgn or
        nt == :cdecl or
        nt == :cvdecl or
        nt == :cvasgn
    literal_value "assignment"
  else
    literal_value "expression"
  end
end

#to_c_gasgn(tree) ⇒ Object



42
43
44
# File 'lib/fastruby/translator/modules/variable.rb', line 42

def to_c_gasgn(tree)
  "_rb_gvar_set((void*)#{global_entry(tree[1])}, #{to_c tree[2]})"
end

#to_c_gvar(tree) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/fastruby/translator/modules/variable.rb', line 34

def to_c_gvar(tree)
  if (tree[1] == :$!)
    "pframe->thread_data->exception"
  else
    "rb_gvar_get((struct global_entry*)#{global_entry(tree[1])})"
  end
end

#to_c_iasgn(tree) ⇒ Object



50
51
52
# File 'lib/fastruby/translator/modules/variable.rb', line 50

def to_c_iasgn(tree)
  "_rb_ivar_set(#{locals_accessor}self,#{intern_num tree[1]},#{to_c tree[2]})"
end

#to_c_ivar(tree) ⇒ Object



46
47
48
# File 'lib/fastruby/translator/modules/variable.rb', line 46

def to_c_ivar(tree)
  "rb_ivar_get(#{locals_accessor}self,#{intern_num tree[1]})"
end

#to_c_lasgn(tree) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fastruby/translator/modules/variable.rb', line 114

def to_c_lasgn(tree)
  if options[:validate_lvar_types]
    klass = @infer_lvar_map[tree[1]]
    if klass

      verify_type_function = proc { |name| "
        static VALUE #{name}(VALUE arg, void* pframe ) {
          if (CLASS_OF(arg)!=#{literal_value klass}) {
            #{_raise(literal_value(FastRuby::TypeMismatchAssignmentException), "Illegal assignment at runtime (type mismatch)")};
          }
          return arg;
        }
      "
      }


      "_lvar_assing(&#{locals_accessor}#{tree[1]}, #{anonymous_function(&verify_type_function)}(#{to_c tree[2]},pframe))"
    else
      "_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
    end
  else
    "_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
  end
end

#to_c_lvar(tree) ⇒ Object



139
140
141
# File 'lib/fastruby/translator/modules/variable.rb', line 139

def to_c_lvar(tree)
  locals_accessor + tree[1].to_s
end