Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 82 lines (64 sloc) 1.76 KB
#!/usr/bin/env rbx
file = __FILE__
file = File.readlink(file) while File.symlink? file
$LOAD_PATH.unshift(File.expand_path('../../lib', file))
require 'lambra'
evals = []
settings = Hash.new { |h,k| h[k] = false }
options = Rubinius::Options.new "Usage: #$0 [options] [script]", 20
options.on( "-A", "Print the AST" ) { settings[:ast] = true }
options.on( "-B", "Print the bytecode" ) { settings[:bc] = true }
options.on "-e", "CODE", "Execute CODE" do |e|
evals << ['(eval)', e]
end
options.on "-v", "--version", "Show version" do
puts "Lambra #{Lambra::VERSION}"
exit 0
end
options.on "-h", "--help", "Display this help" do
puts options
exit 0
end
info = proc do |cond, name, &block|
next unless settings[cond]
puts '', " #{name} ".center(80, "=")
block[]
puts "-" * 80, ''
end
display = proc do |file, code, ev=true, binding=nil|
begin
if settings[:ast]
ast = if file == '(eval)'
Lambra::Parser.parse_string(code)
else
Lambra::Parser.parse_file(file)
end
info.call(:ast, 'AST') { RBX::AST::AsciiGrapher.new(ast).print }
end
info.call(:bc, "Bytecode") { puts Lambra::Compiler.compile_string(code, file).decode }
if ev
puts "=> %p" % Lambra::Compiler.eval(code, file, binding)
else
Lambra::Compiler.eval(code, file, binding)
end
rescue Exception => e
e.render
end
end
options.parse(ARGV).each do |file|
evals << [file, File.read(file), false]
end
if evals.empty?
if $stdin.tty?
require 'readline'
binding = Object.send(:binding)
loop do
code = Readline.readline "lm> "
exit 0 unless code and code != "exit"
display['(repl)', code, true, binding]
end
else
evals << ['(stdin)', STDIN.read]
end
end
evals.each(&display)