Exemplo n.º 1
0
func init() {
	QNames := []term.TemplateID{Plus, Times, Minus, Equal, Less, More,
		LessOrEqual, MoreOrEqual,
		Divide,
	}
	QFuncs := [](func(dynamics.Dwimmer, *term.SettingT, ...term.T) term.T){
		dynamics.Args2(addNative),
		dynamics.Args2(multiplyNative),
		dynamics.Args2(subtractNative),
		dynamics.Args2(testEquality),
		dynamics.Args2(testLess),
		dynamics.Args2(testMore),
		dynamics.Args2(testLessOrEqual),
		dynamics.Args2(testMoreOrEqual),
		dynamics.Args2(divideNative),
	}

	for i := range QNames {
		s := term.InitS()
		s = dynamics.ExpectQuestion(s, QNames[i], "Q", "a", "b")
		s = dynamics.AddSimple(s, term.ViewS(term.Sr("a")))
		s.AppendTemplate(term.Int(0).Head())
		s = dynamics.AddSimple(s, term.ViewS(term.Sr("b")))
		s.AppendTemplate(term.Int(0).Head())
		dynamics.AddNative(s, QFuncs[i], "a", "b")
	}
}
Exemplo n.º 2
0
func divideNative(d dynamics.Dwimmer, s *term.SettingT, n, m term.T) term.T {
	a := int(n.(term.Int))
	b := int(m.(term.Int))
	remainder := a % b
	if remainder == 0 {
		return core.Answer.T(term.Int(a / b))
	}
	return QuotientAndRemainder.T(term.Int(a/b), term.Int(remainder))
}
Exemplo n.º 3
0
func init() {
	s := term.InitS()
	s = dynamics.ExpectQuestion(s, GetTemplate, "Q", "t")
	s = dynamics.AddSimple(s, term.ViewS(term.Sr("t")))

	t := s.Copy().AppendTemplate(term.Quoted{}.Head())
	t = dynamics.AddSimple(t, term.AskS(Explicit.S(term.Sr("t"))))
	t = dynamics.ExpectAnswer(t, core.Answer, "A", "explicit")
	t = dynamics.AddSimple(t, term.AskS(GetTemplate.S(term.Sr("explicit"))))
	t = dynamics.ExpectAnswer(t, core.Answer, "A2", "result")
	t = dynamics.AddSimple(t, term.ReturnS(core.Answer.S(term.Sr("result"))))

	t = s.Copy().AppendTemplate(QuotedQuoteT, "q")
	dynamics.AddSimple(t, term.ReturnS(core.Answer.S(
		term.Sc(Template(term.Quoted{}.Head())),
	)))

	t = s.Copy().AppendTemplate(QuotedIntT, "q")
	dynamics.AddSimple(t, term.ReturnS(core.Answer.S(
		term.Sc(Template(term.Int(0).Head())),
	)))

	t = s.Copy().AppendTemplate(QuotedStrT, "q")
	dynamics.AddSimple(t, term.ReturnS(core.Answer.S(
		term.Sc(Template(term.Str(0).Head())),
	)))

	t = s.Copy().AppendTemplate(QuotedWrapperT, "q")
	dynamics.AddSimple(t, term.ReturnS(core.Answer.S(
		term.Sc(Template(term.Wrap(nil).Head())),
	)))

	t = s.Copy().AppendTemplate(QuotedCompoundT, "template", "args")
	t = dynamics.AddSimple(t, term.ReturnS(core.Answer.S(term.Sr("template"))))
}
Exemplo n.º 4
0
func getID(d dynamics.Dwimmer, s *term.SettingT, quoted term.T) term.T {
	var result int
	switch quoted.Head() {
	case represent.QuotedSetting:
		setting, err := represent.ToSetting(d, quoted)
		if err != nil {
			return term.Make("was asked to find the ID of a setting, " +
				"but while converting to a setting received []").T(err)
		}
		result = int(setting.ID)
	case term.Int(0).Head():
		result = int(term.IDer.PackInt(int(quoted.(term.Int))).(intern.ID))
	case term.Str("").Head():
		result = int(term.IDer.PackString(string(quoted.(term.Str))).(intern.ID))
	}
	return core.Answer.T(represent.Int(result))
}
Exemplo n.º 5
0
func Int(n int) term.T {
	return term.Int(n)
	/*
		switch {
		case n == 0:
			return ints.Zero.T()
		case n == 1:
			return ints.One.T()
		case n < 0:
			return ints.Negative.T(Int(-n))
		case n%2 == 0:
			return ints.Double.T(Int(n / 2))
		case n%2 == 1:
			return ints.DoublePlusOne.T(Int(n / 2))
		default:
			panic("unreachable")
		}
	*/
}
Exemplo n.º 6
0
func multiplyNative(d dynamics.Dwimmer, s *term.SettingT, n, m term.T) term.T {
	return core.Answer.T(term.Int(int(n.(term.Int)) * int(m.(term.Int))))
}
Exemplo n.º 7
0
func subtractNative(d dynamics.Dwimmer, s *term.SettingT, n, m term.T) term.T {
	return core.Answer.T(term.Int(int(n.(term.Int)) - int(m.(term.Int))))
}
Exemplo n.º 8
0
func SuggestedTemplates(d dynamics.Dwimmer, s *term.Setting, n int, templatess ...[]*term.Template) (templates []*term.Template) {
	for _, x := range templatess {
		templates = append(templates, x...)
	}
	if s.Size == 0 {
		return
	}
	defer func() {
		if len(templates) > n {
			templates = templates[:n]
		}
	}()
	ShouldAddTemplate := func(template *term.Template) bool {
		for _, other := range templates {
			if other.String() == template.String() {
				return false
			}
		}
		excluded := []term.T{
			term.Int(0), term.Str(""), term.Quoted{}, term.Wrapper{},
		}
		for _, exclude := range excluded {
			if template == exclude.Head().Template() {
				return false
			}
		}
		excludedIDs := []term.TemplateID{
			dynamics.ParentChannel, dynamics.OpenChannel,
		}
		for _, exclude := range excludedIDs {
			if template.ID() == exclude {
				return false
			}
		}
		return true
	}
	AddTemplate := func(template *term.Template) {
		if ShouldAddTemplate(template) {
			templates = append(templates, template)
		}
	}
	AddTemplates := func(action term.ActionC) {
		actionTemplates := action.AllTemplates()
		for _, template := range actionTemplates {
			AddTemplate(template)
		}
	}

	actions, _ := Suggestions(d, s, n)
	for _, action := range actions {
		AddTemplates(action)
	}
	lastLine := s.Last
	switch line := lastLine.(type) {
	case term.ActionCID:
		AddTemplates(line.ActionC())
	case term.TemplateID:
		AddTemplate(line.Template())
	}
	if len(templates) < n {
		templates = SuggestedTemplates(d, s.Previous, n, templates)
	}
	return
}
Exemplo n.º 9
0
func length(d dynamics.Dwimmer, s *term.SettingT, a term.T) term.T {
	return core.Answer.T(term.Int(len(string(a.(term.Str)))))
}