Sha256: d51a205ee9dec3dc75c741634110a6479a1927a206f55bfe4c4f401fffce4699

Contents?: true

Size: 1.67 KB

Versions: 22

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require_relative './front_end/parser'
require_relative './ast/ast_visitor'
require_relative './back_end/engine'

module Loxxy
  # 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.
  # @note WIP: very crude implementation.
  class Interpreter
    # return [Hash]
    attr_reader :config

    # @param theOptions [Hash]
    def initialize(theOptions = {})
      @config = theOptions
    end

    # Evaluate the given Lox program.
    # Return the result of the last executed expression (if any)
    # @param lox_input [String] Lox program to evaluate
    # @return [Loxxy::Datatype::BuiltinDatatype]
    def evaluate(lox_input)
      raw_evaluate(lox_input).first
    end

    # 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)
    # @param lox_input [String] Lox program to evaluate
    # @return Loxxy::Datatype::BuiltinDatatype, Loxxy::BackEnd::Engine]
    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
  end # class
end # module

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
loxxy-0.4.08 lib/loxxy/interpreter.rb
loxxy-0.4.07 lib/loxxy/interpreter.rb
loxxy-0.4.06 lib/loxxy/interpreter.rb
loxxy-0.4.05 lib/loxxy/interpreter.rb
loxxy-0.4.04 lib/loxxy/interpreter.rb
loxxy-0.4.03 lib/loxxy/interpreter.rb
loxxy-0.4.02 lib/loxxy/interpreter.rb
loxxy-0.4.01 lib/loxxy/interpreter.rb
loxxy-0.4.00 lib/loxxy/interpreter.rb
loxxy-0.3.03 lib/loxxy/interpreter.rb
loxxy-0.3.02 lib/loxxy/interpreter.rb
loxxy-0.3.01 lib/loxxy/interpreter.rb
loxxy-0.3.00 lib/loxxy/interpreter.rb
loxxy-0.2.06 lib/loxxy/interpreter.rb
loxxy-0.2.05 lib/loxxy/interpreter.rb
loxxy-0.2.04 lib/loxxy/interpreter.rb
loxxy-0.2.03 lib/loxxy/interpreter.rb
loxxy-0.2.02 lib/loxxy/interpreter.rb
loxxy-0.2.01 lib/loxxy/interpreter.rb
loxxy-0.2.00 lib/loxxy/interpreter.rb