go-vise

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

commit cdd0f97d046e60946e0688294831493aebdc0f50
parent 7bb678ca54afa29cd8a7073ef968384828c1614f
Author: lash <dev@holbrook.no>
Date:   Mon, 26 Aug 2024 03:16:36 +0100

Add reuse function with sym example

Diffstat:
Aexamples/reuse/Makefile | 10++++++++++
Aexamples/reuse/bar.vis | 3+++
Aexamples/reuse/foo.vis | 3+++
Aexamples/reuse/main.go | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/reuse/root | 1+
Aexamples/reuse/root.vis | 6++++++
6 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/examples/reuse/Makefile b/examples/reuse/Makefile @@ -0,0 +1,10 @@ +INPUTS = $(wildcard ./*.vis) +TXTS = $(wildcard ./*.txt.orig) + +%.vis: + go run ../../dev/asm $(basename $@).vis > $(basename $@).bin + +all: $(INPUTS) $(TXTS) + +%.txt.orig: + cp -v $(basename $@).orig $(basename $@) diff --git a/examples/reuse/bar.vis b/examples/reuse/bar.vis @@ -0,0 +1,3 @@ +LOAD do_bar 0 +MAP do_bar +HALT diff --git a/examples/reuse/foo.vis b/examples/reuse/foo.vis @@ -0,0 +1,3 @@ +LOAD do_foo 0 +MAP do_foo +HALT diff --git a/examples/reuse/main.go b/examples/reuse/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + "fmt" + "os" + "path" + + 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" +) + +const ( + USERFLAG = iota + state.FLAG_USERSTART +) + +var ( + baseDir = testdataloader.GetBasePath() + scriptDir = path.Join(baseDir, "examples", "reuse") + emptyResult = resource.Result{} +) + +func same(ctx context.Context, sym string, input []byte) (resource.Result, error) { + return resource.Result{ + Content: "You came through the symbol " + sym, + }, nil +} + +func main() { + root := "root" + fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + + st := state.NewState(0) + rs := resource.NewFsResource(scriptDir) + rs.AddLocalFunc("do_foo", same) + rs.AddLocalFunc("do_bar", same) + ca := cache.NewCache() + cfg := engine.Config{ + Root: "root", + } + ctx := context.Background() + en := engine.NewEngine(ctx, cfg, &st, rs, 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/reuse/root b/examples/reuse/root @@ -0,0 +1 @@ +choose diff --git a/examples/reuse/root.vis b/examples/reuse/root.vis @@ -0,0 +1,6 @@ +MOUT foo 0 +MOUT bar 1 +HALT +INCMP foo 0 +INCMP bar 1 +INCMP ^ *