object.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package selsl
  2. import "fmt"
  3. type Slots map[string]Object
  4. func (s Slots) Set(name string, inst Object) Slots {
  5. s[name] = inst
  6. return s
  7. }
  8. func (s Slots) Clone() Slots {
  9. clone := Slots{}
  10. for k, v := range s {
  11. clone[k] = v.Clone()
  12. }
  13. return clone
  14. }
  15. // Constants for special object slot names
  16. const (
  17. ParentSlot = "*parent"
  18. SelfSlot = "*self"
  19. MeSlot = "*me"
  20. ArgsSlot = "*args"
  21. )
  22. // Effect is the effect an object has on return from a Run
  23. type Effect int
  24. // Constants for effects
  25. const (
  26. EffectNone Effect = 0
  27. EffectBreak Effect = 1
  28. EffectReturn Effect = 2
  29. EffectThrow Effect = 4
  30. )
  31. // Object is the interface for all objects in SELSL.
  32. type Object interface {
  33. Name() string
  34. Slots() Slots
  35. Run(env Object) Object
  36. Effect() Effect
  37. Clone() Object
  38. }
  39. var Nil Object = NewInstance("nil")
  40. func ParentOf(val Object) Object {
  41. res, ok := val.Slots()[ParentSlot]
  42. if ok {
  43. return res
  44. }
  45. return Nil
  46. }
  47. type Environment struct {
  48. Instance
  49. }
  50. func NewEnvironment(parent, me, to Object, args ...Object) *Environment {
  51. name := "_env"
  52. if parent != nil {
  53. name = parent.Name() + "_env"
  54. }
  55. res := &Environment{Instance{name: name, slots: Slots{}}}
  56. res.Set(ParentSlot, parent)
  57. res.Set(SelfSlot, to)
  58. res.Set(ArgsSlot, List(args))
  59. res.Set(MeSlot, me)
  60. return res
  61. }
  62. func Send(to Object, message string, me, here Object, args ...Object) Object {
  63. env := NewEnvironment(here, me, to, args...)
  64. lookup := to
  65. value, ok := lookup.Slots()[message]
  66. for !ok || value == nil {
  67. lookup = ParentOf(lookup)
  68. if lookup == Nil || lookup == nil {
  69. panic("Object does not support message " + message)
  70. }
  71. value, ok = lookup.Slots()[message]
  72. }
  73. return value.Run(env)
  74. }
  75. type Instance struct {
  76. name string
  77. slots Slots
  78. }
  79. func (i Instance) Name() string {
  80. return i.name
  81. }
  82. func (i Instance) Slots() Slots {
  83. return i.slots
  84. }
  85. func (i Instance) Effect() Effect {
  86. return EffectNone
  87. }
  88. func (i Instance) Run(env Object) Object {
  89. return i
  90. }
  91. func (i *Instance) Set(name string, inst Object) *Instance {
  92. i.slots.Set(name, inst)
  93. return i
  94. }
  95. func (i Instance) DeepCopy() Instance {
  96. return Instance{name: i.name + "", slots: i.slots.Clone()}
  97. }
  98. func (i Instance) Clone() Object {
  99. return i.DeepCopy()
  100. }
  101. func NewInstance(name string) *Instance {
  102. return &Instance{name: name, slots: Slots{}}
  103. }
  104. type Primitive func(env Object) Object
  105. func (p Primitive) Name() string {
  106. return "primitive"
  107. }
  108. func (p Primitive) Slots() Slots {
  109. emptySlots := Slots{}
  110. return emptySlots
  111. }
  112. func (p Primitive) Effect() Effect {
  113. return EffectNone
  114. }
  115. func (p Primitive) Run(env Object) Object {
  116. return p(env)
  117. }
  118. func (p Primitive) Clone() Object {
  119. return p
  120. }
  121. func Self(o Object) Object {
  122. return o.Slots()[SelfSlot]
  123. }
  124. func Unary(unary func(target Object) Object) Primitive {
  125. return func(env Object) Object {
  126. return unary(Self(env))
  127. }
  128. }
  129. func Nullary(nullary func() Object) Primitive {
  130. return func(env Object) Object {
  131. return nullary()
  132. }
  133. }
  134. type String string
  135. func (s String) Name() string {
  136. return string(s)
  137. }
  138. var StringType Object = NewInstance("String").
  139. Set(ParentSlot, RootType)
  140. var StringSlots Slots = Slots{}.Set(ParentSlot, StringType)
  141. func (s String) Slots() Slots {
  142. return StringSlots
  143. }
  144. func (s String) Effect() Effect {
  145. return EffectNone
  146. }
  147. func (s String) Run(env Object) Object {
  148. return s
  149. }
  150. func (s String) Clone() Object {
  151. return String(s + "")
  152. }
  153. func NameOf(o Object) Object {
  154. return String(o.Name())
  155. }
  156. func NameString(o Object) func() Object {
  157. return func() Object {
  158. return NameOf(o)
  159. }
  160. }
  161. var RootType Object = NewInstance("Type").Set("name", Unary(NameOf))
  162. type List []Object
  163. func (l List) Name() string {
  164. res := fmt.Sprintf("list: ")
  165. sep := ""
  166. for _, o := range l {
  167. res = fmt.Sprintf("%s %s %s", res, sep, o.Name())
  168. sep = "and"
  169. }
  170. res += ";"
  171. return res
  172. }
  173. var ListType Object = NewInstance("List").
  174. Set(ParentSlot, RootType)
  175. var ListSlots Slots = Slots{}.Set(ParentSlot, ListType)
  176. func (l List) Slots() Slots {
  177. return ListSlots
  178. }
  179. func (l List) Effect() Effect {
  180. return EffectNone
  181. }
  182. func (l List) Run(env Object) Object {
  183. return l
  184. }
  185. func (l List) DeepCopy() List {
  186. res := List{}
  187. for _, o := range l {
  188. res = append(res, o.Clone())
  189. }
  190. return res
  191. }
  192. func (l List) Clone() Object {
  193. return l.DeepCopy()
  194. }
  195. /*
  196. var RootType Object = NewInstance("Type").Slot("string")
  197. var InstanceType Type = NewType("Instance", &RootType, nil)
  198. var ErrorType Type = NewType("Error", &InstanceType, nil)
  199. var StringType Type = NewType("String", &InstanceType, nil)
  200. var IntType Type = NewType("Int", &InstanceType, nil)
  201. var ListType Type = NewType("List", &InstanceType, nil)
  202. var MethodType Type = NewType("Method", &InstanceType, nil)
  203. type Error struct {
  204. Instance
  205. Object error
  206. }
  207. func (s Error) String() string {
  208. return fmt.Sprintf("%s", s.Object)
  209. }
  210. func NewError(name string, Slots Slots, Object error) Error {
  211. return Error{Instance: NewInstance(name, ErrorType, Slots),
  212. Object: Object}
  213. }
  214. type Int struct {
  215. Instance
  216. Object int64
  217. }
  218. func NewInt(name string, Slots Slots, Object int64) Int {
  219. return Int{Instance: NewInstance(name, IntType, Slots),
  220. Object: Object}
  221. }
  222. func (i Int) String() string {
  223. return fmt.Sprintf("%d", i.Object)
  224. }
  225. type List struct {
  226. Instance
  227. Object []Object
  228. }
  229. func NewList(name string, Slots Slots, Object ...Object) List {
  230. return List{Instance: NewInstance(name, ListType, Slots),
  231. Object: Object}
  232. }
  233. func (l List) String() string {
  234. return fmt.Sprintf("[%v]", l.Object)
  235. }
  236. type Method struct {
  237. Instance
  238. Signature List
  239. Object func(env Object, self Object, args ...Object) List
  240. }
  241. func NewMethod(name string, Slots Slots, signature List,
  242. Object func(env Object, self Object, args ...Object) List) Method {
  243. return Method{Instance: NewInstance(name, MethodType, Slots),
  244. Signature: signature, Object: Object}
  245. }
  246. func (m Method) String() string {
  247. return fmt.Sprintf("Method: %s", m.Name())
  248. }
  249. var _ Object = RootType
  250. var _ Object = InstanceType
  251. var _ Object = Error{}
  252. var _ Object = String{}
  253. var _ Object = Int{}
  254. var _ Object = List{}
  255. var _ Object = Method{}
  256. */