|
| 1 | +export BaseStructure, SlotDefinition, |
| 2 | + Top, Object, Class, BuiltInClass |
| 3 | + |
| 4 | +mutable struct BaseStructure |
| 5 | + class_of_reference::Any #= Supposed to be another BaseStructure =# |
| 6 | + slots::Dict{Symbol, Any} |
| 7 | +end |
| 8 | + |
| 9 | +mutable struct SlotDefinition |
| 10 | + name::Symbol |
| 11 | + initform::Any |
| 12 | +end |
| 13 | + |
| 14 | +mutable struct SlotValue |
| 15 | + value::Any |
| 16 | + getter::Any |
| 17 | + setter::Any |
| 18 | +end |
| 19 | + |
| 20 | +function Base.hash(one::SlotDefinition) |
| 21 | + return hash(one.name) |
| 22 | +end |
| 23 | + |
| 24 | +function Base.:(==)(one::SlotDefinition, another::SlotDefinition) |
| 25 | + return one.name == another.name |
| 26 | +end |
| 27 | + |
| 28 | +function Base.:(==)(one::Symbol, another::SlotDefinition) |
| 29 | + return one == another.name |
| 30 | +end |
| 31 | + |
| 32 | +function Base.:(==)(one::SlotDefinition, another::Symbol) |
| 33 | + return one.name == another |
| 34 | +end |
| 35 | + |
| 36 | +function slot_value_factory(slot_name::Symbol, slot_value) |
| 37 | + return SlotValue( |
| 38 | + slot_value, |
| 39 | + (instance) -> getfield(instance, :slots)[slot_name].value, |
| 40 | + (instance, new_value) -> getfield(instance, :slots)[slot_name].value = new_value |
| 41 | + ) |
| 42 | +end |
| 43 | + |
| 44 | +Top = BaseStructure( |
| 45 | + nothing, |
| 46 | + Dict( |
| 47 | + :name=>slot_value_factory(:name, :Top), |
| 48 | + :direct_superclasses=>slot_value_factory(:direct_superclasses, []), |
| 49 | + :direct_slots=>slot_value_factory(:direct_slots, []), |
| 50 | + :class_precedence_list=>slot_value_factory(:class_precedence_list, []), |
| 51 | + :slots=>slot_value_factory(:slots, []), |
| 52 | + ) |
| 53 | +) |
| 54 | + |
| 55 | +pushfirst!(getfield(Top, :slots)[:class_precedence_list].value, Top) |
| 56 | + |
| 57 | +Object = BaseStructure( |
| 58 | + nothing, |
| 59 | + Dict( |
| 60 | + :name=>slot_value_factory(:name, :Object), |
| 61 | + :direct_superclasses=>slot_value_factory(:direct_superclasses, [Top]), |
| 62 | + :direct_slots=>slot_value_factory(:direct_slots, []), |
| 63 | + :class_precedence_list=>slot_value_factory(:class_precedence_list, [Top]), |
| 64 | + :slots=>slot_value_factory(:slots, []), |
| 65 | + ) |
| 66 | +) |
| 67 | + |
| 68 | +pushfirst!(getfield(Object, :slots)[:class_precedence_list].value, Object) |
| 69 | + |
| 70 | +Class = BaseStructure( |
| 71 | + nothing, |
| 72 | + Dict( |
| 73 | + :name => slot_value_factory(:name, :Class), |
| 74 | + :direct_superclasses => slot_value_factory(:direct_superclasses, [Object]), |
| 75 | + :direct_slots => slot_value_factory(:direct_slots, [ |
| 76 | + SlotDefinition(:name, missing), |
| 77 | + SlotDefinition(:direct_superclasses, []), |
| 78 | + SlotDefinition(:class_precedence_list, []), |
| 79 | + SlotDefinition(:direct_slots, []), |
| 80 | + SlotDefinition(:slots, [])]), |
| 81 | + :class_precedence_list => slot_value_factory(:class_precedence_list, [Object, Top]), |
| 82 | + :slots => slot_value_factory(:slots, [ |
| 83 | + SlotDefinition(:name, missing), |
| 84 | + SlotDefinition(:direct_superclasses, []), |
| 85 | + SlotDefinition(:class_precedence_list, []), |
| 86 | + SlotDefinition(:direct_slots, []), |
| 87 | + SlotDefinition(:slots, [])]) |
| 88 | + ) |
| 89 | +) |
| 90 | + |
| 91 | +pushfirst!(getfield(Class, :slots)[:class_precedence_list].value, Class) |
| 92 | + |
| 93 | +setfield!(Class, :class_of_reference, Class) |
| 94 | +setfield!(Object, :class_of_reference, Class) |
| 95 | +setfield!(Top, :class_of_reference, Class) |
| 96 | + |
| 97 | +BuiltInClass = BaseStructure( |
| 98 | + Class, |
| 99 | + Dict( |
| 100 | + :name => slot_value_factory(:name, :BuiltInClass), |
| 101 | + :direct_superclasses => slot_value_factory(:direct_superclasses, [Class]), |
| 102 | + :direct_slots => slot_value_factory(:direct_slots, []), |
| 103 | + :class_precedence_list => slot_value_factory(:class_precedence_list, [Class]), |
| 104 | + :slots => slot_value_factory(:slots, [ |
| 105 | + SlotDefinition(:name, missing), |
| 106 | + SlotDefinition(:direct_superclasses, []), |
| 107 | + SlotDefinition(:class_precedence_list, []), |
| 108 | + SlotDefinition(:direct_slots, []), |
| 109 | + SlotDefinition(:slots, [])]) |
| 110 | + ) |
| 111 | +) |
| 112 | + |
| 113 | +pushfirst!(getfield(BuiltInClass, :slots)[:class_precedence_list].value, BuiltInClass) |
| 114 | + |
| 115 | +function Base.getproperty(obj::BaseStructure, sym::Symbol) |
| 116 | + getfield(obj, :slots)[sym].getter(obj) |
| 117 | +end |
| 118 | + |
| 119 | +function Base.setproperty!(obj::BaseStructure, sym::Symbol, value) |
| 120 | + getfield(obj, :slots)[sym].setter(obj, value) |
| 121 | +end |
0 commit comments