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
|
# File 'lib/kalc/repl.rb', line 13
def run
puts heading
load_env
puts "You are ready to go. Have fun!"
puts ""
ast = nil
function_list = [
'quit', 'exit', 'functions', 'variables', 'ast'
] + @kalc.interpreter.env.functions.map { |f| f.first }
begin
comp = proc { |s| function_list.grep( /^#{Regexp.escape(s)}/ ) }
Readline.completion_append_character = ""
Readline.completion_proc = comp
while input = Readline.readline("kalc-#{Kalc::VERSION} > ", true)
begin
case
when (input == 'quit' || input == 'exit')
break
when input == "functions"
puts @kalc.interpreter.env.functions.map { |f| f.first }.join(", ")
when input == 'variables'
puts @kalc.interpreter.env.variables.map { |v| "#{v[0]} = #{v[1]}" }.join("\n\r")
when input == 'reload'
load_env
when input == 'ast'
pp @kalc.ast
when input != ""
puts @kalc.run(input)
end
rescue Parslet::ParseFailed => e
puts e, g.root.error_tree
rescue Exception => e
puts e
puts e.backtrace
end
end
rescue Exception => e
puts e
puts e.backtrace
end
end
|