示例#1
0
func AddNativeTo(table *TransitionTable, s *term.SettingS,
	f func(Dwimmer, *term.SettingT, ...term.T) term.T, names ...string) {

	indices := make([]int, len(names))
	for i, name := range names {
		for j, key := range s.Names {
			if name == key {
				indices[i] = j
			}
		}
	}
	g := func(d Dwimmer, s *term.SettingT) term.T {
		args := make([]term.T, len(indices))
		for i, index := range indices {
			args[i] = s.Args[index]
		}
		s.AppendAction(term.AskC(NativeQ.C()))
		result := f(d, s, args...)
		if result != nil {
			s.AppendTerm(core.Answer.T(result))
			s.AppendAction(term.ReturnC(term.Cr(len(s.Args) - 1)))
		}
		return result
	}
	table.Save(s.Setting, NativeTransition(g))
}
示例#2
0
func (r *lexer) setActionResult(head string, e *Expr, n int) {
	var a term.ActionC
	var t term.C
	if e != nil {
		t = toC(e)
	}
	switch strings.ToLower(head) {
	case "return", "reply", "say", "respond", "answer":
		if t == nil {
			t = term.Cc(represent.Int(n))
		}
		a = term.ReturnC(t)
	case "ask", "inquire", "do":
		a = term.AskC(t)
	case "view", "check", "inspect", "see":
		if t == nil {
			t = term.Cc(represent.Int(n))
		}
		a = term.ViewC(t)
	case "replace", "rewrite", "change", "jump", "set":
		a = term.ReplaceC(t, n)
	case "replay", "redo", "repeat":
		a = term.ReplayC(n)
	case "correct", "fix", "debug":
		a = term.CorrectC(n)
	case "meta", "self", "here", "this":
		a = term.MetaC()
	case "close", "dismiss", "stop", "delete", "del", "remove":
		c := toC(e)
		switch c := c.(type) {
		case term.ReferenceC:
			a = term.DeleteC(c.Index)
		}
	default:
		return
	}
	r.actionResult = &a
}
示例#3
0
func TestRepresentations(t *testing.T) {
	d := dwimmer.TestDwimmer()
	defer d.Close()
	template := term.Make("term with argument [] and second half here")
	template2, err := represent.ToTemplate(d, represent.Template(template))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if template != template2 {
		t.Errorf("%v != %v", template, template2)
	}
	setting := term.Init().Append(template)
	setting2, err := represent.ToSetting(d, represent.Setting(setting))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if term.IDSetting(setting) != term.IDSetting(setting2) {
		t.Errorf("%v != %v", setting, setting2)
	}
	actions := []term.ActionC{term.ReturnC(term.Cr(3)), term.ClarifyC(term.Cr(2), core.OK.C()), term.DeleteC(7)}
	for _, action := range actions {
		action2, err := represent.ToActionC(d, represent.ActionC(action))
		if err != nil {
			t.Errorf("received error %v", err)
		}
		if term.IDActionC(action) != term.IDActionC(action2) {
			t.Errorf("%v != %v", action, action2)
		}
	}
	stub := term.Make("stub")
	tm := template.T(stub.T())
	tm2, err := represent.ToT(d, represent.T(tm))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if tm2.String() != tm.String() {
		t.Errorf("%v != %v", tm2, tm)
	}
	rep, err := d.Answer(represent.Explicit.T(represent.T(tm)))
	if err != nil {
		t.Errorf("failed to make representation explicit: %v", err)
	}
	tm3, err := represent.ToT(d, rep)
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if tm3.String() != tm.String() {
		t.Errorf("%v != %v", tm3, tm)
	}
	settingT := term.InitT().AppendTerm(tm)
	settingT2, err := represent.ToSettingT(d, represent.SettingT(settingT))
	if err != nil {
		t.Errorf("received error %v")
	}
	if settingT2.Setting.ID != settingT.Setting.ID {
		t.Errorf("%v != %v", settingT2, settingT)
	}

	n := -127
	n2, err := represent.ToInt(d, represent.Int(n))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if n != n2 {
		t.Errorf("%v != %v", n, n2)
	}

	s := "hello ₳"
	s2, err := represent.ToStr(d, represent.Str(s))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if s != s2 {
		t.Errorf("%s != %s", s, s2)
	}
}
示例#4
0
func TestEncoding(t *testing.T) {
	collection := Collection("testing")
	collection.Empty("testing")
	template := term.Make("test [] term")
	c := template.C(term.ReferenceC{0})
	cc := term.ConstC{template.T(term.Make("stub").T())}
	action := term.ReturnC(c)
	settingS := term.InitS().AppendTemplate(template, "q").AppendAction(action)
	setting := settingS.Setting
	collection.Set(1, term.SaveSetting(setting))
	collection.Set(2, term.SaveC(cc))
	found := 0
	for _, x := range collection.All() {
		if x["key"] == 1 {
			t.Log(x["value"])
			newVal, ok := term.LoadSetting(x["value"])
			if !ok {
				t.Error("failed to load setting")
			}
			newID := newVal.ID
			oldID := setting.ID
			if newID != oldID {
				t.Errorf("%v != %v", newVal, setting)
			}
			found++
		}
		if x["key"] == 2 {
			newVal, ok := term.LoadC(x["value"])
			if !ok {
				t.Error("failed to load C")
			}
			newID := term.IDC(newVal)
			oldID := term.IDC(cc)
			if newID != oldID {
				t.Errorf("%v != %v", newVal, cc)
			}
			found++
		}
	}
	if found < 2 {
		t.Errorf("found %d < 2 items", found)
	}
	{
		savedSetting, ok := collection.Get(1)
		if !ok {
			t.Error("failed to retrieve from database")
		}
		newSetting, ok := term.LoadSetting(savedSetting)
		if !ok {
			t.Errorf("failed to load setting %v", savedSetting)
		}
		newID := newSetting.ID
		oldID := setting.ID
		if newID != oldID {
			t.Errorf("%v != %v", newSetting, setting)
		}
	}
	{
		savedC, ok := collection.Get(2)
		if !ok {
			t.Error("failed to retriev from database")
		}
		newC, ok := term.LoadC(savedC)
		if !ok {
			t.Errorf("failed to load %v", savedC)
		}
		newID := term.IDC(newC)
		oldID := term.IDC(cc)
		if newID != oldID {
			t.Errorf("%v != %v", newC, cc)
		}
	}
}