diff --git a/build/context.go b/build/context.go index 7e82e62..2a5f2a3 100644 --- a/build/context.go +++ b/build/context.go @@ -20,9 +20,7 @@ func NewContext() *Context { } } -//go:generate avogen -output zinstructions.go build - -func (c *Context) TEXT(name string) { +func (c *Context) Function(name string) { c.function = avo.NewFunction(name) c.file.Functions = append(c.file.Functions, c.function) } @@ -35,6 +33,8 @@ func (c *Context) Instruction(i avo.Instruction) { c.function.AddInstruction(i) } +//go:generate avogen -output zinstructions.go build + func (c *Context) AddError(err error) { c.errs = append(c.errs, err) } diff --git a/build/global.go b/build/global.go index 3dbecc3..ffe1318 100644 --- a/build/global.go +++ b/build/global.go @@ -1,9 +1,35 @@ package build -import "os" +import ( + "flag" + "io" + "log" + "os" +) // ctx provides a global build context. var ctx = NewContext() -func TEXT(name string) { ctx.TEXT(name) } -func EOF() { ctx.Generate(os.Stdout, os.Stderr) } +func TEXT(name string) { ctx.Function(name) } + +var ( + output = flag.String("output", "", "output filename (default stdout)") +) + +func EOF() { + if !flag.Parsed() { + flag.Parse() + } + + var w io.Writer = os.Stdout + if *output != "" { + if f, err := os.Create(*output); err != nil { + log.Fatal(err) + } else { + defer f.Close() + w = f + } + } + + ctx.Generate(w, os.Stderr) +}