Browse Source

Add simple test Check function, and List type. Simplyfy calling Run by requiring an Environment only that contains the arguments, me and self.

Beoran 2 years ago
parent
commit
99904e3568
3 changed files with 81 additions and 25 deletions
  1. 16 1
      README
  2. 25 9
      selsl/object.go
  3. 40 15
      selsl/object_test.go

+ 16 - 1
README

@@ -71,7 +71,6 @@ End of teach.
 
 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
@@ -104,6 +103,22 @@ OFS -> of | s .
 BLOCK -> ob SCRIPT END .
 END -> end COMMAND .
 
+Including queries:
+
+SCRIPT -> COMMAND eoc EOC SCRIPT | .
+EOC -> eoc EOC | .
+COMMAND -> word EXPRESSIONS .
+EXPRESSIONS -> EXPRESSION separator EXPRESSIONS | .
+EXPRESSION -> OBJECT | number | BLOCK | QUERY .
+QUERY ->  ISQUERY | QWQUERY.
+ISQUERY -> is EXPRESSION qm .
+QWQUERY -> qw EXPRESSION qm .
+OBJECT-> word ATTRIBUTES .
+ATTRIBUTES -> OFS EXPRESSION | .
+OFS -> of | s .
+BLOCK -> ob SCRIPT END .
+END -> end COMMAND .
+
 Conjunction List
 
 After

+ 25 - 9
selsl/object.go

@@ -9,6 +9,14 @@ func (s Slots) Set(name string, inst Object) Slots {
 	return s
 }
 
+func (s Slots) Get(name string) Object {
+	res, ok := s[name]
+	if !ok {
+		return Nil
+	}
+	return res
+}
+
 func (s Slots) Clone() Slots {
 	clone := Slots{}
 	for k, v := range s {
@@ -48,11 +56,7 @@ type Object interface {
 var Nil Object = NewInstance("nil")
 
 func ParentOf(val Object) Object {
-	res, ok := val.Slots()[ParentSlot]
-	if ok {
-		return res
-	}
-	return Nil
+	return val.Slots().Get(ParentSlot)
 }
 
 type Environment struct {
@@ -75,13 +79,13 @@ func NewEnvironment(parent, me, to Object, args ...Object) *Environment {
 func Send(to Object, message string, me, here Object, args ...Object) Object {
 	env := NewEnvironment(here, me, to, args...)
 	lookup := to
-	value, ok := lookup.Slots()[message]
-	for !ok || value == nil {
+	value := lookup.Slots().Get(message)
+	for value == Nil {
 		lookup = ParentOf(lookup)
 		if lookup == Nil || lookup == nil {
 			panic("Object does not support message " + message)
 		}
-		value, ok = lookup.Slots()[message]
+		value = lookup.Slots().Get(message)
 	}
 	return value.Run(env)
 }
@@ -148,7 +152,19 @@ func (p Primitive) Clone() Object {
 }
 
 func Self(o Object) Object {
-	return o.Slots()[SelfSlot]
+	return o.Slots().Get(SelfSlot)
+}
+
+func Me(o Object) Object {
+	return o.Slots().Get(MeSlot)
+}
+
+func Args(o Object) List {
+	list := o.Slots().Get(ArgsSlot)
+	if list == Nil {
+		return List{}
+	}
+	return list.(List)
 }
 
 func Unary(unary func(target Object) Object) Primitive {

+ 40 - 15
selsl/object_test.go

@@ -2,13 +2,30 @@ package selsl
 
 import "testing"
 
+func Check(t *testing.T, ok bool, mesg ...interface{}) {
+	reporter := t.Logf
+	head := "Check:"
+	if !ok {
+		reporter = t.Errorf
+		head = "Check failed:"
+	}
+
+	if len(mesg) < 1 {
+		reporter(head)
+	} else {
+		reporter(head+mesg[0].(string), mesg[1:]...)
+	}
+}
+
+func TestCheck(t *testing.T) {
+	Check(t, true, "true")
+}
+
 func TestRootType(t *testing.T) {
 	name := Send(RootType, "name", nil, nil)
 	nameString := string(name.(String))
-	if nameString != "Type" {
-		t.Errorf("Root type name wrong: %v", name)
-	}
-	t.Logf("Name root type: %s", nameString)
+	Check(t, nameString == "Type",
+		"Root type name: %v", name)
 }
 
 func TestStringType(t *testing.T) {
@@ -16,19 +33,27 @@ func TestStringType(t *testing.T) {
 	str := String(raw)
 	name := Send(str, "name", nil, nil)
 	nameString := string(name.(String))
-	if nameString != raw {
-		t.Errorf("String type name wrong: %v", name)
-	}
-	t.Logf("Name string type: %s", nameString)
+	Check(t, nameString == raw,
+		"String type name: %v", name)
 }
 
 func TestListType(t *testing.T) {
-	raw := [ "apple", "banana", "pear" ]
-	list :=  List{ String(raw[0]),
-				String(raw[1]),
-				String(raw[2]) }
-	clone := list.Clone()
-	clone[1] = String("cumquat")
+	raw := []string{"apple", "banana", "pear"}
+	list := List{String(raw[0]),
+		String(raw[1]),
+		String(raw[2]),
+	}
+	clone := list.Clone().(List)
+	alt := []string{"apple", "kumquat", "pear"}
+	clone[1] = String(alt[1])
 
-}
+	for i, s := range raw {
+		f := string(list[i].(String))
+		Check(t, s == f, "list %s <-> %s", s, f)
+	}
 
+	for i, s := range alt {
+		f := string(clone[i].(String))
+		Check(t, s == f, "clone %s <-> %s", s, f)
+	}
+}