예제 #1
0
func getInput(d dynamics.Dwimmer, context *term.SettingT, hintstrings, toolmap term.T) term.T {
	quotedHints, err := represent.ToList(d, hintstrings)
	if err != nil {
		return represent.ConversionError.T(hintstrings, err)
	}
	hints := make([]string, len(quotedHints))
	for i, quoted := range quotedHints {
		hints[i], err = represent.ToStr(d, quoted)
		if err != nil {
			return represent.ConversionError.T(quoted, err)
		}
	}
	tools := make(map[rune]string)
	for toolmap.Head() != maps.Empty {
		switch toolmap.Head() {
		case maps.Cons:
			vs := toolmap.Values()
			c, err := represent.ToRune(d, vs[0])
			if err != nil {
				return represent.ConversionError.T(vs[0], err)
			}
			s, err := represent.ToStr(d, vs[1])
			if err != nil {
				return represent.ConversionError.T(vs[1], err)
			}
			tools[c] = s
			toolmap = vs[2]
		default:
			context.AppendTerm(UnrecognizedDictionary.T())
			return nil
		}
	}
	input := d.Readln(" < ", hints, tools)
	return core.Answer.T(represent.Str(input))
}
예제 #2
0
func parseInput(d dynamics.Dwimmer, context *term.SettingT, quotedInput, quotedNames term.T) term.T {
	input, err := represent.ToStr(d, quotedInput)
	if err != nil {
		return represent.ConversionError.T(quotedInput, err)
	}
	quotedList, err := represent.ToList(d, quotedNames)
	if err != nil {
		return represent.ConversionError.T(quotedNames, err)
	}
	names := make([]string, len(quotedList))
	for i, quoted := range quotedList {
		names[i], err = represent.ToStr(d, quoted)
		if err != nil {
			return represent.ConversionError.T(quoted, err)
		}
	}
	if input == "jump up" {
		return GoMeta.T()
	}
	a := parsing.ParseAction(input, names)
	if a == nil {
		c := parsing.ParseTerm(input, names)
		if c != nil {
			switch c := c.(type) {
			case *term.CompoundC:
				if questionLike(c) {
					a = new(term.ActionC)
					*a = term.AskC(c)
				}
			case term.ReferenceC:
				a = new(term.ActionC)
				*a = term.ViewC(c)
			}
			context.AppendTerm(IsTerm.T())
			return nil
		} else {
			context.AppendTerm(ParseError.T())
			return nil
		}
	} else {
		for _, n := range a.IntArgs {
			if n < 0 {
				context.AppendTerm(ParseError.T())
				return nil
			}
		}
		return core.Answer.T(represent.ActionC(*a))
	}
}
예제 #3
0
func getChar(d dynamics.Dwimmer, s *term.SettingT, quotedN, quotedS term.T) term.T {
	n, err := represent.ToInt(d, quotedN)
	if err != nil {
		return term.Make("asked to index into a string, but received " +
			"[] while converting the index to native format").T(err)
	}
	str, err := represent.ToStr(d, quotedS)
	if err != nil {
		return term.Make("asked to index into a string, but received " +
			"[] while converting the string to native format").T(err)
	}
	return core.Answer.T(represent.Rune(rune(str[n])))
}
예제 #4
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)
	}
}