Class: Riml::Repl

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

Constant Summary collapse

COMPILE_ON =
%w(compile c)
RELOAD_ON =
%w(reload r)
EXIT_ON =
%w(quit q)
HELP_MSG =
<<msg
compile riml line(s):             #{COMPILE_ON.join(', ')}
clear previous class definitions: #{RELOAD_ON.join(', ')}
exit repl:                        #{EXIT_ON.join(', ')}
msg

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vi_readline = false, compile_options = {}) ⇒ Repl

Returns a new instance of Repl.



25
26
27
28
29
30
31
32
# File 'lib/repl.rb', line 25

def initialize(vi_readline = false, compile_options = {})
  @indent_amount = 0
  @line = nil
  @compiler_options = DEFAULT_COMPILE_OPTIONS.merge(compile_options)
  prepare_new_context
  Readline.vi_editing_mode if vi_readline
  trap(:INT) { reset!; puts }
end

Instance Attribute Details

#compiler_optionsObject (readonly)

Returns the value of attribute compiler_options.



12
13
14
# File 'lib/repl.rb', line 12

def compiler_options
  @compiler_options
end

#lineObject (readonly)

Returns the value of attribute line.



11
12
13
# File 'lib/repl.rb', line 11

def line
  @line
end

Instance Method Details

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/repl.rb', line 34

def run
  puts HELP_MSG, "\n"
  while @line = Readline.readline(current_indent, true)
    line.strip!
    next if line.empty?
    line_dc = line.downcase
    if COMPILE_ON.include?(line_dc)
      next if current_compilation_unit.empty?
      compile_unit!
    elsif RELOAD_ON.include?(line_dc)
      reload!
      puts "reloaded"
    elsif EXIT_ON.include?(line_dc)
      exit_repl
    else
      current_compilation_unit << line
      check_indents
    end
  end
end