go-vise

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

commit 8ddfd68f3372d2d7723404692f9354d894f8b55c
parent 95bee7f8e07e85630285c9ad84a74aa3f32264da
Author: lash <dev@holbrook.no>
Date:   Sun,  9 Apr 2023 15:58:35 +0100

Factor out engine loop code

Diffstat:
Mgo/dev/interactive.go | 30++----------------------------
Ago/engine/loop.go | 39+++++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 28 deletions(-)

diff --git a/go/dev/interactive.go b/go/dev/interactive.go @@ -1,12 +1,9 @@ package main import ( - "bufio" - "bytes" "context" "flag" "fmt" - "strings" "os" "git.defalsify.org/festive/engine" @@ -22,32 +19,9 @@ func main() { ctx := context.Background() en := engine.NewDefaultEngine(dir) - err := en.Init(root, ctx) + err := engine.Loop(root, &en, ctx) if err != nil { - fmt.Fprintf(os.Stderr, "cannot init: %v\n", err) + fmt.Fprintf(os.Stderr, "loop exited with error: %v", err) os.Exit(1) } - - b := bytes.NewBuffer(nil) - en.WriteResult(b) - fmt.Println(b.String()) - - running := true - for running { - reader := bufio.NewReader(os.Stdin) - in, err := reader.ReadString('\n') - if err != nil { - fmt.Fprintf(os.Stderr, "cannot read input: %v\n", err) - os.Exit(1) - } - in = strings.TrimSpace(in) - running, err = en.Exec([]byte(in), ctx) - if err != nil { - fmt.Fprintf(os.Stderr, "unexpected termination: %v\n", err) - os.Exit(1) - } - b := bytes.NewBuffer(nil) - en.WriteResult(b) - fmt.Println(b.String()) - } } diff --git a/go/engine/loop.go b/go/engine/loop.go @@ -0,0 +1,39 @@ +package engine + +import ( + "bufio" + "bytes" + "context" + "fmt" + "os" + "strings" +) + +func Loop(startSym string, en *Engine, ctx context.Context) error { + err := en.Init(startSym, ctx) + if err != nil { + return fmt.Errorf("cannot init: %v\n", err) + } + + b := bytes.NewBuffer(nil) + en.WriteResult(b) + fmt.Println(b.String()) + + running := true + for running { + reader := bufio.NewReader(os.Stdin) + in, err := reader.ReadString('\n') + if err != nil { + return fmt.Errorf("cannot read input: %v\n", err) + } + in = strings.TrimSpace(in) + running, err = en.Exec([]byte(in), ctx) + if err != nil { + return fmt.Errorf("unexpected termination: %v\n", err) + } + b := bytes.NewBuffer(nil) + en.WriteResult(b) + fmt.Println(b.String()) + } + return nil +}