From f0bfff3a203340443cdd400a1cbb7e1107261eee Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 6 Apr 2023 09:14:53 +0100 Subject: [PATCH] Add display initial to engine execution --- go/dev/interactive.go | 4 ++++ go/engine/engine.go | 7 +++++-- go/engine/engine_test.go | 8 ++++++-- go/state/state.go | 5 +++++ go/testdata/testdata.go | 2 ++ go/vm/runner.go | 12 +++++++----- 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/go/dev/interactive.go b/go/dev/interactive.go index 2fc05f5..d8657ba 100644 --- a/go/dev/interactive.go +++ b/go/dev/interactive.go @@ -28,6 +28,10 @@ func main() { os.Exit(1) } + b := bytes.NewBuffer(nil) + en.WriteResult(b) + fmt.Println(b.String()) + running := true for running { reader := bufio.NewReader(os.Stdin) diff --git a/go/engine/engine.go b/go/engine/engine.go index acb23f8..344aac9 100644 --- a/go/engine/engine.go +++ b/go/engine/engine.go @@ -32,8 +32,11 @@ func NewEngine(st *state.State, rs resource.Resource) Engine { // // It makes sure bootstrapping code has been executed, and that the exposed bytecode is ready for user input. func(en *Engine) Init(sym string, ctx context.Context) error { + err := en.st.SetInput([]byte{}) + if err != nil { + return err + } b := vm.NewLine(nil, vm.MOVE, []string{sym}, nil, nil) - var err error b, err = vm.Run(b, en.st, en.rs, ctx) if err != nil { return err @@ -61,7 +64,7 @@ func (en *Engine) Exec(input []byte, ctx context.Context) error { if err != nil { return err } - log.Printf("new execution with input 0x%x (%v)", input, len(input)) + log.Printf("new execution with input '%s' (0x%x)", input, input) code, err := en.st.GetCode() if err != nil { return err diff --git a/go/engine/engine_test.go b/go/engine/engine_test.go index 6a27ccf..06d4fb1 100644 --- a/go/engine/engine_test.go +++ b/go/engine/engine_test.go @@ -88,8 +88,12 @@ func TestEngineInit(t *testing.T) { t.Fatal(err) } b := w.Bytes() - if !bytes.Equal(b, []byte("hello world")) { - t.Fatalf("expected result 'hello world', got %v", b) + expect_str := `hello world +1:do the foo +2:go to the bar` + + if !bytes.Equal(b, []byte(expect_str)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect_str, b) } input := []byte("1") diff --git a/go/state/state.go b/go/state/state.go index 19c81db..2428a65 100644 --- a/go/state/state.go +++ b/go/state/state.go @@ -394,6 +394,11 @@ func(st *State) GetInput() ([]byte, error) { // SetInput is used to record the latest client input. func(st *State) SetInput(input []byte) error { +// if input == nil { +// log.Printf("clearing input") +// st.input = nil +// return nil +// } l := len(input) if l > 255 { return fmt.Errorf("input size %v too large (limit %v)", l, 255) diff --git a/go/testdata/testdata.go b/go/testdata/testdata.go index b18cd12..2ab1612 100644 --- a/go/testdata/testdata.go +++ b/go/testdata/testdata.go @@ -37,6 +37,8 @@ func out(sym string, b []byte, tpl string) error { func root() error { b := []byte{} + b = vm.NewLine(b, vm.MOUT, []string{"1", "do the foo"}, nil, nil) + b = vm.NewLine(b, vm.MOUT, []string{"2", "go to the bar"}, nil, nil) b = vm.NewLine(b, vm.HALT, nil, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"1", "foo"}, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"2", "bar"}, nil, nil) diff --git a/go/vm/runner.go b/go/vm/runner.go index 60894fc..f769b79 100644 --- a/go/vm/runner.go +++ b/go/vm/runner.go @@ -19,12 +19,12 @@ import ( func Run(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) { running := true for running { - log.Printf("execute code %x", b) op, bb, err := opSplit(b) if err != nil { return b, err } b = bb + log.Printf("execute code %x (%s) %x", op, OpcodeString[op], b) switch op { case CATCH: b, err = RunCatch(b, st, rs, ctx) @@ -170,11 +170,13 @@ func RunInCmp(b []byte, st *state.State, rs resource.Resource, ctx context.Conte if err != nil { return b, err } - if sym == string(input) { - log.Printf("input match for '%s'", input) - _, err = st.SetFlag(state.FLAG_INMATCH) - st.Down(target) + if sym != string(input) { + return b, nil } + + log.Printf("input match for '%s'", input) + _, err = st.SetFlag(state.FLAG_INMATCH) + st.Down(target) code, err := rs.GetCode(target) if err != nil { return b, err