2023-03-31 11:52:04 +02:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"context"
|
2023-03-31 19:04:11 +02:00
|
|
|
"fmt"
|
2023-03-31 16:24:29 +02:00
|
|
|
"log"
|
2023-03-31 11:52:04 +02:00
|
|
|
|
|
|
|
"git.defalsify.org/festive/resource"
|
2023-03-31 19:04:11 +02:00
|
|
|
"git.defalsify.org/festive/router"
|
|
|
|
"git.defalsify.org/festive/state"
|
2023-03-31 11:52:04 +02:00
|
|
|
)
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
//type Runner func(instruction []byte, st state.State, rs resource.Resource, ctx context.Context) (state.State, []byte, error)
|
2023-03-31 11:52:04 +02:00
|
|
|
|
2023-03-31 20:24:30 +02:00
|
|
|
func argFromBytes(input []byte) (string, []byte, error) {
|
|
|
|
if len(input) == 0 {
|
|
|
|
return "", input, fmt.Errorf("zero length input")
|
|
|
|
}
|
|
|
|
sz := input[0]
|
|
|
|
out := input[1:1+sz]
|
|
|
|
return string(out), input[1+sz:], nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// Apply applies input to router bytecode to resolve the node symbol to execute.
|
|
|
|
//
|
|
|
|
// The execution byte code is initialized with the appropriate MOVE
|
|
|
|
//
|
|
|
|
// If the router indicates an argument input, the optional argument is set on the state.
|
|
|
|
//
|
|
|
|
// TODO: the bytecode load is a separate step so Run should be run separately.
|
2023-04-01 15:47:03 +02:00
|
|
|
func Apply(input []byte, instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 18:27:10 +02:00
|
|
|
var err error
|
2023-03-31 20:24:30 +02:00
|
|
|
|
2023-04-01 15:47:03 +02:00
|
|
|
log.Printf("running input %v against instruction %v", input, instruction)
|
2023-03-31 20:24:30 +02:00
|
|
|
arg, input, err := argFromBytes(input)
|
2023-03-31 18:27:10 +02:00
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return input, err
|
2023-03-31 18:27:10 +02:00
|
|
|
}
|
2023-03-31 20:24:30 +02:00
|
|
|
|
2023-04-01 15:47:03 +02:00
|
|
|
rt := router.FromBytes(instruction)
|
2023-03-31 20:24:30 +02:00
|
|
|
sym := rt.Get(arg)
|
2023-03-31 19:04:11 +02:00
|
|
|
if sym == "" {
|
|
|
|
sym = rt.Default()
|
2023-03-31 20:24:30 +02:00
|
|
|
st.PutArg(arg)
|
2023-04-01 15:47:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 20:24:30 +02:00
|
|
|
if sym == "" {
|
2023-03-31 20:40:54 +02:00
|
|
|
instruction = NewLine([]byte{}, MOVE, []string{"_catch"}, nil, nil)
|
2023-03-31 20:24:30 +02:00
|
|
|
} else {
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = rs.GetCode(sym)
|
|
|
|
if err != nil {
|
|
|
|
return instruction, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sym == "_" {
|
|
|
|
instruction = NewLine([]byte{}, BACK, nil, nil, nil)
|
|
|
|
} else {
|
|
|
|
new_instruction := NewLine([]byte{}, MOVE, []string{sym}, nil, nil)
|
|
|
|
instruction = append(new_instruction, instruction...)
|
|
|
|
}
|
2023-03-31 20:24:30 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = Run(instruction, st, rs, ctx)
|
2023-03-31 20:24:30 +02:00
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 19:04:11 +02:00
|
|
|
}
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, nil
|
2023-03-31 18:27:10 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// Run extracts individual op codes and arguments and executes them.
|
|
|
|
//
|
|
|
|
// Each step may update the state.
|
|
|
|
//
|
|
|
|
// On error, the remaining instructions will be returned. State will not be rolled back.
|
2023-04-01 15:47:03 +02:00
|
|
|
func Run(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 16:03:54 +02:00
|
|
|
var err error
|
|
|
|
for len(instruction) > 0 {
|
2023-03-31 16:24:29 +02:00
|
|
|
log.Printf("instruction is now %v", instruction)
|
2023-03-31 16:03:54 +02:00
|
|
|
op := binary.BigEndian.Uint16(instruction[:2])
|
|
|
|
if op > _MAX {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, fmt.Errorf("opcode value %v out of range (%v)", op, _MAX)
|
2023-03-31 16:03:54 +02:00
|
|
|
}
|
|
|
|
switch op {
|
|
|
|
case CATCH:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunCatch(instruction[2:], st, rs, ctx)
|
2023-03-31 16:03:54 +02:00
|
|
|
case CROAK:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunCroak(instruction[2:], st, rs, ctx)
|
2023-03-31 16:03:54 +02:00
|
|
|
case LOAD:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunLoad(instruction[2:], st, rs, ctx)
|
2023-03-31 16:03:54 +02:00
|
|
|
case RELOAD:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunReload(instruction[2:], st, rs, ctx)
|
2023-03-31 16:03:54 +02:00
|
|
|
case MAP:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunMap(instruction[2:], st, rs, ctx)
|
2023-03-31 20:24:30 +02:00
|
|
|
case MOVE:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunMove(instruction[2:], st, rs, ctx)
|
2023-03-31 21:05:57 +02:00
|
|
|
case BACK:
|
2023-04-01 15:47:03 +02:00
|
|
|
instruction, err = RunBack(instruction[2:], st, rs, ctx)
|
2023-03-31 16:03:54 +02:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unhandled state: %v", op)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 16:03:54 +02:00
|
|
|
}
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, nil
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunMap executes the MAP opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunMap(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 11:59:55 +02:00
|
|
|
head, tail, err := instructionSplit(instruction)
|
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 11:59:55 +02:00
|
|
|
}
|
2023-03-31 22:08:06 +02:00
|
|
|
err = st.Map(head)
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, err
|
2023-03-31 11:59:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunMap executes the CATCH opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunCatch(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 15:04:08 +02:00
|
|
|
head, tail, err := instructionSplit(instruction)
|
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 15:04:08 +02:00
|
|
|
}
|
2023-04-01 10:03:03 +02:00
|
|
|
bitFieldSize := tail[0]
|
|
|
|
bitField := tail[1:1+bitFieldSize]
|
|
|
|
tail = tail[1+bitFieldSize:]
|
|
|
|
if st.GetIndex(bitField) {
|
|
|
|
log.Printf("catch at flag %v, moving to %v", bitField, head)
|
|
|
|
st.Down(head)
|
|
|
|
tail = []byte{}
|
|
|
|
}
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, nil
|
2023-03-31 11:59:55 +02:00
|
|
|
}
|
2023-03-31 11:52:04 +02:00
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunMap executes the CROAK opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunCroak(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 15:04:08 +02:00
|
|
|
head, tail, err := instructionSplit(instruction)
|
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 15:04:08 +02:00
|
|
|
}
|
|
|
|
_ = head
|
2023-03-31 20:24:30 +02:00
|
|
|
_ = tail
|
2023-03-31 15:04:08 +02:00
|
|
|
st.Reset()
|
2023-04-01 15:47:03 +02:00
|
|
|
return []byte{}, nil
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunLoad executes the LOAD opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunLoad(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 14:24:14 +02:00
|
|
|
head, tail, err := instructionSplit(instruction)
|
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 14:24:14 +02:00
|
|
|
}
|
2023-03-31 15:04:08 +02:00
|
|
|
if !st.Check(head) {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, fmt.Errorf("key %v already loaded", head)
|
2023-03-31 15:04:08 +02:00
|
|
|
}
|
2023-03-31 21:23:45 +02:00
|
|
|
sz := uint16(tail[0])
|
2023-03-31 16:03:54 +02:00
|
|
|
tail = tail[1:]
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
r, err := refresh(head, rs, ctx)
|
2023-03-31 14:24:14 +02:00
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, err
|
2023-03-31 14:24:14 +02:00
|
|
|
}
|
2023-03-31 21:23:45 +02:00
|
|
|
err = st.Add(head, r, sz)
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, err
|
2023-03-31 16:03:54 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunLoad executes the RELOAD opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunReload(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 16:03:54 +02:00
|
|
|
head, tail, err := instructionSplit(instruction)
|
2023-03-31 14:24:14 +02:00
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 14:24:14 +02:00
|
|
|
}
|
2023-03-31 23:35:13 +02:00
|
|
|
r, err := refresh(head, rs, ctx)
|
2023-03-31 16:03:54 +02:00
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, err
|
2023-03-31 16:03:54 +02:00
|
|
|
}
|
2023-03-31 17:12:14 +02:00
|
|
|
st.Update(head, r)
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, nil
|
2023-03-31 16:03:54 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunLoad executes the MOVE opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunMove(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 20:24:30 +02:00
|
|
|
head, tail, err := instructionSplit(instruction)
|
|
|
|
if err != nil {
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, err
|
2023-03-31 20:24:30 +02:00
|
|
|
}
|
|
|
|
st.Down(head)
|
2023-04-01 15:47:03 +02:00
|
|
|
return tail, nil
|
2023-03-31 16:03:54 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// RunLoad executes the BACK opcode
|
2023-04-01 15:47:03 +02:00
|
|
|
func RunBack(instruction []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-03-31 21:05:57 +02:00
|
|
|
st.Up()
|
2023-04-01 15:47:03 +02:00
|
|
|
return instruction, nil
|
2023-03-31 21:05:57 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// retrieve data for key
|
|
|
|
func refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
|
2023-03-31 16:03:54 +02:00
|
|
|
fn, err := rs.FuncFor(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-04-01 15:47:03 +02:00
|
|
|
if fn == nil {
|
|
|
|
return "", fmt.Errorf("no retrieve function for external symbol %v", key)
|
|
|
|
}
|
2023-03-31 23:35:13 +02:00
|
|
|
return fn(ctx)
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:35:13 +02:00
|
|
|
// split instruction into symbol and arguments
|
2023-03-31 16:03:54 +02:00
|
|
|
func instructionSplit(b []byte) (string, []byte, error) {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return "", nil, fmt.Errorf("argument is empty")
|
|
|
|
}
|
|
|
|
sz := uint8(b[0])
|
|
|
|
if sz == 0 {
|
|
|
|
return "", nil, fmt.Errorf("zero-length argument")
|
|
|
|
}
|
|
|
|
tailSz := uint8(len(b))
|
|
|
|
if tailSz < sz {
|
|
|
|
return "", nil, fmt.Errorf("corrupt instruction, len %v less than symbol length: %v", tailSz, sz)
|
|
|
|
}
|
|
|
|
r := string(b[1:1+sz])
|
|
|
|
return r, b[1+sz:], nil
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|