Example #1
0
func run(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSettingT(d, quotedSetting)
	if err != nil {
		return represent.ConversionError.T(quotedSetting, err)
	}
	result := d.Run(setting)
	if result == nil {
		return NoOutput.T(represent.SettingT(setting))
	}
	return Result.T(represent.T(result), represent.SettingT(setting))
}
Example #2
0
func (d *Dwimmer) Run(setting *term.SettingT) term.T {
	goMeta := func() {
		StartShell(d, Interrupted.T(represent.SettingT(setting)))
	}
	for {
		//d.watchdog(setting)
		char, sent := d.CheckCh()
		if sent {
			if char == 'q' {
				panic("interrupted")
			} else if char == 's' {
				goMeta()
			} else {
				d.Clear()
				d.Debug("(Type [s] to interrupt execution and drop into a shell)")
				d.Debug("(Type [q] to interrupt execution and quit)")
			}
		}
		transition, ok := d.Get(setting.Setting)
		if !ok {
			transition = ElicitAction(d, setting.Copy(), setting.Setting)
		}
		result := transition.Step(d, setting)
		if result != nil {
			return result
		}
	}
}
Example #3
0
func settingForChannel(d dynamics.Dwimmer, context *term.SettingT, channel term.T) term.T {
	c, ok := channel.(term.Channel)
	if !ok {
		return NotAChannel.T()
	}
	return core.Answer.T(represent.SettingT(c.Setting))
}
Example #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)
	}
}
Example #5
0
func (d *Dwimmer) Do(a term.ActionT, s *term.SettingT) term.T {
	switch a.Act {
	case term.Return:
		return a.Args[0]
	case term.Ask:
		Q := a.Args[0]
		child := term.InitT()
		dynamics.SubRun(d, Q, s, child)
		return nil
	case term.View:
		value := a.Args[0]
		if value != nil {
			s.AppendTerm(value)
		} else {
			s.AppendTerm(Closed.T())
		}
		return nil
	case term.Replace:
		//value := a.Args[0]
		//n := a.IntArgs[0]
		//s.Rollback(n).AppendTerm(value)
		return nil
	case term.Replay:
		n := a.IntArgs[0]
		s.Rollback(n)
		return nil
	case term.Clarify:
		Q := a.Args[1]
		//TODO handle null pointers much better...
		//(e.g. one part of an expression may refer to a deleted variable)
		if a.Args[0] == nil {
			s.AppendTerm(Closed.T())
			return nil
		}
		var target *term.SettingT
		channel, err := represent.ToChannel(d, a.Args[0])
		if err == nil {
			target = channel.(term.Channel).Instantiate()
		} else {
			var othererr term.T
			target, othererr = represent.ToSettingT(d, a.Args[0])
			if othererr != nil {
				s.AppendTerm(NotAChannel.T(err))
				return nil
			}
		}
		dynamics.SubRun(d, Q, s, target)
		return nil
	case term.Correct:
		n := a.IntArgs[0]
		old := s.Setting.Rollback(n)
		transition := ElicitAction(d, term.InitT(), old)
		d.Save(old, transition)
		s.AppendTerm(core.OK.T())
		return nil
	case term.Delete:
		n := a.IntArgs[0]
		s.Args[n] = nil
		s.AppendTerm(core.OK.T())
		return nil
	case term.Meta:
		s.AppendTerm(CurrentSetting.T(represent.SettingT(s)))
		return nil
	}
	panic("unknown kind of action")
}
Example #6
0
func OldElicitAction(d dynamics.Dwimmer, context *term.SettingT, subject *term.Setting, hints bool) term.ActionC {
	settingS := addNames(subject)
	ShowSettingS(d, settingS)
	hint_strings := []string{}
	tool_map := make(map[rune]string)
	if hints {
		hint_strings = GetHints(d, context, settingS, 6)
		tools := similarity.SuggestedTemplates(d, subject, 6)
		if len(hint_strings) > 0 || len(tools) > 0 {
			d.Println("")
		}
		if len(tools) > 0 {
			d.Println("")
		}
		tips := []rune{'a', 's', 'd', 'w', 'e', 'j'}
		for i, tool := range tools {
			tool_map[tips[i]] = tool.String()
			d.Println(fmt.Sprintf("%c: %v", tips[i], tool))
		}
		if len(hint_strings) > 0 {
			d.Println("")
		}
		for i, hint := range hint_strings {
			d.Println(fmt.Sprintf("%d. %s", i, hint))
		}
	}
	d.Println("")
	for {
		input := d.Readln(" < ", hint_strings, tool_map)
		if input == "jump up" {
			StartShell(d, Interrupted.T(represent.SettingT(context)))
		}
		a := parsing.ParseAction(input, settingS.Names)
		if a == nil {
			c := parsing.ParseTerm(input, settingS.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)
				}
				d.Println("please input an action (ask, view, return, close, delete, correct, or tell)")
			} else {
				d.Println("that response wasn't parsed correctly")
			}
		}
		if a != nil {
			for i, n := range a.IntArgs {
				if n == -1 {
					a.IntArgs[i] = subject.Size - 1
				}
			}
			return *a
		}
	}
}