Sha256: 459c9023797a2e0530b78ab1e6ad66569ee39d7de30589146539de3f430471a5

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Rus3

  module Evaluator

    class SchemeEvaluator < Evaluator
      EVALUATOR_VERSION = "0.2.0"

      def version
        vmsg = "(scheme-evaluator :version #{EVALUATOR_VERSION})"
        if @translator
          vmsg += " #{@translator.version}"
        end
        super + " (#{vmsg})"
      end

      def initialize
        super

        @env = Environment.new
        @translator = Translator.new
        @translator.add_procedure_map(ARITHMETIC_OPS_MAP)
        @translator.add_procedure_map(COMPARISON_OPS_MAP)
      end

      def verbose=(verbose)
        super
        @translator and @translator.verbose = verbose
      end

      def instance_variable_set(var, value)
        @env.binding.receiver.instance_variable_set(var, value)
      end

      def instance_eval(&proc)
        @env.binding.receiver.instance_eval(&proc)
      end

      def eval(ast)
        ruby_source = translate_ast(ast)
        pp ruby_source if @verbose
        @env.binding.eval(ruby_source)
      end

      def binding
        @env.binding
      end

      ARITHMETIC_OPS_MAP = {
        "+"  => "add",
        "-"  => "subtract",
        "*"  => "mul",
        "/"  => "div",
        "%"  => "mod",
      }

      COMPARISON_OPS_MAP = {
        "<"  => "lt?",
        "<=" => "le?",
        ">"  => "gt?",
        ">=" => "ge?",
        "=" => "same_value?",
      }

      private

      def translate_ast(ast)
        ast.map{|node| @translator.translate(node)}.join("\n")
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rus3-0.2.1 lib/rus3/evaluator/scheme_evaluator.rb