Sha256: 25abad079dcd8bca1f1e7d98bc739262a514e2c53b80740082a196a5d1e24a60

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Carbon
  module Compiler
    module Visitor
      class Generation
        # Context for building function bodies.
        class Context
          attr_reader :index
          attr_reader :function

          def initialize(definition, index, function)
            @locals = []
            @definition = definition
            @index = index
            @function = function
          end

          def block
            @block ||= @definition.add("entry")
          end

          def new(name = "")
            @definition.add(name)
          end

          def build(*p)
            return block.build(*p) unless block_given?
            result = nil
            block.build(*p) { |*a| result = yield(*a) }
            result
          end

          def swap(other)
            old, @block = @block, other
            old
          end

          def find(name)
            index.fetch(name)
          end

          def key?(name)
            current.key?(name)
          end

          def [](name)
            current[name]
          end

          def []=(name, value)
            top[name] = value
          end

          def current
            @locals.inject({}, :merge)
          end

          def push
            @locals << {}
          end

          def pop
            @locals.pop
          end

          def top
            @locals.last
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carbon-compiler-0.2.0 lib/carbon/compiler/visitor/generation/context.rb