Class: Layo::InteractiveInterpreter

Inherits:
BaseInterpreter show all
Defined in:
lib/layo/interactive_interpreter.rb

Instance Attribute Summary collapse

Attributes inherited from BaseInterpreter

#input, #output

Instance Method Summary collapse

Methods inherited from BaseInterpreter

#call_func, #cast, #create_variable_table, #eval_assignment_stmt, #eval_binary_expr, #eval_block, #eval_break_stmt, #eval_cast_expr, #eval_cast_stmt, #eval_condition_stmt, #eval_constant_expr, #eval_declaration_stmt, #eval_expr, #eval_expression_stmt, #eval_function_expr, #eval_input_stmt, #eval_loop_stmt, #eval_nary_expr, #eval_print_stmt, #eval_return_stmt, #eval_switch_stmt, #eval_unary_expr, #eval_variable_expr, #init_tables, #interpolate_string, #with_guard

Constructor Details

#initialize(parser) ⇒ InteractiveInterpreter

Returns a new instance of InteractiveInterpreter.



5
6
7
8
# File 'lib/layo/interactive_interpreter.rb', line 5

def initialize(parser)
  @parser = parser
  super
end

Instance Attribute Details

#parserObject

Returns the value of attribute parser.



3
4
5
# File 'lib/layo/interactive_interpreter.rb', line 3

def parser
  @parser
end

Instance Method Details

#eval_function_stmt(stmt) ⇒ Object



35
36
37
38
39
40
# File 'lib/layo/interactive_interpreter.rb', line 35

def eval_function_stmt(stmt)
  if @functions.has_key?(stmt.name)
    raise RuntimeError, "Function '#{stmt.name}' is already declared"
  end
  @functions[stmt.name] = { args: stmt.args, block: stmt.block }
end

#interpretObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/layo/interactive_interpreter.rb', line 10

def interpret
  init_tables
  @stmt_line = 1
  @output.puts 'Press Control-C to exit'
  while true
    begin
      @output.print ' > '
      break if @parser.tokenizer.try(:eof)
      begin
        statement = @parser.parse_statement
        with_guard { send("eval_#{statement.type}_stmt", statement) }
      end until @parser.tokenizer.lexer.buffer_empty?
    rescue Layo::SyntaxError => e
      @parser.tokenizer.reset
      @parser.tokenizer.lexer.reset
      $stderr.puts "Syntax error: #{e}"
    rescue Layo::RuntimeError => e
      $stderr.puts "Runtime error: #{e}"
    rescue Interrupt
      @output.puts 'Exiting'
      break
    end
  end
end