Class: Loxxy::Interpreter

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

Overview

Note:

WIP: very crude implementation.

A Lox tree-walking interpreter. It acts as a facade object that:

  • hides the internal plumbings of the front-end and back-end parts.
  • delegates all the core work to its subordinate objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theOptions = {}) ⇒ Interpreter

Returns a new instance of Interpreter.

Parameters:

  • theOptions (Hash) (defaults to: {})


18
19
20
# File 'lib/loxxy/interpreter.rb', line 18

def initialize(theOptions = {})
  @config = theOptions
end

Instance Attribute Details

#configObject (readonly)

return [Hash]



15
16
17
# File 'lib/loxxy/interpreter.rb', line 15

def config
  @config
end

Instance Method Details

#evaluate(lox_input) ⇒ Loxxy::Datatype::BuiltinDatatype

Evaluate the given Lox program. Return the result of the last executed expression (if any)

Parameters:

  • lox_input (String)

    Lox program to evaluate

Returns:



26
27
28
# File 'lib/loxxy/interpreter.rb', line 26

def evaluate(lox_input)
  raw_evaluate(lox_input).first
end

#raw_evaluate(lox_input) ⇒ Object

Evaluate the given Lox program. Return the pair [result, a BackEnd::Engine instance] where result is the value of the last executed expression (if any)

Parameters:

  • lox_input (String)

    Lox program to evaluate

Returns:

  • Loxxy::Datatype::BuiltinDatatype, Loxxy::BackEnd::Engine]



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/loxxy/interpreter.rb', line 35

def raw_evaluate(lox_input)
  # Front-end scans, parses the input and blurps an AST...
  parser = FrontEnd::Parser.new

  # The AST is the data object passed to the back-end
  ast_tree = parser.parse(lox_input)
  visitor = Ast::ASTVisitor.new(ast_tree)

  # Back-end launches the tree walking & responds to visit events
  # by executing the code determined by the visited AST node.
  engine = BackEnd::Engine.new(config)
  result = engine.execute(visitor)

  [result, engine]
end