go-vise

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

main.go (2542B)


      1 // Example: Basic flags and input processing, and symbol execution.
      2 package main
      3 
      4 import (
      5 	"context"
      6 	"flag"
      7 	"fmt"
      8 	"os"
      9 	"path"
     10 	"strings"
     11 
     12 	testdataloader "github.com/peteole/testdata-loader"
     13 
     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 const (
     22 	USERFLAG_HAVESOMETHING = iota + state.FLAG_USERSTART
     23 )
     24 
     25 var (
     26 	baseDir = testdataloader.GetBasePath()
     27 	scriptDir = path.Join(baseDir, "examples", "intro")
     28 )
     29 
     30 type introResource struct {
     31 	*resource.DbResource
     32 	c int64
     33 	v []string
     34 }
     35 
     36 func newintroResource(ctx context.Context) introResource {
     37 	store := fsdb.NewFsDb()
     38 	err := store.Connect(ctx, scriptDir) 
     39 	if err != nil {
     40 		panic(err)
     41 	}
     42 	rs := resource.NewDbResource(store)
     43 	return introResource{rs, 0, []string{}}
     44 }
     45 
     46 // increment counter.
     47 // return a string representing the current value of the counter.
     48 func(c *introResource) count(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     49 	s := "%v time"
     50 	if c.c != 1 {
     51 		s += "s"
     52 	}
     53 	r := resource.Result{
     54 		Content: fmt.Sprintf(s, c.c),
     55 	}
     56 	c.c += 1 
     57 	return  r, nil
     58 }
     59 
     60 // if input is suppled, append it to the stored string vector and set the HAVESOMETHING flag.
     61 // return the stored string vector value, one string per line.
     62 func(c *introResource) something(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     63 	c.v = append(c.v, string(input))
     64 	r := resource.Result{
     65 		Content: strings.Join(c.v, "\n"),
     66 	}
     67 	if len(input) > 0 {
     68 		r.FlagSet = []uint32{USERFLAG_HAVESOMETHING}
     69 	}
     70 	return r, nil
     71 }
     72 
     73 func main() {
     74 	var err error
     75 	var dir string
     76 	var root string
     77 	var size uint
     78 	var sessionId string
     79 	flag.UintVar(&size, "s", 0, "max size of output")
     80 	flag.StringVar(&root, "root", "root", "entry point symbol")
     81 	flag.StringVar(&sessionId, "session-id", "default", "session id")
     82 	flag.Parse()
     83 	fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
     84 	
     85 	ctx := context.Background()
     86 	st := state.NewState(3)
     87 	rs := newintroResource(ctx)
     88 	rs.AddLocalFunc("count", rs.count)
     89 	rs.AddLocalFunc("something", rs.something)
     90 	ca := cache.NewCache()
     91 	cfg := engine.Config{
     92 		Root: "root",
     93 		SessionId: sessionId,
     94 		OutputSize: uint32(size),
     95 	}
     96 	en := engine.NewEngine(cfg, rs)
     97 	en = en.WithState(st)
     98 	en = en.WithMemory(ca)
     99 
    100 	err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
    101 	if err != nil {
    102 		fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
    103 		os.Exit(1)
    104 	}
    105 }