Sha256: 721cbc054453babe173c61e589d6b7983266dc3eb51bf311b2885b0f65271e4a

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

# Bootstrap the runtime. This is where we assemble all the classes and objects together
# to form the runtime.
                                            # What's happening in the runtime:
sweet_class = SweetClass.new            #  Class
sweet_class.runtime_class = sweet_class #  Class.class = Class
object_class = SweetClass.new             #  Object = Class.new
object_class.runtime_class = sweet_class  #  Object.class = Class

# Create the Runtime object (the root context) on which all code will start its
# evaluation.
Runtime = Context.new(object_class.new)

Runtime["Class"] = sweet_class
Runtime["Object"] = object_class
Runtime["Number"] = SweetClass.new
Runtime["String"] = SweetClass.new

# Everything is an object in our language, even true, false and nil. So they need
# to have a class too.
Runtime["TrueClass"] = SweetClass.new
Runtime["FalseClass"] = SweetClass.new
Runtime["NilClass"] = SweetClass.new

Runtime["true"] = Runtime["TrueClass"].new_with_value(true)
Runtime["false"] = Runtime["FalseClass"].new_with_value(false)
Runtime["nil"] = Runtime["NilClass"].new_with_value(nil)

# Add a few core methods to the runtime.

# Add the `new` method to classes, used to instantiate a class:
#  eg.: Object.new, Number.new, String.new, etc.
Runtime["Class"].runtime_methods["new"] = proc do |receiver, arguments|
  receiver.new
end

# Print an object to the console.
# eg.: print("hi there!")
Runtime["Object"].runtime_methods["print"] = proc do |receiver, arguments|
	@code << arguments.first.ruby_value
  Runtime["nil"]
end

Version data entries

4 entries across 4 versions & 1 rubygems

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