Class: CRewriter
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- CRewriter
- Defined in:
- lib/crewriter.rb
Overview
CRewriter (should probably move this out to its own file) does rewritings that are language specific to C.
Constant Summary collapse
- REWRITES =
REWRITES maps a function signature to a proc responsible for generating the appropriate sexp for that rewriting.
{ [CType.str, :+, CType.str] => proc { |l,n,r| t(:call, nil, :strcat, r.unshift(r.shift, l), CType.str) }, [CType.file, :puts, CType.str] => proc { |l,n,r| t(:call, nil, :fputs, r.push(l)) }, }
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#extra_methods ⇒ Object
readonly
Returns the value of attribute extra_methods.
Instance Method Summary collapse
-
#free ⇒ Object
REFACTOR: this is a violation of responsibility, should be in Env.
-
#initialize ⇒ CRewriter
constructor
:nodoc:.
-
#process(exp, _src = nil, _timeout = nil) ⇒ Object
def rewrite exp result = super result.c_type ||= exp.c_type if Sexp === exp and exp.c_type result end.
-
#process_call(exp) ⇒ Object
Rewrites function calls by looking them up in the REWRITES map.
- #process_class(exp) ⇒ Object
-
#process_iter(exp) ⇒ Object
TODO register statics.
- #process_lasgn(exp) ⇒ Object
- #process_lvar(exp) ⇒ Object
- #var_names_in(exp) ⇒ Object
Constructor Details
#initialize ⇒ CRewriter
:nodoc:
30 31 32 33 34 35 36 |
# File 'lib/crewriter.rb', line 30 def initialize # :nodoc: super self.auto_shift_type = true self.expected = TypedSexp @env = ::R2CEnvironment.new @extra_methods = [] end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
27 28 29 |
# File 'lib/crewriter.rb', line 27 def env @env end |
#extra_methods ⇒ Object (readonly)
Returns the value of attribute extra_methods.
28 29 30 |
# File 'lib/crewriter.rb', line 28 def extra_methods @extra_methods end |
Instance Method Details
#free ⇒ Object
REFACTOR: this is a violation of responsibility, should be in Env
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/crewriter.rb', line 50 def free # REFACTOR: this is a violation of responsibility, should be in Env parent = @env.env[0..-2] bound_in_parent = parent.map { |h| h.keys }.flatten env = @env.all free = env.select { |k, (_, v)| bound_in_parent.include? k or not v } vars = free.map { |k, (t, _)| [k, t] } return vars end |
#process(exp, _src = nil, _timeout = nil) ⇒ Object
def rewrite exp
result = super
result.c_type ||= exp.c_type if Sexp === exp and exp.c_type
result
end
44 45 46 47 48 |
# File 'lib/crewriter.rb', line 44 def process exp, _src = nil, _timeout = nil result = super(exp) result.c_type ||= exp.c_type if Sexp === exp and exp.c_type result end |
#process_call(exp) ⇒ Object
Rewrites function calls by looking them up in the REWRITES map. If a match exists, it invokes the block passing in the lhs, rhs, and function name. If one does not exist, it simply repacks the sexp and sends it along.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/crewriter.rb', line 67 def process_call(exp) lhs = process exp.shift name = exp.shift rhs = process exp.shift lhs_type = lhs.c_type rescue nil type_signature = [lhs_type, name] type_signature += rhs[1..-1].map { |sexp| sexp.c_type }.to_a unless rhs.nil? result = if REWRITES.has_key? type_signature then REWRITES[type_signature].call(lhs, name, rhs) else t(:call, lhs, name, rhs, exp.c_type) end return result end |
#process_class(exp) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/crewriter.rb', line 85 def process_class(exp) klassname = exp.shift superklassname = exp.shift methods = [] until exp.empty? do methods << process(exp.shift) end @extra_methods.reverse_each do |defx| methods.unshift defx end @extra_methods.clear result = t(:class, klassname, superklassname, CType.zclass) result.push(*methods) return result end |
#process_iter(exp) ⇒ Object
TODO register statics
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 |
# File 'lib/crewriter.rb', line 107 def process_iter(exp) iter_method_name = Unique.next value_var_name = Unique.next value_var_type = CType.unknown memo_var_name = Unique.next call = process exp.shift vars = process exp.shift body = nil free_vars = @env.scope do body = process exp.shift self.free.map { |name, type| [name, :"static_#{Unique.next}", type] } end var_names = var_names_in vars frees = t(:array, CType.void) statics = t(:array, CType.void) defx_body_block = t(:block) # set statics first so block vars can update statics free_vars.each do |name, static_name, type| # free vars go on both sides frees << t(:lvar, name, type) statics << t(:lvar, static_name, type) defx_body_block << t(:lasgn, name, t(:lvar, static_name, type), type) end if var_names.length == 1 then # expand block args to lasgn value_var_type = var_names.first.last defx_body_block << t(:lasgn, var_names.first.first, t(:lvar, value_var_name, var_names.first.last), var_names.first.last) else # expand block args to masgn value_var_type = CType.value dyn_vars = t(:array) var_names.each do |name, type| dyn_vars << t(:lasgn, name, nil, type) end defx_body_block << t(:masgn, dyn_vars, t(:to_ary, t(:lvar, value_var_name, CType.value))) end defx_body_block << body free_vars.each do |name, static_name, type| defx_body_block << t(:lasgn, static_name, t(:lvar, name, type), type) @extra_methods << t(:static, "static VALUE #{static_name};", CType.fucked) end defx_body_block << t(:return, t(:nil, CType.value)) defx = t(:defx, iter_method_name, t(:args, t(value_var_name, value_var_type), t(memo_var_name, CType.value)), t(:scope, defx_body_block), CType.void) @extra_methods << defx args = t(:args, frees, statics, CType.void) return t(:iter, call, args, iter_method_name) end |
#process_lasgn(exp) ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/crewriter.rb', line 183 def process_lasgn(exp) name = exp.shift value = process(exp.shift) @env.add name, exp.c_type @env.set_val name, true return t(:lasgn, name, value, exp.c_type) end |
#process_lvar(exp) ⇒ Object
193 194 195 196 197 198 199 200 |
# File 'lib/crewriter.rb', line 193 def process_lvar(exp) name = exp.shift @env.add name, CType.value @env.lookup name rescue @env.set_val name, false return t(:lvar, name, exp.c_type) end |
#var_names_in(exp) ⇒ Object
202 203 204 205 206 207 208 209 210 |
# File 'lib/crewriter.rb', line 202 def var_names_in(exp) return [[exp.last, exp.c_type]] if exp.length == 2 and not Sexp === exp.last var_names = [] exp.each_of_type :dasgn_curr do |sexp| var_names << [sexp.sexp_body.first, sexp.c_type] end return var_names end |