コード例 #1
0
ファイル: strings.go プロジェクト: paulfchristiano/dwimmer
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])))
}
コード例 #2
0
ファイル: elicit.go プロジェクト: paulfchristiano/dwimmer
func setTransition(d dynamics.Dwimmer, s *term.SettingT, quotedTransition, quotedSetting term.T) term.T {
	transition, err := represent.ToTransition(d, quotedTransition)
	if err != nil {
		return term.Make("asked to set a setting to transition [], "+
			"but while converting to a transition received []").T(quotedTransition, err)
	}
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to set a transition in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	d.Save(setting, transition)
	return core.OK.T()
}
コード例 #3
0
ファイル: ui.go プロジェクト: paulfchristiano/dwimmer
func nativeSetCursor(d dynamics.Dwimmer, s *term.SettingT, xt, yt term.T) term.T {
	x, err := represent.ToInt(d, xt)
	if err != nil {
		return term.Make("asked to move cursor, but received [] "+
			"while converting coordinate [] to an integer").T(err, xt)
	}
	y, err := represent.ToInt(d, yt)
	if err != nil {
		return term.Make("asked to move cursor, but received [] "+
			"while converting coordinate [] to an integer").T(err, yt)
	}
	d.SetCursor(x, y)
	return core.OK.T()
}
コード例 #4
0
ファイル: similarity.go プロジェクト: paulfchristiano/dwimmer
func nativeSuggestedActions(d dynamics.Dwimmer, context *term.SettingT, quotedSetting, quotedN term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("was asked to generate suggestions in setting [], "+
			"but received [] while converting it to native form").T(quotedSetting, err)
	}
	n, err := represent.ToInt(d, quotedN)
	if err != nil {
		return term.Make("was asked to generate [] suggestions, "+
			"but received [] while converting it to native form").T(quotedN, err)
	}
	suggestions, _ := Suggestions(d, setting, n)
	quotedSuggestions := make([]term.T, len(suggestions))
	for i, suggestion := range suggestions {
		quotedSuggestions[i] = represent.ActionC(suggestion)
	}
	return core.Answer.T(represent.List(quotedSuggestions))

}
コード例 #5
0
ファイル: ui.go プロジェクト: paulfchristiano/dwimmer
func nativePutChar(d dynamics.Dwimmer, s *term.SettingT, char, xt, yt term.T) term.T {
	c, err := represent.ToRune(d, char)
	if err != nil {
		return term.Make("asked to write character, but received [] " +
			"while converting to a character").T(err)
	}
	x, err := represent.ToInt(d, xt)
	if err != nil {
		return term.Make("asked to write character, but received [] "+
			"while converting coordinate [] to an integer").T(err, xt)
	}
	y, err := represent.ToInt(d, yt)
	if err != nil {
		return term.Make("asked to write character, but received [] "+
			"while converting coordinate [] to an integer").T(err, yt)
	}
	d.SetCursor(x, y)
	d.PrintCh(c)
	return core.OK.T()
}
コード例 #6
0
ファイル: elicit.go プロジェクト: paulfchristiano/dwimmer
func fallThrough(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	settingT, err := represent.ToSettingT(d, quotedSetting)
	if err != nil {
		return term.Make("asked to decide what to do in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	transition := ElicitAction(d, s, settingT.Setting)
	if shouldSave(transition) {
		d.Save(settingT.Setting, transition)
	}
	return TakeTransition.T(represent.Transition(transition))
}
コード例 #7
0
ファイル: elicit.go プロジェクト: paulfchristiano/dwimmer
func getTransition(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to get a transition in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	result, ok := d.Get(setting)
	if !ok {
		return NoCompiledAction.T()
	}
	return core.Answer.T(represent.Transition(result))
}
コード例 #8
0
ファイル: elicit.go プロジェクト: paulfchristiano/dwimmer
func findAction(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to decide what to do in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	transition, ok := d.Get(setting)
	if !ok {
		transition := ElicitAction(d, s, setting)
		d.Save(setting, transition)
	}
	return core.Answer.T(represent.Transition(transition))
}
コード例 #9
0
ファイル: elicit.go プロジェクト: paulfchristiano/dwimmer
func getContinuations(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to return the continuations from a setting, " +
			"but while converting to a setting received []").T(err)
	}
	continuations := d.Continuations(setting)
	result := make([]term.T, len(continuations))
	for i, c := range continuations {
		result[i] = represent.Setting(c)
	}
	return core.Answer.T(represent.List(result))
}
コード例 #10
0
ファイル: ids.go プロジェクト: paulfchristiano/dwimmer
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))
}
コード例 #11
0
ファイル: questions.go プロジェクト: paulfchristiano/dwimmer
package represent

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	RepresentSetting = term.Make("what term represents the setting []?")
)

func init() {
	s := term.InitS()
	s = dynamics.ExpectQuestion(s, RepresentSetting, "Q", "s")
	dynamics.AddNative(s, dynamics.Args1(quote), "s")
}

func quote(d dynamics.Dwimmer, s *term.SettingT, t term.T) term.T {
	return core.Answer.T(term.Quote(t))
}
コード例 #12
0
ファイル: quote.go プロジェクト: paulfchristiano/dwimmer
package represent

import (
	"fmt"

	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/data/lists"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	QuotedNil = term.Make("nothing")

	QuotedRune     = term.Make("the character with unicode representation []")
	ByRunes        = term.Make("the string containing the sequence of characters []")
	QuotedSetting  = term.Make("the setting with the list of lines []")
	QuotedSettingT = term.Make("the concrete setting with template [] " +
		"and list of arguments []")
	QuotedTemplate = term.Make("the term template that has parts []")

	ActionLookup = map[term.Action]term.TemplateID{
		term.Return: term.Make("the parametrized action that returns the instantiation of its first argument"),
		term.View:   term.Make("the parametrized action that views the instantiation of its first argument"),
		term.Ask:    term.Make("the parametrized action that asks the instantiation of its first argument"),
		term.Replace: term.Make("the parametrized action that replaces the line given by its first index with " +
			"with the instantiation of its first argument"),
		term.Replay: term.Make("the parametrized action that replays the line given by its first index"),
		term.Clarify: term.Make("the parametrized action that sends the instantiation of its first argument " +
			"to the instantiation of its second argument"),
		term.Correct: term.Make("the parametrized action that prompts the user to correct its first index"),
コード例 #13
0
ファイル: store.go プロジェクト: paulfchristiano/dwimmer
package store

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	GetState = term.Make("what is the current state of the interpreter?")
	SetState = term.Make("the state of the interpreter should become []")
)

func init() {
	s := dynamics.ExpectQuestion(term.InitS(), GetState, "Q")
	dynamics.AddNative(s, dynamics.Args0(getState))

	s = dynamics.ExpectQuestion(term.InitS(), SetState, "Q", "s")
	dynamics.AddNative(s, dynamics.Args1(setState), "s")
}

func getState(d dynamics.Dwimmer, s *term.SettingT) term.T {
	return core.Answer.T(d.GetStorage())
}

func setState(d dynamics.Dwimmer, s *term.SettingT, t term.T) term.T {
	d.SetStorage(t)
	return core.OK.T()
}
コード例 #14
0
ファイル: misc.go プロジェクト: paulfchristiano/dwimmer
package meta

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/data/represent"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	SettingForChannel = term.Make("what setting corresponds to the channel []?")
	NotAChannel       = term.Make("the argument is not a channel")
)

func init() {
	s := dynamics.ExpectQuestion(term.InitS(), SettingForChannel, "Q", "c")
	s = dynamics.AddSimple(s, term.ViewS(term.Sr("c")))
	s = s.AppendTemplate(term.Channel{}.Head())
	dynamics.AddNative(s, dynamics.Args1(settingForChannel), "c")
}

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))
}
コード例 #15
0
ファイル: strings.go プロジェクト: paulfchristiano/dwimmer
)

func concat(d dynamics.Dwimmer, s *term.SettingT, a, b term.T) term.T {
	return core.Answer.T(term.Str(string(a.(term.Str)) + string(b.(term.Str))))
}

func length(d dynamics.Dwimmer, s *term.SettingT, a term.T) term.T {
	return core.Answer.T(term.Int(len(string(a.(term.Str)))))
}

func bracketed(d dynamics.Dwimmer, s *term.SettingT, a term.T) term.T {
	return core.Answer.T(term.Str(fmt.Sprint("[%s]", string(a.(term.Str)))))
}

var (
	Len       = term.Make("what is the length of []?")
	Concat    = term.Make("what is the result of concatenating [] to []?")
	Bracketed = term.Make("what is the string formed by enclosing [] in brackets?")
	GetChar   = term.Make("what is the character that comes after [] others in the string []?")
)

func init() {
	s := term.InitS()
	s = dynamics.ExpectQuestion(s, Len, "Q", "s")
	s = dynamics.AddSimple(s, term.ViewS(term.Sr("s")))
	s.AppendTemplate(term.Str("").Head())
	dynamics.AddNative(s, dynamics.Args1(length), "s")

	s = term.InitS()
	s = dynamics.ExpectQuestion(s, Bracketed, "Q", "s")
	s = dynamics.AddSimple(s, term.ViewS(term.Sr("s")))
コード例 #16
0
ファイル: similarity.go プロジェクト: paulfchristiano/dwimmer
package similarity

import (
	"container/heap"

	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/data/represent"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
	"github.com/xrash/smetrics"
)

var (
	RelatedSettings = term.Make("what settings have been encountered before that are most analogous " +
		"to the setting [], and what is their relationship to that setting?")
	SuggestedActions = term.Make("what actions is the user most likely to take in setting [], " +
		"based on analogies with similar settings they have encountered in the past? " +
		"[] items should be returned as a list, sorted with most promising first")
)

func init() {
	dynamics.AddNativeResponse(SuggestedActions, 2, dynamics.Args2(nativeSuggestedActions))
}

func nativeSuggestedActions(d dynamics.Dwimmer, context *term.SettingT, quotedSetting, quotedN term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("was asked to generate suggestions in setting [], "+
			"but received [] while converting it to native form").T(quotedSetting, err)
	}
	n, err := represent.ToInt(d, quotedN)
コード例 #17
0
ファイル: quote_test.go プロジェクト: paulfchristiano/dwimmer
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)
	}
}
コード例 #18
0
ファイル: functions.go プロジェクト: paulfchristiano/dwimmer
package functions

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/data/lists"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	Apply   = term.Make("what is the result of applying function [] to argument []?")
	Compose = term.Make("the function that applies [], then applies [] to the result")
	Map     = term.Make("the function that applies [] to each element of its argument, which should be a list")
	Fold    = term.Make("what is the result of using [] to combine all of the arguments " +
		"in the list [] into one, starting from [] and assuming that the function is associative?")

	Apply2 = term.Make("what is the result of applying function [] to the arguments [] and []?")
)

var applyState, applyState2 *term.SettingS

func ApplicationSetting() *term.SettingS {
	return applyState.Copy()
}

func ApplicationSetting2() *term.SettingS {
	return applyState2.Copy()
}

func init() {
	applyState = term.InitS()
コード例 #19
0
ファイル: examples.go プロジェクト: paulfchristiano/dwimmer
package dynamics

import "github.com/paulfchristiano/dwimmer/term"

func ExpectQuestion(s *term.SettingS, t term.TemplateID, names ...string) *term.SettingS {
	result := s.Copy()
	result.AppendTemplate(ParentChannel, names[0])
	result.AppendTemplate(t, names[1:]...)
	return result
}

func ExpectAnswer(s *term.SettingS, t term.TemplateID, names ...string) *term.SettingS {
	result := s.Copy()
	result.AppendTemplate(OpenChannel, names[0])
	result.AppendTemplate(t, names[1:]...)
	return result
}

func Parent(s *term.SettingT) term.T {
	return ParentChannel.T(term.MakeChannel(s))
}

var (
	OpenChannel   = term.Make("@[]")
	ParentChannel = term.Make("@[]*")
)
コード例 #20
0
ファイル: dummy.go プロジェクト: paulfchristiano/dwimmer
package storage

import "github.com/paulfchristiano/dwimmer/term"

func Dummy() *DummyStorage {
	return &DummyStorage{}
}

var (
	DummyState = term.Make("a placeholder returned when accessing the state of a test harness")
)

type DummyStorage struct{}

func (_ *DummyStorage) CloseStorage()      {}
func (_ *DummyStorage) GetStorage() term.T { return DummyState.T() }
func (_ *DummyStorage) SetStorage(term.T)  {}

var _ StorageImplementer = &DummyStorage{}
コード例 #21
0
ファイル: shell.go プロジェクト: paulfchristiano/dwimmer
package dwimmer

import (
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	RunShell     = term.Make("run a shell with prompt [], and reply with the user's return value")
	shellSetting = term.InitT().AppendTerm(term.Make("[Home]").T())
	oldShells    []*term.SettingT
)

func startWithPrompt(d dynamics.Dwimmer, setting *term.SettingT, question term.T) term.T {
	return StartShell(d, dynamics.Parent(setting), question)
}

func pushShell() {
	oldShells = append(oldShells, shellSetting.Copy())
}

func popShell() {
	shellSetting = oldShells[len(oldShells)-1]
	oldShells = oldShells[:len(oldShells)-1]
}

func Show(t term.T) {
	shellSetting.AppendTerm(t)
}

func StartShell(d dynamics.Dwimmer, ts ...term.T) term.T {
コード例 #22
0
ファイル: get.go プロジェクト: paulfchristiano/dwimmer
package represent

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	GetTemplate             = term.Make("what is the template of term []?")
	GetArguments            = term.Make("what are the arguments of term []?")
	GetTemplateAndArguments = term.Make("what are the template and arguments of term []?")
	GetSetting              = term.Make("what is the abstract setting associated with the concrete setting []?")
	TemplateAndArguments    = term.Make("the template is [] and the arguments are []")
	NumArguments            = term.Make("how many arguments does the term [] have?")
)

func init() {
	s := term.InitS()
	s = dynamics.ExpectQuestion(s, GetSetting, "Q", "s")
	s = dynamics.AddSimple(s, term.ViewS(term.Sr("s")))
	s.AppendTemplate(QuotedSettingT, "lines", "arguments", "children")
	s = dynamics.AddSimple(s, term.ReturnS(core.Answer.S(QuotedSetting.S(term.Sr("lines")))))
}

func init() {
	s := term.InitS()
	s = dynamics.ExpectQuestion(s, GetTemplateAndArguments, "Q", "t")
	s = dynamics.AddSimple(s, term.AskS(GetTemplate.S(term.Sr("t"))))
	s = dynamics.ExpectAnswer(s, core.Answer, "A", "template")
	s = dynamics.AddSimple(s, term.AskS(GetArguments.S(term.Sr("t"))))
コード例 #23
0
ファイル: basic.go プロジェクト: paulfchristiano/dwimmer
package maps

import "github.com/paulfchristiano/dwimmer/term"

var (
	Empty    = term.Make("the map that doesn't send anything to anything")
	Cons     = term.Make("the map sends [] to [] and maps all other inputs according to []")
	Lookup   = term.Make("what is the image of [] in the map []?")
	Insert   = term.Make("what map maps [] to [] and all other inputs according to []?")
	NotFound = term.Make("the requested key was not found")
)
コード例 #24
0
ファイル: dwimmer.go プロジェクト: paulfchristiano/dwimmer
	Stack
	Transitions
	ui.UIImplementer
	storage.StorageImplementer

	Close()
}

var DefaultInitializers []term.T

func AddInitializer(t term.T) {
	DefaultInitializers = append(DefaultInitializers, t)
}

var SetupState = term.Make("initialize the interpreter's state")

func init() {
	AddInitializer(SetupState.T())
}

func SubRun(d Dwimmer, Q term.T, parent *term.SettingT, optionalChild ...*term.SettingT) term.T {
	var child *term.SettingT
	if len(optionalChild) == 1 {
		child = optionalChild[0]
	} else {
		child = term.InitT()
	}
	child.AppendTerm(Parent(parent))
	child.AppendTerm(Q)
	d.Push(Q)
コード例 #25
0
ファイル: dwimmer.go プロジェクト: paulfchristiano/dwimmer
		StorageImplementer: store,
		Stack:              &dynamics.BasicStack{},
	}
	result.InitUI()
	defer func() {
		e := recover()
		if e != nil {
			result.Close()
			panic(e)
		}
	}()
	RunDefaultInitializers(result)
	return result
}

var Initialization = term.Make("initializing a new dwimmer")

func RunDefaultInitializers(d dynamics.Dwimmer) {
	s := term.InitT()
	s.AppendTerm(Initialization.T())
	for _, t := range dynamics.DefaultInitializers {
		c := term.InitT()
		dynamics.SubRun(d, t, s, c)
	}
}

func DoC(d dynamics.Dwimmer, a term.ActionC, s *term.SettingT) term.T {
	return d.Do(a.Instantiate(s.Args), s)
}

func addToStack(d dynamics.Dwimmer, t term.T) {
コード例 #26
0
ファイル: ids.go プロジェクト: paulfchristiano/dwimmer
package meta

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/data/represent"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
	"github.com/paulfchristiano/dwimmer/term/intern"
)

var (
	GetID         = term.Make("what is the internal identifier of the object []?")
	SettingFromID = term.Make("what is the setting with ID []?")
	NoSetting     = term.Make("there is no setting with that ID")
)

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))
	}
コード例 #27
0
ファイル: ints.go プロジェクト: paulfchristiano/dwimmer
package ints

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	DoublePlusOne = term.Make("two times [] plus one")
	Double        = term.Make("two times []")
	Zero          = term.Make("the number zero")
	One           = term.Make("the number one")
	Negative      = term.Make("negative []")
)

/*
var (
	ExplicitQ = term.Make("what is []? the representation should not involve any wrapped Go objects")
)

func makeExplicit(d dynamics.Dwimmer, s *term.SettingT, n term.T) term.T {
	return core.Answer.T(Explicit(int(n.(term.Int))))
}

func init() {
	s := term.InitS().AppendTemplate(ExplicitQ, "x")
	s = dynamics.AddSimple(s, term.ViewS(term.Sr("x")))

	for _, tm := range []term.TemplateID{DoublePlusOne, Double, Negative} {
		t := s.Copy().AppendTemplate(tm, "y")
コード例 #28
0
func AddSimple(s *term.SettingS, a term.ActionS) *term.SettingS {
	return DefaultTransitions.SaveSimpleS(s, a)
}

func AddNativeResponse(t term.TemplateID, n int, f func(Dwimmer, *term.SettingT, ...term.T) term.T) {
	names := append([]string{"_Q"}, allNames[:n]...)
	s := ExpectQuestion(term.InitS(), t, names...)
	AddNative(s, f, allNames[:n]...)
}

func AddNative(s *term.SettingS, f func(Dwimmer, *term.SettingT, ...term.T) term.T, names ...string) {
	AddNativeTo(DefaultTransitions, s, f, names...)
}

var (
	NativeQ = term.Make("what does the appropriate native function return in this setting?")
)

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))
コード例 #29
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)
		}
	}
}
コード例 #30
0
ファイル: ui.go プロジェクト: paulfchristiano/dwimmer
package meta

import (
	"github.com/paulfchristiano/dwimmer/data/core"
	"github.com/paulfchristiano/dwimmer/data/represent"
	"github.com/paulfchristiano/dwimmer/dynamics"
	"github.com/paulfchristiano/dwimmer/term"
)

var (
	PutChar = term.Make("put the character [] at the location with [] spots to its left " +
		"and [] spots above it, and leave the cursor there")
	Clear   = term.Make("clear the screen")
	Size    = term.Make("what are the dimensions of the screen?")
	GetChar = term.Make("refresh the screen, wait until the user types something, " +
		"and return the next character that the user types")
	SetCursor = term.Make("put the cursor at the location with [] spots to its left " +
		"and [] spots above it")
	GetCursor = term.Make("where is the cursor?")
)

func init() {
	var s *term.SettingS
	s = dynamics.ExpectQuestion(term.InitS(), PutChar, "Q", "char", "x", "y")
	dynamics.AddNative(s, dynamics.Args3(nativePutChar), "char", "x", "y")

	s = dynamics.ExpectQuestion(term.InitS(), SetCursor, "Q", "x", "y")
	dynamics.AddNative(s, dynamics.Args2(nativeSetCursor), "x", "y")

	/*
		s = dynamics.ExpectQuestion(term.InitS(), GetCursor, "Q")