go-vise

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

commit 355b466746ea9230b318afeb45facb6264b8d461
parent d8c112fa926fb1cd80b2bdd9a53e6fcebe459e68
Author: lash <dev@holbrook.no>
Date:   Sat, 15 Apr 2023 07:06:03 +0100

WIP add profile menu example

Diffstat:
A.gitignore | 1+
AMakefile | 6++++++
Masm/asm.go | 14+++++++++++---
Mcache/cache.go | 9++++++---
Mdev/interactive/main.go | 2++
Mengine/engine.go | 2+-
Aexamples/compile.bash | 5+++++
Aexamples/profile/entry_name | 2++
Aexamples/profile/entry_name.vis | 6++++++
Aexamples/profile/entry_name_save | 0
Aexamples/profile/entry_name_save.vis | 3+++
Aexamples/profile/main.go | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/profile/myname.txt | 2++
Aexamples/profile/root | 1+
Aexamples/profile/root.vis | 3+++
Rtestdata/foo.fst -> testdata/foo.vis | 0
Mvm/runner.go | 2+-
17 files changed, 108 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +examples/**/*.bin diff --git a/Makefile b/Makefile @@ -0,0 +1,6 @@ +examples: profile + +.PHONY: profile + +profile: + bash examples/compile.sh examples/profile diff --git a/asm/asm.go b/asm/asm.go @@ -93,8 +93,13 @@ func parseTwoSym(b *bytes.Buffer, arg Arg) (int, error) { selector = strconv.FormatUint(uint64(*arg.Size), 10) sym = *arg.Selector } else if arg.Selector != nil { - sym = *arg.Sym - selector = *arg.Selector + if *arg.Sym == "*" { + sym = *arg.Selector + selector = *arg.Sym + } else { + sym = *arg.Sym + selector = *arg.Selector + } } n, err := writeSym(b, selector) @@ -255,7 +260,7 @@ var ( {"Comment", `(?:#)[^\n]*`}, {"Ident", `^[A-Z]+`}, {"Size", `[0-9]+`}, - {"Sym", `[a-zA-Z_][a-zA-Z0-9_]*`}, + {"Sym", `[a-zA-Z_\*][a-zA-Z0-9_]*`}, {"Whitespace", `[ \t]+`}, {"EOL", `[\n\r]+`}, {"Quote", `["']`}, @@ -297,6 +302,9 @@ func writeDisplay(w *bytes.Buffer, s string) (int, error) { return w.WriteString(s) } func writeSize(w *bytes.Buffer, n uint32) (int, error) { + if n == 0 { + return w.Write([]byte{0x01, 0x00}) + } bn := [4]byte{} sz := numSize(n) if sz > 4 { diff --git a/cache/cache.go b/cache/cache.go @@ -51,9 +51,12 @@ func(ca *Cache) Add(key string, value string, sizeLimit uint16) error { } return fmt.Errorf("key %v already defined in frame %v", key, checkFrame) } - sz := ca.checkCapacity(value) - if sz == 0 { - return fmt.Errorf("Cache capacity exceeded %v of %v", ca.CacheUseSize + sz, ca.CacheSize) + var sz uint32 + if len(value) > 0 { + sz = ca.checkCapacity(value) + if sz == 0 { + return fmt.Errorf("Cache capacity exceeded %v of %v", ca.CacheUseSize + sz, ca.CacheSize) + } } log.Printf("add key %s value size %v limit %v", key, sz, sizeLimit) ca.Cache[len(ca.Cache)-1][key] = value diff --git a/dev/interactive/main.go b/dev/interactive/main.go @@ -13,9 +13,11 @@ func main() { var dir string var root string var size uint + //var sessionId string flag.StringVar(&dir, "d", ".", "resource dir to read from") flag.UintVar(&size, "s", 0, "max size of output") flag.StringVar(&root, "root", "root", "entry point symbol") + //flag.StringVar(&sessionId, "session-id", "default", "session id") flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) diff --git a/engine/engine.go b/engine/engine.go @@ -37,13 +37,13 @@ func NewEngine(cfg Config, st *state.State, rs resource.Resource, ca cache.Memor if cfg.OutputSize > 0 { szr = render.NewSizer(cfg.OutputSize) } + ctx = context.WithValue(ctx, "sessionId", cfg.SessionId) engine := Engine{ st: st, rs: rs, ca: ca, vm: vm.NewVm(st, rs, ca, szr), } - //if cfg.Root != "" { if st.Moves == 0 { engine.Init(cfg.Root, ctx) } diff --git a/examples/compile.bash b/examples/compile.bash @@ -0,0 +1,5 @@ +for f in $(ls $1); do + b=$(basename $f) + b=${b%.*} + go run ./dev/asm $1/$b.vis > $1/$b.bin +done diff --git a/examples/profile/entry_name b/examples/profile/entry_name @@ -0,0 +1,2 @@ +Name is currently: {{.myname}} +Enter new name. diff --git a/examples/profile/entry_name.vis b/examples/profile/entry_name.vis @@ -0,0 +1,6 @@ +LOAD myname 32 +MAP myname +MOUT 0 "abort" +HALT +INCMP 0 _ +INCMP * entry_name_save diff --git a/examples/profile/entry_name_save b/examples/profile/entry_name_save diff --git a/examples/profile/entry_name_save.vis b/examples/profile/entry_name_save.vis @@ -0,0 +1,3 @@ +LOAD entry_name_save 0 +RELOAD myname +MOVE _ diff --git a/examples/profile/main.go b/examples/profile/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "context" + "flag" + "fmt" + "io/ioutil" + "os" + "path" + + testdataloader "github.com/peteole/testdata-loader" + + "git.defalsify.org/vise/cache" + "git.defalsify.org/vise/engine" + "git.defalsify.org/vise/resource" + "git.defalsify.org/vise/state" +) + +var ( + baseDir = testdataloader.GetBasePath() + scriptDir = path.Join(baseDir, "examples", "profile") +) + +func nameSave(sym string, input []byte, ctx context.Context) (resource.Result, error) { + fp := path.Join(scriptDir, "myname.txt") + err := ioutil.WriteFile(fp, input, 0600) + return resource.Result{}, err +} + +func main() { + var dir string + var root string + var size uint + var sessionId string + flag.UintVar(&size, "s", 0, "max size of output") + flag.StringVar(&root, "root", "root", "entry point symbol") + flag.StringVar(&sessionId, "session-id", "default", "session id") + flag.Parse() + fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) + + st := state.NewState(0) + rs := resource.NewFsResource(scriptDir) + rs.AddLocalFunc("entry_name_save", nameSave) + ca := cache.NewCache() + cfg := engine.Config{ + Root: "root", + SessionId: sessionId, + OutputSize: uint32(size), + } + ctx := context.Background() + en := engine.NewEngine(cfg, &st, &rs, ca, ctx) + + err := engine.Loop(&en, os.Stdin, os.Stdout, ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "loop exited with error: %v", err) + os.Exit(1) + } +} diff --git a/examples/profile/myname.txt b/examples/profile/myname.txt @@ -0,0 +1 @@ +inky +\ No newline at end of file diff --git a/examples/profile/root b/examples/profile/root @@ -0,0 +1 @@ +Please input profile data diff --git a/examples/profile/root.vis b/examples/profile/root.vis @@ -0,0 +1,3 @@ +DOWN entry_name 0 "name" +DOWN entry_email 1 "email" +DOWN entry_sex 2 "sex" diff --git a/testdata/foo.fst b/testdata/foo.vis diff --git a/vm/runner.go b/vm/runner.go @@ -264,7 +264,7 @@ func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) { if err != nil { return b, err } - log.Printf("loaded additional code: %x", code) + log.Printf("loaded code for %s: %x", sym, code) b = append(b, code...) vm.Reset() return b, nil