Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created May 21, 2016 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/36331920d6a92fc21cb6671e8ca09630 to your computer and use it in GitHub Desktop.
Save yorickpeterse/36331920d6a92fc21cb6671e8ca09630 to your computer and use it in GitHub Desktop.
![no_module]
let object_class = __get_toplevel(_)
let object_iproto = __set_object(_)
# Make sure that instance methods of Object are also available to Object (the
# class) itself.
__set_prototype(object_class, object_iproto)
__set_literal_attr(object_class, '__iproto', object_iproto)
__set_literal_attr(object_iproto, '__class', object_class)
let class_class = __set_object(_, object_class)
let class_iproto = __set_object(_, object_iproto)
__set_literal_attr(class_class, '__iproto', class_iproto)
__set_literal_attr(class_iproto, '__class', class_class)
__set_literal_attr(class_class, '__class', class_class)
# Now that Class is set up we can set Object's class to Class.
__set_literal_attr(object_class, '__class', class_class)
# Next we set up the Module class, which inherits from Object.
let module_class = __set_object(_, object_class)
let module_iproto = __set_object(_, object_iproto)
__set_literal_attr(module_class, '__iproto', module_iproto)
__set_literal_attr(module_class, '__class', class_class)
__set_literal_attr(module_iproto, '__class', module_class)
# Now we can define the "core" module and its child modules.
let core_mod = __set_object(_, module_iproto)
let class_mod = __set_object(_, module_iproto)
let obj_mod = __set_object(_, module_iproto)
let module_mod = __set_object(_, module_iproto)
__set_literal_const(obj_mod, 'Object', object_class)
__set_literal_const(class_mod, 'Class', class_class)
__set_literal_const(module_mod, 'Module', module_class)
__set_literal_const(core_mod, 'object', obj_mod)
__set_literal_const(core_mod, 'class', class_mod)
__set_literal_const(core_mod, 'module', module_mod)
# Set the objects that defined the various classes/modules.
__set_outer_scope(object_class, obj_mod)
__set_outer_scope(class_class, class_mod)
__set_outer_scope(module_class, module_mod)
__set_outer_scope(obj_mod, core_mod)
__set_outer_scope(class_mod, core_mod)
__set_outer_scope(module_mod, core_mod)
# Last but not least we'll need to store the "core" module in the Object class
# so it's available everywhere.
__set_literal_const(object_class, 'core', core_mod)
class core::class::Class:
pub def self.new(parent = core::object::Object) -> Object:
let creator = __get_caller(_)
let parent_iproto = __get_literal_attr(_, parent, '__iproto')
let obj = __set_object(_, @__iproto)
let iproto = __set_object(_, parent_iproto)
__set_literal_attr(obj, '__iproto', iproto)
__set_literal_attr(obj, '__class', self)
__set_outer_scope(obj, creator)
__set_outer_scope(iproto, obj)
obj
pub def new(args...) -> self:
let instance = __set_object(_, @__iproto)
__set_literal_attr(instance, '__class', self)
__send_literal(_, instance, 'construct', 1, 1, args)
instance
class core::object::Object:
def new:
let instance = __set_object(_, @__iproto)
__set_literal_attr(instance, '__class', self)
instance
def construct:
self
pub def class:
@__class
pub def inspect:
'Object'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment