/
atomy
executable file
·128 lines (104 loc) · 2.93 KB
/
atomy
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
#!/usr/bin/env rbx
# vim: ft=ruby
base = File.expand_path "../../lib/", __FILE__
kernel = File.expand_path "../../kernel/", __FILE__
require 'readline'
require 'optparse'
require base + "/atomy"
options = {}
unless ARGV.empty? || ARGV[0][0] != ?-
OptionParser.new do |o|
o.banner = "usage: atomy [options]"
o.on("-B", "--print-bytecode", "print out compiled bytecode") do |v|
options[:debug] = v
end
o.on("-e", "--evaluate EXPR", "evaluate EXPR and print the result") do |v|
options[:evaluate] = v
end
o.on("-s", "--before-start EXPR", "evaluate EXPR beforehand") do |v|
options[:start] = v
end
o.on("-l", "--load FILE", "load FILENAME and start the REPL") do |v|
options[:load] = v
end
o.on("-d", "--documentation DIR", "generate documentation with output to DIR") do |v|
Atomy::CodeLoader.documentation = v
end
o.on("-x", "--skip-kernel", "don't load the kernel") do
options[:skip_kernel] = true
end
o.on_tail("-h", "--help", "show this message") do
puts o
exit
end
end.parse!
end
unless options[:skip_kernel]
a = Time.now
puts "loading kernel"
Atomy.import(kernel + "/boot.ay")
puts Time.now - a
end
Atomy::Namespace.ensure(:user)
def run_atomy(options)
if str = options[:evaluate] || options[:start]
$0 = "(eval)"
res = Atomy::Compiler.evaluate(
str,
nil,
"(eval)",
1,
options[:debug]
)
write = Atomy.namespaced("atomy", "write").to_sym
if options[:evaluate]
if res.respond_to?(write)
res.send(write)
else
p(res)
end
return
end
end
if file = options[:load] || ARGV[0]
$0 = file
Atomy.import(file, ARGV[0] ? :run : :load, options[:debug])
return if ARGV[0]
end
if respond_to?(Atomy.namespaced("atomy", "repl").to_sym, true)
send(Atomy.namespaced("atomy", "repl").to_sym, File.expand_path("~/.atomy_history"))
else
$stderr.puts("main REPL startup failed! here's a basic one:")
history_file = File.expand_path("~/.atomy_history")
if File.exists?(history_file)
File.open(history_file, "r") do |f|
f.readlines.each do |l|
Readline::HISTORY << l.strip
end
end
end
begin
sane_history = []
while str = Readline.readline("> ")
next if str.empty?
sane_history << str
begin
res = Atomy::Compiler.evaluate str, TOPLEVEL_BINDING
if res.respond_to?(:pretty)
puts "=> #{res.send(:pretty).send(Atomy.namespaced("atomy", Atomy.namespaced("doc", "render")))}"
else
puts "=> #{res.inspect}"
end
rescue StandardError => e
puts "#{e.class.name}: #{e.message}"
puts Rubinius::Backtrace.backtrace(e.locations).show
end
end
ensure
File.open(history_file, "a") do |f|
f.puts(*sane_history)
end
end
end
end
run_atomy(options)