#include #include #include #include #include #include "Compiler.h" #include "DataType.h" #include "tokenizer/Tokenizer.h" #include "utils/Functions.h" #include "utils/Variables.h" #include "vm/Operation.h" #define ERROR_LENGTH 256 #define RETURN_BUFFER 16 #define BREAK_BUFFER 32 #define DT_OPERATION(op) \ case DT_INT32: cAddOperation(OP_##op##_INT32); break; \ case DT_INT64: cAddOperation(OP_##op##_INT64); break; \ case DT_BOOL: cAddOperation(OP_##op##_BOOL); break; \ case DT_FLOAT: cAddOperation(OP_##op##_FLOAT); break; typedef DataType (*Parser)(void); static jmp_buf errorJump; static char error[ERROR_LENGTH] = {'\0'}; static ByteCode* code; static int16 line = 1; static bool onLine = false; static Variables vars; 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 { Operation intOp; Operation longOp; Operation floatOp; Operation boolOp; Operation pointerOp; const char* name; } TypedOp; #define TYPE_OP(NAME, FLOAT, BOOL, POINTER, text) \ static const TypedOp TYPED_##NAME = {OP_##NAME##_INT32, OP_##NAME##_INT64, \ OP_##FLOAT, OP_##BOOL, \ OP_##POINTER, text}; TYPE_OP(MUL, MUL_FLOAT, NOTHING, NOTHING, "*") TYPE_OP(DIV, DIV_FLOAT, NOTHING, NOTHING, "/") TYPE_OP(MOD, NOTHING, NOTHING, NOTHING, "%") TYPE_OP(ADD, ADD_FLOAT, NOTHING, NOTHING, "+") TYPE_OP(SUB, SUB_FLOAT, NOTHING, NOTHING, "-") TYPE_OP(LESS, LESS_FLOAT, NOTHING, NOTHING, "<") TYPE_OP(LESS_EQUAL, LESS_EQUAL_FLOAT, NOTHING, NOTHING, "<=") TYPE_OP(GREATER, GREATER_FLOAT, NOTHING, NOTHING, ">") TYPE_OP(GREATER_EQUAL, GREATER_EQUAL_FLOAT, NOTHING, NOTHING, ">=") TYPE_OP(EQUAL, EQUAL_FLOAT, EQUAL_BOOL, EQUAL_POINTER, "==") TYPE_OP(NOT_EQUAL, NOT_EQUAL_FLOAT, NOT_EQUAL_BOOL, NOT_EQUAL_POINTER, "!=") TYPE_OP(BIT_OR, NOTHING, NOTHING, NOTHING, "|") TYPE_OP(BIT_XOR, NOTHING, NOTHING, NOTHING, "^") TYPE_OP(BIT_AND, NOTHING, NOTHING, NOTHING, "&") TYPE_OP(LEFT_SHIFT, NOTHING, NOTHING, NOTHING, "<<") TYPE_OP(RIGHT_SHIFT, NOTHING, NOTHING, NOTHING, ">>") static void cError(const char* format, ...) { va_list args; va_start(args, format); vsnprintf(error, ERROR_LENGTH, 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() { cError("too much function arguments"); } static void cUnexpectedToken(Token t) { cError("unexpected token: %s", tGetName(t)); } static void cAddOperation(Operation token) { unsigned char c = token; bcAddBytes(code, &c, 1); } static int32 cReserveInt32() { 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 cAddInt64(int64 i) { bcAddBytes(code, &i, sizeof(int64)); } 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 i) { cAddOperation(OP_LINE); bcAddBytes(code, &i, sizeof(int16)); } static Token cReadTokenAndLine() { hasReturn--; Token t = tReadToken(); if(tReadInt16(&line)) { return t; } return T_END; } 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() { int32 value; if(!tReadInt32(&value)) { cError("int token without an int"); } cAddInt32Operation(OP_PUSH_INT32, value); } static void cConstantInt64() { int64 value; if(!tReadInt64(&value)) { cError("long token without an long"); } cAddOperation(OP_PUSH_INT64); cAddInt64(value); } static void cConstantFloat() { float value; if(!tReadFloat(&value)) { cError("float token without a float"); } cAddOperation(OP_PUSH_FLOAT); bcAddBytes(code, &value, sizeof(float)); } static const char* cReadString() { 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 cDereference(DataType dt) { if(dtDereference(&dt)) { cError("%s dereferenced too often", cGetName(dt)); } return dt; } static DataType cExtendType(DataType dt) { while(cConsumeTokenIf(T_MUL)) { dt = cDereference(dt); } return dt; } static DataType cReadType(Token t) { switch(t) { case T_INT32: return cExtendType(dtInt32()); case T_INT64: return cExtendType(dtInt64()); case T_BOOL: return cExtendType(dtBool()); case T_FLOAT: return cExtendType(dtFloat()); case T_LITERAL: { Struct* st = stsSearch(&structs, cReadString()); if(st == NULL) { cError("struct %s does not exist"); } return cExtendType(dtStruct(st)); } default: cUnexpectedToken(t); return dtVoid(); } } static DataType cExpression(); static void cLoadRef(DataType type) { if(dtIsPointer(type)) { cAddOperation(OP_REFERENCE); return; } switch(type.type) { DT_OPERATION(LOAD); case DT_STRUCT: cAddInt32Operation(OP_LOAD, cGetSize(type)); break; default: cError("cannot load type %s", cGetName(type)); } } static DataType cUnpack(DataType dt) { if(dtRemoveVariable(&dt)) { cLoadRef(dt); } return dt; } static DataType cUnpackedExpression() { 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) { cError("unknown function"); } else if(found->global) { cAddInt32Operation(OP_CALL, found->line); return found->returnType; } char push[1 + sizeof(int32)] = {OP_PUSH_INT32}; 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 bool cCompare(DataType left, DataType right) { return dtCompare(left, right) || (dtIsPointer(left) && dtIsNull(right)); } static void cStore(DataType left, DataType right, const char* name) { if(!cCompare(left, right)) { cInvalidOperation(left, right, name); } else if(dtIsPointer(left)) { cAddOperation(OP_STORE_POINTER); return; } switch(left.type) { DT_OPERATION(STORE); default: cError("cannot store type %s", cGetName(left)); } } static DataType cLiteral() { const char* literal = cReadString(); if(cConsumeTokenIf(T_OPEN_BRACKET)) { return cCallFunction(literal); } Variable v; if(vsSearch(&vars, &v, literal)) { cNotDeclared(literal); } cAddInt32Operation(OP_DEREFERENCE_VAR, v.address); return dtToVariable(v.type); } static DataType cBracketPrimary() { DataType result = cExpression(); cConsumeToken(T_CLOSE_BRACKET); return result; } static void cArrayIndex(const char* name) { if(!dtIsInt32(cUnpackedExpression())) { cError("array %s must be an int", name); } } static DataType cAllocArray() { DataType dt = cReadType(cReadTokenAndLine()); cConsumeToken(T_OPEN_SQUARE_BRACKET); cArrayIndex("size"); cConsumeToken(T_CLOSE_SQUARE_BRACKET); cAddInt32Operation(OP_NEW, cGetSize(dt)); return cDereference(dt); } static DataType cLength() { DataType pointer = cUnpackedExpression(); if(!dtIsPointer(pointer)) { cError("length expects a pointer"); } cAddOperation(OP_LENGTH); return dtInt32(); } static DataType cPrimary() { Token t = cReadTokenAndLine(); switch(t) { case T_CONST_INT32: cConstantInt32(); return dtInt32(); case T_CONST_INT64: cConstantInt64(); return dtInt64(); case T_CONST_FLOAT: cConstantFloat(); return dtFloat(); case T_TRUE: cAddOperation(OP_PUSH_TRUE); return dtBool(); case T_FALSE: cAddOperation(OP_PUSH_FALSE); return dtBool(); case T_NULLPTR: cAddOperation(OP_PUSH_NULLPTR); return dtNull(); 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(!dtRemoveVariable(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, int change) { if(onLine) { cAddByteOperation(op, change); *dt = dtVoid(); } else { cAddByteOperation(pushOp, change); } } static void cPostChange(DataType* dt, int change, const char* name) { cRemoveReference(dt, name); if(dtCompare(*dt, dtInt32())) { cChangeType(dt, OP_CHANGE_INT32, OP_PUSH_POST_CHANGE_INT32, change); } else if(dtCompare(*dt, dtInt64())) { cChangeType(dt, OP_CHANGE_INT64, OP_PUSH_POST_CHANGE_INT64, change); } else { cError("%s needs an int or long not %s", name, cGetName(*dt)); } } static DataType cStructAccess(DataType dt, int pointers) { Struct* st = dtGetStruct(&structs, dt); if(st == NULL || dt.pointers != pointers) { cError(pointers == 0 ? ". expects a struct" : "-> expects a struct*"); } 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_INT32, inner.address); cAddInt32Operation(OP_ADD_REFERENCE, 1); } return dtToVariable(inner.type); } static DataType cAccess() { 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, 0); } else if(cConsumeTokenIf(T_ARROW)) { dt = cStructAccess(cUnpack(dt), 1); } else if(cConsumeTokenIf(T_OPEN_SQUARE_BRACKET)) { dt = cUnpack(dt); if(!dtIsPointer(dt)) { cError("[] needs a pointer"); } cArrayIndex("index"); cConsumeToken(T_CLOSE_SQUARE_BRACKET); dt = dtReference(dt); cAddInt32Operation(OP_ADD_REFERENCE, cGetSize(dt)); dt = dtToVariable(dt); } else { return dt; } } } static DataType cPreChange(DataType dt, int change, const char* name) { cRemoveReference(&dt, name); if(dtCompare(dt, dtInt32())) { cChangeType(&dt, OP_CHANGE_INT32, OP_PUSH_PRE_CHANGE_INT32, change); } else if(dtCompare(dt, dtInt64())) { cChangeType(&dt, OP_CHANGE_INT64, OP_PUSH_PRE_CHANGE_INT64, change); } else { cError("%s needs an int or long not %s", name, cGetName(dt)); } return dt; } static DataType cInvertSign(DataType dt) { if(dtIsInt32(dt)) { cAddOperation(OP_INVERT_SIGN_INT32); } else if(dtIsInt64(dt)) { cAddOperation(OP_INVERT_SIGN_INT64); } 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 b, Operation bOp, DataType out) { if(dtCompare(in, a)) { cAddOperation(aOp); } else if(dtCompare(in, b)) { cAddOperation(bOp); } else { cError("cannot cast %s to %s", cGetName(in), cGetName(out)); } return out; } static DataType cPreUnary() { int marker = tGetMarker(); if(cConsumeTokenIf(T_OPEN_BRACKET)) { if(cConsumeTokenIf(T_FLOAT) && cConsumeTokenIf(T_CLOSE_BRACKET)) { return cCast(cUnpack(cPreUnary()), dtInt32(), OP_INT32_TO_FLOAT, dtInt64(), OP_INT64_TO_FLOAT, dtFloat()); } else if(cConsumeTokenIf(T_INT32) && cConsumeTokenIf(T_CLOSE_BRACKET)) { return cCast(cUnpack(cPreUnary()), dtFloat(), OP_FLOAT_TO_INT32, dtInt64(), OP_INT64_TO_INT32, dtInt32()); } else if(cConsumeTokenIf(T_INT64) && cConsumeTokenIf(T_CLOSE_BRACKET)) { return cCast(cUnpack(cPreUnary()), dtInt32(), OP_INT32_TO_INT64, dtFloat(), OP_FLOAT_TO_INT64, dtInt64()); } } 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)) { DataType dt = cPreUnary(); cExpectType(dt, dtBool(), "!"); cAddOperation(OP_NOT); return dt; } else if(cConsumeTokenIf(T_BIT_NOT)) { DataType dt = cPreUnary(); if(dtCompare(dt, dtInt32())) { cAddOperation(OP_BIT_NOT_INT32); } else if(dtCompare(dt, dtInt64())) { cAddOperation(OP_BIT_NOT_INT64); } else { cError("~ needs an int or long not %s", cGetName(dt)); } return dt; } else if(cConsumeTokenIf(T_BIT_AND)) { DataType dt = cPreUnary(); cRemoveReference(&dt, "&"); return cDereference(dt); } else if(cConsumeTokenIf(T_MUL)) { DataType dt = cPreUnary(); if(!dtIsPointer(dt)) { cError("* expects a pointer"); } dt = dtReference(dt); if(!dtIsPointer(dt) && !dtIsVariable(dt)) { dt = dtToVariable(dt); } else { cAddOperation(OP_REFERENCE); } return dt; } 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(dtIsInt32(*a) && op->intOp != OP_NOTHING) { cAddOperation(op->intOp); } else if(dtIsInt64(*a) && op->longOp != OP_NOTHING) { cAddOperation(op->longOp); } else if(dtIsFloat(*a) && op->floatOp != OP_NOTHING) { cAddOperation(op->floatOp); } else if(dtIsBool(*a) && op->boolOp != OP_NOTHING) { cAddOperation(op->boolOp); } else if(dtIsPointer(*a) && op->pointerOp != OP_NOTHING) { cAddOperation(op->pointerOp); } else { cInvalidOperation(*a, b, op->name); } } static DataType cMul() { 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() { 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() { 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() { DataType a = cShift(); while(true) { if(cConsumeTokenIf(T_LESS)) { cAddTypeOperation(&a, cShift, &TYPED_LESS); a = dtBool(); } else if(cConsumeTokenIf(T_LESS_EQUAL)) { cAddTypeOperation(&a, cShift, &TYPED_LESS_EQUAL); a = dtBool(); } else if(cConsumeTokenIf(T_GREATER)) { cAddTypeOperation(&a, cShift, &TYPED_GREATER); a = dtBool(); } else if(cConsumeTokenIf(T_GREATER_EQUAL)) { cAddTypeOperation(&a, cShift, &TYPED_GREATER_EQUAL); a = dtBool(); } else { return a; } } } static DataType cEqual() { DataType a = cComparison(); while(true) { if(cConsumeTokenIf(T_EQUAL)) { cAddTypeOperation(&a, cComparison, &TYPED_EQUAL); a = dtBool(); } else if(cConsumeTokenIf(T_NOT_EQUAL)) { cAddTypeOperation(&a, cComparison, &TYPED_NOT_EQUAL); a = dtBool(); } 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() { return cRepeat(T_BIT_AND, cEqual, &TYPED_BIT_AND); } static DataType cBitXor() { return cRepeat(T_BIT_XOR, cBitAnd, &TYPED_BIT_XOR); } static DataType cBitOr() { 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)) { a = cUnpack(a); cAddOperation(jump); int32 p = cReserveInt32(); DataType b = cUnpack(f()); if(!dtIsBool(a) || !dtIsBool(b)) { cInvalidOperation(a, b, tGetName(t)); } cAddOperation(op); cSetInt32(p, code->length); } return a; } static DataType cAnd() { return cLogical(cBitOr, T_AND, OP_PEEK_FALSE_GOTO, OP_AND); } static DataType cExpression() { return cLogical(cAnd, T_OR, OP_PEEK_TRUE_GOTO, OP_OR); } static void cLine(); static void cConsumeBody() { int oldLine = line; while(!cConsumeTokenIf(T_CLOSE_CURVED_BRACKET)) { if(tPeekToken() == T_END) { line = oldLine; cError("non closed curved bracket"); } cLine(); } } static void cConsumeScope() { 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() { if(dtIsVoid(returnType)) { cConsumeToken(T_SEMICOLON); cAddReturn(OP_RETURN); hasReturn = 2; return; } DataType dt = cUnpackedExpression(); if(!cCompare(returnType, dt)) { cError("wrong return type, should be %s", cGetName(returnType)); } else if(dtIsInt32(dt)) { cAddReturn(OP_RETURN_INT32); } else if(dtIsInt64(dt)) { cAddReturn(OP_RETURN_INT64); } else if(dtIsBool(dt)) { cAddReturn(OP_RETURN_BOOL); } else if(dtIsFloat(dt)) { cAddReturn(OP_RETURN_FLOAT); } else if(dtIsPointer(dt) || dtIsNull(dt)) { cAddReturn(OP_RETURN_POINTER); } else { cError("cannot return %s", cGetName(dt)); } cConsumeToken(T_SEMICOLON); hasReturn = 2; } static void cIf() { cConsumeToken(T_OPEN_BRACKET); cExpectType(cUnpackedExpression(), dtBool(), "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() { int start = code->length; cConsumeToken(T_OPEN_BRACKET); cExpectType(cUnpackedExpression(), dtBool(), "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 cDelete() { DataType pointer = cUnpackedExpression(); if(!dtIsPointer(pointer)) { cError("delete expects a pointer"); } cAddOperation(OP_DELETE); } static void cOperationSet(DataType left, const TypedOp* op) { cAddOperation(OP_DUPLICATE_REFERENCE); cLoadRef(left); cAddTypeOperation(&left, cUnpackedExpression, op); cStore(left, left, "="); } static void cSetVariable() { 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 cDeclare(DataType dt) { dt = cExtendType(dt); cConsumeToken(T_LITERAL); const char* var = cReadString(); if(vsInScope(&vars, var)) { cDeclared(var); } Variable* v = vsAdd(&vars, var, dt, &structs); if(dt.type != DT_STRUCT || dtIsPointer(dt)) { cConsumeToken(T_SET); cAddInt32Operation(OP_DEREFERENCE_VAR, v->address); cStore(v->type, cUnpackedExpression(), "="); } } static void cLineExpression() { int marker = tGetMarker(); Token t = cReadTokenAndLine(); if(t == T_LITERAL) { const char* literal = cReadString(); Struct* st = stsSearch(&structs, literal); if(st != NULL) { cDeclare(dtStruct(st)); return; } } switch(t) { case T_INT32: cDeclare(dtInt32()); break; case T_INT64: cDeclare(dtInt64()); break; case T_BOOL: cDeclare(dtBool()); break; case T_FLOAT: cDeclare(dtFloat()); break; case T_DELETE: cDelete(); break; default: tReset(marker); cSetVariable(); } } static void cFor() { Scope scope; vsEnterScope(&vars, &scope); cConsumeToken(T_OPEN_BRACKET); cLineExpression(); cConsumeToken(T_SEMICOLON); int startCheck = code->length; cExpectType(cUnpackedExpression(), dtBool(), "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() { 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() { if(forWhileStack == 0) { cError("continue without for or while"); } cAddInt32Operation(OP_GOTO, continueAt); cConsumeToken(T_SEMICOLON); } static void cLine() { 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) { cConsumeToken(T_LITERAL); fInit(f, cReadString(), line); f->returnType = rType; vsReset(&vars); cConsumeToken(T_OPEN_BRACKET); if(cConsumeTokenIf(T_CLOSE_BRACKET)) { return; } while(true) { DataType dt = cReadType(cReadTokenAndLine()); cConsumeToken(T_LITERAL); const char* name = cReadString(); if(vsInScope(&vars, name)) { cDeclared(name); } 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) { rType = cExtendType(rType); Function f; cBuildFunction(&f, rType); 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() { 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 = cReadType(cReadTokenAndLine()); 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) { switch(t) { case T_VOID: cFunction(dtVoid()); break; case T_STRUCT: cStruct(); break; default: cFunction(cReadType(t)); } } static void cCallMain() { Function f; fInit(&f, "main", line); Function* found = fsSearch(&functions, &f); if(found != NULL && dtIsVoid(found->returnType)) { cAddInt32Operation(OP_PUSH_INT32, 0); cAddInt32Operation(OP_GOSUB, found->address); cAddInt32(found->size); } } static void cForEachLine() { while(true) { Token t = cReadTokenAndLine(); if(t == T_END) { break; } cGlobalScope(t); } cCallMain(); } static void cLinkQueuedFunctions() { 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; cError("unknown function"); } cSetInt32(f->address, found->address); } } static void cAllocAndCompile() { forWhileStack = 0; breakIndex = 0; returnType = dtVoid(); onLine = false; vsInit(&vars); fsInit(&functions); fsInit(&functionQueue); stsInit(&structs); if(!setjmp(errorJump)) { cForEachLine(); cLinkQueuedFunctions(); } stsDelete(&structs); fsDelete(&functionQueue); fsDelete(&functions); vsDelete(&vars); } ByteCode* cCompile() { error[0] = '\0'; code = bcInit(); cAllocAndCompile(); if(error[0] != '\0') { bcDelete(code); return NULL; } return code; } const char* cGetError() { return error; } int cGetLine() { return line; }