// 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 }
// 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 }
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)*/ }