Esempio n. 1
0
func TestNestedIfExpr(t *testing.T) {
	expr := &ast.Expr{
		Function: &ast.Function{
			Name: "not",
			Params: []*ast.Expr{
				{
					Terminal: &ast.Terminal{
						BoolValue: proto.Bool(false),
					},
				},
			},
		},
	}

	ifexpr := &ast.IfExpr{
		Condition: expr,
		ThenClause: &ast.StateExpr{
			IfExpr: &ast.IfExpr{
				Condition: expr,
				ThenClause: &ast.StateExpr{
					State: proto.String("true"),
				},
				ElseClause: &ast.StateExpr{
					State: proto.String("falser"),
				},
			},
		},
		ElseClause: &ast.StateExpr{
			State: proto.String("false"),
		},
	}

	states := nameToState(map[string]int{
		"true":   1,
		"false":  2,
		"falser": 3,
	})
	t.Logf("states = %v", states)

	c := funcs.NewCatcher(false)

	stateExpr, err := Compile(ifexpr, states, c)

	if err != nil {
		panic(err)
	}

	res := stateExpr.Eval(nil)
	if res != 1 {
		t.Fatalf("Expected true state, but got %d", res)
	}

	if err := c.GetError(); err != nil {
		panic(err)
	}

}
Esempio n. 2
0
func TestSetCatcher(t *testing.T) {
	c := funcs.NewCatcher(false)
	errFunc := new(myErrorFunc)
	errFunc.SetCatcher(c)
	errFunc.Throw(errors.New("hello"))
	if c.GetError() == nil {
		t.Fatalf("expected err")
	}
}
Esempio n. 3
0
func Compile(rules *ast.Rules, tokens Tokens) (eval *exec.Exec, rootToken int, err error) {
	tab := table.New(rules.GetTransitions(), rules.GetIfExprs())
	catcher := funcs.NewCatcher(false)
	link, err := link.NewLink(rules, tokens, tab, catcher)
	if err != nil {
		return nil, 0, err
	}
	rootToken, err = tokens.GetTokenId(rules.GetRoot().GetPackage() + "." + rules.GetRoot().GetMessage())
	if err != nil {
		return nil, 0, err
	}
	startState, err := findStartState(rules.GetRoot(), rules.GetInits(), tab)
	if err != nil {
		return nil, 0, err
	}
	acceptState, err := tab.NameToState("accept")
	if err != nil {
		return nil, 0, err
	}
	eval = exec.NewExec(tab, link, catcher, startState, acceptState)
	return eval, rootToken, nil
}