123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762 |
- #include <stdarg.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "utils/Functions.h"
- #include "vm/Operation.h"
- #include "vm/Script.h"
- static const char* VALUE_TYPE_NAMES[] = {"?", "int", "float", "pointer",
- "array"};
- void sError(Script* sc, const char* format, ...) {
- va_list args;
- va_start(args, format);
- vsnprintf(sc->error, SCRIPT_ERROR_SIZE, format, args);
- va_end(args);
- }
- static bool sRead(Script* sc, void* buffer, int length) {
- if(sc->readIndex + length > sc->code->length) {
- sError(sc, "cannot read expected %d bytes of data from bytecode");
- return true;
- }
- memcpy(buffer, sc->code->code + sc->readIndex, length);
- sc->readIndex += length;
- return false;
- }
- static Operation sReadOperation(Script* sc) {
- unsigned char c;
- if(sRead(sc, &c, 1)) {
- return OP_NOTHING;
- }
- return c;
- }
- static Value* sPeekStack(Script* sc, int type) {
- if(sc->stackIndex <= 0) {
- sError(sc, "stack underflow");
- return NULL;
- }
- int stackType = sc->stack[sc->stackIndex - 1].type;
- if(stackType != type) {
- sError(sc, "expected %s on stack but got %s", VALUE_TYPE_NAMES[type],
- VALUE_TYPE_NAMES[stackType]);
- return NULL;
- }
- return sc->stack + (sc->stackIndex - 1);
- }
- bool sPopInt32(Script* sc, int32* i) {
- Value* v = sPeekStack(sc, VT_INT);
- if(v == NULL) {
- return true;
- }
- *i = v->data.intValue;
- sc->stackIndex--;
- return false;
- }
- bool sPopFloat(Script* sc, float* f) {
- Value* v = sPeekStack(sc, VT_FLOAT);
- if(v == NULL) {
- return true;
- }
- *f = v->data.floatValue;
- sc->stackIndex--;
- return false;
- }
- static bool sReadInt32(Script* sc, int32* i) {
- return sRead(sc, i, sizeof(int32));
- }
- static bool sReadFloat(Script* sc, float* f) {
- return sRead(sc, f, sizeof(float));
- }
- static Value* sPushStack(Script* sc, int values) {
- if(sc->stackIndex + values > SCRIPT_STACK_SIZE) {
- sError(sc, "stack overflow");
- return NULL;
- }
- Value* v = sc->stack + sc->stackIndex;
- for(int i = 0; i < values; i++) {
- v[i].type = VT_NOT_SET;
- }
- sc->stackIndex += values;
- return v;
- }
- bool sPushInt32(Script* sc, int32 i) {
- Value* v = sPushStack(sc, 1);
- if(v == NULL) {
- return true;
- }
- v->type = VT_INT;
- v->data.intValue = i;
- return false;
- }
- bool sPushFloat(Script* sc, float f) {
- Value* v = sPushStack(sc, 1);
- if(v == NULL) {
- return true;
- }
- v->type = VT_FLOAT;
- v->data.floatValue = f;
- return false;
- }
- static void sPushInt32Value(Script* sc) {
- int32 value = 0;
- if(!sReadInt32(sc, &value)) {
- sPushInt32(sc, value);
- }
- }
- static void sPushFloatValue(Script* sc) {
- float value = 0;
- if(!sReadFloat(sc, &value)) {
- sPushFloat(sc, value);
- }
- }
- #define ZERO_CHECK(name) \
- if(values[0] == 0) { \
- sError(sc, name " by 0"); \
- return; \
- }
- #define OP_BASE(type, Type, RType, op, check) \
- { \
- type values[2]; \
- if(!sPop##Type(sc, values) && !sPop##Type(sc, values + 1)) { \
- check; \
- sPush##RType(sc, values[1] op values[0]); \
- } \
- }
- #define CHECKED_NUMBER_OP(type, Type, op, check) \
- OP_BASE(type, Type, Type, op, check)
- #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, )
- #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Int32, op, )
- #define DIVISION(type, Type) \
- CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division"));
- #define MODULE(type, Type) \
- CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module"));
- #define INVERT_SIGN(type, Type) \
- { \
- type value = 0; \
- if(!sPop##Type(sc, &value)) { \
- sPush##Type(sc, -value); \
- } \
- }
- static void sReserveBytes(Script* sc) {
- int32 values = 0;
- int32 offset = 0;
- if(sReadInt32(sc, &values) || sReadInt32(sc, &offset)) {
- return;
- }
- int32 oldIndex = sc->stackVarIndex;
- sc->stackVarIndex = sc->stackIndex - offset;
- sPushStack(sc, values - offset);
- sPushInt32(sc, oldIndex);
- }
- static void sGlobalReserveBytes(Script* sc) {
- int32 values = 0;
- if(sReadInt32(sc, &values)) {
- return;
- }
- sc->stackVarIndex = sc->stackIndex;
- if(values >= 0) {
- sPushStack(sc, values);
- } else {
- sc->stackIndex += values;
- if(sc->stackIndex < 0) {
- sError(sc, "invalid global free %d", values);
- }
- }
- }
- static void sNot(Script* sc) {
- int32 value = 0;
- if(!sPopInt32(sc, &value)) {
- sPushInt32(sc, !value);
- }
- }
- static void sBitNotInt32(Script* sc) {
- int32 value = 0;
- if(!sPopInt32(sc, &value)) {
- sPushInt32(sc, ~value);
- }
- }
- static void sLine(Script* sc) {
- sRead(sc, &sc->line, 2);
- }
- static void sGoTo(Script* sc) {
- int32 gotoIndex;
- if(!sReadInt32(sc, &gotoIndex)) {
- sc->readIndex = gotoIndex;
- }
- }
- static void sGoSub(Script* sc) {
- int32 gotoIndex;
- int32 offset;
- if(sReadInt32(sc, &gotoIndex) || sReadInt32(sc, &offset)) {
- return;
- }
- int index = sc->stackIndex - offset - 1;
- if(index < 0 || index >= SCRIPT_STACK_SIZE) {
- sError(sc, "invalid gosub offset");
- return;
- } else if(sc->stack[index].type != VT_INT) {
- sError(sc, "gosub expects an int got %s",
- VALUE_TYPE_NAMES[sc->stack[index].type]);
- return;
- }
- sc->stack[index].data.intValue = sc->readIndex;
- sc->readIndex = gotoIndex;
- }
- static void sReturn(Script* sc) {
- int32 values = 0;
- int32 varIndex = 0;
- if(sReadInt32(sc, &values) || sPopInt32(sc, &varIndex)) {
- return;
- }
- sc->stackVarIndex = varIndex;
- if(sc->stackIndex < values) {
- sError(sc, "invalid return index");
- return;
- }
- sc->stackIndex -= values;
- if(sPopInt32(sc, &sc->readIndex) || sc->readIndex < 0) {
- sError(sc, "read index is corrupt");
- }
- }
- static void sReturnPointer(Script* sc) {
- Value* v = sPeekStack(sc, VT_ARRAY);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- sReturn(sc);
- Value* p = sPushStack(sc, 1);
- if(p != NULL) {
- *p = *v;
- }
- }
- #define RETURN(type, Type) \
- { \
- type value; \
- if(!sPop##Type(sc, &value)) { \
- sReturn(sc); \
- sPush##Type(sc, value); \
- } \
- }
- static void sIfGoTo(Script* sc) {
- int32 gotoIndex = 0;
- int32 value = false;
- if(!sReadInt32(sc, &gotoIndex) && !sPopInt32(sc, &value) && !value) {
- sc->readIndex = gotoIndex;
- }
- }
- static void sPeekFalseGoTo(Script* sc) {
- int32 gotoIndex = 0;
- if(sReadInt32(sc, &gotoIndex)) {
- return;
- }
- Value* v = sPeekStack(sc, VT_INT);
- if(v != NULL && v->data.intValue == 0) {
- sc->readIndex = gotoIndex;
- }
- }
- static void sPeekTrueGoTo(Script* sc) {
- int32 gotoIndex = 0;
- if(sReadInt32(sc, &gotoIndex)) {
- return;
- }
- Value* v = sPeekStack(sc, VT_INT);
- if(v != NULL && v->data.intValue != 0) {
- sc->readIndex = gotoIndex;
- }
- }
- static void sNewArray(Script* sc) {
- int32 length = 0;
- int32 size = 0;
- if(sReadInt32(sc, &size) || sPopInt32(sc, &length)) {
- return;
- }
- Value* p = sPushStack(sc, 1);
- if(p == NULL) {
- return;
- }
- p->type = VT_ARRAY;
- p->offset = 0;
- p->data.intValue = asAllocate(&sc->arrays, size, length);
- if(p->data.intValue == -1) {
- sError(sc, "out of memory");
- } else if(p->data.intValue == -2) {
- sError(sc, "bad allocation");
- }
- }
- static void sLength(Script* sc) {
- Value* v = sPeekStack(sc, VT_ARRAY);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- SnuviArray* a = asGet(&sc->arrays, v->data.intValue);
- if(a == NULL) {
- sError(sc, "invalid heap pointer %d", v->data.intValue);
- return;
- }
- sPushInt32(sc, a->length);
- }
- static void sDereference(Script* sc) {
- int32 address = 0;
- if(sReadInt32(sc, &address)) {
- return;
- }
- Value* v = sPushStack(sc, 1);
- if(v != NULL) {
- v->type = VT_POINTER;
- v->offset = address + sc->stackVarIndex;
- v->data.intValue = -1;
- }
- }
- static void sGlobalDereference(Script* sc) {
- int32 address = 0;
- if(sReadInt32(sc, &address)) {
- return;
- }
- Value* v = sPushStack(sc, 1);
- if(v != NULL) {
- v->type = VT_POINTER;
- v->offset = address;
- v->data.intValue = -1;
- }
- }
- static void sDuplicateReference(Script* sc) {
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v != NULL) {
- Value* copy = sPushStack(sc, 1);
- if(copy != NULL) {
- *copy = *v;
- }
- }
- }
- static void sAddReference(Script* sc) {
- int32 size = 0;
- int32 add = 0;
- if(sReadInt32(sc, &size) || sPopInt32(sc, &add)) {
- return;
- }
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v != NULL) {
- v->offset += add * size;
- } else {
- sc->error[0] = '\0';
- Value* v = sPeekStack(sc, VT_ARRAY);
- if(v != NULL) {
- v->type = VT_POINTER;
- v->offset += add * size;
- }
- }
- }
- static void sPushStructReference(Script* sc) {
- int32 address = 0;
- if(sReadInt32(sc, &address)) {
- return;
- }
- Value* v = sPushStack(sc, 1);
- if(v != NULL) {
- int index = address + sc->stackVarIndex;
- if(index < 0 || index >= SCRIPT_STACK_SIZE) {
- sError(sc, "invalid struct reference address");
- return;
- }
- *v = sc->stack[index];
- }
- }
- static Value* sLoadFromPointer(Script* sc, Value* p, int type) {
- if(p->data.intValue < 0) {
- if(p->offset < 0 || p->offset >= SCRIPT_STACK_SIZE) {
- sError(sc, "load offset overflow");
- return NULL;
- }
- Value* v = sc->stack + p->offset;
- if(v->type != type && v->type != VT_NOT_SET) {
- sError(sc, "pointer did not point to %s but %s",
- VALUE_TYPE_NAMES[type], VALUE_TYPE_NAMES[v->type]);
- return NULL;
- }
- return v;
- }
- SnuviArray* a = asGet(&sc->arrays, p->data.intValue);
- if(a == NULL) {
- sError(sc, "invalid heap pointer %d", p->data.intValue);
- return NULL;
- }
- if(p->offset < 0 || p->offset >= a->realLength) {
- sError(sc, "invalid heap pointer offset %d %d", p->offset,
- a->realLength);
- return NULL;
- }
- return a->data + p->offset;
- }
- SnuviArray* sGetArray(Script* sc) {
- Value* v = sPeekStack(sc, VT_ARRAY);
- if(v == NULL) {
- return NULL;
- }
- sc->stackIndex--;
- return asGet(&sc->arrays, v->data.intValue);
- }
- Value* sPopStructPointer(Script* sc, int type) {
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return NULL;
- }
- sc->stackIndex--;
- return sLoadFromPointer(sc, v, type);
- }
- static void sStoreInt32(Script* sc) {
- int32 i = 0;
- if(sPopInt32(sc, &i)) {
- return;
- }
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- v = sLoadFromPointer(sc, v, VT_INT);
- if(v != NULL) {
- v->data.intValue = i;
- v->type = VT_INT;
- }
- }
- static void sStoreFloat(Script* sc) {
- float f = 0;
- if(sPopFloat(sc, &f)) {
- return;
- }
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- v = sLoadFromPointer(sc, v, VT_FLOAT);
- if(v != NULL) {
- v->data.floatValue = f;
- v->type = VT_FLOAT;
- }
- }
- static void sStoreArray(Script* sc) {
- Value* array = sPeekStack(sc, VT_ARRAY);
- if(array == NULL) {
- return;
- }
- sc->stackIndex--;
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- v = sLoadFromPointer(sc, v, VT_ARRAY);
- if(v != NULL) {
- *v = *array;
- }
- }
- static void sLoadInt32(Script* sc) {
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- v = sLoadFromPointer(sc, v, VT_INT);
- if(v != NULL) {
- sPushInt32(sc, v->data.intValue);
- }
- }
- static void sLoadFloat(Script* sc) {
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- v = sLoadFromPointer(sc, v, VT_FLOAT);
- if(v != NULL) {
- sPushFloat(sc, v->data.floatValue);
- }
- }
- static void sLoadArray(Script* sc) {
- Value* v = sPeekStack(sc, VT_POINTER);
- if(v == NULL) {
- return;
- }
- sc->stackIndex--;
- v = sLoadFromPointer(sc, v, VT_ARRAY);
- if(v != NULL) {
- Value* array = sPushStack(sc, 1);
- if(array != NULL) {
- *array = *v;
- }
- }
- }
- static void sEqualArrays(Script* sc, bool wanted) {
- Value* a = sPeekStack(sc, VT_ARRAY);
- if(a == NULL) {
- return;
- }
- sc->stackIndex--;
- Value* b = sPeekStack(sc, VT_ARRAY);
- if(b == NULL) {
- return;
- }
- sc->stackIndex--;
- sPushInt32(sc, (a->offset == b->offset &&
- a->data.intValue == b->data.intValue) == wanted);
- }
- static void sCall(Script* sc) {
- int32 function = 0;
- if(sReadInt32(sc, &function)) {
- return;
- }
- if(gfsCall(sc, function)) {
- sError(sc, "invalid function call");
- }
- }
- static void sPushText(Script* sc) {
- int32 length;
- if(sReadInt32(sc, &length)) {
- return;
- }
- if(length < 0 || length > 65535) {
- sError(sc, "too large string");
- return;
- }
- Value* p = sPushStack(sc, 1);
- if(p == NULL) {
- return;
- }
- p->type = VT_ARRAY;
- p->offset = 0;
- p->data.intValue = asAllocate(&sc->arrays, 1, length);
- if(p->data.intValue == -1) {
- sError(sc, "out of memory");
- return;
- } else if(p->data.intValue == -2) {
- sError(sc, "bad allocation");
- return;
- }
- SnuviArray* a = asGet(&sc->arrays, p->data.intValue);
- if(a == NULL) {
- sError(sc, "cannot find text array");
- return;
- }
- for(int i = 0; i < length; i++) {
- sReadInt32(sc, &(a->data[i].data.intValue));
- a->data[i].type = VT_INT;
- }
- }
- static Value* sChangeBase(Script* sc, char* c) {
- if(sRead(sc, c, sizeof(char))) {
- return NULL;
- }
- Value* p = sPeekStack(sc, VT_POINTER);
- if(p == NULL) {
- return NULL;
- }
- sc->stackIndex--;
- return sLoadFromPointer(sc, p, VT_INT);
- }
- static void sChange(Script* sc) {
- char c;
- Value* v = sChangeBase(sc, &c);
- if(v != NULL) {
- v->data.intValue += c;
- }
- }
- static void sPushBeforeChange(Script* sc) {
- char c;
- Value* v = sChangeBase(sc, &c);
- if(v != NULL) {
- sPushInt32(sc, v->data.intValue);
- v->data.intValue += c;
- }
- }
- static void sPushAfterChange(Script* sc) {
- char c;
- Value* v = sChangeBase(sc, &c);
- if(v != NULL) {
- v->data.intValue += c;
- sPushInt32(sc, v->data.intValue);
- }
- }
- static void sFloatToInt32(Script* sc) {
- Value* v = sPeekStack(sc, VT_FLOAT);
- if(v != NULL) {
- v->data.intValue = v->data.floatValue;
- v->type = VT_INT;
- }
- }
- static void sInt32ToFloat(Script* sc) {
- Value* v = sPeekStack(sc, VT_INT);
- if(v != NULL) {
- v->data.floatValue = v->data.intValue;
- v->type = VT_FLOAT;
- }
- }
- #define CASE_NUMBER_OP(name, op) \
- case OP_##name##_INT: NUMBER_OP(int32, Int32, op); break; \
- case OP_##name##_FLOAT: \
- NUMBER_OP(float, Float, op); \
- break;
- #define CASE_BOOL_OP(name, op) \
- case OP_##name##_INT: BOOL_OP(int32, Int32, op); break; \
- case OP_##name##_FLOAT: \
- BOOL_OP(float, Float, op); \
- break;
- #define CASE_TYPE(TYPE, Type, type) \
- case OP_RETURN_##TYPE: RETURN(type, Type); break; \
- case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break; \
- case OP_NOT_EQUAL_##TYPE: BOOL_OP(type, Type, !=); break;
- static void sConsumeInstruction(Script* sc) {
- switch(sReadOperation(sc)) {
- CASE_NUMBER_OP(ADD, +);
- CASE_NUMBER_OP(SUB, -);
- CASE_NUMBER_OP(MUL, *);
- CASE_BOOL_OP(LESS, <);
- CASE_BOOL_OP(LESS_EQUAL, <=);
- CASE_BOOL_OP(GREATER, >);
- CASE_BOOL_OP(GREATER_EQUAL, >=);
- CASE_TYPE(INT, Int32, int32);
- CASE_TYPE(FLOAT, Float, float);
- case OP_STORE_INT: sStoreInt32(sc); break;
- case OP_STORE_FLOAT: sStoreFloat(sc); break;
- case OP_LOAD_INT: sLoadInt32(sc); break;
- case OP_LOAD_FLOAT: sLoadFloat(sc); break;
- case OP_PUSH_PRE_CHANGE_INT: sPushAfterChange(sc); break;
- case OP_PUSH_POST_CHANGE_INT: sPushBeforeChange(sc); break;
- case OP_CHANGE_INT: sChange(sc); break;
- case OP_FLOAT_TO_INT: sFloatToInt32(sc); break;
- case OP_INT_TO_FLOAT: sInt32ToFloat(sc); break;
- case OP_BIT_AND_INT: NUMBER_OP(int32, Int32, &); break;
- case OP_BIT_OR_INT: NUMBER_OP(int32, Int32, |); break;
- case OP_BIT_XOR_INT: NUMBER_OP(int32, Int32, ^); break;
- case OP_LEFT_SHIFT_INT: NUMBER_OP(int32, Int32, <<); break;
- case OP_RIGHT_SHIFT_INT: NUMBER_OP(int32, Int32, >>); break;
- case OP_NOTHING: break;
- case OP_PUSH_INT: sPushInt32Value(sc); break;
- case OP_PUSH_FLOAT: sPushFloatValue(sc); break;
- case OP_PUSH_TEXT: sPushText(sc); break;
- case OP_DIV_INT: DIVISION(int32, Int32); break;
- case OP_DIV_FLOAT: DIVISION(float, Float); break;
- case OP_MOD_INT: MODULE(int32, Int32); break;
- case OP_INVERT_SIGN_INT: INVERT_SIGN(int32, Int32); break;
- case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
- case OP_NOT: sNot(sc); break;
- case OP_AND: BOOL_OP(int32, Int32, &&); break;
- case OP_OR: BOOL_OP(int32, Int32, ||); break;
- case OP_BIT_NOT_INT: sBitNotInt32(sc); break;
- case OP_LINE: sLine(sc); break;
- case OP_GOTO: sGoTo(sc); break;
- case OP_IF_GOTO: sIfGoTo(sc); break;
- case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
- case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
- case OP_GOSUB: sGoSub(sc); break;
- case OP_RETURN: sReturn(sc); break;
- case OP_RETURN_POINTER: sReturnPointer(sc); break;
- case OP_RESERVE: sReserveBytes(sc); break;
- case OP_GRESERVE: sGlobalReserveBytes(sc); break;
- case OP_DEREFERENCE_VAR: sDereference(sc); break;
- case OP_DEREFERENCE_GVAR: sGlobalDereference(sc); break;
- case OP_LOAD_ARRAY: sLoadArray(sc); break;
- case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); break;
- case OP_ADD_REFERENCE: sAddReference(sc); break;
- case OP_PUSH_STRUCT_REFERENCE: sPushStructReference(sc); break;
- case OP_NEW: sNewArray(sc); break;
- case OP_LENGTH: sLength(sc); break;
- case OP_STORE_ARRAY: sStoreArray(sc); break;
- case OP_EQUAL_POINTER: sEqualArrays(sc, true); break;
- case OP_NOT_EQUAL_POINTER: sEqualArrays(sc, false); break;
- case OP_CALL: sCall(sc); break;
- }
- }
- static bool sHasData(Script* sc) {
- return sc->readIndex < sc->code->length;
- }
- Script* sInit(ByteCode* code) {
- Script* sc = malloc(sizeof(Script));
- sc->error[0] = '\0';
- sc->code = code;
- sc->readIndex = 0;
- sc->stackIndex = 0;
- sc->stackVarIndex = 0;
- sc->line = 0;
- asInit(&sc->arrays);
- return sc;
- }
- void sDelete(Script* sc) {
- bcDelete(sc->code);
- asDelete(&sc->arrays);
- free(sc);
- }
- void sRun(Script* sc) {
- while(sHasData(sc)) {
- sConsumeInstruction(sc);
- if(sc->error[0] != '\0') {
- return;
- }
- }
- }
|