func test(t *testing.T, src string, code []cpu.Word) { var ast parser.AST files, err := path.SourceFiles(src) if err != nil { t.Fatal(err) } for i := range files { err = ast.Parse(files[i], 0) if err != nil { t.Fatal(err) } } ar, err := Assemble(&ast, 0) if err != nil { t.Fatal(err) } if len(code) != len(ar.Code) { t.Fatalf("Code size mismatch. Expected %d, have %d\n%04x\n%04x", len(code), len(ar.Code), code, ar.Code) } for i := range code { if code[i] != cpu.Word(ar.Code[i]) { t.Fatalf("Code mismatch at instruction %d:\nWant: %04x\nHave: %04x", i, code, ar.Code) } } }
// findDeps creates a tree of dependencies (import paths) for // the given package. It parses the package source code for this purpose. func findDeps(importpath string, list *[]string) bool { if containsDep(*list, importpath) { return true } // Find source files for the given package. sources, err := path.SourceFiles(importpath) if err != nil { return false } // Parse sources into a partial AST. // We are only interested in the import statements. var ast parser.AST for _, file := range sources { err = ast.Parse(file, parser.ImportsOnly) if err != nil { return false } } // Create a new dependency entry. *list = append(*list, importpath) for _, imp := range ast.Imports { if !findDeps(imp.Path.Value, list) { return false } } return true }
func TestAST(t *testing.T) { var ast AST files, err := path.SourceFiles("test/hw") if err != nil { t.Fatal(err) } for i := range files { err = ast.Parse(files[i], 0) if err != nil { t.Fatal(err) } } }
// build builds the given package and its dependencies. func build(importpath string, flags asm.AsmFlags) (err error) { files, _ := path.SourceFiles(importpath) if len(files) == 0 { return nil // No source files } var ast parser.AST for i := range files { err = ast.Parse(files[i], 0) if err != nil { return } } ar, err := asm.Assemble(&ast, flags) if err != nil { return } return writeArchive(ar, importpath) }