first pass at a "builder" interface

This commit is contained in:
Michael McLoughlin
2018-11-30 20:43:31 -08:00
parent b65b6c02b8
commit 9b9f5b7e0c
11 changed files with 10069 additions and 84 deletions

65
build/context.go Normal file
View File

@@ -0,0 +1,65 @@
package build
import (
"errors"
"io"
"log"
"github.com/mmcloughlin/avo"
)
type Context struct {
file *avo.File
function *avo.Function
errs []error
}
func NewContext() *Context {
return &Context{
file: avo.NewFile(),
}
}
//go:generate avogen -output zinstructions.go build
func (c *Context) TEXT(name string) {
c.function = avo.NewFunction(name)
c.file.Functions = append(c.file.Functions, c.function)
}
func (c *Context) Instruction(i avo.Instruction) {
if c.function == nil {
c.AddErrorMessage("no active function")
return
}
c.function.AddInstruction(i)
}
func (c *Context) AddError(err error) {
c.errs = append(c.errs, err)
}
func (c *Context) AddErrorMessage(msg string) {
c.AddError(errors.New(msg))
}
func (c *Context) Result() (*avo.File, []error) {
return c.file, c.errs
}
func (c *Context) Generate(wout, werr io.Writer) {
diag := log.New(werr, "", 0)
f, errs := c.Result()
if errs != nil {
for _, err := range errs {
diag.Println(err)
}
return
}
p := avo.NewGoPrinter(wout)
if err := p.Print(f); err != nil {
diag.Fatal(err)
}
}

9
build/global.go Normal file
View File

@@ -0,0 +1,9 @@
package build
import "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) }

9838
build/zinstructions.go Normal file

File diff suppressed because it is too large Load Diff