Sha256: 391ffd990592feb5f1ab4c6c8a6d271322f6507d58bc54f4128c9a15048bc871

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

# Represents a Sweet class in the Ruby world. Classes are objects in Sweet so they
# inherit from SweetObject.
class SweetClass < SweetObject
  attr_reader :runtime_methods

  # Creates a new class. Number is an instance of Class for example.
  def initialize
    @runtime_methods = {}
  
    # Check if we're bootstrapping (launching the runtime). During this process the 
    # runtime is not fully initialized and core classes do not yet exists, so we defer 
    # using those once the language is bootstrapped.
    # This solves the chicken-or-the-egg problem with the Class class. We can 
    # initialize Class then set Class.class = Class.
    if defined?(Runtime)
      runtime_class = Runtime["Class"]
    else
      runtime_class = nil
    end
  
    super(runtime_class)
  end

  # Lookup a method
  def lookup(method_name)
    method = @runtime_methods[method_name]
    unless method
      raise "Method not found: #{method_name}"
    end
    method
  end

  # Create a new instance of this class
  def new
    SweetObject.new(self)
  end
  
  # Create an instance of this Sweet class that holds a Ruby value. Like a String, 
  # Number or true.
  def new_with_value(value)
    SweetObject.new(self, value)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sweet-lang-0.3.7 lib/runtime/class.rb
sweet-lang-0.2.2 lib/runtime/class.rb
sweet-lang-0.1.9 lib/runtime/class.rb
sweet-lang-0.1.0 lib/runtime/class.rb