func (self *Run) Run() { if self.Filename == "" { self.Filename = "main.l" } build := ir.NewBuild() pname := "main" gen := codegen.NewGen(pname, build) astree, errs := parser.ParseBytes(self.Filename, self.Source) if self.err(errs...) { return } gen.AddFile(astree) if self.err(gen.Gen()...) { return } buf := new(bytes.Buffer) errs = build.Build(pname, buf) // TODO: stderr? if self.err(errs...) { return } self.Image = buf.Bytes() vm, e := img.Make(bytes.NewBuffer(self.Image)) if self.err(e) { return } stdoutBuf := new(bytes.Buffer) var stdout io.Writer = stdoutBuf if self.Stdout != nil { stdout = io.MultiWriter(stdout, self.Stdout) } vm.SetPC(mem.SegCode) vm.Stdout = stdout if self.TimeoutCycle == 0 { self.TimeoutCycle = math.MaxInt64 } ncycle := vm.Run(self.TimeoutCycle) self.UsedCycle = ncycle self.HaltValue = vm.HaltValue() self.AddrError = vm.AddrError() self.Output = stdoutBuf.Bytes() }
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) } }