Class: MicroRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/microruby.rb

Class Method Summary collapse

Class Method Details

.compile(code) ⇒ Object


21
22
23
24
# File 'lib/microruby.rb', line 21

def self.compile(code)
  seq = RubyVM::InstructionSequence.compile(code).to_a
  Bytecode.encode(seq).to_bin
end

.compile_file(path) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/microruby.rb', line 5

def self.compile_file(path)
  raise "File not found: #{path}" unless File.file?(path)

  dir = File.dirname(path)
  filename = File.basename(path, ".rb")

  bc = compile File.read(path)

  target_path = "#{dir}/#{filename}.mrbc"
  File.open(target_path, "w") do |f|
    f.write(bc)
    puts "Compiled to #{target_path}"
  end

end