123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #include <stdarg.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #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) {
- if(o->type == OT_INT) {
- printf("%d\n", o->data.intValue);
- 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 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 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 value = 0;
- if(sReadInt(sc, &value)) {
- for(int i = 0; i < value; i++) {
- sPushNull(sc);
- }
- }
- }
- static void sPopVars(Script* sc) {
- int value = 0;
- if(sReadInt(sc, &value)) {
- 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);
- }
- }
- static void sGet(Script* sc) {
- int value = 0;
- if(sReadInt(sc, &value)) {
- sPush(sc, sc->stack + value);
- }
- }
- 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 bool sToFloat(Script* sc, Object* o, float* r) {
- if(o->type == OT_FLOAT) {
- *r = o->data.floatValue;
- return true;
- } else if(o->type == OT_INT) {
- *r = o->data.intValue;
- return true;
- }
- sError(sc, "object is not a number on line %d", sc->line);
- return false;
- }
- static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
- Object o[2];
- if(sPop(sc, o) || sPop(sc, o + 1)) {
- return;
- }
- if(o[0].type == OT_INT && o[1].type == OT_INT) {
- sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
- return;
- }
- float f[2];
- if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
- sPushFloat(sc, fFloat(f[0], f[1]));
- }
- }
- static int sIntAdd(int a, int b) {
- return a + b;
- }
- static int sIntMul(int a, int b) {
- return a * b;
- }
- static float sFloatAdd(float a, float b) {
- return a + b;
- }
- static float sFloatMul(float a, float b) {
- return a * b;
- }
- 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;
- if(sReadInt(sc, &gotoIndex) && sPushInt(sc, sc->readIndex)) {
- sc->readIndex = gotoIndex;
- }
- }
- static void sReturn(Script* sc) {
- Object o;
- if(sPop(sc, &o)) {
- return;
- } else if(o.type != OT_INT) {
- sError(sc, "return address on stack is not an int");
- return;
- }
- sc->readIndex = o.data.intValue;
- }
- 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: sPushVars(sc); break;
- case OP_POP: sPopVars(sc); break;
- case OP_SET: sSet(sc); break;
- case OP_GET: sGet(sc); break;
- case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
- case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); 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_RETURN: sReturn(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->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;
- }
|