Add display initial to engine execution
This commit is contained in:
parent
c748daa8f7
commit
f0bfff3a20
@ -28,6 +28,10 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b := bytes.NewBuffer(nil)
|
||||||
|
en.WriteResult(b)
|
||||||
|
fmt.Println(b.String())
|
||||||
|
|
||||||
running := true
|
running := true
|
||||||
for running {
|
for running {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
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.
|
// 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 {
|
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)
|
b := vm.NewLine(nil, vm.MOVE, []string{sym}, nil, nil)
|
||||||
var err error
|
|
||||||
b, err = vm.Run(b, en.st, en.rs, ctx)
|
b, err = vm.Run(b, en.st, en.rs, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -61,7 +64,7 @@ func (en *Engine) Exec(input []byte, ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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()
|
code, err := en.st.GetCode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -88,8 +88,12 @@ func TestEngineInit(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
b := w.Bytes()
|
b := w.Bytes()
|
||||||
if !bytes.Equal(b, []byte("hello world")) {
|
expect_str := `hello world
|
||||||
t.Fatalf("expected result 'hello world', got %v", b)
|
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")
|
input := []byte("1")
|
||||||
|
@ -394,6 +394,11 @@ func(st *State) GetInput() ([]byte, error) {
|
|||||||
|
|
||||||
// SetInput is used to record the latest client input.
|
// SetInput is used to record the latest client input.
|
||||||
func(st *State) SetInput(input []byte) error {
|
func(st *State) SetInput(input []byte) error {
|
||||||
|
// if input == nil {
|
||||||
|
// log.Printf("clearing input")
|
||||||
|
// st.input = nil
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
l := len(input)
|
l := len(input)
|
||||||
if l > 255 {
|
if l > 255 {
|
||||||
return fmt.Errorf("input size %v too large (limit %v)", 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 {
|
func root() error {
|
||||||
b := []byte{}
|
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.HALT, nil, nil, nil)
|
||||||
b = vm.NewLine(b, vm.INCMP, []string{"1", "foo"}, nil, nil)
|
b = vm.NewLine(b, vm.INCMP, []string{"1", "foo"}, nil, nil)
|
||||||
b = vm.NewLine(b, vm.INCMP, []string{"2", "bar"}, 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) {
|
func Run(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
||||||
running := true
|
running := true
|
||||||
for running {
|
for running {
|
||||||
log.Printf("execute code %x", b)
|
|
||||||
op, bb, err := opSplit(b)
|
op, bb, err := opSplit(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return b, err
|
return b, err
|
||||||
}
|
}
|
||||||
b = bb
|
b = bb
|
||||||
|
log.Printf("execute code %x (%s) %x", op, OpcodeString[op], b)
|
||||||
switch op {
|
switch op {
|
||||||
case CATCH:
|
case CATCH:
|
||||||
b, err = RunCatch(b, st, rs, ctx)
|
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 {
|
if err != nil {
|
||||||
return b, err
|
return b, err
|
||||||
}
|
}
|
||||||
if sym == string(input) {
|
if sym != string(input) {
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("input match for '%s'", input)
|
log.Printf("input match for '%s'", input)
|
||||||
_, err = st.SetFlag(state.FLAG_INMATCH)
|
_, err = st.SetFlag(state.FLAG_INMATCH)
|
||||||
st.Down(target)
|
st.Down(target)
|
||||||
}
|
|
||||||
code, err := rs.GetCode(target)
|
code, err := rs.GetCode(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return b, err
|
return b, err
|
||||||
|
Loading…
Reference in New Issue
Block a user