Esempio n. 1
0
func TestAssignmentFolding(t *testing.T) {
	test := FoldTest{src: "(= a (* 1 1))", expect: "1"}
	name := "assign"
	expr, _ := parse.ParseExpression(name, test.src)
	o := ir.FoldConstants(ir.MakeExpr(ir.MakePackage(&ast.Package{}, name), expr))
	validate_constant(t, name, o.(*ir.Assignment).Rhs, test)
}
Esempio n. 2
0
func TestCallFolding(t *testing.T) {
	src := "(fn (== 3 2) (+ 2 2))"
	name := "call"
	expr, _ := parse.ParseExpression(name, src)
	o := ir.FoldConstants(ir.MakeExpr(ir.MakePackage(&ast.Package{}, name), expr))
	validate_constant(t, name, o.(*ir.Call).Args[0], FoldTest{src, "false"})
	validate_constant(t, name, o.(*ir.Call).Args[1], FoldTest{src, "4"})
}
Esempio n. 3
0
func TestVarFolding(t *testing.T) {
	test := FoldTest{src: "(var (a:int):int (= a (/ 24 3)))", expect: "8"}
	name := "var"
	expr, _ := parse.ParseExpression(name, test.src)
	o := ir.FoldConstants(ir.MakeExpr(ir.MakePackage(&ast.Package{}, name), expr))
	o = o.(*ir.Variable).Body[0].(*ir.Assignment).Rhs
	validate_constant(t, name, o, test)
}
Esempio n. 4
0
func TestIfFolding(t *testing.T) {
	src := "(if (== false (!= 3 3)):int (/ 9 3) (* 1 2 3))"
	name := "if"
	expr, _ := parse.ParseExpression(name, src)
	o := ir.FoldConstants(ir.MakeExpr(ir.MakePackage(&ast.Package{}, name), expr))
	validate_constant(t, name, o.(*ir.If).Cond, FoldTest{src, "true"})
	validate_constant(t, name, o.(*ir.If).Then, FoldTest{src, "3"})
	validate_constant(t, name, o.(*ir.If).Else, FoldTest{src, "6"})
}
Esempio n. 5
0
func test_handler(t *testing.T, test Test, name string, n ast.Node) {
	var o ir.Object
	pkg := ir.MakePackage(&ast.Package{}, name)
	switch x := n.(type) {
	case *ast.DefineStmt:
		o = ir.MakeDefine(pkg, x)
	case *ast.Package:
		o = ir.MakePackage(x, name)
	case ast.Expr:
		o = ir.MakeExpr(pkg, x)
		t.Log("makexpr, test:", test.src)
	}
	t.Log(o)
	fset := token.NewFileSet()
	fset.Add(name, len(test.src))
	if err := ir.TypeCheck(o, fset); (err == nil) != test.pass {
		t.Logf("expected %v got %v", test.pass, err == nil)
		if err != nil {
			t.Log(err)
		}
		t.Fail()
	} /*
		ir.Tag(o)*/
}
Esempio n. 6
0
func test_folding(t *testing.T, name string, test FoldTest) {
	expr, _ := parse.ParseExpression(name, test.src)
	o := ir.FoldConstants(ir.MakeExpr(ir.MakePackage(&ast.Package{}, name), expr))
	validate_constant(t, name, o, test)
}