9. Run

A block of serial code can be run with the function txtvm_run.

<<funcdefs>>=
int txtvm_run(txtvm *vm, const char *buf, unsigned long sz);
<<funcs>>=
#include <stdio.h>
int txtvm_run(txtvm *vm, const char *buf, unsigned long sz)
{
    int rc;
    txtvm_io_readbuf_data d;

    d.buf = buf;
    d.pos = 0;
    d.sz = sz;
    rc = TXTVM_OK;

    txtvm_io_readbuf(&vm->io, &d);

    while (d.pos < d.sz) {
        char c;
        int n;

        n = txtvm_io_read(&vm->io, 1, &c);
        if (n <= 0) return TXTVM_NOT_OK;

        switch (c) {
            case TYPE_INT6: {
                int x;
                rc = txtvm_read_int6(vm, &x);
                TXTVM_ERROR_CHECK(rc);
                printf("INT6: %d\n", x);
                txtvm_push_int(vm, x);
                break;
            }
            case TYPE_INT12: {
                int x;
                rc = txtvm_read_int12(vm, &x);
                TXTVM_ERROR_CHECK(rc);
                printf("INT12: %d\n", x);
                txtvm_push_int(vm, x);
                break;
            }
            case TYPE_CALL: {
                int x;
                rc = txtvm_pop_int(vm, &x);
                TXTVM_ERROR_CHECK(rc);
                printf("CALL %d\n", x);
                rc = txtvm_call(vm, x);
                TXTVM_ERROR_CHECK(rc);
                break;
            }
            default:
                return TXTVM_NOT_OK;
        }
    }


    return rc;
}



prev | home | next