go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit c8c8a25862ee2e5baf9a9caf888cabf322b0f191
parent b1da54293db9b39c08a079d1652921149a21b532
Author: lash <dev@holbrook.no>
Date:   Mon, 26 Aug 2024 22:22:50 +0100

Factor out preprocessor flag translator

Diffstat:
Aasm/preprocess.go | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdev/asm/main.go | 70+++++++++++++++++++++-------------------------------------------------
2 files changed, 82 insertions(+), 49 deletions(-)

diff --git a/asm/preprocess.go b/asm/preprocess.go @@ -0,0 +1,61 @@ +package asm + +import ( + "encoding/csv" + "fmt" + "io" + "os" + "strconv" +) + +type PreProcessor struct { + flags map[string]string +} + +func NewPreProcessor() *PreProcessor { + return &PreProcessor{ + flags: make(map[string]string), + } +} + +func(pp *PreProcessor) Get(key string) (string, error) { + v, ok := pp.flags[key] + if !ok { + return "", fmt.Errorf("no flag registered under key: %s", key) + } + return v, nil +} + +func(pp *PreProcessor) Load(fp string) (int, error) { + var i int + f, err := os.Open(fp) + if err != nil { + return 0, err + } + defer f.Close() + r := csv.NewReader(f) + for i = 0; true; i++ { + r, err := r.Read() + if err != nil { + if err == io.EOF { + break + } + return 0, err + } + if r[0] == "flag" { + if len(r) < 3 { + return 0, fmt.Errorf("Not enough fields for flag setting in line %d", i) + } + _, err = strconv.Atoi(r[2]) + if err != nil { + return 0, fmt.Errorf("Flag translation value must be numeric") + } + pp.flags[r[1]] = r[2] + Logg.Debugf("added flag translation", "from", r[1], "to", r[2]) + } + } + + return i, nil +} + + diff --git a/dev/asm/main.go b/dev/asm/main.go @@ -1,10 +1,8 @@ package main import ( - "encoding/csv" "flag" "fmt" - "io" "io/ioutil" "log" "os" @@ -35,65 +33,40 @@ type asmAsm struct { Instructions []*instruction `@@*` } -type preProcessor struct { - flags map[string]string -} - -func newPreProcessor() *preProcessor { - return &preProcessor{ - flags: make(map[string]string), - } -} +type processor struct { + *asm.PreProcessor + +} -func(pp *preProcessor) load(fp string) (int, error) { - var i int - f, err := os.Open(fp) - if err != nil { - return 0, err +func newProcessor(fp string) (*processor, error) { + o := &processor{ + asm.NewPreProcessor(), } - defer f.Close() - r := csv.NewReader(f) - for i = 0; true; i++ { - r, err := r.Read() - if err != nil { - if err == io.EOF { - break - } - return 0, err - } - if r[0] == "flag" { - if len(r) < 3 { - return 0, fmt.Errorf("Not enough fields for flag setting in line %d", i) - } - _, err = strconv.Atoi(r[2]) - if err != nil { - return 0, fmt.Errorf("Flag translation value must be numeric") - } - pp.flags[r[1]] = r[2] - log.Printf("added flag translation %s -> %s", r[1], r[2]) - } - } - - return i, nil + _, err := o.Load(fp) + return o, err } -func(pp *preProcessor) processFlag(s []string, one *string, two *string) ([]string, error) { +func(p *processor) processFlag(s []string, one *string, two *string) ([]string, error) { _, err := strconv.Atoi(*one) if err != nil { - r, ok := pp.flags[*one] - if !ok { - return nil, fmt.Errorf("No flag translation found for '%s'", *one) + //r, ok := p.flags[*one] + //if !ok { + // return nil, fmt.Errorf("No flag translation found for '%s'", *one) + //} + r, err := p.Get(*one) + if err != nil { + return nil, err } log.Printf("translated flag %s to %s", *one, r) - s = append(s, r) + s = append(s, r) } else { s = append(s, *one) } return append(s, *two), nil } -func(pp *preProcessor) pass(s []string, a arg) []string { +func(p *processor) pass(s []string, a arg) []string { for _, r := range []*string{a.One, a.Two, a.Three} { if r == nil { break @@ -103,7 +76,7 @@ func(pp *preProcessor) pass(s []string, a arg) []string { return s } -func(pp *preProcessor) run(b []byte) ([]byte, error) { +func(pp *processor) run(b []byte) ([]byte, error) { asmLexer := lexer.MustSimple([]lexer.SimpleRule{ {"Comment", `(?:#)[^\n]*`}, {"Ident", `^[A-Z]+`}, @@ -164,8 +137,7 @@ func main() { } if len(ppfp) > 0 { - pp := newPreProcessor() - _, err := pp.load(ppfp) + pp, err := newProcessor(ppfp) if err != nil { fmt.Fprintf(os.Stderr, "preprocessor load error: %v\n", err) os.Exit(1)