Class: Python::FileInterpreter

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

Constant Summary collapse

ParsingError =
Class.new(RuntimeError)

Instance Method Summary collapse

Constructor Details

#initialize(code, bind = {}) ⇒ FileInterpreter

Returns a new instance of FileInterpreter.



9
10
11
12
# File 'lib/python/file_interpreter.rb', line 9

def initialize(code, bind={})
  @code = code
  @bind = bind
end

Instance Method Details

#executeObject



25
26
27
# File 'lib/python/file_interpreter.rb', line 25

def execute
  parse().eval(Environment.new(@bind))
end

#parseObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/python/file_interpreter.rb', line 14

def parse
  parser = Parser::StatementParser.file_input
  result = parser.parse(Parser::IndentConverter.new.convert(@code))

  if result.is_a?(Parser::Succeeded) && result.rest == ""
    result.parsed
  else
    raise ParsingError.new
  end
end