object.go 6.1 KB

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