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

132
build/cli.go Normal file
View File

@@ -0,0 +1,132 @@
package build
import (
"flag"
"io"
"log"
"os"
"github.com/mmcloughlin/avo/pass"
"github.com/mmcloughlin/avo/printer"
)
type Config struct {
ErrOut io.Writer
Passes []pass.Interface
}
func Main(cfg *Config, context *Context) int {
diag := log.New(cfg.ErrOut, "", 0)
f, errs := context.Result()
if errs != nil {
for _, err := range errs {
diag.Println(err)
}
return 1
}
p := pass.Concat(cfg.Passes...)
if err := p.Execute(f); err != nil {
diag.Println(err)
return 1
}
return 0
}
type Flags struct {
errout *outputValue
printers []*printerValue
}
func NewFlags(fs *flag.FlagSet) *Flags {
f := &Flags{}
f.errout = newOutputValue(os.Stderr)
fs.Var(f.errout, "log", "diagnostics output")
goasm := newPrinterValue(printer.NewGoAsm, os.Stdout)
fs.Var(goasm, "out", "assembly output")
f.printers = append(f.printers, goasm)
stubs := newPrinterValue(printer.NewStubs, nil)
fs.Var(stubs, "stubs", "go stub file")
f.printers = append(f.printers, stubs)
return f
}
func (f *Flags) Config() *Config {
pc := printer.NewDefaultConfig()
passes := []pass.Interface{pass.Compile}
for _, pv := range f.printers {
p := pv.Build(pc)
if p != nil {
passes = append(passes, p)
}
}
return &Config{
ErrOut: f.errout.w,
Passes: passes,
}
}
type outputValue struct {
w io.WriteCloser
filename string
}
func newOutputValue(dflt io.WriteCloser) *outputValue {
return &outputValue{w: dflt}
}
func (o *outputValue) String() string {
if o == nil {
return ""
}
return o.filename
}
func (o *outputValue) Set(s string) error {
o.filename = s
if s == "-" {
o.w = nopwritecloser{os.Stdout}
return nil
}
f, err := os.Create(s)
if err != nil {
return err
}
o.w = f
return nil
}
type printerValue struct {
*outputValue
Builder printer.Builder
}
func newPrinterValue(b printer.Builder, dflt io.WriteCloser) *printerValue {
return &printerValue{
outputValue: newOutputValue(dflt),
Builder: b,
}
}
func (p *printerValue) Build(cfg printer.Config) pass.Interface {
if p.outputValue.w == nil {
return nil
}
return &pass.Output{
Writer: p.outputValue.w,
Printer: p.Builder(cfg),
}
}
// nopwritecloser wraps a Writer and provides a null implementation of Close().
type nopwritecloser struct {
io.Writer
}
func (nopwritecloser) Close() error { return nil }

View File

@@ -2,13 +2,9 @@ package build
import (
"errors"
"io"
"log"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/pass"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/reg"
)
@@ -73,28 +69,3 @@ func (c *Context) AddErrorMessage(msg string) {
func (c *Context) Result() (*avo.File, []error) {
return c.file, c.errs
}
func (c *Context) Main(wout, werr io.Writer) int {
diag := log.New(werr, "", 0)
f, errs := c.Result()
if errs != nil {
for _, err := range errs {
diag.Println(err)
}
return 1
}
if err := pass.Compile.Execute(f); err != nil {
diag.Println(err)
return 1
}
p := avo.NewGoPrinter(wout)
if err := p.Print(f); err != nil {
diag.Println(err)
return 1
}
return 0
}

View File

@@ -2,8 +2,6 @@ package build
import (
"flag"
"io"
"log"
"os"
"github.com/mmcloughlin/avo/gotypes"
@@ -23,26 +21,14 @@ func TEXT(name, signature string) {
func LABEL(name string) { ctx.Label(avo.Label(name)) }
var (
output = flag.String("output", "", "output filename (default stdout)")
)
var flags = NewFlags(flag.CommandLine)
func EOF() {
func Generate() {
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
}
}
os.Exit(ctx.Main(w, os.Stderr))
cfg := flags.Config()
os.Exit(Main(cfg, ctx))
}
func GP8v() reg.Virtual { return ctx.GP8v() }