Example #1
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
}
Example #2
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)
	}
}
Example #3
0
File: comp.go Project: 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
}
Example #4
0
File: comp.go Project: unreal/calc
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)
	}
}