Exemple #1
0
func fmtFile(fname string) (bool, error) {
	input, e := ioutil.ReadFile(fname)
	if e != nil {
		return false, e
	}

	f, rec, es := parse.File(fname, bytes.NewBuffer(input), false)
	if es != nil {
		return false, fmt.Errorf("%d errors found at parsing", len(es))
	}

	var output bytes.Buffer
	gfmt.FprintFile(&output, f, rec)
	if bytes.Compare(input, output.Bytes()) == 0 {
		return false, nil
	}

	tempfile, e := ioutil.TempFile(tempDir, "e8fmt")
	if e != nil {
		return false, e
	}
	if _, e := tempfile.Write(output.Bytes()); e != nil {
		return false, e
	}
	if e := tempfile.Close(); e != nil {
		return false, e
	}
	return true, os.Rename(tempfile.Name(), fname)
}
Exemple #2
0
func main() {
	flag.Parse()

	args := flag.Args()
	if len(args) != 1 {
		exit(errors.New("need exactly one input input file"))
	}
	fname := args[0]
	input, e := ioutil.ReadFile(fname)
	if e != nil {
		exit(e)
	}

	if *bare {
		if *parseAST {
			stmts, es := parse.Stmts(fname, bytes.NewBuffer(input))
			printErrs(es)
			gfmt.FprintStmts(os.Stdout, stmts)
		} else {
			bs, es, irLog := g8.CompileBareFunc(fname, string(input))
			printErrs(es)
			printIRLog(irLog, *ir)
			runImage(bs, *dasm, *ncycle)
		}
	} else {
		if *parseAST {
			f, rec, es := parse.File(fname, bytes.NewBuffer(input), true)
			printErrs(es)
			gfmt.FprintFile(os.Stdout, f, rec)
		} else {
			bs, es, irLog := g8.CompileSingle(fname, string(input), *golike)
			printErrs(es)
			printIRLog(irLog, *ir)
			runImage(bs, *dasm, *ncycle)
		}
	}
}