Browse Source

Add error type and int type.

Beoran 2 years ago
parent
commit
50736d7655
2 changed files with 33 additions and 41 deletions
  1. 4 1
      README
  2. 29 40
      selsl/object.go

+ 4 - 1
README

@@ -73,7 +73,7 @@ Set hp of me as add 1 with divide hp of me by 2 end end
 
 Create a red door as a door.
 
-open red door with green key
+open red door with green ke
 
 cast cure light at smart bob
 
@@ -228,3 +228,6 @@ with
 within
 without
 
+
+
+

+ 29 - 40
selsl/object.go

@@ -268,72 +268,61 @@ var StringType Type = NewType("String", &InstanceType, nil)
 var IntType Type = NewType("Int", &InstanceType, nil)
 var ListType Type = NewType("List", &InstanceType, nil)
 var MethodType Type = NewType("Method", &InstanceType, nil)
-
+*/
 
 type Error struct {
-	Instance
-	Object error
+	*Instance
+	Error error
 }
 
-func (s Error) String() string {
-	return fmt.Sprintf("%s", s.Object)
+func (e Error) Name() string {
+	return string(fmt.Sprintf("error `%s`", e.Error))
 }
 
-func NewError(name string, Slots Slots, Object error) Error {
-	return Error{Instance: NewInstance(name, ErrorType, Slots),
-		Object: Object}
-}
+var ErrorType Object = NewInstance("Error").
+	Set(ParentSlot, RootType)
 
+var ErrorSlots Slots = Slots{}.Set(ParentSlot, ErrorType)
 
-type Int struct {
-	Instance
-	Object int64
+func (e Error) Slots() Slots {
+	return ErrorSlots
 }
 
-func NewInt(name string, Slots Slots, Object int64) Int {
-	return Int{Instance: NewInstance(name, IntType, Slots),
-		Object: Object}
+func (e Error) Effect() Effect {
+	return EffectThrow
 }
 
-func (i Int) String() string {
-	return fmt.Sprintf("%d", i.Object)
+func (e Error) Run(env Object) Object {
+	return e
 }
 
-type List struct {
-	Instance
-	Object []Object
+func (e Error) Clone() Object {
+	i := e.Instance.Clone().(Instance)
+	return Error{&i, e.Error}
 }
 
-func NewList(name string, Slots Slots, Object ...Object) List {
-	return List{Instance: NewInstance(name, ListType, Slots),
-		Object: Object}
+func (s Error) String() string {
+	return fmt.Sprintf("%s", s.Error)
 }
 
-func (l List) String() string {
-	return fmt.Sprintf("[%v]", l.Object)
+func NewError(name string, Slots Slots, err error) Error {
+	return Error{Instance: NewInstance(name), Error: err}
 }
 
-type Method struct {
-	Instance
-	Signature List
-	Object    func(env Object, self Object, args ...Object) List
+type Int struct {
+	*Instance
+	Int int64
 }
 
-func NewMethod(name string, Slots Slots, signature List,
-	Object func(env Object, self Object, args ...Object) List) Method {
-	return Method{Instance: NewInstance(name, MethodType, Slots),
-		Signature: signature, Object: Object}
+func NewInt(name string, Slots Slots, i int64) Int {
+	return Int{Instance: NewInstance(name), Int: i}
 }
 
-func (m Method) String() string {
-	return fmt.Sprintf("Method: %s", m.Name())
+func (i Int) String() string {
+	return fmt.Sprintf("%d", i.Int)
 }
 
-var _ Object = RootType
-var _ Object = InstanceType
 var _ Object = Error{}
-var _ Object = String{}
+var _ Object = String("")
 var _ Object = Int{}
 var _ Object = List{}
-var _ Object = Method{}
-*/