printing: commit some refactors (probably broken)

This commit is contained in:
Michael McLoughlin
2018-12-11 00:18:22 -08:00
parent 4dc909a81e
commit c882e52510
21 changed files with 398 additions and 222 deletions

View File

@@ -1,12 +1,10 @@
package gen
import (
"bytes"
"fmt"
"go/format"
"strings"
"github.com/mmcloughlin/avo/internal/inst"
"github.com/mmcloughlin/avo/printer"
)
type Interface interface {
@@ -19,23 +17,7 @@ func (f Func) Generate(is []inst.Instruction) ([]byte, error) {
return f(is)
}
type Config struct {
Name string
Argv []string
}
func (c Config) GeneratedBy() string {
if c.Argv == nil {
return c.Name
}
return fmt.Sprintf("command: %s", strings.Join(c.Argv, " "))
}
func (c Config) GeneratedWarning() string {
return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy())
}
type Builder func(Config) Interface
type Builder func(printer.Config) Interface
// GoFmt formats Go code produced from the given generator.
func GoFmt(i Interface) Interface {
@@ -47,27 +29,3 @@ func GoFmt(i Interface) Interface {
return format.Source(b)
})
}
type generator struct {
buf bytes.Buffer
err error
}
func (g *generator) Printf(format string, args ...interface{}) {
if g.err != nil {
return
}
if _, err := fmt.Fprintf(&g.buf, format, args...); err != nil {
g.AddError(err)
}
}
func (g *generator) AddError(err error) {
if err != nil && g.err == nil {
g.err = err
}
}
func (g *generator) Result() ([]byte, error) {
return g.buf.Bytes(), g.err
}