Exemple #1
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
}
Exemple #2
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))
}
Exemple #3
0
//TODO only do this sometimes, or control better the length, or something...
func GetHints(d dynamics.Dwimmer, context *term.SettingT, s *term.SettingS, n int) []string {
	suggestions, err := d.Answer(similarity.SuggestedActions.T(
		represent.Setting(s.Setting),
		represent.Int(n),
	), context)
	if err != nil {
		return []string{}
	}
	suggestionList, err := represent.ToList(d, suggestions)
	if err != nil {
		return []string{}
	}
	result := []string{}
	for _, suggestion := range suggestionList {
		actionC, err := represent.ToActionC(d, suggestion)
		if err != nil {
			continue
		}
		actionS := actionC.Uninstantiate(s.Names)
		result = append(result, actionS.String())
	}
	reverseHints(result)
	return result
}
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)
	}
}
Exemple #5
0
func nativeSize(d dynamics.Dwimmer, s *term.SettingT) term.T {
	x, y := d.Size()
	return WidthHeight.T(represent.Int(x), represent.Int(y))
}
Exemple #6
0
func (yyrcvr *yyParserImpl) Parse(yylex yyLexer) int {
	var yyn int
	var yylval yySymType
	var yyVAL yySymType
	var yyDollar []yySymType
	_ = yyDollar // silence set and not used
	yyS := make([]yySymType, yyMaxDepth)

	Nerrs := 0   /* number of errors */
	Errflag := 0 /* error recovery flag */
	yystate := 0
	yychar := -1
	yytoken := -1 // yychar translated into internal numbering
	yyrcvr.lookahead = func() int { return yychar }
	defer func() {
		// Make sure we report no lookahead when not parsing.
		yystate = -1
		yychar = -1
		yytoken = -1
	}()
	yyp := -1
	goto yystack

ret0:
	return 0

ret1:
	return 1

yystack:
	/* put a state and value onto the stack */
	if yyDebug >= 4 {
		__yyfmt__.Printf("char %v in %v\n", yyTokname(yytoken), yyStatname(yystate))
	}

	yyp++
	if yyp >= len(yyS) {
		nyys := make([]yySymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
	}
	yyS[yyp] = yyVAL
	yyS[yyp].yys = yystate

yynewstate:
	yyn = yyPact[yystate]
	if yyn <= yyFlag {
		goto yydefault /* simple state */
	}
	if yychar < 0 {
		yychar, yytoken = yylex1(yylex, &yylval)
	}
	yyn += yytoken
	if yyn < 0 || yyn >= yyLast {
		goto yydefault
	}
	yyn = yyAct[yyn]
	if yyChk[yyn] == yytoken { /* valid shift */
		yychar = -1
		yytoken = -1
		yyVAL = yylval
		yystate = yyn
		if Errflag > 0 {
			Errflag--
		}
		goto yystack
	}

yydefault:
	/* default state action */
	yyn = yyDef[yystate]
	if yyn == -2 {
		if yychar < 0 {
			yychar, yytoken = yylex1(yylex, &yylval)
		}

		/* look through exception table */
		xi := 0
		for {
			if yyExca[xi+0] == -1 && yyExca[xi+1] == yystate {
				break
			}
			xi += 2
		}
		for xi += 2; ; xi += 2 {
			yyn = yyExca[xi+0]
			if yyn < 0 || yyn == yytoken {
				break
			}
		}
		yyn = yyExca[xi+1]
		if yyn < 0 {
			goto ret0
		}
	}
	if yyn == 0 {
		/* error ... attempt to resume parsing */
		switch Errflag {
		case 0: /* brand new error */
			yylex.Error(yyErrorMessage(yystate, yytoken))
			Nerrs++
			if yyDebug >= 1 {
				__yyfmt__.Printf("%s", yyStatname(yystate))
				__yyfmt__.Printf(" saw %s\n", yyTokname(yytoken))
			}
			fallthrough

		case 1, 2: /* incompletely recovered error ... try again */
			Errflag = 3

			/* find a state where "error" is a legal shift action */
			for yyp >= 0 {
				yyn = yyPact[yyS[yyp].yys] + yyErrCode
				if yyn >= 0 && yyn < yyLast {
					yystate = yyAct[yyn] /* simulate a shift of "error" */
					if yyChk[yystate] == yyErrCode {
						goto yystack
					}
				}

				/* the current p has no shift on "error", pop stack */
				if yyDebug >= 2 {
					__yyfmt__.Printf("error recovery pops state %d\n", yyS[yyp].yys)
				}
				yyp--
			}
			/* there is no state on the stack with an error shift ... abort */
			goto ret1

		case 3: /* no shift yet; clobber input char */
			if yyDebug >= 2 {
				__yyfmt__.Printf("error recovery discards %s\n", yyTokname(yytoken))
			}
			if yytoken == yyEofCode {
				goto ret1
			}
			yychar = -1
			yytoken = -1
			goto yynewstate /* try again in the same state */
		}
	}

	/* reduction by production yyn */
	if yyDebug >= 2 {
		__yyfmt__.Printf("reduce %v in:\n\t%v\n", yyn, yyStatname(yystate))
	}

	yynt := yyn
	yypt := yyp
	_ = yypt // guard against "declared and not used"

	yyp -= yyR2[yyn]
	// yyp is now the index of $0. Perform the default action. Iff the
	// reduced production is ε, $1 is possibly out of range.
	if yyp+1 >= len(yyS) {
		nyys := make([]yySymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
	}
	yyVAL = yyS[yyp+1]

	/* consult goto table to find next state */
	yyn = yyR1[yyn]
	yyg := yyPgo[yyn]
	yyj := yyg + yyS[yyp].yys + 1

	if yyj >= yyLast {
		yystate = yyAct[yyg]
	} else {
		yystate = yyAct[yyj]
		if yyChk[yystate] != -yyn {
			yystate = yyAct[yyg]
		}
	}
	// dummy call; replaced with literal code
	switch yynt {

	case 1:
		yyDollar = yyS[yypt-6 : yypt+1]
		//line parser.y:30
		{
			yylex.(*lexer).setActionResult(yyDollar[2].string, yyDollar[6].expr, yyDollar[4].int)
		}
	case 2:
		yyDollar = yyS[yypt-4 : yypt+1]
		//line parser.y:31
		{
			yylex.(*lexer).setActionResult(yyDollar[2].string, yyDollar[4].expr, -1)
		}
	case 3:
		yyDollar = yyS[yypt-2 : yypt+1]
		//line parser.y:32
		{
			yylex.(*lexer).setActionResult(yyDollar[2].string, nil, -1)
		}
	case 4:
		yyDollar = yyS[yypt-2 : yypt+1]
		//line parser.y:33
		{
			yylex.(*lexer).setTermResult(yyDollar[2].expr)
		}
	case 5:
		yyDollar = yyS[yypt-4 : yypt+1]
		//line parser.y:34
		{
			{
				yylex.(*lexer).setActionResult(yyDollar[2].string, nil, yyDollar[4].int)
			}
		}
	case 6:
		yyDollar = yyS[yypt-8 : yypt+1]
		//line parser.y:35
		{
			yylex.(*lexer).setTransitiveActionResult(yyDollar[2].string, yyDollar[6].string, yyDollar[8].expr)
		}
	case 7:
		yyDollar = yyS[yypt-7 : yypt+1]
		//line parser.y:36
		{
			yylex.(*lexer).setTransitiveActionResult(yyDollar[2].string, yyDollar[5].string, yyDollar[7].expr)
		}
	case 8:
		yyDollar = yyS[yypt-7 : yypt+1]
		//line parser.y:37
		{
			yylex.(*lexer).setTransitiveActionResult(yyDollar[2].string, yyDollar[5].string, yyDollar[7].expr)
		}
	case 9:
		yyDollar = yyS[yypt-6 : yypt+1]
		//line parser.y:38
		{
			yylex.(*lexer).setTransitiveActionResult(yyDollar[2].string, yyDollar[4].string, yyDollar[6].expr)
		}
	case 11:
		yyDollar = yyS[yypt-0 : yypt+1]
		//line parser.y:42
		{
			yyVAL.expr = EmptyExpr()
		}
	case 12:
		yyDollar = yyS[yypt-2 : yypt+1]
		//line parser.y:43
		{
			yyVAL.expr = yyDollar[1].expr
			yyVAL.expr.append(yyDollar[2].clause)
		}
	case 16:
		yyDollar = yyS[yypt-1 : yypt+1]
		//line parser.y:46
		{
			yyVAL.clause = exprText(yyDollar[1].string)
		}
	case 17:
		yyDollar = yyS[yypt-1 : yypt+1]
		//line parser.y:47
		{
			yyVAL.clause = exprTerm{toC(yyDollar[1].expr)}
		}
	case 18:
		yyDollar = yyS[yypt-3 : yypt+1]
		//line parser.y:49
		{
			yyVAL.expr = yyDollar[2].expr
		}
	case 19:
		yyDollar = yyS[yypt-3 : yypt+1]
		//line parser.y:50
		{
			yyVAL.expr = yyDollar[2].expr
		}
	case 20:
		yyDollar = yyS[yypt-3 : yypt+1]
		//line parser.y:52
		{
			yyVAL.clause = exprTerm{term.ConstC{represent.Str(yyDollar[2].string)}}
		}
	case 21:
		yyDollar = yyS[yypt-2 : yypt+1]
		//line parser.y:53
		{
			yyVAL.clause = exprTerm{term.ConstC{represent.Str("")}}
		}
	case 22:
		yyDollar = yyS[yypt-1 : yypt+1]
		//line parser.y:55
		{
			yyVAL.clause = exprTerm{term.ConstC{represent.Int(yyDollar[1].int)}}
		}
	case 23:
		yyDollar = yyS[yypt-1 : yypt+1]
		//line parser.y:57
		{
			yyVAL.clause = yylex.(*lexer).parseWord(yyDollar[1].string)
		}
	case 26:
		yyDollar = yyS[yypt-2 : yypt+1]
		//line parser.y:59
		{
			yyVAL.string = yyDollar[1].string + yyDollar[2].string
		}
	case 39:
		yyDollar = yyS[yypt-2 : yypt+1]
		//line parser.y:65
		{
			yyVAL.string = yyDollar[1].string + yyDollar[2].string
		}
	}
	goto yystack /* stack new state and value */
}