Class: Campa::Repl

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

Instance Method Summary collapse

Constructor Details

#initialize(evaler, context, reader: Reader) ⇒ Repl

It creates a new Context that uses one given as a parameter to this contructor to evaluate expressions typed in the REPL.

Parameters:

  • evaler (Evaler)

    used to extract value from expressions

  • context (Context)

    against which expressions will be evaled

  • reader (Reader) (defaults to: Reader)

    to return expressions to be evaled



10
11
12
13
14
15
16
# File 'lib/campa/repl.rb', line 10

def initialize(evaler, context, reader: Reader)
  @reader = reader
  @evaler = evaler
  @context = context
  @environment = @context.push(Context.new)
  @printer = Printer.new
end

Instance Method Details

#run(input, output) ⇒ Object

Uses the #getc method from the parameter input to create a (probably blocking) loop to evaluate code “live” creating a REPL session.

Captures the Interrupt exception to break the loop and finish the current REPL session.

rubocop: disable Metrics/MethodLength

Parameters:

  • input (IO)

    an IO like object from where tokens will be extracted

  • output (IO)

    IO like object to receive the output from evaluating the forms



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/campa/repl.rb', line 30

def run(input, output)
  output.print "=> "
  reader = @reader.new(input)

  loop do
    begin
      token = reader.next
      break if token.nil?

      show(output, evaler.call(token, environment))
    rescue ExecutionError => e
      handle_exec_error(output, e)
    rescue StandardError => e
      handle_standard_error(output, e)
    end

    output.print "=> "
  end
rescue Interrupt
  output.puts "see you soon"
end