a basic interpret command for cli

part of the benchmark effort
determine amount of objects
This commit is contained in:
2019-07-25 21:23:55 +03:00
parent 00bf38a0e6
commit 14c965360d
2 changed files with 52 additions and 10 deletions

View File

@ -1,5 +1,6 @@
require "thor"
require "rubyx"
require "risc/interpreter"
class RubyXC < Thor
desc "compile FILE" , "Compile given FILE to binary"
@ -27,7 +28,44 @@ class RubyXC < Thor
outfile = file.split("/").last.gsub(".rb" , ".o")
writer.save outfile
end
desc "interpret FILE" , "Interpret given FILE "
long_desc <<-LONGDESC
Compiles the given file to an intermediate RISC format, and runs the
Interpreter.
RISC is the last abstract layer inside the compiler. It is in nature
very close to arm (without quirks and much smaller).
An interpreter was originally developed for the RISC layer for debugging purposes.
Running the interpreter is about 50k slower than binary, but it can be used
to veryfy simple programs.
No output file will be generated, the only output is generated by the
given program.
The program must define a main method on the Space class, which will be invoked.
LONGDESC
def interpret(file)
begin
ruby = File.read(file)
rescue
fail MalformattedArgumentError , "No such file #{file}"
end
options = {
parfait: { factory: 3*1024, },
load_parfait: false ,
}
compiler = RubyX::RubyXCompiler.new(options)
linker = compiler.ruby_to_binary(ruby, :interpreter)
puts "interpreting #{file}"
interpreter = Risc::Interpreter.new(linker , STDOUT )
interpreter.start_program
interpreter.tick while(interpreter.instruction)
return outfile
end
end