#include #include #include #include #include #include "Operation.h" #include "Script.h" static 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 sPrinter(Object* o) { switch(o->type) { case OT_INT: printf("%d\n", o->data.intValue); return false; case OT_FLOAT: printf("%.2f\n", o->data.floatValue); return false; case OT_NULL: printf("null\n"); return false; case OT_BOOL: printf(o->data.intValue ? "true\n" : "false\n"); return false; case OT_VOID: printf("void\n"); return false; } return true; } static ObjectPrinter printer = sPrinter; 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 on line %d", sc->line); 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 bool sReadInt(Script* sc, int* i) { return !sRead(sc, i, sizeof(int)); } static bool sCheckType(Script* sc, Object* o, ObjectType ot) { if(o->type == ot) { return true; } sError(sc, "object is not of type %s on line %d", oGetName(ot), sc->line); return false; } static bool sPush(Script* sc, Object* o) { if(sc->stackIndex >= SCRIPT_STACK_SIZE) { sError(sc, "stack overflow on line %d", sc->line); return false; } sc->stack[sc->stackIndex++] = *o; return true; } static bool sPop(Script* sc, Object* o) { if(sc->stackIndex <= 0) { sError(sc, "stack underflow on line %d", sc->line); return true; } *o = sc->stack[--sc->stackIndex]; return false; } static void sPopEmpty(Script* sc) { Object o; sPop(sc, &o); } static Object* sPeek(Script* sc) { if(sc->stackIndex <= 0) { sError(sc, "stack underflow on line %d", sc->line); return NULL; } return sc->stack + (sc->stackIndex - 1); } static bool sPushInt(Script* sc, int value) { Object o = {.type = OT_INT, .data.intValue = value}; return sPush(sc, &o); } static void sPushFloat(Script* sc, float value) { Object o = {.type = OT_FLOAT, .data.floatValue = value}; sPush(sc, &o); } static void sPushNull(Script* sc) { Object o = {.type = OT_NULL}; sPush(sc, &o); } static void sPushBool(Script* sc, bool value) { Object o = {.type = OT_BOOL, .data.intValue = value}; sPush(sc, &o); } static void sPushVars(Script* sc) { int vars = 0; int offset = 0; if(sReadInt(sc, &vars) && sReadInt(sc, &offset)) { int stackVarIndex = sc->stackVarIndex; sc->stackVarIndex = sc->stackIndex - offset; for(int i = 0; i < vars - offset; i++) { sPushNull(sc); } sPushInt(sc, stackVarIndex); } } static void sPopVars(Script* sc) { int value = 0; Object o; if(sReadInt(sc, &value) && !sPop(sc, &o) && sCheckType(sc, &o, OT_INT)) { sc->stackVarIndex = o.data.intValue; if(sc->stackIndex < value) { sError(sc, "stack underflow on line %d", sc->line); } else { sc->stackIndex -= value; } } } static void sSet(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { sPop(sc, sc->stack + value + sc->stackVarIndex); } } static void sGet(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { sPush(sc, sc->stack + value + sc->stackVarIndex); } } static void sPreIncrement(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { Object* o = sc->stack + value + sc->stackVarIndex; if(o->type == OT_INT) { o->data.intValue++; } else if(o->type == OT_FLOAT) { o->data.floatValue++; } else { sError(sc, "variable is not a number on line %d", sc->line); return; } sPush(sc, o); } } static void sPostIncrement(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { Object* o = sc->stack + value + sc->stackVarIndex; if(o->type == OT_INT) { sPush(sc, o); o->data.intValue++; } else if(o->type == OT_FLOAT) { sPush(sc, o); o->data.floatValue++; } else { sError(sc, "variable is not a number on line %d", sc->line); } } } static void sPreDecrement(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { Object* o = sc->stack + value + sc->stackVarIndex; if(o->type == OT_INT) { o->data.intValue--; } else if(o->type == OT_FLOAT) { o->data.floatValue--; } else { sError(sc, "variable is not a number on line %d", sc->line); return; } sPush(sc, o); } } static void sPostDecrement(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { Object* o = sc->stack + value + sc->stackVarIndex; if(o->type == OT_INT) { sPush(sc, o); o->data.intValue--; } else if(o->type == OT_FLOAT) { sPush(sc, o); o->data.floatValue--; } else { sError(sc, "variable is not a number on line %d", sc->line); } } } static void sPushCodeInt(Script* sc) { int value = 0; if(sReadInt(sc, &value)) { sPushInt(sc, value); } } static void sPushCodeFloat(Script* sc) { float value = 0; if(sRead(sc, &value, sizeof(float))) { sError(sc, "cannot read a float from the bytecode on line %d", sc->line); return; } sPushFloat(sc, value); } static void sNumberBinary(Script* sc, int (*fInt)(Script*, int, int), float (*fFloat)(float, float)) { Object o[2]; if(sPop(sc, o) || sPop(sc, o + 1)) { return; } else if(o[0].type == OT_INT && o[1].type == OT_INT) { sPushInt(sc, fInt(sc, o[0].data.intValue, o[1].data.intValue)); } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) { sPushFloat(sc, fFloat(o[0].data.floatValue, o[1].data.intValue)); } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) { sPushFloat(sc, fFloat(o[0].data.intValue, o[1].data.floatValue)); } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) { sPushFloat(sc, fFloat(o[0].data.floatValue, o[1].data.floatValue)); } else { sError(sc, "object is not a number on line %d", sc->line); } } static void sIntBinary(Script* sc, int (*f)(Script*, int, int)) { Object o[2]; if(!sPop(sc, o) && !sPop(sc, o + 1) && sCheckType(sc, o, OT_INT) && sCheckType(sc, o + 1, OT_INT)) { sPushInt(sc, f(sc, o[0].data.intValue, o[1].data.intValue)); } } static int sIntAdd(Script* sc, int a, int b) { (void)sc; return a + b; } static int sIntSub(Script* sc, int a, int b) { (void)sc; return b - a; } static int sIntMul(Script* sc, int a, int b) { (void)sc; return a * b; } static int sIntDiv(Script* sc, int a, int b) { if(a == 0) { sError(sc, "division by 0 on line %d", sc->line); return b; } return b / a; } static int sMod(Script* sc, int a, int b) { if(a == 0) { sError(sc, "module of 0 on line %d", sc->line); return b; } return b % a; } static float sFloatAdd(float a, float b) { return a + b; } static float sFloatSub(float a, float b) { return b - a; } static float sFloatMul(float a, float b) { return a * b; } static float sFloatDiv(float a, float b) { return b / a; } static void sBoolBinary(Script* sc, bool (*fInt)(int, int), bool (*fFloat)(float, float)) { Object o[2]; if(sPop(sc, o) || sPop(sc, o + 1)) { return; } else if(o[0].type == OT_INT && o[1].type == OT_INT) { sPushBool(sc, fInt(o[0].data.intValue, o[1].data.intValue)); } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) { sPushBool(sc, fFloat(o[0].data.floatValue, o[1].data.intValue)); } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) { sPushBool(sc, fFloat(o[0].data.intValue, o[1].data.floatValue)); } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) { sPushBool(sc, fFloat(o[0].data.floatValue, o[1].data.floatValue)); } else { sError(sc, "object is not a number on line %d", sc->line); } } static bool sIntLess(int a, int b) { return b < a; } static bool sIntGreater(int a, int b) { return b > a; } static bool sFloatLess(float a, float b) { return b < a; } static bool sFloatGreater(float a, float b) { return b > a; } static void sInvertSign(Script* sc) { Object* o = sPeek(sc); if(o == NULL) { return; } else if(o->type == OT_INT) { o->data.intValue = -o->data.intValue; } else if(o->type == OT_FLOAT) { o->data.floatValue = -o->data.floatValue; } else { sError(sc, "object is not a number on line %d", sc->line); } } static void sEqual(Script* sc) { Object o[2]; if(sPop(sc, o) || sPop(sc, o + 1)) { return; } else if(o[0].type == OT_INT && o[1].type == OT_INT) { sPushBool(sc, o[0].data.intValue == o[1].data.intValue); } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) { sPushBool(sc, o[0].data.intValue == o[1].data.floatValue); } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) { sPushBool(sc, o[0].data.floatValue == o[1].data.intValue); } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) { sPushBool(sc, o[0].data.floatValue == o[1].data.floatValue); } else if(o[0].type == OT_BOOL && o[1].type == OT_BOOL) { sPushBool(sc, o[0].data.intValue == o[1].data.intValue); } else if(o[0].type == OT_NULL && o[1].type == OT_NULL) { sPushBool(sc, true); } else { sError(sc, "object types do not match on line %d", sc->line); } } static void sNot(Script* sc) { Object* o = sPeek(sc); if(o != NULL && sCheckType(sc, o, OT_BOOL)) { o->data.intValue = !o->data.intValue; } } static void sAnd(Script* sc) { Object o[2]; if(!sPop(sc, o) && !sPop(sc, o + 1) && sCheckType(sc, o, OT_BOOL) && sCheckType(sc, o + 1, OT_BOOL)) { sPushBool(sc, o[0].data.intValue && o[1].data.intValue); } } static void sOr(Script* sc) { Object o[2]; if(!sPop(sc, o) && !sPop(sc, o + 1) && sCheckType(sc, o, OT_BOOL) && sCheckType(sc, o + 1, OT_BOOL)) { sPushBool(sc, o[0].data.intValue || o[1].data.intValue); } } static void sPrint(Script* sc) { Object o; if(!sPop(sc, &o) && printer(&o)) { sError(sc, "cannot print given object on line %d", sc->line); } } static void sLine(Script* sc) { if(sRead(sc, &sc->line, 2)) { sError(sc, "line operation without a line near line %d", sc->line); } } static void sGoTo(Script* sc) { int gotoIndex; if(sReadInt(sc, &gotoIndex)) { sc->readIndex = gotoIndex; } } static void sGoSub(Script* sc) { int gotoIndex; int arguments; if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments)) { int returnStackIndex = sc->stackIndex - arguments - 1; if(returnStackIndex < 0 || sc->stack[returnStackIndex].type != OT_INT) { sError(sc, "cannot find return address entry on stack on line %d", sc->line); return; } sc->stack[returnStackIndex].data.intValue = sc->readIndex; sc->readIndex = gotoIndex; } } static void sIfGoTo(Script* sc) { int gotoIndex; Object o; if(sReadInt(sc, &gotoIndex) && !sPop(sc, &o) && sCheckType(sc, &o, OT_BOOL) && !o.data.intValue) { sc->readIndex = gotoIndex; } } static void sSetReturn(Script* sc) { sPop(sc, &sc->returnValue); } static void sReturn(Script* sc) { Object o; if(!sPop(sc, &o) && sCheckType(sc, &o, OT_INT)) { sc->readIndex = o.data.intValue; if(sc->returnValue.type != OT_VOID) { sPush(sc, &sc->returnValue); sc->returnValue.type = OT_VOID; } } } static void sDuplicate(Script* sc) { Object* o = sPeek(sc); if(o != NULL) { sPush(sc, o); } } static void sConsumeInstruction(Script* sc) { switch(sReadOperation(sc)) { case OP_NOTHING: break; case OP_PUSH_INT: sPushCodeInt(sc); break; case OP_PUSH_FLOAT: sPushCodeFloat(sc); break; case OP_PUSH_NULL: sPushNull(sc); break; case OP_PUSH_TRUE: sPushBool(sc, true); break; case OP_PUSH_FALSE: sPushBool(sc, false); break; case OP_PUSH_VARS: sPushVars(sc); break; case OP_POP_VARS: sPopVars(sc); break; case OP_POP: sPopEmpty(sc); break; case OP_SET: sSet(sc); break; case OP_GET: sGet(sc); break; case OP_PRE_INCREMENT: sPreIncrement(sc); break; case OP_POST_INCREMENT: sPostIncrement(sc); break; case OP_PRE_DECREMENT: sPreDecrement(sc); break; case OP_POST_DECREMENT: sPostDecrement(sc); break; case OP_ADD: sNumberBinary(sc, sIntAdd, sFloatAdd); break; case OP_SUB: sNumberBinary(sc, sIntSub, sFloatSub); break; case OP_MUL: sNumberBinary(sc, sIntMul, sFloatMul); break; case OP_DIV: sNumberBinary(sc, sIntDiv, sFloatDiv); break; case OP_MOD: sIntBinary(sc, sMod); break; case OP_INVERT_SIGN: sInvertSign(sc); break; case OP_LESS: sBoolBinary(sc, sIntLess, sFloatLess); break; case OP_GREATER: sBoolBinary(sc, sIntGreater, sFloatGreater); break; case OP_EQUAL: sEqual(sc); break; case OP_NOT: sNot(sc); break; case OP_AND: sAnd(sc); break; case OP_OR: sOr(sc); break; case OP_PRINT: sPrint(sc); break; case OP_LINE: sLine(sc); break; case OP_GOTO: sGoTo(sc); break; case OP_GOSUB: sGoSub(sc); break; case OP_IF_GOTO: sIfGoTo(sc); break; case OP_SET_RETURN: sSetReturn(sc); break; case OP_RETURN: sReturn(sc); break; case OP_DUPLICATE: sDuplicate(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->returnValue.type = OT_VOID; sc->stackIndex = 0; sc->stackVarIndex = 0; sc->line = 0; return sc; } void sDelete(Script* sc) { bcDelete(sc->code); free(sc); } void sRun(Script* sc) { while(sHasData(sc)) { sConsumeInstruction(sc); if(sc->error[0] != '\0') { puts(sc->error); return; } } } void sSetPrinter(ObjectPrinter p) { printer = p; } static void sPrintInt(Script* sc, const char* msg) { int value = 0; sReadInt(sc, &value); printf("%s %d\n", msg, value); } static void sPrint2Int(Script* sc, const char* msg) { int a = 0; sReadInt(sc, &a); int b = 0; sReadInt(sc, &b); printf("%s %d %d\n", msg, a, b); } static void sPrintInt16(Script* sc, const char* msg) { int value = 0; sRead(sc, &value, 2); printf("%s %d\n", msg, value); } static void sPrintFloat(Script* sc, const char* msg) { float value = 0; sRead(sc, &value, sizeof(float)); printf("%s %.2f\n", msg, value); } void sPrintCode(Script* sc) { int oldRead = sc->readIndex; sc->readIndex = 0; while(sHasData(sc)) { printf(" %3d | ", sc->readIndex); switch(sReadOperation(sc)) { case OP_NOTHING: puts("Nothing"); break; case OP_PUSH_INT: sPrintInt(sc, "Push Int"); break; case OP_PUSH_FLOAT: sPrintFloat(sc, "Push Float"); break; case OP_PUSH_NULL: puts("Push null"); break; case OP_PUSH_TRUE: puts("Push true"); break; case OP_PUSH_FALSE: puts("Push false"); break; case OP_PUSH_VARS: sPrint2Int(sc, "Push Vars"); break; case OP_POP_VARS: sPrintInt(sc, "Pop Vars"); break; case OP_POP: puts("Pop"); break; case OP_SET: sPrintInt(sc, "Set"); break; case OP_GET: sPrintInt(sc, "Get"); break; case OP_PRE_INCREMENT: sPrintInt(sc, "Pre Increment"); break; case OP_POST_INCREMENT: sPrintInt(sc, "Post Increment"); break; case OP_PRE_DECREMENT: sPrintInt(sc, "Pre Decrement"); break; case OP_POST_DECREMENT: sPrintInt(sc, "Post Decrement"); break; case OP_ADD: puts("Add"); break; case OP_SUB: puts("Sub"); break; case OP_MUL: puts("Mul"); break; case OP_DIV: puts("Div"); break; case OP_MOD: puts("Mod"); break; case OP_INVERT_SIGN: puts("Invert Sign"); break; case OP_LESS: puts("Less"); break; case OP_GREATER: puts("Greater"); break; case OP_EQUAL: puts("Equal"); break; case OP_NOT: puts("Not"); break; case OP_AND: puts("And"); break; case OP_OR: puts("And"); break; case OP_PRINT: puts("Print"); break; case OP_LINE: sPrintInt16(sc, "------------ Line"); break; case OP_GOTO: sPrintInt(sc, "GoTo"); break; case OP_GOSUB: sPrint2Int(sc, "GoSub"); break; case OP_IF_GOTO: sPrintInt(sc, "If GoTo"); break; case OP_SET_RETURN: puts("Set Return"); break; case OP_RETURN: puts("Return"); break; case OP_DUPLICATE: puts("Duplicate"); break; } } sc->readIndex = oldRead; }