commit 04d6c7945c662ce4f06f49ec7ada4d81c7dbe9df
parent 14f84b79b2c2bf5afc668ab5f15b3f095f034e59
Author: lash <dev@holbrook.no>
Date: Tue, 27 Aug 2024 00:10:11 +0100
Include flag description in preprocessor example
Diffstat:
6 files changed, 52 insertions(+), 25 deletions(-)
diff --git a/asm/flag.go b/asm/flag.go
@@ -20,7 +20,7 @@ func NewFlagParser() *FlagParser {
}
}
-func(pp *FlagParser) Get(key string) (string, error) {
+func(pp *FlagParser) GetAsString(key string) (string, error) {
v, ok := pp.flag[key]
if !ok {
return "", fmt.Errorf("no flag registered under key: %s", key)
@@ -28,6 +28,23 @@ func(pp *FlagParser) Get(key string) (string, error) {
return v, nil
}
+func(pp *FlagParser) GetFlag(key string) (uint32, error) {
+ v, err := pp.GetAsString(key)
+ if err != nil {
+ return 0, err
+ }
+ r, err := strconv.Atoi(v) // cannot fail
+ return uint32(r), nil
+}
+
+func(pp *FlagParser) GetDescription(idx uint32) (string, error) {
+ v, ok := pp.flagDescription[idx]
+ if !ok {
+ return "", fmt.Errorf("no description for flag idx: %v", idx)
+ }
+ return v, nil
+}
+
func(pp *FlagParser) Load(fp string) (int, error) {
var i int
f, err := os.Open(fp)
diff --git a/dev/asm/main.go b/dev/asm/main.go
@@ -49,11 +49,7 @@ func newProcessor(fp string) (*processor, error) {
func(p *processor) processFlag(s []string, one *string, two *string) ([]string, error) {
_, err := strconv.Atoi(*one)
if err != nil {
- //r, ok := p.flags[*one]
- //if !ok {
- // return nil, fmt.Errorf("No flag translation found for '%s'", *one)
- //}
- r, err := p.Get(*one)
+ r, err := p.GetAsString(*one)
if err != nil {
return nil, err
}
diff --git a/examples/preprocessor/main.go b/examples/preprocessor/main.go
@@ -9,6 +9,7 @@ import (
testdataloader "github.com/peteole/testdata-loader"
+ "git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/resource"
@@ -19,20 +20,25 @@ import (
var (
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "preprocessor")
- stringFlags = make(map[string]int)
)
type countResource struct {
- resource.Resource
+ parser *asm.FlagParser
count int
}
-func newCountResource() countResource {
- fs := resource.NewFsResource(scriptDir)
- return countResource{
- Resource: fs,
- count: 0,
+func newCountResource(fp string) (*countResource, error) {
+ var err error
+ pfp := path.Join(fp, "pp.csv")
+ parser := asm.NewFlagParser()
+ _, err = parser.Load(pfp)
+ if err != nil {
+ return nil, err
}
+ return &countResource{
+ count: 0,
+ parser: parser,
+ }, nil
}
func(rsc* countResource) poke(ctx context.Context, sym string, input []byte) (resource.Result, error) {
@@ -40,16 +46,21 @@ func(rsc* countResource) poke(ctx context.Context, sym string, input []byte) (re
ss := strings.Split(sym, "_")
+ r.Content = "You will see this if this flag did not have a description"
r.FlagReset = []uint32{8, 9, 10}
- v, ok := stringFlags[ss[1]]
- if ok {
- r.FlagSet = []uint32{uint32(v)}
- } else {
+ v, err := rsc.parser.GetFlag(ss[1])
+ if err != nil {
+ v = 8 + uint32(rsc.count) + 1
r.FlagSet = []uint32{8 + uint32(rsc.count) + 1}
}
+ r.FlagSet = []uint32{uint32(v)}
+ s, err := rsc.parser.GetDescription(v)
+ if err == nil {
+ r.Content = s
+ }
+
rsc.count++
- r.Content = "You will see this if no flag was set from code"
- engine.Logg.DebugCtxf(ctx, "countresource >>>>>> foo", "v", v, "ok", ok, "r", r)
+
return r, nil
}
@@ -57,13 +68,14 @@ func main() {
root := "root"
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir)
- stringFlags["foo"] = 8
- stringFlags["bar"] = 10
-
st := state.NewState(5)
st.UseDebug()
rsf := resource.NewFsResource(scriptDir)
- rs := newCountResource()
+ rs, err := newCountResource(scriptDir)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "aux handler fail: %v\n", err)
+ os.Exit(1)
+ }
rsf.AddLocalFunc("flag_foo", rs.poke)
rsf.AddLocalFunc("flag_bar", rs.poke)
rsf.AddLocalFunc("flag_schmag", rs.poke)
@@ -74,7 +86,7 @@ func main() {
}
ctx := context.Background()
en := engine.NewEngine(ctx, cfg, &st, rsf, ca)
- var err error
+
_, err = en.Init(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err)
diff --git a/examples/preprocessor/mid b/examples/preprocessor/mid
@@ -1 +1,2 @@
this is the middle page
+{{.flag_schmag}}
diff --git a/examples/preprocessor/mid.vis b/examples/preprocessor/mid.vis
@@ -1,2 +1,3 @@
+MAP flag_schmag
HALT
MOVE ^
diff --git a/examples/preprocessor/pp.csv b/examples/preprocessor/pp.csv
@@ -1,3 +1,3 @@
flag,foo,8
-flag,bar,10
+flag,bar,10,and this is the description of the flag 'bar'
flag,baz,12