mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
I GIVE UP
This commit is contained in:
@@ -19,6 +19,10 @@ void initCompiler(Compiler* compiler) {
|
||||
|
||||
//separated out, so it can be recursive
|
||||
static int writeLiteralTypeToCacheOpt(LiteralArray* literalCache, Literal literal, bool skipDuplicationOptimisation) {
|
||||
printf(WARN);
|
||||
printLiteral(literal);
|
||||
printf(RESET);
|
||||
|
||||
//if it's a compound type, recurse and store the results
|
||||
if (AS_TYPE(literal).typeOf == LITERAL_ARRAY || AS_TYPE(literal).typeOf == LITERAL_DICTIONARY) {
|
||||
//I don't like storing types in an array, but it's the easiest and most straight forward method
|
||||
@@ -31,6 +35,7 @@ static int writeLiteralTypeToCacheOpt(LiteralArray* literalCache, Literal litera
|
||||
for (int i = 0; i < AS_TYPE(literal).count; i++) {
|
||||
//write the values to the cache, and the indexes to the store
|
||||
int subIndex = writeLiteralTypeToCacheOpt(literalCache, ((Literal*)(AS_TYPE(literal).subtypes))[i], false);
|
||||
|
||||
Literal lit = TO_INTEGER_LITERAL(subIndex);
|
||||
pushLiteralArray(store, lit);
|
||||
freeLiteral(lit);
|
||||
@@ -46,12 +51,15 @@ static int writeLiteralTypeToCacheOpt(LiteralArray* literalCache, Literal litera
|
||||
int index = findLiteralIndex(literalCache, literal);
|
||||
if (index < 0) {
|
||||
index = pushLiteralArray(literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
else {
|
||||
return pushLiteralArray(literalCache, literal);
|
||||
int index = pushLiteralArray(literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +139,8 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) {
|
||||
|
||||
//push the store to the cache, with instructions about how pack it
|
||||
Literal literal = TO_DICTIONARY_LITERAL(store);
|
||||
index = pushLiteralArray(&compiler->literalCache, literal); //WARNING: pushed as a dictionary, so below can recognize it
|
||||
literal.type = LITERAL_DICTIONARY_INTERMEDIATE; //god damn it
|
||||
index = pushLiteralArray(&compiler->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
}
|
||||
else if (node->compound.literalType == LITERAL_ARRAY) {
|
||||
@@ -257,7 +266,7 @@ static int writeLiteralToCompiler(Compiler* compiler, Literal literal) {
|
||||
|
||||
static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAddressesPtr, void* continueAddressesPtr) {
|
||||
//grow if the bytecode space is too small
|
||||
if (compiler->capacity < compiler->count + 1) {
|
||||
if (compiler->count + 32 > compiler->capacity) {
|
||||
int oldCapacity = compiler->capacity;
|
||||
|
||||
compiler->capacity = GROW_CAPACITY_FAST(oldCapacity);
|
||||
@@ -844,13 +853,10 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
||||
for (int i = 0; i < ptr->count; i++) {
|
||||
emitShort(&collation, &capacity, &count, (unsigned short)AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
|
||||
}
|
||||
|
||||
freeLiteralArray(ptr);
|
||||
FREE(LiteralArray, ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case LITERAL_DICTIONARY: {
|
||||
case LITERAL_DICTIONARY_INTERMEDIATE: {
|
||||
emitByte(&collation, &capacity, &count, LITERAL_DICTIONARY);
|
||||
|
||||
LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
|
||||
@@ -862,9 +868,6 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
||||
for (int i = 0; i < ptr->count; i++) {
|
||||
emitShort(&collation, &capacity, &count, (unsigned short)AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
|
||||
}
|
||||
|
||||
freeLiteralArray(ptr);
|
||||
FREE(LiteralArray, ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -892,7 +895,8 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
||||
emitShort(&collation, &capacity, &count, (unsigned short)(fnIndex++));
|
||||
|
||||
freeCompiler((Compiler*)fnCompiler);
|
||||
FREE(Compiler, fnCompiler);
|
||||
FREE(compiler, fnCompiler);
|
||||
FREE_ARRAY(unsigned char, bytes, size);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -927,7 +931,7 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
||||
LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
|
||||
|
||||
//the base literal
|
||||
Literal typeLiteral = ptr->literals[0];
|
||||
Literal typeLiteral = copyLiteral(ptr->literals[0]);
|
||||
|
||||
//what type this literal represents
|
||||
emitByte(&collation, &capacity, &count, AS_TYPE(typeLiteral).typeOf);
|
||||
@@ -941,8 +945,7 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
||||
}
|
||||
}
|
||||
|
||||
freeLiteralArray(ptr);
|
||||
FREE(LiteralArray, ptr);
|
||||
freeLiteral(typeLiteral);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -980,7 +983,7 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
||||
|
||||
*size = count;
|
||||
|
||||
return collation;
|
||||
return collation;
|
||||
}
|
||||
|
||||
unsigned char* collateCompiler(Compiler* compiler, int* size) {
|
||||
|
||||
@@ -42,6 +42,8 @@ bool injectNativeFn(Interpreter* interpreter, char* name, NativeFn func) {
|
||||
setLiteralDictionary(&interpreter->scope->variables, identifier, fn);
|
||||
setLiteralDictionary(&interpreter->scope->types, identifier, type);
|
||||
|
||||
freeLiteral(identifier);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -526,7 +528,9 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
|
||||
//concat the strings
|
||||
char buffer[MAX_STRING_LENGTH];
|
||||
snprintf(buffer, MAX_STRING_LENGTH, "%s%s", AS_STRING(lhs), AS_STRING(rhs));
|
||||
pushLiteralArray(&interpreter->stack, TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer) ));
|
||||
Literal literal = TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer) );
|
||||
pushLiteralArray(&interpreter->stack, literal);
|
||||
freeLiteral(literal);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1451,7 +1455,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
case LITERAL_BOOLEAN: {
|
||||
//read the booleans
|
||||
const bool b = readByte(interpreter->bytecode, &interpreter->count);
|
||||
pushLiteralArray(&interpreter->literalCache, TO_BOOLEAN_LITERAL(b));
|
||||
Literal literal = TO_BOOLEAN_LITERAL(b);
|
||||
pushLiteralArray(&interpreter->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
|
||||
if (command.verbose) {
|
||||
printf("(boolean %s)\n", b ? "true" : "false");
|
||||
@@ -1461,7 +1467,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
|
||||
case LITERAL_INTEGER: {
|
||||
const int d = readInt(interpreter->bytecode, &interpreter->count);
|
||||
pushLiteralArray(&interpreter->literalCache, TO_INTEGER_LITERAL(d));
|
||||
Literal literal = TO_INTEGER_LITERAL(d);
|
||||
pushLiteralArray(&interpreter->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
|
||||
if (command.verbose) {
|
||||
printf("(integer %d)\n", d);
|
||||
@@ -1471,7 +1479,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
|
||||
case LITERAL_FLOAT: {
|
||||
const float f = readFloat(interpreter->bytecode, &interpreter->count);
|
||||
pushLiteralArray(&interpreter->literalCache, TO_FLOAT_LITERAL(f));
|
||||
Literal literal = TO_FLOAT_LITERAL(f);
|
||||
pushLiteralArray(&interpreter->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
|
||||
if (command.verbose) {
|
||||
printf("(float %f)\n", f);
|
||||
@@ -1481,7 +1491,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
|
||||
case LITERAL_STRING: {
|
||||
char* s = readString(interpreter->bytecode, &interpreter->count);
|
||||
pushLiteralArray(&interpreter->literalCache, TO_STRING_LITERAL( copyString(s, strlen(s)), strlen(s) ));
|
||||
Literal literal = TO_STRING_LITERAL( copyString(s, strlen(s)), strlen(s) );
|
||||
pushLiteralArray(&interpreter->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
|
||||
if (command.verbose) {
|
||||
printf("(string \"%s\")\n", s);
|
||||
@@ -1503,12 +1515,16 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
|
||||
if (command.verbose) {
|
||||
printf("(array ");
|
||||
printLiteral(TO_ARRAY_LITERAL(array));
|
||||
Literal literal = TO_ARRAY_LITERAL(array);
|
||||
printLiteral(literal);
|
||||
freeLiteral(literal);
|
||||
printf(")\n");
|
||||
}
|
||||
|
||||
//finally, push the array proper
|
||||
pushLiteralArray(&interpreter->literalCache, TO_ARRAY_LITERAL(array));
|
||||
Literal literal = TO_ARRAY_LITERAL(array);
|
||||
pushLiteralArray(&interpreter->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1527,12 +1543,16 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
|
||||
if (command.verbose) {
|
||||
printf("(dictionary ");
|
||||
printLiteral(TO_DICTIONARY_LITERAL(dictionary));
|
||||
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
||||
printLiteral(literal);
|
||||
freeLiteral(literal);
|
||||
printf(")\n");
|
||||
}
|
||||
|
||||
//finally, push the dictionary proper
|
||||
pushLiteralArray(&interpreter->literalCache, TO_DICTIONARY_LITERAL(dictionary));
|
||||
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
||||
pushLiteralArray(&interpreter->literalCache, literal);
|
||||
freeLiteral(literal);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1563,6 +1583,8 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
if (command.verbose) {
|
||||
printf("(identifier %s (hash: %x))\n", AS_IDENTIFIER(identifier), identifier.as.identifier.hash);
|
||||
}
|
||||
|
||||
freeLiteral(identifier);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1581,6 +1603,8 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
printLiteral(typeLiteral);
|
||||
printf(")\n");
|
||||
}
|
||||
|
||||
freeLiteral(typeLiteral);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1614,6 +1638,8 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
||||
printLiteral(typeLiteral);
|
||||
printf(")\n");
|
||||
}
|
||||
|
||||
freeLiteral(typeLiteral);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -34,13 +34,15 @@ void freeLiteral(Literal literal) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (IS_ARRAY(literal)) {
|
||||
if (IS_ARRAY(literal) || literal.type == LITERAL_DICTIONARY_INTERMEDIATE || literal.type == LITERAL_TYPE_INTERMEDIATE) {
|
||||
freeLiteralArray(AS_ARRAY(literal));
|
||||
FREE(LiteralArray, AS_ARRAY(literal));
|
||||
return;
|
||||
}
|
||||
|
||||
if (IS_DICTIONARY(literal)) {
|
||||
freeLiteralDictionary(AS_DICTIONARY(literal));
|
||||
FREE(LiteralDictionary, AS_DICTIONARY(literal));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ typedef enum {
|
||||
LITERAL_IDENTIFIER,
|
||||
LITERAL_TYPE,
|
||||
LITERAL_TYPE_INTERMEDIATE, //used to process types in the compiler only
|
||||
LITERAL_DICTIONARY_INTERMEDIATE, //used to process dictionaries in the compiler only
|
||||
LITERAL_FUNCTION_INTERMEDIATE, //used to process functions in the compiler only
|
||||
LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters
|
||||
LITERAL_FUNCTION_NATIVE, //for handling native functions
|
||||
|
||||
@@ -72,7 +72,7 @@ Literal copyLiteral(Literal original) {
|
||||
return lit;
|
||||
}
|
||||
|
||||
case LITERAL_TYPE_INTERMEDIATE: {
|
||||
case LITERAL_DICTIONARY_INTERMEDIATE: {
|
||||
LiteralArray* array = ALLOCATE(LiteralArray, 1);
|
||||
initLiteralArray(array);
|
||||
|
||||
@@ -81,6 +81,22 @@ Literal copyLiteral(Literal original) {
|
||||
pushLiteralArray(array, copyLiteral(AS_ARRAY(original)->literals[i]));
|
||||
}
|
||||
|
||||
Literal ret = TO_ARRAY_LITERAL(array);
|
||||
ret.type = LITERAL_DICTIONARY_INTERMEDIATE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
case LITERAL_TYPE_INTERMEDIATE: {
|
||||
LiteralArray* array = ALLOCATE(LiteralArray, 1);
|
||||
initLiteralArray(array);
|
||||
|
||||
//copy each element
|
||||
for (int i = 0; i < AS_ARRAY(original)->count; i++) {
|
||||
Literal literal = copyLiteral(AS_ARRAY(original)->literals[i]);
|
||||
pushLiteralArray(array, literal );
|
||||
freeLiteral(literal);
|
||||
}
|
||||
|
||||
Literal ret = TO_ARRAY_LITERAL(array);
|
||||
ret.type = LITERAL_TYPE_INTERMEDIATE;
|
||||
return ret;
|
||||
@@ -141,6 +157,7 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
|
||||
return !strncmp(AS_STRING(lhs), AS_STRING(rhs), strlen(AS_STRING(lhs)));
|
||||
|
||||
case LITERAL_ARRAY:
|
||||
case LITERAL_DICTIONARY_INTERMEDIATE: //BUGFIX
|
||||
case LITERAL_TYPE_INTERMEDIATE: //BUGFIX: used for storing types as an array
|
||||
//mismatched sizes
|
||||
if (AS_ARRAY(lhs)->count != AS_ARRAY(rhs)->count) {
|
||||
@@ -304,7 +321,7 @@ int hashLiteral(Literal lit) {
|
||||
|
||||
//utils
|
||||
static void stdoutWrapper(const char* output) {
|
||||
fprintf(stdout, "%s", output);
|
||||
printf("%s", output);
|
||||
}
|
||||
|
||||
//buffer the prints
|
||||
@@ -323,7 +340,7 @@ static void printToBuffer(const char* str) {
|
||||
globalPrintBuffer = GROW_ARRAY(char, globalPrintBuffer, oldCapacity, globalPrintCapacity);
|
||||
}
|
||||
|
||||
snprintf(globalPrintBuffer + globalPrintCount, strlen(str) + 1, "%s", str);
|
||||
snprintf(globalPrintBuffer + globalPrintCount, strlen(str), "%s", str);
|
||||
globalPrintCount += strlen(str);
|
||||
}
|
||||
|
||||
|
||||
@@ -1370,7 +1370,6 @@ static void varDecl(Parser* parser, Node** nodeHandle) {
|
||||
//TODO: static type checking?
|
||||
|
||||
//declare it
|
||||
freeNode(*nodeHandle); //free the initial node
|
||||
emitNodeVarDecl(nodeHandle, identifier, typeLiteral, expressionNode);
|
||||
|
||||
consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of var declaration");
|
||||
@@ -1515,7 +1514,6 @@ static void fnDecl(Parser* parser, Node** nodeHandle) {
|
||||
blockStmt(parser, &blockNode);
|
||||
|
||||
//declare it
|
||||
freeNode(*nodeHandle); //free the initial node, because WTF?
|
||||
emitNodeFnDecl(nodeHandle, identifier, argumentNode, returnNode, blockNode);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user