Add display initial to engine execution
This commit is contained in:
parent
c748daa8f7
commit
f0bfff3a20
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
|
2
go/testdata/testdata.go
vendored
2
go/testdata/testdata.go
vendored
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user