mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
I GIVE UP
This commit is contained in:
2
scripts/small.toy
Normal file
2
scripts/small.toy
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
print "hello world";
|
||||||
|
|
||||||
@@ -19,6 +19,10 @@ void initCompiler(Compiler* compiler) {
|
|||||||
|
|
||||||
//separated out, so it can be recursive
|
//separated out, so it can be recursive
|
||||||
static int writeLiteralTypeToCacheOpt(LiteralArray* literalCache, Literal literal, bool skipDuplicationOptimisation) {
|
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 it's a compound type, recurse and store the results
|
||||||
if (AS_TYPE(literal).typeOf == LITERAL_ARRAY || AS_TYPE(literal).typeOf == LITERAL_DICTIONARY) {
|
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
|
//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++) {
|
for (int i = 0; i < AS_TYPE(literal).count; i++) {
|
||||||
//write the values to the cache, and the indexes to the store
|
//write the values to the cache, and the indexes to the store
|
||||||
int subIndex = writeLiteralTypeToCacheOpt(literalCache, ((Literal*)(AS_TYPE(literal).subtypes))[i], false);
|
int subIndex = writeLiteralTypeToCacheOpt(literalCache, ((Literal*)(AS_TYPE(literal).subtypes))[i], false);
|
||||||
|
|
||||||
Literal lit = TO_INTEGER_LITERAL(subIndex);
|
Literal lit = TO_INTEGER_LITERAL(subIndex);
|
||||||
pushLiteralArray(store, lit);
|
pushLiteralArray(store, lit);
|
||||||
freeLiteral(lit);
|
freeLiteral(lit);
|
||||||
@@ -46,12 +51,15 @@ static int writeLiteralTypeToCacheOpt(LiteralArray* literalCache, Literal litera
|
|||||||
int index = findLiteralIndex(literalCache, literal);
|
int index = findLiteralIndex(literalCache, literal);
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
index = pushLiteralArray(literalCache, literal);
|
index = pushLiteralArray(literalCache, literal);
|
||||||
|
freeLiteral(literal);
|
||||||
}
|
}
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
else {
|
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
|
//push the store to the cache, with instructions about how pack it
|
||||||
Literal literal = TO_DICTIONARY_LITERAL(store);
|
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);
|
freeLiteral(literal);
|
||||||
}
|
}
|
||||||
else if (node->compound.literalType == LITERAL_ARRAY) {
|
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) {
|
static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAddressesPtr, void* continueAddressesPtr) {
|
||||||
//grow if the bytecode space is too small
|
//grow if the bytecode space is too small
|
||||||
if (compiler->capacity < compiler->count + 1) {
|
if (compiler->count + 32 > compiler->capacity) {
|
||||||
int oldCapacity = compiler->capacity;
|
int oldCapacity = compiler->capacity;
|
||||||
|
|
||||||
compiler->capacity = GROW_CAPACITY_FAST(oldCapacity);
|
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++) {
|
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
|
emitShort(&collation, &capacity, &count, (unsigned short)AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
|
||||||
}
|
}
|
||||||
|
|
||||||
freeLiteralArray(ptr);
|
|
||||||
FREE(LiteralArray, ptr);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LITERAL_DICTIONARY: {
|
case LITERAL_DICTIONARY_INTERMEDIATE: {
|
||||||
emitByte(&collation, &capacity, &count, LITERAL_DICTIONARY);
|
emitByte(&collation, &capacity, &count, LITERAL_DICTIONARY);
|
||||||
|
|
||||||
LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
|
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++) {
|
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
|
emitShort(&collation, &capacity, &count, (unsigned short)AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
|
||||||
}
|
}
|
||||||
|
|
||||||
freeLiteralArray(ptr);
|
|
||||||
FREE(LiteralArray, ptr);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -892,7 +895,8 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
|||||||
emitShort(&collation, &capacity, &count, (unsigned short)(fnIndex++));
|
emitShort(&collation, &capacity, &count, (unsigned short)(fnIndex++));
|
||||||
|
|
||||||
freeCompiler((Compiler*)fnCompiler);
|
freeCompiler((Compiler*)fnCompiler);
|
||||||
FREE(Compiler, fnCompiler);
|
FREE(compiler, fnCompiler);
|
||||||
|
FREE_ARRAY(unsigned char, bytes, size);
|
||||||
}
|
}
|
||||||
break;
|
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
|
LiteralArray* ptr = AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
|
||||||
|
|
||||||
//the base literal
|
//the base literal
|
||||||
Literal typeLiteral = ptr->literals[0];
|
Literal typeLiteral = copyLiteral(ptr->literals[0]);
|
||||||
|
|
||||||
//what type this literal represents
|
//what type this literal represents
|
||||||
emitByte(&collation, &capacity, &count, AS_TYPE(typeLiteral).typeOf);
|
emitByte(&collation, &capacity, &count, AS_TYPE(typeLiteral).typeOf);
|
||||||
@@ -941,8 +945,7 @@ static unsigned char* collateCompilerHeaderOpt(Compiler* compiler, int* size, bo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
freeLiteralArray(ptr);
|
freeLiteral(typeLiteral);
|
||||||
FREE(LiteralArray, ptr);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ bool injectNativeFn(Interpreter* interpreter, char* name, NativeFn func) {
|
|||||||
setLiteralDictionary(&interpreter->scope->variables, identifier, fn);
|
setLiteralDictionary(&interpreter->scope->variables, identifier, fn);
|
||||||
setLiteralDictionary(&interpreter->scope->types, identifier, type);
|
setLiteralDictionary(&interpreter->scope->types, identifier, type);
|
||||||
|
|
||||||
|
freeLiteral(identifier);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -526,7 +528,9 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
|
|||||||
//concat the strings
|
//concat the strings
|
||||||
char buffer[MAX_STRING_LENGTH];
|
char buffer[MAX_STRING_LENGTH];
|
||||||
snprintf(buffer, MAX_STRING_LENGTH, "%s%s", AS_STRING(lhs), AS_STRING(rhs));
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1451,7 +1455,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
case LITERAL_BOOLEAN: {
|
case LITERAL_BOOLEAN: {
|
||||||
//read the booleans
|
//read the booleans
|
||||||
const bool b = readByte(interpreter->bytecode, &interpreter->count);
|
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) {
|
if (command.verbose) {
|
||||||
printf("(boolean %s)\n", b ? "true" : "false");
|
printf("(boolean %s)\n", b ? "true" : "false");
|
||||||
@@ -1461,7 +1467,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
|
|
||||||
case LITERAL_INTEGER: {
|
case LITERAL_INTEGER: {
|
||||||
const int d = readInt(interpreter->bytecode, &interpreter->count);
|
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) {
|
if (command.verbose) {
|
||||||
printf("(integer %d)\n", d);
|
printf("(integer %d)\n", d);
|
||||||
@@ -1471,7 +1479,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
|
|
||||||
case LITERAL_FLOAT: {
|
case LITERAL_FLOAT: {
|
||||||
const float f = readFloat(interpreter->bytecode, &interpreter->count);
|
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) {
|
if (command.verbose) {
|
||||||
printf("(float %f)\n", f);
|
printf("(float %f)\n", f);
|
||||||
@@ -1481,7 +1491,9 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
|
|
||||||
case LITERAL_STRING: {
|
case LITERAL_STRING: {
|
||||||
char* s = readString(interpreter->bytecode, &interpreter->count);
|
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) {
|
if (command.verbose) {
|
||||||
printf("(string \"%s\")\n", s);
|
printf("(string \"%s\")\n", s);
|
||||||
@@ -1503,12 +1515,16 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
|
|
||||||
if (command.verbose) {
|
if (command.verbose) {
|
||||||
printf("(array ");
|
printf("(array ");
|
||||||
printLiteral(TO_ARRAY_LITERAL(array));
|
Literal literal = TO_ARRAY_LITERAL(array);
|
||||||
|
printLiteral(literal);
|
||||||
|
freeLiteral(literal);
|
||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//finally, push the array proper
|
//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;
|
break;
|
||||||
|
|
||||||
@@ -1527,12 +1543,16 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
|
|
||||||
if (command.verbose) {
|
if (command.verbose) {
|
||||||
printf("(dictionary ");
|
printf("(dictionary ");
|
||||||
printLiteral(TO_DICTIONARY_LITERAL(dictionary));
|
Literal literal = TO_DICTIONARY_LITERAL(dictionary);
|
||||||
|
printLiteral(literal);
|
||||||
|
freeLiteral(literal);
|
||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//finally, push the dictionary proper
|
//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;
|
break;
|
||||||
|
|
||||||
@@ -1563,6 +1583,8 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
if (command.verbose) {
|
if (command.verbose) {
|
||||||
printf("(identifier %s (hash: %x))\n", AS_IDENTIFIER(identifier), identifier.as.identifier.hash);
|
printf("(identifier %s (hash: %x))\n", AS_IDENTIFIER(identifier), identifier.as.identifier.hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(identifier);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1581,6 +1603,8 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
printLiteral(typeLiteral);
|
printLiteral(typeLiteral);
|
||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(typeLiteral);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1614,6 +1638,8 @@ static void readInterpreterSections(Interpreter* interpreter) {
|
|||||||
printLiteral(typeLiteral);
|
printLiteral(typeLiteral);
|
||||||
printf(")\n");
|
printf(")\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeLiteral(typeLiteral);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,13 +34,15 @@ void freeLiteral(Literal literal) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_ARRAY(literal)) {
|
if (IS_ARRAY(literal) || literal.type == LITERAL_DICTIONARY_INTERMEDIATE || literal.type == LITERAL_TYPE_INTERMEDIATE) {
|
||||||
freeLiteralArray(AS_ARRAY(literal));
|
freeLiteralArray(AS_ARRAY(literal));
|
||||||
|
FREE(LiteralArray, AS_ARRAY(literal));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_DICTIONARY(literal)) {
|
if (IS_DICTIONARY(literal)) {
|
||||||
freeLiteralDictionary(AS_DICTIONARY(literal));
|
freeLiteralDictionary(AS_DICTIONARY(literal));
|
||||||
|
FREE(LiteralDictionary, AS_DICTIONARY(literal));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ typedef enum {
|
|||||||
LITERAL_IDENTIFIER,
|
LITERAL_IDENTIFIER,
|
||||||
LITERAL_TYPE,
|
LITERAL_TYPE,
|
||||||
LITERAL_TYPE_INTERMEDIATE, //used to process types in the compiler only
|
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_INTERMEDIATE, //used to process functions in the compiler only
|
||||||
LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters
|
LITERAL_FUNCTION_ARG_REST, //used to process function rest parameters
|
||||||
LITERAL_FUNCTION_NATIVE, //for handling native functions
|
LITERAL_FUNCTION_NATIVE, //for handling native functions
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ Literal copyLiteral(Literal original) {
|
|||||||
return lit;
|
return lit;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LITERAL_TYPE_INTERMEDIATE: {
|
case LITERAL_DICTIONARY_INTERMEDIATE: {
|
||||||
LiteralArray* array = ALLOCATE(LiteralArray, 1);
|
LiteralArray* array = ALLOCATE(LiteralArray, 1);
|
||||||
initLiteralArray(array);
|
initLiteralArray(array);
|
||||||
|
|
||||||
@@ -81,6 +81,22 @@ Literal copyLiteral(Literal original) {
|
|||||||
pushLiteralArray(array, copyLiteral(AS_ARRAY(original)->literals[i]));
|
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);
|
Literal ret = TO_ARRAY_LITERAL(array);
|
||||||
ret.type = LITERAL_TYPE_INTERMEDIATE;
|
ret.type = LITERAL_TYPE_INTERMEDIATE;
|
||||||
return ret;
|
return ret;
|
||||||
@@ -141,6 +157,7 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
|
|||||||
return !strncmp(AS_STRING(lhs), AS_STRING(rhs), strlen(AS_STRING(lhs)));
|
return !strncmp(AS_STRING(lhs), AS_STRING(rhs), strlen(AS_STRING(lhs)));
|
||||||
|
|
||||||
case LITERAL_ARRAY:
|
case LITERAL_ARRAY:
|
||||||
|
case LITERAL_DICTIONARY_INTERMEDIATE: //BUGFIX
|
||||||
case LITERAL_TYPE_INTERMEDIATE: //BUGFIX: used for storing types as an array
|
case LITERAL_TYPE_INTERMEDIATE: //BUGFIX: used for storing types as an array
|
||||||
//mismatched sizes
|
//mismatched sizes
|
||||||
if (AS_ARRAY(lhs)->count != AS_ARRAY(rhs)->count) {
|
if (AS_ARRAY(lhs)->count != AS_ARRAY(rhs)->count) {
|
||||||
@@ -304,7 +321,7 @@ int hashLiteral(Literal lit) {
|
|||||||
|
|
||||||
//utils
|
//utils
|
||||||
static void stdoutWrapper(const char* output) {
|
static void stdoutWrapper(const char* output) {
|
||||||
fprintf(stdout, "%s", output);
|
printf("%s", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
//buffer the prints
|
//buffer the prints
|
||||||
@@ -323,7 +340,7 @@ static void printToBuffer(const char* str) {
|
|||||||
globalPrintBuffer = GROW_ARRAY(char, globalPrintBuffer, oldCapacity, globalPrintCapacity);
|
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);
|
globalPrintCount += strlen(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1370,7 +1370,6 @@ static void varDecl(Parser* parser, Node** nodeHandle) {
|
|||||||
//TODO: static type checking?
|
//TODO: static type checking?
|
||||||
|
|
||||||
//declare it
|
//declare it
|
||||||
freeNode(*nodeHandle); //free the initial node
|
|
||||||
emitNodeVarDecl(nodeHandle, identifier, typeLiteral, expressionNode);
|
emitNodeVarDecl(nodeHandle, identifier, typeLiteral, expressionNode);
|
||||||
|
|
||||||
consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of var declaration");
|
consume(parser, TOKEN_SEMICOLON, "Expected ';' at end of var declaration");
|
||||||
@@ -1515,7 +1514,6 @@ static void fnDecl(Parser* parser, Node** nodeHandle) {
|
|||||||
blockStmt(parser, &blockNode);
|
blockStmt(parser, &blockNode);
|
||||||
|
|
||||||
//declare it
|
//declare it
|
||||||
freeNode(*nodeHandle); //free the initial node, because WTF?
|
|
||||||
emitNodeFnDecl(nodeHandle, identifier, argumentNode, returnNode, blockNode);
|
emitNodeFnDecl(nodeHandle, identifier, argumentNode, returnNode, blockNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ all: $(OBJ) $(TESTS:%.c=../$(OUTDIR)/%.exe)
|
|||||||
../$(OUTDIR)/%.exe: $(ODIR)/%.o
|
../$(OUTDIR)/%.exe: $(ODIR)/%.o
|
||||||
@$(CC) -o $@ $< $(TARGETS:../source/%.c=$(ODIR)/%.o) $(CFLAGS) $(LIBS)
|
@$(CC) -o $@ $< $(TARGETS:../source/%.c=$(ODIR)/%.o) $(CFLAGS) $(LIBS)
|
||||||
ifeq ($(shell uname),Linux)
|
ifeq ($(shell uname),Linux)
|
||||||
valgrind $@
|
valgrind --leak-check=full $@
|
||||||
else
|
else
|
||||||
@echo please run these tests with valgrind on linux
|
@echo please run these tests with valgrind on linux
|
||||||
endif
|
endif
|
||||||
|
|||||||
1018
test/sample_code.toy
1018
test/sample_code.toy
File diff suppressed because it is too large
Load Diff
@@ -96,9 +96,18 @@ int main() {
|
|||||||
initCompiler(&compiler);
|
initCompiler(&compiler);
|
||||||
|
|
||||||
Node* node = scanParser(&parser);
|
Node* node = scanParser(&parser);
|
||||||
|
while (node != NULL) {
|
||||||
|
if (node->type == NODE_ERROR) {
|
||||||
|
fprintf(stderr, ERROR "ERROR: Error node found" RESET);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
//write
|
//write
|
||||||
writeCompiler(&compiler, node);
|
writeCompiler(&compiler, node);
|
||||||
|
freeNode(node);
|
||||||
|
|
||||||
|
node = scanParser(&parser);
|
||||||
|
}
|
||||||
|
|
||||||
//collate
|
//collate
|
||||||
int size = 0;
|
int size = 0;
|
||||||
@@ -107,7 +116,6 @@ int main() {
|
|||||||
//cleanup
|
//cleanup
|
||||||
FREE_ARRAY(char, source, sourceLength);
|
FREE_ARRAY(char, source, sourceLength);
|
||||||
FREE_ARRAY(unsigned char, bytecode, size);
|
FREE_ARRAY(unsigned char, bytecode, size);
|
||||||
freeNode(node);
|
|
||||||
freeParser(&parser);
|
freeParser(&parser);
|
||||||
freeCompiler(&compiler);
|
freeCompiler(&compiler);
|
||||||
}
|
}
|
||||||
|
|||||||
129
test/test_interpreter.c
Normal file
129
test/test_interpreter.c
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "compiler.h"
|
||||||
|
#include "interpreter.h"
|
||||||
|
|
||||||
|
#include "console_colors.h"
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
//IO functions
|
||||||
|
char* readFile(char* path, size_t* fileSize) {
|
||||||
|
FILE* file = fopen(path, "rb");
|
||||||
|
|
||||||
|
if (file == NULL) {
|
||||||
|
fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(file, 0L, SEEK_END);
|
||||||
|
*fileSize = ftell(file);
|
||||||
|
rewind(file);
|
||||||
|
|
||||||
|
char* buffer = (char*)malloc(*fileSize + 1);
|
||||||
|
|
||||||
|
if (buffer == NULL) {
|
||||||
|
fprintf(stderr, ERROR "Not enough memory to read \"%s\"\n" RESET, path);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t bytesRead = fread(buffer, sizeof(char), *fileSize, file);
|
||||||
|
|
||||||
|
buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this
|
||||||
|
|
||||||
|
if (bytesRead < *fileSize) {
|
||||||
|
fprintf(stderr, ERROR "Could not read file \"%s\"\n" RESET, path);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
{
|
||||||
|
//test init & free
|
||||||
|
Interpreter interpreter;
|
||||||
|
initInterpreter(&interpreter);
|
||||||
|
freeInterpreter(&interpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
//source
|
||||||
|
char* source = "print null;";
|
||||||
|
|
||||||
|
//test basic compilation & collation
|
||||||
|
Lexer lexer;
|
||||||
|
Parser parser;
|
||||||
|
Compiler compiler;
|
||||||
|
Interpreter interpreter;
|
||||||
|
|
||||||
|
initLexer(&lexer, source);
|
||||||
|
initParser(&parser, &lexer);
|
||||||
|
initCompiler(&compiler);
|
||||||
|
initInterpreter(&interpreter);
|
||||||
|
|
||||||
|
Node* node = scanParser(&parser);
|
||||||
|
|
||||||
|
//write
|
||||||
|
writeCompiler(&compiler, node);
|
||||||
|
|
||||||
|
//collate
|
||||||
|
int size = 0;
|
||||||
|
unsigned char* bytecode = collateCompiler(&compiler, &size);
|
||||||
|
|
||||||
|
//run
|
||||||
|
runInterpreter(&interpreter, bytecode, size);
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
freeNode(node);
|
||||||
|
freeParser(&parser);
|
||||||
|
freeCompiler(&compiler);
|
||||||
|
freeInterpreter(&interpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
//source
|
||||||
|
size_t sourceLength = 0;
|
||||||
|
char* source = readFile("sample_code.toy", &sourceLength);
|
||||||
|
|
||||||
|
//test basic compilation & collation
|
||||||
|
Lexer lexer;
|
||||||
|
Parser parser;
|
||||||
|
Compiler compiler;
|
||||||
|
Interpreter interpreter;
|
||||||
|
|
||||||
|
initLexer(&lexer, source);
|
||||||
|
initParser(&parser, &lexer);
|
||||||
|
initCompiler(&compiler);
|
||||||
|
initInterpreter(&interpreter);
|
||||||
|
|
||||||
|
Node* node = scanParser(&parser);
|
||||||
|
|
||||||
|
//write
|
||||||
|
writeCompiler(&compiler, node);
|
||||||
|
|
||||||
|
//collate
|
||||||
|
int size = 0;
|
||||||
|
unsigned char* bytecode = collateCompiler(&compiler, &size);
|
||||||
|
|
||||||
|
//run
|
||||||
|
runInterpreter(&interpreter, bytecode, size);
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
FREE_ARRAY(char, source, sourceLength);
|
||||||
|
freeNode(node);
|
||||||
|
freeParser(&parser);
|
||||||
|
freeCompiler(&compiler);
|
||||||
|
freeInterpreter(&interpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(NOTICE "All good\n" RESET);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user