go-vise

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

main.go (2357B)


      1 // Example: Assemble and retrieve state flags using string identifiers specified in csv file.
      2 package main
      3 
      4 import (
      5 	"context"
      6 	"fmt"
      7 	"os"
      8 	"path"
      9 	"strings"
     10 
     11 	testdataloader "github.com/peteole/testdata-loader"
     12 
     13 	"git.defalsify.org/vise.git/asm"
     14 	"git.defalsify.org/vise.git/cache"
     15 	"git.defalsify.org/vise.git/engine"
     16 	"git.defalsify.org/vise.git/resource"
     17 	"git.defalsify.org/vise.git/state"
     18 	fsdb "git.defalsify.org/vise.git/db/fs"
     19 )
     20 
     21 
     22 var (
     23 	baseDir = testdataloader.GetBasePath()
     24 	scriptDir = path.Join(baseDir, "examples", "preprocessor")
     25 )
     26 
     27 type countResource struct {
     28 	parser *asm.FlagParser
     29 	count int
     30 }
     31 
     32 func newCountResource(fp string) (*countResource, error) {
     33 	var err error
     34 	pfp := path.Join(fp, "pp.csv")
     35 	parser := asm.NewFlagParser()
     36 	_, err = parser.Load(pfp)
     37 	if err != nil {
     38 		return nil, err
     39 	}
     40 	return &countResource{
     41 		count: 0,
     42 		parser: parser,
     43 	}, nil
     44 }
     45 
     46 func(rsc* countResource) poke(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     47 	var r resource.Result
     48 
     49 	ss := strings.Split(sym, "_")
     50 
     51 	r.Content = "You will see this if this flag did not have a description"
     52 	r.FlagReset = []uint32{8, 9, 10}
     53 	v, err := rsc.parser.GetFlag(ss[1])
     54 	if err != nil {
     55 		v = 8 + uint32(rsc.count) + 1
     56 		r.FlagSet = []uint32{8 + uint32(rsc.count) + 1}
     57 	}
     58 	r.FlagSet = []uint32{uint32(v)}
     59 	s, err := rsc.parser.GetDescription(v)
     60 	if err == nil {
     61 		r.Content = s 
     62 	}
     63 
     64 	rsc.count++
     65 
     66 	return r, nil
     67 }
     68 
     69 func main() {
     70 	root := "root"
     71 	fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir)
     72 
     73 	ctx := context.Background()
     74 	st := state.NewState(5)
     75 	st.UseDebug()
     76 	store := fsdb.NewFsDb()
     77 	err := store.Connect(ctx, scriptDir)
     78 	if err != nil {
     79 		panic(err)
     80 	}
     81 	rsf := resource.NewDbResource(store)
     82 	rs, err := newCountResource(scriptDir)
     83 	if err != nil {
     84 		fmt.Fprintf(os.Stderr, "aux handler fail: %v\n", err)
     85 		os.Exit(1)
     86 	}
     87 	rsf.AddLocalFunc("flag_foo", rs.poke)
     88 	rsf.AddLocalFunc("flag_bar", rs.poke)
     89 	rsf.AddLocalFunc("flag_schmag", rs.poke)
     90 	rsf.AddLocalFunc("flag_start", rs.poke)
     91 	ca := cache.NewCache()
     92 	cfg := engine.Config{
     93 		Root: "root",
     94 	}
     95 	en := engine.NewEngine(cfg, rsf)
     96 	en = en.WithState(st)
     97 	en = en.WithMemory(ca)
     98 
     99 	err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
    100 	if err != nil {
    101 		fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
    102 		os.Exit(1)
    103 	}
    104 }