printing: commit some refactors (probably broken)
This commit is contained in:
@@ -7,17 +7,19 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
)
|
||||
|
||||
type asmtest struct {
|
||||
cfg Config
|
||||
cfg printer.Config
|
||||
sym string // reference to the test function symbol
|
||||
rel8 string // label to be used for near jumps
|
||||
rel32 string // label for far jumps
|
||||
generator
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewAsmTest(cfg Config) Interface {
|
||||
func NewAsmTest(cfg printer.Config) Interface {
|
||||
return &asmtest{cfg: cfg}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package gen
|
||||
|
||||
import "github.com/mmcloughlin/avo/internal/inst"
|
||||
import (
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
)
|
||||
|
||||
type build struct {
|
||||
cfg Config
|
||||
generator
|
||||
cfg printer.Config
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewBuild(cfg Config) Interface {
|
||||
func NewBuild(cfg printer.Config) Interface {
|
||||
return GoFmt(&build{cfg: cfg})
|
||||
}
|
||||
|
||||
|
||||
@@ -6,14 +6,16 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
)
|
||||
|
||||
type ctors struct {
|
||||
cfg Config
|
||||
generator
|
||||
cfg printer.Config
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewCtors(cfg Config) Interface {
|
||||
func NewCtors(cfg printer.Config) Interface {
|
||||
return GoFmt(&ctors{cfg: cfg})
|
||||
}
|
||||
|
||||
|
||||
@@ -3,15 +3,18 @@ package gen
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
)
|
||||
|
||||
type ctorstest struct {
|
||||
cfg Config
|
||||
generator
|
||||
cfg printer.Config
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewCtorsTest(cfg Config) Interface {
|
||||
func NewCtorsTest(cfg printer.Config) Interface {
|
||||
return GoFmt(&ctorstest{cfg: cfg})
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@ package gen
|
||||
|
||||
import (
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
)
|
||||
|
||||
type godata struct {
|
||||
cfg Config
|
||||
generator
|
||||
cfg printer.Config
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewGoData(cfg Config) Interface {
|
||||
func NewGoData(cfg printer.Config) Interface {
|
||||
return GoFmt(&godata{cfg: cfg})
|
||||
}
|
||||
|
||||
@@ -65,11 +67,11 @@ func (g *godata) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
}
|
||||
|
||||
type godatatest struct {
|
||||
cfg Config
|
||||
generator
|
||||
cfg printer.Config
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewGoDataTest(cfg Config) Interface {
|
||||
func NewGoDataTest(cfg printer.Config) Interface {
|
||||
return GoFmt(&godatatest{cfg: cfg})
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,16 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
)
|
||||
|
||||
type mov struct {
|
||||
cfg Config
|
||||
generator
|
||||
cfg printer.Config
|
||||
prnt.Generator
|
||||
}
|
||||
|
||||
func NewMOV(cfg Config) Interface {
|
||||
func NewMOV(cfg printer.Config) Interface {
|
||||
return GoFmt(&mov{cfg: cfg})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user