go-vise

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

main.go (1180B)


      1 // Example: Reuse go functions for multiple LOAD symbols.
      2 package main
      3 
      4 import (
      5 	"context"
      6 	"fmt"
      7 	"os"
      8 	"path"
      9 
     10 	testdataloader "github.com/peteole/testdata-loader"
     11 
     12 	"git.defalsify.org/vise.git/engine"
     13 	"git.defalsify.org/vise.git/resource"
     14 	fsdb "git.defalsify.org/vise.git/db/fs"
     15 )
     16 
     17 var (
     18 	baseDir = testdataloader.GetBasePath()
     19 	scriptDir = path.Join(baseDir, "examples", "reuse")
     20 	emptyResult = resource.Result{}
     21 )
     22 
     23 func same(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     24 	return resource.Result{
     25 		Content: "You came through the symbol " + sym,
     26 	}, nil
     27 }
     28 
     29 func main() {
     30 	root := "root"
     31 	fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir)
     32 
     33 	ctx := context.Background()
     34 	store := fsdb.NewFsDb()
     35 	err := store.Connect(ctx, scriptDir)
     36 	if err != nil {
     37 		panic(err)
     38 	}
     39 	rs := resource.NewDbResource(store)
     40 	
     41 	rs.AddLocalFunc("do_foo", same)
     42 	rs.AddLocalFunc("do_bar", same)
     43 	cfg := engine.Config{
     44 		Root: "root",
     45 	}
     46 	en := engine.NewEngine(cfg, rs)
     47 	err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
     48 	if err != nil {
     49 		fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
     50 		os.Exit(1)
     51 	}
     52 }