Exemplo n.º 1
0
func main() {
	bare := flag.Bool("bare", false, "parse as bare function")
	parseAST := flag.Bool("parse", false, "parse only and print out the ast")
	ir := flag.Bool("ir", false, "prints out the IR")
	dasm := flag.Bool("d", false, "deassemble the image")
	ncycle := flag.Int("n", 100000, "maximum number of cycles")

	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)
			ast.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, es := parse.File(fname, bytes.NewBuffer(input))
			printErrs(es)
			ast.FprintFile(os.Stdout, f)
		} else {
			bs, es, irLog := g8.CompileSingleFile(fname, string(input))
			printErrs(es)
			printIRLog(irLog, *ir)
			runImage(bs, *dasm, *ncycle)
		}
	}
}
Exemplo n.º 2
0
func (bareFunc) Compile(pinfo *build8.PkgInfo) (
	compiled build8.Linkable, es []*lex8.Error,
) {
	b := newBuilder(pinfo.Path)

	initBuilder(b, pinfo.Import)
	if es = b.Errs(); es != nil {
		return nil, es
	}

	if len(pinfo.Src) == 0 {
		panic("no source file")
	} else if len(pinfo.Src) > 1 {
		e := fmt.Errorf("bare func %q has too many files", pinfo.Path)
		return nil, lex8.SingleErr(e)
	}

	b.irLog = pinfo.CreateLog("ir")
	defer b.irLog.Close()

	for _, r := range pinfo.Src {
		stmts, es := parse.Stmts(r.Path, r)
		if es != nil {
			return nil, es
		}

		lib := buildBareFunc(b, stmts)
		if es = b.Errs(); es != nil {
			return nil, es
		}

		return &pkg{lib}, nil
	}

	panic("unreachable")
}