WIP add profile menu example

This commit is contained in:
lash 2023-04-15 07:06:03 +01:00
parent d8c112fa92
commit 355b466746
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
17 changed files with 107 additions and 8 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
examples/**/*.bin

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
examples: profile
.PHONY: profile
profile:
bash examples/compile.sh examples/profile

View File

@ -93,8 +93,13 @@ func parseTwoSym(b *bytes.Buffer, arg Arg) (int, error) {
selector = strconv.FormatUint(uint64(*arg.Size), 10) selector = strconv.FormatUint(uint64(*arg.Size), 10)
sym = *arg.Selector sym = *arg.Selector
} else if arg.Selector != nil { } else if arg.Selector != nil {
sym = *arg.Sym if *arg.Sym == "*" {
selector = *arg.Selector sym = *arg.Selector
selector = *arg.Sym
} else {
sym = *arg.Sym
selector = *arg.Selector
}
} }
n, err := writeSym(b, selector) n, err := writeSym(b, selector)
@ -255,7 +260,7 @@ var (
{"Comment", `(?:#)[^\n]*`}, {"Comment", `(?:#)[^\n]*`},
{"Ident", `^[A-Z]+`}, {"Ident", `^[A-Z]+`},
{"Size", `[0-9]+`}, {"Size", `[0-9]+`},
{"Sym", `[a-zA-Z_][a-zA-Z0-9_]*`}, {"Sym", `[a-zA-Z_\*][a-zA-Z0-9_]*`},
{"Whitespace", `[ \t]+`}, {"Whitespace", `[ \t]+`},
{"EOL", `[\n\r]+`}, {"EOL", `[\n\r]+`},
{"Quote", `["']`}, {"Quote", `["']`},
@ -297,6 +302,9 @@ func writeDisplay(w *bytes.Buffer, s string) (int, error) {
return w.WriteString(s) return w.WriteString(s)
} }
func writeSize(w *bytes.Buffer, n uint32) (int, error) { func writeSize(w *bytes.Buffer, n uint32) (int, error) {
if n == 0 {
return w.Write([]byte{0x01, 0x00})
}
bn := [4]byte{} bn := [4]byte{}
sz := numSize(n) sz := numSize(n)
if sz > 4 { if sz > 4 {

9
cache/cache.go vendored
View File

@ -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) return fmt.Errorf("key %v already defined in frame %v", key, checkFrame)
} }
sz := ca.checkCapacity(value) var sz uint32
if sz == 0 { if len(value) > 0 {
return fmt.Errorf("Cache capacity exceeded %v of %v", ca.CacheUseSize + sz, ca.CacheSize) 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) log.Printf("add key %s value size %v limit %v", key, sz, sizeLimit)
ca.Cache[len(ca.Cache)-1][key] = value ca.Cache[len(ca.Cache)-1][key] = value

View File

@ -13,9 +13,11 @@ func main() {
var dir string var dir string
var root string var root string
var size uint var size uint
//var sessionId string
flag.StringVar(&dir, "d", ".", "resource dir to read from") flag.StringVar(&dir, "d", ".", "resource dir to read from")
flag.UintVar(&size, "s", 0, "max size of output") flag.UintVar(&size, "s", 0, "max size of output")
flag.StringVar(&root, "root", "root", "entry point symbol") flag.StringVar(&root, "root", "root", "entry point symbol")
//flag.StringVar(&sessionId, "session-id", "default", "session id")
flag.Parse() flag.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)

View File

@ -37,13 +37,13 @@ func NewEngine(cfg Config, st *state.State, rs resource.Resource, ca cache.Memor
if cfg.OutputSize > 0 { if cfg.OutputSize > 0 {
szr = render.NewSizer(cfg.OutputSize) szr = render.NewSizer(cfg.OutputSize)
} }
ctx = context.WithValue(ctx, "sessionId", cfg.SessionId)
engine := Engine{ engine := Engine{
st: st, st: st,
rs: rs, rs: rs,
ca: ca, ca: ca,
vm: vm.NewVm(st, rs, ca, szr), vm: vm.NewVm(st, rs, ca, szr),
} }
//if cfg.Root != "" {
if st.Moves == 0 { if st.Moves == 0 {
engine.Init(cfg.Root, ctx) engine.Init(cfg.Root, ctx)
} }

5
examples/compile.bash Normal file
View File

@ -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

View File

@ -0,0 +1,2 @@
Name is currently: {{.myname}}
Enter new name.

View File

@ -0,0 +1,6 @@
LOAD myname 32
MAP myname
MOUT 0 "abort"
HALT
INCMP 0 _
INCMP * entry_name_save

View File

View File

@ -0,0 +1,3 @@
LOAD entry_name_save 0
RELOAD myname
MOVE _

58
examples/profile/main.go Normal file
View File

@ -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)
}
}

View File

@ -0,0 +1 @@
inky

1
examples/profile/root Normal file
View File

@ -0,0 +1 @@
Please input profile data

View File

@ -0,0 +1,3 @@
DOWN entry_name 0 "name"
DOWN entry_email 1 "email"
DOWN entry_sex 2 "sex"

View File

@ -264,7 +264,7 @@ func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
if err != nil { if err != nil {
return b, err return b, err
} }
log.Printf("loaded additional code: %x", code) log.Printf("loaded code for %s: %x", sym, code)
b = append(b, code...) b = append(b, code...)
vm.Reset() vm.Reset()
return b, nil return b, nil