1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132 |
- #include <setjmp.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "Check.h"
- #include "Compiler.h"
- #include "DataType.h"
- #include "tokenizer/Tokenizer.h"
- #include "utils/Functions.h"
- #include "utils/Variables.h"
- #include "vm/Operation.h"
- static Error* error = NULL;
- static ByteCode* code;
- static int16 line = 1;
- static const char* path = NULL;
- static jmp_buf errorJump;
- #define RETURN_BUFFER 16
- #define BREAK_BUFFER 32
- #define DT_OPERATION(op) \
- case RDT_INT: cAddOperation(OP_##op##_INT); break; \
- case RDT_FLOAT: cAddOperation(OP_##op##_FLOAT); break;
- typedef DataType (*Parser)(void);
- static bool onLine = false;
- static Variables vars;
- static Variables globalVars;
- static Functions functions;
- static Functions functionQueue;
- static Structs structs;
- static int returns[RETURN_BUFFER];
- static int returnIndex = 0;
- static int hasReturn = 0;
- static DataType returnType;
- static int breaks[BREAK_BUFFER];
- static int breakIndex = 0;
- static int forWhileStack = 0;
- static int continueAt = 0;
- typedef struct {
- const char* name;
- Operation intOp;
- Operation floatOp;
- Operation pointerOp;
- char padding[sizeof(Operation)];
- } TypedOp;
- #define TYPE_OP(NAME, FLOAT, POINTER, text) \
- static const TypedOp TYPED_##NAME = { \
- text, OP_##NAME##_INT, OP_##FLOAT, OP_##POINTER, {0}};
- TYPE_OP(MUL, MUL_FLOAT, NOTHING, "*")
- TYPE_OP(DIV, DIV_FLOAT, NOTHING, "/")
- TYPE_OP(MOD, NOTHING, NOTHING, "%")
- TYPE_OP(ADD, ADD_FLOAT, NOTHING, "+")
- TYPE_OP(SUB, SUB_FLOAT, NOTHING, "-")
- TYPE_OP(LESS, LESS_FLOAT, NOTHING, "<")
- TYPE_OP(LESS_EQUAL, LESS_EQUAL_FLOAT, NOTHING, "<=")
- TYPE_OP(GREATER, GREATER_FLOAT, NOTHING, ">")
- TYPE_OP(GREATER_EQUAL, GREATER_EQUAL_FLOAT, NOTHING, ">=")
- TYPE_OP(EQUAL, EQUAL_FLOAT, EQUAL_POINTER, "==")
- TYPE_OP(NOT_EQUAL, NOT_EQUAL_FLOAT, NOT_EQUAL_POINTER, "!=")
- TYPE_OP(BIT_OR, NOTHING, NOTHING, "|")
- TYPE_OP(BIT_XOR, NOTHING, NOTHING, "^")
- TYPE_OP(BIT_AND, NOTHING, NOTHING, "&")
- TYPE_OP(LEFT_SHIFT, NOTHING, NOTHING, "<<")
- TYPE_OP(RIGHT_SHIFT, NOTHING, NOTHING, ">>")
- static check_format(1, 2) void cError(const char* format, ...) {
- va_list args;
- va_start(args, format);
- eInitErrorV(error, path, line, format, args);
- va_end(args);
- longjmp(errorJump, 0);
- }
- static const char* cGetName(DataType dt) {
- return dtGetName(&structs, dt);
- }
- static void cInvalidOperation(DataType a, DataType b, const char* op) {
- cError("invalid operation: %s %s %s", cGetName(a), op, cGetName(b));
- }
- static void cNotDeclared(const char* name) {
- cError("variable %s has not been declared", name);
- }
- static void cDeclared(const char* name) {
- cError("%s has already been declared", name);
- }
- static void cTooMuchArguments(void) {
- cError("too much function arguments");
- }
- static void cUnexpectedToken(Token t) {
- cError("unexpected token: %s", tGetName(t));
- }
- #define BUFFER_SIZE 256
- static void cUnknownFunction(Function* f) {
- int max = 0;
- char buffer[BUFFER_SIZE] = {'\0'};
- for(int i = 0; i < f->arguments; i++) {
- const char* name = dtGetName(&structs, f->argumentTypes[i]);
- max += snprintf(buffer + max, (size_t)(BUFFER_SIZE - max),
- i > 0 ? ", %s" : "%s", name);
- }
- cError("unknown function: %s(%s)", f->name, buffer);
- }
- static void cAddOperation(Operation token) {
- unsigned char c = token;
- bcAddBytes(code, &c, 1);
- }
- static int32 cReserveInt32(void) {
- return bcReserveBytes(code, sizeof(int32));
- }
- static void cSetInt32(int p, int32 i) {
- bcSetBytes(code, p, &i, sizeof(int32));
- }
- static void cAddInt32(int32 i) {
- bcAddBytes(code, &i, sizeof(int32));
- }
- static void cAddInt32Operation(Operation token, int32 i) {
- cAddOperation(token);
- cAddInt32(i);
- }
- static void cAddByteOperation(Operation token, int8 i) {
- cAddOperation(token);
- bcAddBytes(code, &i, sizeof(int8));
- }
- static void cAddLine(int16 l) {
- cAddOperation(OP_LINE);
- bcAddBytes(code, &l, sizeof(int16));
- }
- static Token cReadTokenAndLine(void) {
- hasReturn--;
- Token t;
- if(tReadTokenAndLine(&t, &line)) {
- return T_END;
- }
- return t;
- }
- static void cConsumeToken(Token wanted) {
- Token t = cReadTokenAndLine();
- if(wanted != t) {
- cError("expected '%s' got '%s'", tGetName(wanted), tGetName(t));
- }
- }
- static bool cConsumeTokenIf(Token t) {
- return tPeekToken() == t && cReadTokenAndLine() == t;
- }
- static void cConstantInt32(void) {
- int32 value;
- if(tReadInt32(&value)) {
- cError("int token without an int");
- }
- cAddInt32Operation(OP_PUSH_INT, value);
- }
- static void cConstantFloat(void) {
- float value;
- if(tReadFloat(&value)) {
- cError("float token without a float");
- }
- cAddOperation(OP_PUSH_FLOAT);
- bcAddBytes(code, &value, sizeof(float));
- }
- static const char* cReadString(void) {
- const char* literal = tReadString();
- if(literal == NULL) {
- cError("literal without string on line %d", line);
- }
- return literal;
- }
- static int cGetSize(DataType dt) {
- return dtGetSize(dt, &structs);
- }
- static DataType cExtendType(DataType dt) {
- if(cConsumeTokenIf(T_OPEN_SQUARE_BRACKET)) {
- cConsumeToken(T_CLOSE_SQUARE_BRACKET);
- dt = dtToArray(dt);
- }
- return dt;
- }
- static DataType cReadType(Token t, bool force) {
- switch(t) {
- case T_INT: return dtInt();
- case T_FLOAT: return dtFloat();
- case T_LITERAL: {
- const char* name = cReadString();
- Struct* st = stsSearch(&structs, name);
- if(st == NULL) {
- if(force) {
- cError("struct %s does not exist", name);
- } else {
- return dtVoid();
- }
- }
- return dtStruct(st);
- }
- default:
- if(force) {
- cUnexpectedToken(t);
- }
- return dtVoid();
- }
- }
- static DataType cReadExtendedType(Token t, bool force) {
- DataType dt = cReadType(t, force);
- if(!dtIsVoid(dt)) {
- dt = cExtendType(dt);
- }
- return dt;
- }
- static DataType cExpression(void);
- static void cLoadRef(DataType type) {
- if(dtIsArray(type)) {
- cAddOperation(OP_LOAD_ARRAY);
- return;
- }
- switch(dtGetType(type)) {
- DT_OPERATION(LOAD);
- default: cError("cannot load type %s", cGetName(type));
- }
- }
- static DataType cUnpack(DataType dt) {
- if(!dtIsStruct(dt) && dtRemovePointer(&dt)) {
- cLoadRef(dt);
- }
- return dt;
- }
- static DataType cUnpackedExpression(void) {
- return cUnpack(cExpression());
- }
- static void cCallFunctionArguments(Function* f) {
- if(cConsumeTokenIf(T_CLOSE_BRACKET)) {
- return;
- }
- while(true) {
- DataType dt = cUnpackedExpression();
- if(fAddArgument(f, dt, &structs)) {
- cTooMuchArguments();
- } else if(cConsumeTokenIf(T_CLOSE_BRACKET)) {
- return;
- }
- cConsumeToken(T_COMMA);
- }
- }
- static DataType cCallFunction(const char* name) {
- int returnAddress = bcGetAddress(code);
- Function f;
- fInit(&f, name, line);
- int oldOnLine = onLine;
- onLine = false;
- cCallFunctionArguments(&f);
- onLine = oldOnLine;
- Function* found = fsSearch(&functions, &f);
- if(found == NULL) {
- cUnknownFunction(&f);
- } else if(found->global) {
- cAddInt32Operation(OP_CALL, found->line);
- return found->returnType;
- }
- char push[1 + sizeof(int32)] = {OP_PUSH_INT};
- bcInsertBytes(code, push, sizeof(push), returnAddress);
- cAddOperation(OP_GOSUB);
- if(found->address == -1) {
- f.returnType = found->returnType;
- f.address = cReserveInt32();
- fsAdd(&functionQueue, &f);
- } else {
- cAddInt32(found->address);
- }
- cAddInt32(found->size);
- return found->returnType;
- }
- static void cStore(DataType left, DataType right, const char* name) {
- if(dtIsArray(left)) {
- if(!dtCompare(left, right)) {
- cInvalidOperation(left, right, name);
- }
- cAddOperation(OP_STORE_ARRAY);
- return;
- }
- if(!dtCompare(left, right)) {
- cInvalidOperation(left, right, name);
- }
- switch(dtGetType(left)) {
- DT_OPERATION(STORE);
- default: cError("cannot store type %s", cGetName(left));
- }
- }
- static DataType cLiteral(void) {
- const char* literal = cReadString();
- if(cConsumeTokenIf(T_OPEN_BRACKET)) {
- return cCallFunction(literal);
- }
- Variable v;
- if(!vsSearch(&vars, &v, literal)) {
- if(dtIsPointer(v.type)) {
- if(!dtIsStruct(v.type)) {
- cError("non struct type %s should not be a pointer here",
- cGetName(v.type));
- }
- cAddInt32Operation(OP_PUSH_STRUCT_REFERENCE, v.address);
- } else {
- cAddInt32Operation(OP_DEREFERENCE_VAR, v.address);
- }
- return dtToPointer(v.type);
- } else if(!vsSearch(&globalVars, &v, literal)) {
- cAddInt32Operation(OP_DEREFERENCE_GVAR, v.address);
- return dtToPointer(v.type);
- }
- cNotDeclared(literal);
- return dtVoid();
- }
- static DataType cText(void) {
- cAddOperation(OP_PUSH_TEXT);
- int32 lengthAddress = cReserveInt32();
- int32 length = 0;
- int32 c;
- while(!tReadInt32(&c) && c != 0) {
- cAddInt32(c);
- length++;
- }
- cSetInt32(lengthAddress, length);
- return dtText();
- }
- static DataType cBracketPrimary(void) {
- DataType result = cExpression();
- cConsumeToken(T_CLOSE_BRACKET);
- return result;
- }
- static void cArrayIndex(void) {
- if(!dtIsInt(cUnpackedExpression())) {
- cError("array index must be an int");
- }
- }
- static DataType cAllocArray(void) {
- DataType dt = cReadType(cReadTokenAndLine(), true);
- if(dtIsArray(dt)) {
- cError("array type must not be an array");
- }
- cConsumeToken(T_OPEN_SQUARE_BRACKET);
- cArrayIndex();
- cConsumeToken(T_CLOSE_SQUARE_BRACKET);
- cAddInt32Operation(OP_NEW, cGetSize(dt));
- return dtToArray(dt);
- }
- static DataType cLength(void) {
- DataType array = cUnpackedExpression();
- if(!dtIsArray(array)) {
- cError("length expects an array");
- }
- cAddOperation(OP_LENGTH);
- return dtInt();
- }
- static DataType cPrimary(void) {
- Token t = cReadTokenAndLine();
- switch(t) {
- case T_INT_VALUE: cConstantInt32(); return dtInt();
- case T_FLOAT_VALUE: cConstantFloat(); return dtFloat();
- case T_TEXT: return cText();
- case T_OPEN_BRACKET: return cBracketPrimary();
- case T_LITERAL: return cLiteral();
- case T_NEW: return cAllocArray();
- case T_LENGTH: return cLength();
- default: cUnexpectedToken(t); return dtVoid();
- }
- }
- static void cRemoveReference(DataType* dt, const char* name) {
- if(!dtRemovePointer(dt)) {
- cError("%s needs a reference not %s", name, cGetName(*dt));
- }
- }
- static void cExpectType(DataType actual, DataType wanted, const char* name) {
- if(!dtCompare(actual, wanted)) {
- cError("%s needs %s not %s", name, cGetName(wanted), cGetName(actual));
- }
- }
- static void cChangeType(DataType* dt, Operation op, Operation pushOp,
- int8 change) {
- if(onLine) {
- cAddByteOperation(op, change);
- *dt = dtVoid();
- } else {
- cAddByteOperation(pushOp, change);
- }
- }
- static void cPostChange(DataType* dt, int8 change, const char* name) {
- cRemoveReference(dt, name);
- if(dtIsInt(*dt)) {
- cChangeType(dt, OP_CHANGE_INT, OP_PUSH_POST_CHANGE_INT, change);
- } else {
- cError("%s needs an int not %s", name, cGetName(*dt));
- }
- }
- static DataType cStructAccess(DataType dt) {
- Struct* st = dtGetStruct(&structs, dt);
- if(st == NULL) {
- cError(". expects a struct not %s", cGetName(dt));
- }
- cConsumeToken(T_LITERAL);
- const char* name = cReadString();
- Variable inner;
- if(vSearchStruct(&inner, &structs, st, name)) {
- cError("%s has no member %s", st->name, name);
- } else if(inner.address > 0) {
- cAddInt32Operation(OP_PUSH_INT, inner.address);
- cAddInt32Operation(OP_ADD_REFERENCE, 1);
- }
- return dtToPointer(inner.type);
- }
- static DataType cAccess(void) {
- DataType dt = cPrimary();
- while(true) {
- if(cConsumeTokenIf(T_INCREMENT)) {
- cPostChange(&dt, 1, "++");
- } else if(cConsumeTokenIf(T_DECREMENT)) {
- cPostChange(&dt, -1, "--");
- } else if(cConsumeTokenIf(T_POINT)) {
- cRemoveReference(&dt, ".");
- dt = cStructAccess(dt);
- } else if(cConsumeTokenIf(T_OPEN_SQUARE_BRACKET)) {
- if(!dtIsArray(dt)) {
- cError("[] needs an array");
- }
- cAddOperation(OP_LOAD_ARRAY);
- dtRemovePointer(&dt);
- cArrayIndex();
- cConsumeToken(T_CLOSE_SQUARE_BRACKET);
- cAddInt32Operation(OP_ADD_REFERENCE, cGetSize(dt));
- dt = dtToPointer(dtRemoveArray(dt));
- } else {
- return dt;
- }
- }
- }
- static DataType cPreChange(DataType dt, int8 change, const char* name) {
- cRemoveReference(&dt, name);
- if(dtIsInt(dt)) {
- cChangeType(&dt, OP_CHANGE_INT, OP_PUSH_PRE_CHANGE_INT, change);
- } else {
- cError("%s needs an int not %s", name, cGetName(dt));
- }
- return dt;
- }
- static DataType cInvertSign(DataType dt) {
- if(dtIsInt(dt)) {
- cAddOperation(OP_INVERT_SIGN_INT);
- } else if(dtIsFloat(dt)) {
- cAddOperation(OP_INVERT_SIGN_FLOAT);
- } else {
- cError("cannot invert sign of %s", cGetName(dt));
- }
- return dt;
- }
- static DataType cCast(DataType in, DataType a, Operation aOp, DataType out) {
- if(dtCompare(in, a)) {
- cAddOperation(aOp);
- } else {
- cError("cannot cast %s to %s", cGetName(in), cGetName(out));
- }
- return out;
- }
- static DataType cUnaryNot(DataType dt) {
- cExpectType(dt, dtInt(), "!");
- cAddOperation(OP_NOT);
- return dt;
- }
- static DataType cUnaryBitNot(DataType dt) {
- if(dtIsInt(dt)) {
- cAddOperation(OP_BIT_NOT_INT);
- } else {
- cError("~ needs an int not %s", cGetName(dt));
- }
- return dt;
- }
- static DataType cPreUnary(void) {
- int marker = tGetMarker();
- if(cConsumeTokenIf(T_OPEN_BRACKET)) {
- if(cConsumeTokenIf(T_FLOAT) && cConsumeTokenIf(T_CLOSE_BRACKET)) {
- return cCast(cUnpack(cPreUnary()), dtInt(), OP_INT_TO_FLOAT,
- dtFloat());
- } else if(cConsumeTokenIf(T_INT) && cConsumeTokenIf(T_CLOSE_BRACKET)) {
- return cCast(cUnpack(cPreUnary()), dtFloat(), OP_FLOAT_TO_INT,
- dtInt());
- }
- }
- tReset(marker);
- if(cConsumeTokenIf(T_INCREMENT)) {
- return cPreChange(cPreUnary(), 1, "++");
- } else if(cConsumeTokenIf(T_DECREMENT)) {
- return cPreChange(cPreUnary(), -1, "--");
- } else if(cConsumeTokenIf(T_SUB)) {
- return cInvertSign(cUnpack(cPreUnary()));
- } else if(cConsumeTokenIf(T_NOT)) {
- return cUnaryNot(cPreUnary());
- } else if(cConsumeTokenIf(T_BIT_NOT)) {
- return cUnaryBitNot(cPreUnary());
- }
- return cAccess();
- }
- static void cAddTypeOperation(DataType* a, Parser bf, const TypedOp* op) {
- *a = cUnpack(*a);
- DataType b = cUnpack(bf());
- if(!dtCompare(*a, b)) {
- cInvalidOperation(*a, b, op->name);
- } else if(dtIsInt(*a) && op->intOp != OP_NOTHING) {
- cAddOperation(op->intOp);
- } else if(dtIsFloat(*a) && op->floatOp != OP_NOTHING) {
- cAddOperation(op->floatOp);
- } else if(dtIsArray(*a) && op->pointerOp != OP_NOTHING) {
- cAddOperation(op->pointerOp);
- } else {
- cInvalidOperation(*a, b, op->name);
- }
- }
- static DataType cMul(void) {
- DataType a = cPreUnary();
- while(true) {
- if(cConsumeTokenIf(T_MUL)) {
- cAddTypeOperation(&a, cPreUnary, &TYPED_MUL);
- } else if(cConsumeTokenIf(T_DIV)) {
- cAddTypeOperation(&a, cPreUnary, &TYPED_DIV);
- } else if(cConsumeTokenIf(T_MOD)) {
- cAddTypeOperation(&a, cPreUnary, &TYPED_MOD);
- } else {
- return a;
- }
- }
- }
- static DataType cAdd(void) {
- DataType a = cMul();
- while(true) {
- if(cConsumeTokenIf(T_ADD)) {
- cAddTypeOperation(&a, cMul, &TYPED_ADD);
- } else if(cConsumeTokenIf(T_SUB)) {
- cAddTypeOperation(&a, cMul, &TYPED_SUB);
- } else {
- return a;
- }
- }
- }
- static DataType cShift(void) {
- DataType a = cAdd();
- while(true) {
- if(cConsumeTokenIf(T_LEFT_SHIFT)) {
- cAddTypeOperation(&a, cAdd, &TYPED_LEFT_SHIFT);
- } else if(cConsumeTokenIf(T_RIGHT_SHIFT)) {
- cAddTypeOperation(&a, cAdd, &TYPED_RIGHT_SHIFT);
- } else {
- return a;
- }
- }
- }
- static DataType cComparison(void) {
- DataType a = cShift();
- while(true) {
- if(cConsumeTokenIf(T_LESS)) {
- cAddTypeOperation(&a, cShift, &TYPED_LESS);
- a = dtInt();
- } else if(cConsumeTokenIf(T_LESS_EQUAL)) {
- cAddTypeOperation(&a, cShift, &TYPED_LESS_EQUAL);
- a = dtInt();
- } else if(cConsumeTokenIf(T_GREATER)) {
- cAddTypeOperation(&a, cShift, &TYPED_GREATER);
- a = dtInt();
- } else if(cConsumeTokenIf(T_GREATER_EQUAL)) {
- cAddTypeOperation(&a, cShift, &TYPED_GREATER_EQUAL);
- a = dtInt();
- } else {
- return a;
- }
- }
- }
- static DataType cEqual(void) {
- DataType a = cComparison();
- while(true) {
- if(cConsumeTokenIf(T_EQUAL)) {
- cAddTypeOperation(&a, cComparison, &TYPED_EQUAL);
- a = dtInt();
- } else if(cConsumeTokenIf(T_NOT_EQUAL)) {
- cAddTypeOperation(&a, cComparison, &TYPED_NOT_EQUAL);
- a = dtInt();
- } else {
- return a;
- }
- }
- }
- static DataType cRepeat(Token t, Parser f, const TypedOp* op) {
- DataType a = f();
- while(cConsumeTokenIf(t)) {
- cAddTypeOperation(&a, f, op);
- }
- return a;
- }
- static DataType cBitAnd(void) {
- return cRepeat(T_BIT_AND, cEqual, &TYPED_BIT_AND);
- }
- static DataType cBitXor(void) {
- return cRepeat(T_BIT_XOR, cBitAnd, &TYPED_BIT_XOR);
- }
- static DataType cBitOr(void) {
- return cRepeat(T_BIT_OR, cBitXor, &TYPED_BIT_OR);
- }
- static DataType cLogical(Parser f, Token t, Operation jump, Operation op) {
- DataType a = f();
- while(cConsumeTokenIf(t)) {
- cAddOperation(jump);
- int32 p = cReserveInt32();
- DataType b = f();
- if(!dtIsInt(a) || !dtIsInt(b)) {
- cInvalidOperation(a, b, tGetName(t));
- }
- cAddOperation(op);
- cSetInt32(p, code->length);
- }
- return a;
- }
- static DataType cAnd(void) {
- return cLogical(cBitOr, T_AND, OP_PEEK_FALSE_GOTO, OP_AND);
- }
- static DataType cExpression(void) {
- return cLogical(cAnd, T_OR, OP_PEEK_TRUE_GOTO, OP_OR);
- }
- static void cLine(void);
- static void cConsumeBody(void) {
- int16 oldLine = line;
- while(!cConsumeTokenIf(T_CLOSE_CURVED_BRACKET)) {
- if(tPeekToken() == T_END) {
- line = oldLine;
- cError("non closed curved bracket");
- }
- cLine();
- }
- }
- static void cConsumeScope(void) {
- Scope scope;
- vsEnterScope(&vars, &scope);
- cConsumeBody();
- vsLeaveScope(&vars, &scope);
- }
- static void cAddReturn(Operation op) {
- if(returnIndex >= RETURN_BUFFER) {
- cError("too much returns in function");
- }
- cAddOperation(op);
- returns[returnIndex++] = cReserveInt32();
- }
- static void cReturn(void) {
- if(dtIsVoid(returnType)) {
- cConsumeToken(T_SEMICOLON);
- cAddReturn(OP_RETURN);
- hasReturn = 2;
- return;
- }
- DataType dt = cUnpackedExpression();
- if(!dtCompare(returnType, dt)) {
- cError("wrong return type, should be %s", cGetName(returnType));
- } else if(dtIsInt(dt)) {
- cAddReturn(OP_RETURN_INT);
- } else if(dtIsFloat(dt)) {
- cAddReturn(OP_RETURN_FLOAT);
- } else if(dtIsArray(dt)) {
- cAddReturn(OP_RETURN_POINTER);
- } else {
- cError("cannot return %s", cGetName(dt));
- }
- cConsumeToken(T_SEMICOLON);
- hasReturn = 2;
- }
- static void cIf(void) {
- cConsumeToken(T_OPEN_BRACKET);
- cExpectType(cExpression(), dtInt(), "if");
- cConsumeToken(T_CLOSE_BRACKET);
- cAddOperation(OP_IF_GOTO);
- int32 ifP = cReserveInt32();
- cConsumeToken(T_OPEN_CURVED_BRACKET);
- cConsumeScope();
- cSetInt32(ifP, code->length);
- if(cConsumeTokenIf(T_ELSE)) {
- cAddOperation(OP_GOTO);
- int32 elseP = cReserveInt32();
- cSetInt32(ifP, code->length);
- if(cConsumeTokenIf(T_IF)) {
- cIf();
- } else {
- cConsumeToken(T_OPEN_CURVED_BRACKET);
- cConsumeScope();
- }
- cSetInt32(elseP, code->length);
- }
- }
- static void cConsumeBreaks(int start, int address) {
- for(int i = start; i < breakIndex; i++) {
- cSetInt32(breaks[i], address);
- }
- breakIndex = start;
- }
- static void cWhile(void) {
- int start = code->length;
- cConsumeToken(T_OPEN_BRACKET);
- cExpectType(cExpression(), dtInt(), "while");
- cConsumeToken(T_CLOSE_BRACKET);
- cAddOperation(OP_IF_GOTO);
- int32 ifP = cReserveInt32();
- int breakStart = breakIndex;
- forWhileStack++;
- int oldContinue = continueAt;
- continueAt = start;
- cConsumeToken(T_OPEN_CURVED_BRACKET);
- cConsumeScope();
- continueAt = oldContinue;
- forWhileStack--;
- cAddInt32Operation(OP_GOTO, start);
- cSetInt32(ifP, code->length);
- cConsumeBreaks(breakStart, code->length);
- }
- static void cOperationSet(DataType left, const TypedOp* op) {
- cAddOperation(OP_DUPLICATE_REFERENCE);
- cLoadRef(left);
- cAddTypeOperation(&left, cUnpackedExpression, op);
- cStore(left, left, "=");
- }
- static void cSetVariable(void) {
- onLine = true;
- DataType dt = cPreUnary();
- onLine = false;
- if(dtIsVoid(dt)) {
- return;
- }
- cRemoveReference(&dt, "setter");
- Token t = cReadTokenAndLine();
- switch(t) {
- case T_SET: cStore(dt, cUnpackedExpression(), "="); break;
- case T_ADD_SET: cOperationSet(dt, &TYPED_ADD); break;
- case T_SUB_SET: cOperationSet(dt, &TYPED_SUB); break;
- case T_MUL_SET: cOperationSet(dt, &TYPED_MUL); break;
- case T_DIV_SET: cOperationSet(dt, &TYPED_DIV); break;
- case T_MOD_SET: cOperationSet(dt, &TYPED_MOD); break;
- case T_BIT_AND_SET: cOperationSet(dt, &TYPED_BIT_AND); break;
- case T_BIT_OR_SET: cOperationSet(dt, &TYPED_BIT_OR); break;
- case T_BIT_XOR_SET: cOperationSet(dt, &TYPED_BIT_XOR); break;
- case T_LEFT_SHIFT_SET: cOperationSet(dt, &TYPED_LEFT_SHIFT); break;
- case T_RIGHT_SHIFT_SET: cOperationSet(dt, &TYPED_RIGHT_SHIFT); break;
- default: cUnexpectedToken(t);
- }
- }
- static void cDeclareSet(Variables* vs, DataType dt, const char* var,
- Operation op) {
- if(vsInScope(vs, var)) {
- cDeclared(var);
- }
- Variable* v = vsAdd(vs, var, dt, &structs);
- if(!dtIsStruct(dt) || dtIsArray(dt)) {
- cConsumeToken(T_SET);
- cAddInt32Operation(op, v->address);
- DataType right = cUnpackedExpression();
- cStore(dt, right, "=");
- }
- }
- static bool cDeclaration(Token t) {
- DataType dt = cReadExtendedType(t, false);
- if(dtIsVoid(dt)) {
- return false;
- }
- cConsumeToken(T_LITERAL);
- const char* var = cReadString();
- cDeclareSet(&vars, dt, var, OP_DEREFERENCE_VAR);
- return true;
- }
- static void cLineExpression(void) {
- int marker = tGetMarker();
- Token t = cReadTokenAndLine();
- if(cDeclaration(t)) {
- return;
- }
- tReset(marker);
- cSetVariable();
- }
- static void cFor(void) {
- Scope scope;
- vsEnterScope(&vars, &scope);
- cConsumeToken(T_OPEN_BRACKET);
- cLineExpression();
- cConsumeToken(T_SEMICOLON);
- int startCheck = code->length;
- cExpectType(cExpression(), dtInt(), "for");
- cConsumeToken(T_SEMICOLON);
- cAddOperation(OP_IF_GOTO);
- int32 end = cReserveInt32();
- cAddOperation(OP_GOTO);
- int32 beginBody = cReserveInt32();
- int startPerLoop = code->length;
- cLineExpression();
- cAddInt32Operation(OP_GOTO, startCheck);
- cConsumeToken(T_CLOSE_BRACKET);
- cSetInt32(beginBody, code->length);
- int breakStart = breakIndex;
- forWhileStack++;
- int oldContinue = continueAt;
- continueAt = startPerLoop;
- cConsumeToken(T_OPEN_CURVED_BRACKET);
- cConsumeBody();
- continueAt = oldContinue;
- forWhileStack--;
- cAddInt32Operation(OP_GOTO, startPerLoop);
- cSetInt32(end, code->length);
- cConsumeBreaks(breakStart, code->length);
- vsLeaveScope(&vars, &scope);
- }
- static void cBreak(void) {
- if(forWhileStack == 0) {
- cError("break without for or while");
- } else if(breakIndex >= BREAK_BUFFER) {
- cError("too much breaks");
- }
- cAddOperation(OP_GOTO);
- breaks[breakIndex++] = cReserveInt32();
- cConsumeToken(T_SEMICOLON);
- }
- static void cContinue(void) {
- if(forWhileStack == 0) {
- cError("continue without for or while");
- }
- cAddInt32Operation(OP_GOTO, continueAt);
- cConsumeToken(T_SEMICOLON);
- }
- static void cLine(void) {
- int marker = tGetMarker();
- Token t = cReadTokenAndLine();
- cAddLine(line);
- switch(t) {
- case T_OPEN_CURVED_BRACKET: cConsumeScope(); break;
- case T_RETURN: cReturn(); break;
- case T_IF: cIf(); break;
- case T_WHILE: cWhile(); break;
- case T_FOR: cFor(); break;
- case T_BREAK: cBreak(); break;
- case T_CONTINUE: cContinue(); break;
- default:
- tReset(marker);
- cLineExpression();
- cConsumeToken(T_SEMICOLON);
- }
- }
- static void cBuildFunction(Function* f, DataType rType, const char* fName) {
- if(dtIsStruct(rType) && !dtIsArray(rType)) {
- cError("structs cannot be returned");
- }
- fInit(f, fName, line);
- f->returnType = rType;
- vsReset(&vars);
- cConsumeToken(T_OPEN_BRACKET);
- if(cConsumeTokenIf(T_CLOSE_BRACKET)) {
- return;
- }
- while(true) {
- DataType dt = cReadExtendedType(cReadTokenAndLine(), true);
- cConsumeToken(T_LITERAL);
- const char* name = cReadString();
- if(vsInScope(&vars, name)) {
- cDeclared(name);
- }
- if(dtIsStruct(dt)) {
- dt = dtToPointer(dt);
- }
- vsAdd(&vars, name, dt, &structs);
- if(fAddArgument(f, dt, &structs)) {
- cTooMuchArguments();
- } else if(cConsumeTokenIf(T_CLOSE_BRACKET)) {
- return;
- }
- cConsumeToken(T_COMMA);
- }
- }
- static void cAddFunction(Function* found, Function* f) {
- if(found == NULL) {
- fsAdd(&functions, f);
- } else if(found->global) {
- cError("system functions cannot be overwritten");
- } else if(found->address != -1 || f->address == -1 || found->global) {
- cError("function registered twice");
- } else if(!dtCompare(found->returnType, f->returnType)) {
- cError("function redeclared with different return type");
- } else {
- found->address = f->address;
- }
- }
- static void cInnerFunction(Function* f) {
- cConsumeToken(T_OPEN_CURVED_BRACKET);
- cAddOperation(OP_RESERVE);
- int32 p = cReserveInt32();
- cAddInt32(f->size);
- returnIndex = 0;
- cConsumeScope();
- if(!dtIsVoid(returnType) && hasReturn <= 0) {
- cError("missing return");
- }
- cAddInt32Operation(OP_RETURN, vars.maxAddress);
- cSetInt32(p, vars.maxAddress);
- for(int i = 0; i < returnIndex; i++) {
- cSetInt32(returns[i], vars.maxAddress);
- }
- returnIndex = 0;
- }
- static void cFunction(DataType rType, const char* name) {
- Function f;
- cBuildFunction(&f, rType, name);
- Function* found = fsSearch(&functions, &f);
- if(cConsumeTokenIf(T_SEMICOLON)) {
- cAddFunction(found, &f);
- return;
- }
- cAddLine(line);
- cAddOperation(OP_GOTO);
- int32 end = cReserveInt32();
- f.address = code->length;
- cAddFunction(found, &f);
- returnType = rType;
- cInnerFunction(&f);
- cSetInt32(end, code->length);
- }
- static void cStruct(void) {
- cConsumeToken(T_LITERAL);
- const char* name = cReadString();
- if(stsSearch(&structs, name) != NULL) {
- cError("struct '%s' registered twice", name);
- }
- Struct* st = stsAdd(&structs, name);
- DataType self = dtStruct(st);
- cConsumeToken(T_OPEN_CURVED_BRACKET);
- while(!cConsumeTokenIf(T_CLOSE_CURVED_BRACKET)) {
- DataType dt = cReadExtendedType(cReadTokenAndLine(), true);
- if(dtCompare(dt, self)) {
- cError("struct %s contains itself", name);
- }
- cConsumeToken(T_LITERAL);
- stAddVariable(st, cReadString(), dt);
- cConsumeToken(T_SEMICOLON);
- }
- cConsumeToken(T_SEMICOLON);
- }
- static void cGlobalScope(Token t) {
- if(t == T_OPEN_PATH) {
- path = cReadString();
- // printf("OPEN PATH %s\n", path);
- return;
- } else if(t == T_CLOSE_PATH) {
- path = cReadString();
- // printf("CLOSE OLD PATH - OPEN PATH %s\n", path);
- return;
- } else if(t == T_STRUCT) {
- cStruct();
- return;
- }
- DataType dt = dtVoid();
- if(t != T_VOID) {
- dt = cReadExtendedType(t, true);
- }
- cConsumeToken(T_LITERAL);
- const char* name = cReadString();
- if(tPeekToken() == T_OPEN_BRACKET) {
- cFunction(dt, name);
- return;
- }
- cDeclareSet(&globalVars, dt, name, OP_DEREFERENCE_GVAR);
- cConsumeToken(T_SEMICOLON);
- }
- static void cCallMain(void) {
- Function f;
- fInit(&f, "main", line);
- Function* found = fsSearch(&functions, &f);
- if(found != NULL && dtIsVoid(found->returnType)) {
- cAddInt32Operation(OP_PUSH_INT, 0);
- cAddInt32Operation(OP_GOSUB, found->address);
- cAddInt32(found->size);
- }
- }
- static void cForEachLine(void) {
- cAddOperation(OP_GRESERVE);
- int p = cReserveInt32();
- while(true) {
- Token t = cReadTokenAndLine();
- if(t == T_END) {
- break;
- }
- cGlobalScope(t);
- }
- cCallMain();
- cSetInt32(p, globalVars.maxAddress);
- cAddInt32Operation(OP_GRESERVE, -globalVars.maxAddress);
- }
- static void cLinkQueuedFunctions(void) {
- for(int i = 0; i < functionQueue.entries; i++) {
- Function* f = functionQueue.data + i;
- Function* found = fsSearch(&functions, f);
- if(found == NULL || found->address == -1) {
- line = f->line;
- cUnknownFunction(f);
- }
- cSetInt32(f->address, found->address);
- }
- }
- static void cAllocAndCompile(void) {
- forWhileStack = 0;
- breakIndex = 0;
- returnType = dtVoid();
- onLine = false;
- vsInit(&vars);
- vsInit(&globalVars);
- fsInit(&functions);
- fsInit(&functionQueue);
- stsInit(&structs);
- if(!setjmp(errorJump)) {
- cForEachLine();
- cLinkQueuedFunctions();
- }
- stsDelete(&structs);
- fsDelete(&functionQueue);
- fsDelete(&functions);
- vsDelete(&globalVars);
- vsDelete(&vars);
- }
- ByteCode* cCompile(Error* e) {
- error = e;
- eInitSuccess(e);
- code = bcInit();
- cAllocAndCompile();
- if(eHasError(e)) {
- bcDelete(code);
- return NULL;
- }
- return code;
- }
|