Class: Kalc::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/kalc/repl.rb

Instance Method Summary collapse

Instance Method Details

#headingObject



57
58
59
60
61
62
63
64
# File 'lib/kalc/repl.rb', line 57

def heading
  %q{
    This is Kalc, a small line-based language.
    More information about Kalc can be found at https://github.com/mrcsparker/kalc.

    Kalc is free software, provided as is, with no warranty.
  }
end

#load_envObject



8
9
10
# File 'lib/kalc/repl.rb', line 8

def load_env
  @kalc = Kalc::Runner.new(true)
end

#runObject



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
# File 'lib/kalc/repl.rb', line 12

def run
  puts heading

  # Load Kalc with debug
  load_env

  puts 'You are ready to go.  Have fun!'
  puts ''

  function_list = %w(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