123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "Operation.h"
- #include "Script.h"
- static const char* MISSING_INT_CONSTANT = "cannot read an int from the bytecode";
- static const char* NOT_AN_INT = "object is not an int";
- static const char* NOT_PRINTABLE = "cannot print given object";
- static const char* STACK_OVERFLOW = "stack overflow";
- static const char* STACK_UNDERFLOW = "stack underflow";
- 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;
- }
- return c;
- }
- static void sPush(Script* sc, Object* o) {
- if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
- sc->error = STACK_OVERFLOW;
- return;
- }
- sc->stack[sc->stackIndex++] = *o;
- }
- static bool sPop(Script* sc, Object* o) {
- if(sc->stackIndex <= 0) {
- sc->error = STACK_UNDERFLOW;
- return true;
- }
- *o = sc->stack[--sc->stackIndex];
- return false;
- }
- static void sPushInt(Script* sc) {
- int value = 0;
- if(sRead(sc, &value, sizeof(int))) {
- sc->error = MISSING_INT_CONSTANT;
- return;
- }
- Object o = {.type = OT_INT, .data.intValue = value};
- sPush(sc, &o);
- }
- static void sPushNull(Script* sc) {
- Object o = {.type = OT_NULL};
- sPush(sc, &o);
- }
- void sAdd(Script* sc) {
- Object a;
- if(sPop(sc, &a)) {
- return;
- }
- Object b;
- if(sPop(sc, &b)) {
- return;
- }
- if(a.type != OT_INT || b.type != OT_INT) {
- sc->error = NOT_AN_INT;
- return;
- }
- Object o = {.type = OT_INT, .data.intValue = a.data.intValue + b.data.intValue};
- sPush(sc, &o);
- }
- void sMul(Script* sc) {
- Object a;
- if(sPop(sc, &a)) {
- return;
- }
- Object b;
- if(sPop(sc, &b)) {
- return;
- }
- if(a.type != OT_INT || b.type != OT_INT) {
- sc->error = NOT_AN_INT;
- return;
- }
- Object o = {.type = OT_INT, .data.intValue = a.data.intValue * b.data.intValue};
- sPush(sc, &o);
- }
- void sPrint(Script* sc) {
- Object o;
- if(sPop(sc, &o)) {
- return;
- }
- if(printer(&o)) {
- sc->error = NOT_PRINTABLE;
- }
- }
- static void sConsumeInstruction(Script* sc) {
- switch(sReadOperation(sc)) {
- case OP_NOTHING: break;
- case OP_PUSH_INT: sPushInt(sc); break;
- case OP_PUSH_NULL: sPushNull(sc); break;
- case OP_ADD: sAdd(sc); break;
- case OP_MUL: sMul(sc); 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 = NULL;
- sc->byteCode = byteCode;
- sc->byteCodeLength = codeLength;
- sc->readIndex = 0;
- sc->stackIndex = 0;
- return sc;
- }
- void sDelete(Script* sc) {
- free(sc->byteCode);
- }
- void sRun(Script* sc) {
- while(sHasData(sc)) {
- sConsumeInstruction(sc);
- if(sc->error != NULL) {
- puts(sc->error);
- return;
- }
- }
- }
- void sSetPrinter(ObjectPrinter p) {
- printer = p;
- }
|