commit b1da54293db9b39c08a079d1652921149a21b532
parent 01301ae35765d89b988a3ac5d9c520e93b045e37
Author: lash <dev@holbrook.no>
Date: Mon, 26 Aug 2024 22:10:47 +0100
Add preprocessor example
Diffstat:
13 files changed, 142 insertions(+), 23 deletions(-)
diff --git a/dev/asm/main.go b/dev/asm/main.go
@@ -125,23 +125,22 @@ func(pp *preProcessor) run(b []byte) ([]byte, error) {
b = []byte{}
for _, v := range ast.Instructions {
s := []string{v.OpCode}
- if v.OpArg.One == nil {
- continue
- }
- switch v.OpCode {
- case "CATCH":
- s = append(s, *v.OpArg.One)
- s, err = pp.processFlag(s, v.OpArg.Two, v.OpArg.Three)
- if err != nil {
- return nil, err
- }
- case "CROAK":
- s, err = pp.processFlag(s, v.OpArg.One, v.OpArg.Two)
- if err != nil {
- return nil, err
- }
- default:
- s = pp.pass(s, v.OpArg)
+ if v.OpArg.One != nil {
+ switch v.OpCode {
+ case "CATCH":
+ s = append(s, *v.OpArg.One)
+ s, err = pp.processFlag(s, v.OpArg.Two, v.OpArg.Three)
+ if err != nil {
+ return nil, err
+ }
+ case "CROAK":
+ s, err = pp.processFlag(s, v.OpArg.One, v.OpArg.Two)
+ if err != nil {
+ return nil, err
+ }
+ default:
+ s = pp.pass(s, v.OpArg)
+ }
}
b = append(b, []byte(strings.Join(s, " "))...)
b = append(b, 0x0a)
diff --git a/examples/preprocessor/Makefile b/examples/preprocessor/Makefile
@@ -0,0 +1,10 @@
+INPUTS = $(wildcard ./*.vis)
+TXTS = $(wildcard ./*.txt.orig)
+
+%.vis:
+ go run ../../dev/asm -f pp.csv $(basename $@).vis > $(basename $@).bin
+
+all: $(INPUTS) $(TXTS)
+
+%.txt.orig:
+ cp -v $(basename $@).orig $(basename $@)
diff --git a/examples/preprocessor/first b/examples/preprocessor/first
@@ -0,0 +1 @@
+this is the first page
diff --git a/examples/preprocessor/first.vis b/examples/preprocessor/first.vis
@@ -0,0 +1,3 @@
+LOAD flag_foo 0
+HALT
+INCMP ^ *
diff --git a/examples/preprocessor/last b/examples/preprocessor/last
@@ -0,0 +1 @@
+this is the last page
diff --git a/examples/preprocessor/last.vis b/examples/preprocessor/last.vis
@@ -0,0 +1,4 @@
+LOAD flag_bar 0
+HALT
+RELOAD flag_schmag
+INCMP ^ *
diff --git a/examples/preprocessor/main.go b/examples/preprocessor/main.go
@@ -0,0 +1,89 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "path"
+ "strings"
+
+ testdataloader "github.com/peteole/testdata-loader"
+
+ "git.defalsify.org/vise.git/cache"
+ "git.defalsify.org/vise.git/engine"
+ "git.defalsify.org/vise.git/resource"
+ "git.defalsify.org/vise.git/state"
+)
+
+
+var (
+ baseDir = testdataloader.GetBasePath()
+ scriptDir = path.Join(baseDir, "examples", "preprocessor")
+ stringFlags = make(map[string]int)
+)
+
+type countResource struct {
+ resource.Resource
+ count int
+}
+
+func newCountResource() countResource {
+ fs := resource.NewFsResource(scriptDir)
+ return countResource{
+ Resource: fs,
+ count: 0,
+ }
+}
+
+func(rsc* countResource) poke(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+ var r resource.Result
+
+ ss := strings.Split(sym, "_")
+
+ r.FlagReset = []uint32{8, 9, 10}
+ v, ok := stringFlags[ss[1]]
+ if ok {
+ r.FlagSet = []uint32{uint32(v)}
+ } else {
+ r.FlagSet = []uint32{8 + uint32(rsc.count) + 1}
+ }
+ 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
+}
+
+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()
+ rsf.AddLocalFunc("flag_foo", rs.poke)
+ rsf.AddLocalFunc("flag_bar", rs.poke)
+ rsf.AddLocalFunc("flag_schmag", rs.poke)
+ rsf.AddLocalFunc("flag_start", rs.poke)
+ ca := cache.NewCache()
+ cfg := engine.Config{
+ Root: "root",
+ }
+ 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)
+ os.Exit(1)
+ }
+
+ err = engine.Loop(ctx, &en, os.Stdin, os.Stdout)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
+ os.Exit(1)
+ }
+}
diff --git a/examples/preprocessor/mid b/examples/preprocessor/mid
@@ -0,0 +1 @@
+this is the middle page
diff --git a/examples/preprocessor/mid.vis b/examples/preprocessor/mid.vis
@@ -0,0 +1,2 @@
+HALT
+MOVE ^
diff --git a/examples/preprocessor/pp.csv b/examples/preprocessor/pp.csv
@@ -0,0 +1,3 @@
+flag,foo,8
+flag,bar,10
+flag,baz,12
diff --git a/examples/preprocessor/root b/examples/preprocessor/root
@@ -0,0 +1 @@
+that's it
diff --git a/examples/preprocessor/root.vis b/examples/preprocessor/root.vis
@@ -0,0 +1,5 @@
+CROAK baz 1
+CATCH last bar 1
+CATCH first foo 0
+LOAD flag_schmag 0
+MOVE mid
diff --git a/vm/runner.go b/vm/runner.go
@@ -246,7 +246,7 @@ func(vm *Vm) runCatch(ctx context.Context, b []byte) ([]byte, error) {
if err != nil {
return b, err
}
- Logg.InfoCtxf(ctx, "catch!", "flag", sig, "sym", sym, "target", actualSym)
+ Logg.InfoCtxf(ctx, "catch!", "flag", sig, "sym", sym, "target", actualSym, "mode", mode)
sym = actualSym
bh, err := vm.rs.GetCode(sym)
if err != nil {
@@ -270,7 +270,7 @@ func(vm *Vm) runCroak(ctx context.Context, b []byte) ([]byte, error) {
vm.ca.Reset()
b = []byte{}
}
- return []byte{}, nil
+ return b, nil
}
// executes the LOAD opcode
@@ -494,17 +494,17 @@ func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (str
_ = vm.st.SetFlag(state.FLAG_LOADFAIL)
return "", NewExternalCodeError(key, err).WithCode(r.Status)
}
- for _, flag := range r.FlagSet {
+ for _, flag := range r.FlagReset {
if !state.IsWriteableFlag(flag) {
continue
}
- vm.st.SetFlag(flag)
+ vm.st.ResetFlag(flag)
}
- for _, flag := range r.FlagReset {
+ for _, flag := range r.FlagSet {
if !state.IsWriteableFlag(flag) {
continue
}
- vm.st.ResetFlag(flag)
+ vm.st.SetFlag(flag)
}
haveLang := vm.st.MatchFlag(state.FLAG_LANG, true)