/
reak
executable file
·83 lines (67 loc) · 1.51 KB
/
reak
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
#!/usr/bin/env rbx
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'reak'
require 'pp'
ast, sexp, bench, syntax, evals = false, false, false, :reak, []
options = Rubinius::Options.new "Usage: #$0 [options] [script]", 20
options.on "-A", "Print the AST" do
ast = true
end
options.on "-S", "Print the S-Expression" do
sexp = true
end
options.on "-e", "CODE", "Execute CODE" do |e|
evals << e
end
options.on "-s", "SYNTAX", "What SYNTAX to use (ansi, gst, reak)" do |s|
syntax = s.to_sym
end
options.on "-h", "--help", "Display this help" do
puts options
exit 0
end
options.on "-B", "Benchmark parsing" do
bench = true
end
file = options.parse(ARGV).first
display = proc do |code|
tree = nil
parser = Reak::Parser.new(syntax)
if bench
require 'benchmark'
Benchmark.bmbm do |x|
x.report { tree = parser.parse(code) }
end
else
tree = parser.parse(code)
end
if ast
puts "========= AST ========="
tree.graph
puts "------------------------"
end
if sexp
puts "===== S-expression ====="
pp tree.to_sexp
puts "------------------------"
end
begin
puts Reak::Compiler.new.compile(code).call.reak_send("printString")
rescue Exception => e
e.render
end
end
evals << File.read(file) if file
if evals.empty?
if $stdin.tty?
require 'readline'
loop do
code = Readline.readline "st> "
exit 0 unless code and code != "exit"
display[code]
end
else
evals << STDIN.read
end
end
evals.each(&display)