func mainParse(args []string) { fset := flag.NewFlagSet("leaf-parse", flag.ExitOnError) astFlag := fset.Bool("ast", false, "print AST instead of token tree") fset.Parse(args) files := fset.Args() if len(files) == 0 { fmt.Fprintln(os.Stderr, "no input file.") return } for _, f := range files { fmt.Printf("[%s]\n", f) if *astFlag { res, errs := parser.Parse(f) printErrors(errs) if res != nil { p := prt.New(os.Stdout) p.Indent = " " ast.Print(p, res) } } else { tree, errs := parser.ParseTree(f) printErrors(errs) if tree != nil { // might be nil when the file does not exist tree.PrintTree(os.Stdout) } } } }
func mainBuild(args []string) { build := ir.NewBuild() pname := "test" gen := codegen.NewGen(pname, build) files := args for _, f := range files { astree, errs := parser.Parse(f) if len(errs) > 0 { printErrors(errs) os.Exit(-1) } gen.AddFile(astree) } errs := gen.Gen() if len(errs) > 0 { printErrors(errs) os.Exit(1) } // build.Print() fout, e := os.Create("out.e8") if e != nil { printError(e) os.Exit(1) } errs = build.Build(pname, fout) if len(errs) > 0 { printErrors(errs) os.Exit(1) } e = fout.Close() if e != nil { printError(e) os.Exit(1) } }