WIP add profile menu example
This commit is contained in:
parent
d8c112fa92
commit
355b466746
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
examples/**/*.bin
|
6
Makefile
Normal file
6
Makefile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
examples: profile
|
||||||
|
|
||||||
|
.PHONY: profile
|
||||||
|
|
||||||
|
profile:
|
||||||
|
bash examples/compile.sh examples/profile
|
10
asm/asm.go
10
asm/asm.go
@ -93,9 +93,14 @@ 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 {
|
||||||
|
if *arg.Sym == "*" {
|
||||||
|
sym = *arg.Selector
|
||||||
|
selector = *arg.Sym
|
||||||
|
} else {
|
||||||
sym = *arg.Sym
|
sym = *arg.Sym
|
||||||
selector = *arg.Selector
|
selector = *arg.Selector
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
n, err := writeSym(b, selector)
|
n, err := writeSym(b, selector)
|
||||||
rn += n
|
rn += n
|
||||||
@ -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 {
|
||||||
|
5
cache/cache.go
vendored
5
cache/cache.go
vendored
@ -51,10 +51,13 @@ 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 len(value) > 0 {
|
||||||
|
sz = ca.checkCapacity(value)
|
||||||
if sz == 0 {
|
if sz == 0 {
|
||||||
return fmt.Errorf("Cache capacity exceeded %v of %v", ca.CacheUseSize + sz, ca.CacheSize)
|
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
|
||||||
ca.CacheUseSize += sz
|
ca.CacheUseSize += sz
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
@ -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
5
examples/compile.bash
Normal 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
|
2
examples/profile/entry_name
Normal file
2
examples/profile/entry_name
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Name is currently: {{.myname}}
|
||||||
|
Enter new name.
|
6
examples/profile/entry_name.vis
Normal file
6
examples/profile/entry_name.vis
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
LOAD myname 32
|
||||||
|
MAP myname
|
||||||
|
MOUT 0 "abort"
|
||||||
|
HALT
|
||||||
|
INCMP 0 _
|
||||||
|
INCMP * entry_name_save
|
0
examples/profile/entry_name_save
Normal file
0
examples/profile/entry_name_save
Normal file
3
examples/profile/entry_name_save.vis
Normal file
3
examples/profile/entry_name_save.vis
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
LOAD entry_name_save 0
|
||||||
|
RELOAD myname
|
||||||
|
MOVE _
|
58
examples/profile/main.go
Normal file
58
examples/profile/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
1
examples/profile/myname.txt
Normal file
1
examples/profile/myname.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
inky
|
1
examples/profile/root
Normal file
1
examples/profile/root
Normal file
@ -0,0 +1 @@
|
|||||||
|
Please input profile data
|
3
examples/profile/root.vis
Normal file
3
examples/profile/root.vis
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
DOWN entry_name 0 "name"
|
||||||
|
DOWN entry_email 1 "email"
|
||||||
|
DOWN entry_sex 2 "sex"
|
0
testdata/foo.fst → testdata/foo.vis
vendored
0
testdata/foo.fst → testdata/foo.vis
vendored
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user