Exemple #1
0
func test_file(t *testing.T, name string, test Test) {
	f, err := parse.ParseFile(token.NewFileSet(), name, test.src)
	if err != nil {
		t.Fatal(err)
	}
	test_handler(t, test, name, &ast.Package{Files: []*ast.File{f}})
}
Exemple #2
0
func CompileFile(path string) {
	var c compiler

	c.fset = token.NewFileSet()
	f := parse.ParseFile(c.fset, path)
	if f == nil {
		os.Exit(1)
	}

	path = path[:len(path)-len(filepath.Ext(path))]
	fp, err := os.Create(path + ".c")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer fp.Close()

	c.fp = fp
	c.compFile(f)

	if c.errors.Count() != 0 {
		c.errors.Print()
		os.Exit(1)
	}
}
Exemple #3
0
// CompileDir generates C source code for the Calc sources found in the
// directory specified by path. The C source file uses the same name as
// directory rather than any individual file.
func CompileDir(path string, opt bool) error {
	fset := token.NewFileSet()
	p, err := parse.ParseDir(fset, path)
	if err != nil {
		return err
	}

	pkg := ir.MakePackage(p, filepath.Base(path))
	if err := ir.TypeCheck(pkg, fset); err != nil {
		return err
	}
	if opt {
		pkg = ir.FoldConstants(pkg).(*ir.Package)
	}
	//ir.Tag(pkg)

	fp, err := os.Create(filepath.Join(path, filepath.Base(path)) + ".c")
	if err != nil {
		return err
	}
	defer fp.Close()

	c := &compiler{fp: fp, fset: fset}

	c.emitHeaders()
	c.compPackage(pkg)
	c.emitMain()

	if c.errors.Count() != 0 {
		return c.errors
	}
	return nil
}
Exemple #4
0
// ParseExpression parses the given source string and returns an ast.Node
// representing the root of the expression. This function is intended to
// facilitate testing and is not use by the compiler itself. The name is
// used in error reporting
func ParseExpression(name, src string) (ast.Expr, error) {
	var p parser

	fset := token.NewFileSet()
	file := fset.Add(name, len(src))
	p.init(file, name, strings.NewReader(src), nil)
	node := p.parseExpression()

	if p.errors.Count() > 0 {
		return nil, p.errors
	}
	return node, nil
}
Exemple #5
0
func TestPackageFolding(t *testing.T) {
	fs := token.NewFileSet()
	f1, _ := parse.ParseFile(fs, "package", "(define f1 (func:int (+ 1 2)))")
	f2, _ := parse.ParseFile(fs, "package", "(define f2 (func:int (* 8 2)))")
	pkg := &ast.Package{Files: []*ast.File{f1, f2}}
	o := ir.FoldConstants(ir.MakePackage(pkg, "package"))
	o1 := o.(*ir.Package).Scope().Lookup("f1")
	o2 := o.(*ir.Package).Scope().Lookup("f2")
	validate_constant(t, "package", o1.(*ir.Define).Body.(*ir.Function).Body[0],
		FoldTest{"", "3"})
	validate_constant(t, "package", o2.(*ir.Define).Body.(*ir.Function).Body[0],
		FoldTest{"", "16"})
}
Exemple #6
0
func TestParseFile(t *testing.T) {
	test := Test{"bad-ext", "", []Type{}, false}

	// test for file with bad extension
	f, err := ioutil.TempFile("", "")
	if err != nil {
		t.Log(err)
	}
	defer func() {
		f.Close()
		err := os.Remove(f.Name())
		t.Log(err)
	}()
	n, err := parse.ParseFile(token.NewFileSet(), f.Name(), "")
	checkTest(t, test, n, err)

	test = Test{"simple.calc", "(define main (func:int 0))",
		[]Type{FILE, DEFINE, FUNC, BASIC},
		true}
	f, err = os.Create(test.name)
	if err != nil {
		t.Log(err)
	}
	defer func() {
		f.Close()
		if err := os.Remove(f.Name()); err != nil {
			t.Log(err)
		}
	}()

	_, err = f.WriteString(test.src)
	if err != nil {
		t.Fatal(err)
	}

	n, err = parse.ParseFile(token.NewFileSet(), f.Name(), "")
}
Exemple #7
0
func TestDirectory(t *testing.T) {
	gopath := os.Getenv("GOPATH")
	var path string
	for _, p := range strings.Split(gopath, ";") {
		tmp := filepath.Join(p, "src", "github.com", "rthornton128", "calc",
			"examples", "package")
		t.Log("testing path:", tmp)
		if _, err := os.Stat(tmp); err == nil {
			path = tmp
			break
		}
	}

	t.Log("using path:", path)
	_, err := parse.ParseDir(token.NewFileSet(), path)
	if err != nil {
		t.Fatal(err)
	}
}
Exemple #8
0
Fichier : comp.go Projet : enex/RUN
// CompileDir generates C source code for the Calc sources found in the
// directory specified by path. The C source file uses the same name as
// directory rather than any individual file.
func CompileDir(path string) error {
	fs := token.NewFileSet()
	pkg, err := parse.ParseDir(fs, path)
	if err != nil {
		return err
	}

	fp, err := os.Create(filepath.Join(path, filepath.Base(path)) + ".c")
	if err != nil {
		return err
	}
	defer fp.Close()

	c := &compiler{fp: fp, fset: fs}
	c.compPackage(pkg)

	if c.errors.Count() != 0 {
		return c.errors
	}
	return nil
}
Exemple #9
0
func CompileDir(path string) {
	fs := token.NewFileSet()
	pkg := parse.ParseDir(fs, path)
	if pkg == nil {
		os.Exit(1)
	}

	fp, err := os.Create(filepath.Join(path, filepath.Base(path)) + ".c")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer fp.Close()

	c := &compiler{fp: fp, fset: fs}
	c.compPackage(pkg)

	if c.errors.Count() != 0 {
		c.errors.Print()
		os.Exit(1)
	}
}
Exemple #10
0
// CompileFile generates a C source file for the corresponding file
// specified by path. The .calc extension for the filename in path is
// replaced with .c for the C source output.
func CompileFile(path string, opt bool) error {
	fset := token.NewFileSet()
	f, err := parse.ParseFile(fset, path, "")
	if err != nil {
		return err
	}

	pkg := ir.MakePackage(&ast.Package{
		Files: []*ast.File{f},
	}, filepath.Base(path))

	if err := ir.TypeCheck(pkg, fset); err != nil {
		return err
	}
	if opt {
		pkg = ir.FoldConstants(pkg).(*ir.Package)
	}
	//ir.Tag(pkg)

	path = path[:len(path)-len(filepath.Ext(path))]
	fp, err := os.Create(path + ".c")
	if err != nil {
		return err
	}
	defer fp.Close()

	c := &compiler{fp: fp}

	c.emitHeaders()
	c.compPackage(pkg)
	c.emitMain()

	if c.errors.Count() != 0 {
		return c.errors
	}
	return nil
}
Exemple #11
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)*/
}
Exemple #12
0
Fichier : comp.go Projet : enex/RUN
// CompileFile generates a C source file for the corresponding file
// specified by path. The .calc extension for the filename in path is
// replaced with .c for the C source output.
func CompileFile(path string) error {
	var c compiler

	c.fset = token.NewFileSet()
	f, err := parse.ParseFile(c.fset, path, nil)
	if err != nil {
		return err
	}

	path = path[:len(path)-len(filepath.Ext(path))]
	fp, err := os.Create(path + ".c")
	if err != nil {
		return err
	}
	defer fp.Close()

	c.fp = fp
	c.compFile(f)

	if c.errors.Count() != 0 {
		return c.errors
	}
	return nil
}
Exemple #13
0
func TestFileSetPosition(t *testing.T) {
	fs := token.NewFileSet()
	fs.Add("testA.calc", test_expr)
	fs.Add("testB.calc", test_expr)
}
Exemple #14
0
func handleFileTests(t *testing.T, tests []Test) {
	for _, test := range tests {
		f, err := parse.ParseFile(token.NewFileSet(), test.name, test.src)
		checkTest(t, test, f, err)
	}
}