Module: Apricot::Compiler
- Defined in:
- lib/apricot/compiler.rb
Constant Summary collapse
- SELF =
Identifier.intern(:self)
Class Method Summary collapse
- .bytecode(g, form, quoted = false, macroexpand = true) ⇒ Object
- .compile_and_eval_file(file) ⇒ Object
- .compile_form(form, file = "(eval)", line = 1) ⇒ Object
- .eval(code, file = "(eval)", line = 1) ⇒ Object
- .eval_form(form, file = "(eval)", line = 1) ⇒ Object
- .generate(forms, file = "(none)", line = 1, evaluate = false) ⇒ Object
-
.once(g) {|g| ... } ⇒ Object
Some literals, such as regexps and rationals, should only be created the first time they are encountered.
- .pos(g, form) ⇒ Object
Class Method Details
.bytecode(g, form, quoted = false, macroexpand = true) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/apricot/compiler.rb', line 91 def bytecode(g, form, quoted = false, = true) pos(g, form) case form when Identifier if quoted g.push_const :Apricot g.find_const :Identifier g.push_literal form.name g.send :intern, 1 else if form.constant? g.push_const form.const_names.first form.const_names.drop(1).each {|n| g.find_const n } elsif form == SELF g.push_self elsif form.qualified? QualifiedReference.new(form.unqualified_name, form.qualifier).bytecode(g) else g.scope.find_var(form.name).bytecode(g) end end when Seq if quoted || form.empty? g.push_const :Apricot g.find_const :List if form.empty? g.find_const :EMPTY_LIST else form.each {|e| bytecode(g, e, true) } g.send :[], form.count end else callee, args = form.first, form.rest # Handle special forms such as def, let, fn, quote, etc if callee.is_a?(Identifier) if special = SpecialForm[callee.name] special.bytecode(g, args) return end if form = Apricot.(form) if form.is_a?(Seq) # Avoid recursing and macroexpanding again if expansion returns a list bytecode(g, form, false, false) else bytecode(g, form) end return end = callee. # Handle inlinable function calls if && [:inline] && (![:'inline-arities'] || [:'inline-arities'].apricot_call(args.count)) begin inlined_form = [:inline].apricot_call(*args) rescue => e g.compile_error "Inliner function for '#{callee.name}' raised an exception:\n #{e}" end g.tail_position = false bytecode(g, inlined_form) return end if callee.fn? || callee.method? qualifier_id = Identifier.intern(callee.qualifier.name) first_name, *rest_names = qualifier_id.const_names g.push_const first_name rest_names.each {|n| g.find_const(n) } args.each {|arg| bytecode(g, arg) } g.send callee.unqualified_name, args.count return end end # Handle everything else g.tail_position = false bytecode(g, callee) args.each {|arg| bytecode(g, arg) } g.send :apricot_call, args.count end when Symbol g.push_literal form when Array form.each {|e| bytecode(g, e, quoted) } g.make_array form.size when String g.push_literal form g.string_dup # Duplicate string to prevent mutating the literal when Hash # Create a new Hash g.push_const :Hash g.push form.size g.send :new_from_literal, 1 # Add keys and values form.each_pair do |key, value| g.dup # the Hash bytecode(g, key, quoted) bytecode(g, value, quoted) g.send :[]=, 2 g.pop # drop the return value of []= end when Fixnum g.push form when true g.push :true when nil g.push :nil when false g.push :false when Float g.push_unique_literal form when Regexp once(g) do g.push_const :Regexp g.push_literal form.source g.push form. g.send :new, 2 end when Rational once(g) do g.push_self g.push form.numerator g.push form.denominator g.send :Rational, 2, true end when Set g.push_const :Set g.send :new, 0 # TODO: Inline this new? form.each do |elem| bytecode(g, elem, quoted) g.send :add, 1 end when Bignum g.push_unique_literal form else g.compile_error "Can't generate bytecode for #{form} (#{form.class})" end end |
.compile_and_eval_file(file) ⇒ Object
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 |
# File 'lib/apricot/compiler.rb', line 40 def compile_and_eval_file(file) cc = generate(Reader.read_file(file), file, 1, true) if Rubinius::CodeLoader.save_compiled? compiled_name = Rubinius::ToolSet::Runtime::Compiler.compiled_name(file) dir = File.dirname(compiled_name) unless File.directory?(dir) parts = [] until dir == "/" or dir == "." parts << dir dir = File.dirname(dir) end parts.reverse_each do |d| Dir.mkdir d unless File.directory?(d) end end Rubinius::ToolSet::Runtime::CompiledFile.dump cc, compiled_name, Rubinius::Signature, Rubinius::RUBY_LIB_VERSION end cc end |
.compile_form(form, file = "(eval)", line = 1) ⇒ Object
68 69 70 |
# File 'lib/apricot/compiler.rb', line 68 def compile_form(form, file = "(eval)", line = 1) generate([form], file, line) end |
.eval(code, file = "(eval)", line = 1) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/apricot/compiler.rb', line 76 def eval(code, file = "(eval)", line = 1) forms = Reader.read_string(code, file,line) return nil if forms.empty? forms[0..-2].each do |form| eval_form(form, file, line) end # Return the result of the last form in the program. eval_form(forms.last, file, line) end |
.eval_form(form, file = "(eval)", line = 1) ⇒ Object
72 73 74 |
# File 'lib/apricot/compiler.rb', line 72 def eval_form(form, file = "(eval)", line = 1) Rubinius.run_script(compile_form(form, file, line)) end |
.generate(forms, file = "(none)", line = 1, evaluate = false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/apricot/compiler.rb', line 5 def generate(forms, file = "(none)", line = 1, evaluate = false) g = Generator.new g.name = :__apricot__ g.file = file.to_sym g.scopes << TopLevelScope.new g.set_line(line) if forms.empty? g.push_nil else forms.each_with_index do |e, i| g.pop unless i == 0 bytecode(g, e) # We evaluate top level forms as we generate the bytecode for them # so macros can be used immediately after their definitions. eval_form(e, file) if evaluate end end g.ret scope = g.scopes.pop g.local_count = scope.local_count g.local_names = scope.local_names g.close g.encode cc = g.package(Rubinius::CompiledCode) cc.scope = Rubinius::ConstantScope.new(Object) cc end |
.once(g) {|g| ... } ⇒ Object
Some literals, such as regexps and rationals, should only be created the first time they are encountered. We push a literal nil here, and then overwrite the literal value with the created object if it is nil, i.e. the first time only. Subsequent encounters will use the previously created object. This idea was copied from Rubinius::AST::RegexLiteral.
The passed block should take a generator and generate the bytecode to create the object the first time.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/apricot/compiler.rb', line 266 def once(g) idx = g.add_literal(nil) g.push_literal_at idx g.dup g.is_nil lbl = g.new_label g.gif lbl g.pop yield g g.set_literal idx lbl.set! end |
.pos(g, form) ⇒ Object
282 283 284 285 286 |
# File 'lib/apricot/compiler.rb', line 282 def pos(g, form) if ( = form.) && (line = [:line]) g.set_line(line) end end |