-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotruby
executable file
·67 lines (56 loc) · 1.48 KB
/
notruby
1
2
3
4
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
39
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
67
#!/usr/bin/env ruby -s
# NotRuby interpreter
$:.unshift File.expand_path('..', __FILE__)
require 'interpreter'
require 'readline'
require 'pp'
require 'readline_history'
if $h # -h option
abort <<USAGE
Usage:
./notruby # start REPL
./notruby file.rb
./notruby -e='code'
USAGE
end
# Eval some code
# $e = # -e option
if $e
Interpreter.new.eval($e)
elsif ARGV.first
Interpreter.new.eval(File.read(ARGV.first))
# Start the REPL, read-eval-print-loop, or interactive interpreter
else
ReadlineHistory::load_history
puts "NotRuby REPL, 'quit' to quit"
interpreter = Interpreter.new
loop do
begin
line = Readline.readline(">> ")
rescue Interrupt # CTRL-C aborts only the current line being read. Should use 'quit' to quit the REPL.
next
end
next if line.empty?
Readline::HISTORY.push(line) unless Readline::HISTORY.to_a[-1] == line
case line
when 'quit'
break
when 'show'
puts '===== Constants ===== '
pp Context.constants.map { |k, v| k }
# puts "\n===== Locals ====="
# pp Runtime.locals.map { |k, v| { k }
# puts "\n===== Current self ====="
# pp Runtime.current_self
# puts "\n===== Current class ====="
# pp Runtime.current_class
else
begin
result = (r = interpreter.eval(line)).respond_to?(:ruby_value) ? r.ruby_value : r
puts "=> #{result.inspect}"
rescue => error
puts error
end
end
end
end