123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #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->byteCodeLength) {
- return true;
- }
- memcpy(buffer, sc->byteCode + sc->readIndex, length);
- sc->readIndex += length;
- return false;
- }
- static Operation sReadOperation(Script* sc) {
- unsigned char c;
- if(sRead(sc, &c, 1)) {
- return OP_NOTHING;
- } else if(sRead(sc, &sc->line, sizeof(int))) {
- // operation without line
- return OP_NOTHING;
- }
- return c;
- }
- static void sPush(Script* sc, Object* o) {
- if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
- sError(sc, "stack overflow on line %d", sc->line);
- return;
- }
- sc->stack[sc->stackIndex++] = *o;
- }
- 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 sPushInt(Script* sc) {
- int value = 0;
- if(sRead(sc, &value, sizeof(int))) {
- sError(sc, "cannot read an int from the bytecode on line %d", sc->line);
- return;
- }
- Object o = {.type = OT_INT, .data.intValue = value};
- sPush(sc, &o);
- }
- static void sPushFloat(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;
- }
- 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 b) {
- Object o = {.type = OT_BOOL, .data.intValue = b};
- sPush(sc, &o);
- }
- static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
- Object a;
- if(sPop(sc, &a)) {
- return;
- }
- Object b;
- if(sPop(sc, &b)) {
- return;
- }
- if(a.type == OT_INT) {
- if(b.type == OT_INT) {
- Object o = {.type = OT_INT, .data.intValue = fInt(a.data.intValue, b.data.intValue)};
- sPush(sc, &o);
- } else if(b.type == OT_FLOAT) {
- Object o = {.type = OT_FLOAT, .data.floatValue = fFloat(a.data.intValue, b.data.floatValue)};
- sPush(sc, &o);
- } else {
- sError(sc, "first object is not a number on line %d", sc->line);
- }
- } else if(a.type == OT_FLOAT) {
- if(b.type == OT_INT) {
- Object o = {.type = OT_FLOAT, .data.floatValue = fFloat(a.data.floatValue, b.data.intValue)};
- sPush(sc, &o);
- } else if(b.type == OT_FLOAT) {
- Object o = {.type = OT_FLOAT, .data.floatValue = fFloat(a.data.floatValue, b.data.floatValue)};
- sPush(sc, &o);
- } else {
- sError(sc, "first object is not a number on line %d", sc->line);
- }
- } else {
- sError(sc, "second object is not a number on line %d", sc->line);
- }
- }
- 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)) {
- return;
- }
- if(printer(&o)) {
- sError(sc, "cannot print given object on line %d", sc->line);
- }
- }
- static void sConsumeInstruction(Script* sc) {
- switch(sReadOperation(sc)) {
- case OP_NOTHING: break;
- case OP_PUSH_INT: sPushInt(sc); break;
- case OP_PUSH_FLOAT: sPushFloat(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_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
- case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
- case OP_PRINT: sPrint(sc); break;
- }
- }
- static bool sHasData(Script* sc) {
- return sc->readIndex < sc->byteCodeLength;
- }
- Script* sInit(unsigned char* byteCode, int codeLength) {
- Script* sc = malloc(sizeof(Script));
- sc->error[0] = '\0';
- sc->byteCode = byteCode;
- sc->byteCodeLength = codeLength;
- sc->readIndex = 0;
- sc->stackIndex = 0;
- sc->line = 0;
- return sc;
- }
- void sDelete(Script* sc) {
- free(sc->byteCode);
- }
- void sRun(Script* sc) {
- while(sHasData(sc)) {
- sConsumeInstruction(sc);
- if(sc->error[0] != '\0') {
- puts(sc->error);
- return;
- }
- }
- }
- void sSetPrinter(ObjectPrinter p) {
- printer = p;
- }
|