Class: Campa::Repl
- Inherits:
-
Object
- Object
- Campa::Repl
- Defined in:
- lib/campa/repl.rb
Instance Method Summary collapse
-
#initialize(evaler, context, reader: Reader) ⇒ Repl
constructor
It creates a new Context that uses one given as a parameter to this contructor to evaluate expressions typed in the REPL.
-
#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.
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.
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
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 |