diff --git a/source/compiler.c b/source/compiler.c index e1ba003..3b26106 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -2,6 +2,8 @@ #include "memory.h" +#include + void initCompiler(Compiler* compiler) { initLiteralArray(&compiler->literalCache); compiler->bytecode = NULL; @@ -30,6 +32,11 @@ void writeCompiler(Compiler* compiler, Node* node) { //determine node type switch(node->type) { //TODO: more types, like variables, etc. + case NODE_ERROR: { + printf("[internal] NODE_ERROR encountered in writeCompiler()"); + compiler->bytecode[compiler->count++] = OP_EOF; //1 byte + } + break; case NODE_LITERAL: { //ensure the literal is in the cache @@ -83,19 +90,19 @@ void freeCompiler(Compiler* compiler) { compiler->count = 0; } -static void emitByte(char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) { +static void emitByte(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned char byte) { //grow the array if (*countPtr + 1 > *capacityPtr) { int oldCapacity = *capacityPtr; *capacityPtr = GROW_CAPACITY(*capacityPtr); - *collationPtr = GROW_ARRAY(char, *collationPtr, oldCapacity, *capacityPtr); + *collationPtr = GROW_ARRAY(unsigned char, *collationPtr, oldCapacity, *capacityPtr); } //append to the collation (*collationPtr)[(*countPtr)++] = byte; } -static void emitShort(char** collationPtr, int* capacityPtr, int* countPtr, unsigned short bytes) { +static void emitShort(unsigned char** collationPtr, int* capacityPtr, int* countPtr, unsigned short bytes) { char* ptr = (char*)&bytes; emitByte(collationPtr, capacityPtr, countPtr, *ptr); @@ -103,7 +110,7 @@ static void emitShort(char** collationPtr, int* capacityPtr, int* countPtr, unsi emitByte(collationPtr, capacityPtr, countPtr, *ptr); } -static void emitInt(char** collationPtr, int* capacityPtr, int* countPtr, int bytes) { +static void emitInt(unsigned char** collationPtr, int* capacityPtr, int* countPtr, int bytes) { char* ptr = (char*)&bytes; emitByte(collationPtr, capacityPtr, countPtr, *ptr); @@ -115,7 +122,7 @@ static void emitInt(char** collationPtr, int* capacityPtr, int* countPtr, int by emitByte(collationPtr, capacityPtr, countPtr, *ptr); } -static void emitFloat(char** collationPtr, int* capacityPtr, int* countPtr, float bytes) { +static void emitFloat(unsigned char** collationPtr, int* capacityPtr, int* countPtr, float bytes) { char* ptr = (char*)&bytes; emitByte(collationPtr, capacityPtr, countPtr, *ptr); @@ -128,10 +135,10 @@ static void emitFloat(char** collationPtr, int* capacityPtr, int* countPtr, floa } //return the result -char* collateCompiler(Compiler* compiler, int* size) { +unsigned char* collateCompiler(Compiler* compiler, int* size) { int capacity = GROW_CAPACITY(0); int count = 0; - char* collation = ALLOCATE(char, capacity); + unsigned char* collation = ALLOCATE(unsigned char, capacity); //embed the header with version information emitByte(&collation, &capacity, &count, TOY_VERSION_MAJOR); @@ -142,7 +149,7 @@ char* collateCompiler(Compiler* compiler, int* size) { if (strlen(TOY_VERSION_BUILD) + count + 1 > capacity) { int oldCapacity = capacity; capacity = strlen(TOY_VERSION_BUILD) + count + 1; //full header size - collation = GROW_ARRAY(char, collation, oldCapacity, capacity); + collation = GROW_ARRAY(unsigned char, collation, oldCapacity, capacity); } memcpy(&collation[count], TOY_VERSION_BUILD, strlen(TOY_VERSION_BUILD)); @@ -208,7 +215,7 @@ char* collateCompiler(Compiler* compiler, int* size) { emitByte(&collation, &capacity, &count, OP_EOF); //terminate bytecode //finalize - SHRINK_ARRAY(char, collation, capacity, count); + SHRINK_ARRAY(unsigned char, collation, capacity, count); *size = count; diff --git a/source/compiler.h b/source/compiler.h index c186453..3fc43e0 100644 --- a/source/compiler.h +++ b/source/compiler.h @@ -18,4 +18,4 @@ void writeCompiler(Compiler* compiler, Node* node); void freeCompiler(Compiler* compiler); //embed the header with version information, data section, code section, etc. -char* collateCompiler(Compiler* compiler, int* size); +unsigned char* collateCompiler(Compiler* compiler, int* size); diff --git a/source/interpreter.c b/source/interpreter.c index 0313ee2..5f6d7de 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -70,9 +70,9 @@ static float readFloat(unsigned char* tb, int* count) { } static char* readString(unsigned char* tb, int* count) { - char* ret = tb + *count; - *count += strlen(ret) + 1; //+1 for null character - return ret; + unsigned char* ret = tb + *count; + *count += strlen((char*)ret) + 1; //+1 for null character + return (char*)ret; } static void consumeByte(unsigned char byte, unsigned char* tb, int* count) { @@ -194,6 +194,9 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { pushLiteralArray(&interpreter->stack, TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) )); return true; + default: + printf("[internal] bad opcode argument passed to execArithmetic()"); + return false; } } @@ -220,6 +223,10 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { case OP_DIVISION: pushLiteralArray(&interpreter->stack, TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) )); return true; + + default: + printf("[internal] bad opcode argument passed to execArithmetic()"); + return false; } } diff --git a/source/keyword_types.c b/source/keyword_types.c index a669a24..1acb8f2 100644 --- a/source/keyword_types.c +++ b/source/keyword_types.c @@ -32,7 +32,7 @@ KeywordType keywordTypes[] = { {TOKEN_OF, "of"}, {TOKEN_PRINT, "print"}, {TOKEN_RETURN, "return"}, - {TOKEN_USING, "using"}, + {TOKEN_TYPE, "type"}, {TOKEN_VAR, "var"}, {TOKEN_WHILE, "while"}, @@ -41,8 +41,8 @@ KeywordType keywordTypes[] = { {TOKEN_LITERAL_FALSE, "false"}, //meta tokens - {TOKEN_PASS, "pass"}, - {TOKEN_ERROR, "error"}, + {TOKEN_PASS, NULL}, + {TOKEN_ERROR, NULL}, {TOKEN_EOF, NULL}, }; diff --git a/source/makefile b/source/makefile index 0f2c134..1c78cf0 100644 --- a/source/makefile +++ b/source/makefile @@ -1,7 +1,7 @@ CC=gcc IDIR =. -CFLAGS=$(addprefix -I,$(IDIR)) -g # -Wall -W -pedantic +CFLAGS=$(addprefix -I,$(IDIR)) -g # -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable LIBS= ODIR=obj diff --git a/source/node.c b/source/node.c index 728a22d..044bf38 100644 --- a/source/node.c +++ b/source/node.c @@ -11,6 +11,10 @@ void freeNode(Node* node) { } switch(node->type) { + case NODE_ERROR: + //NO-OP + break; + case NODE_LITERAL: freeLiteral(node->atomic.literal); break; @@ -71,6 +75,10 @@ void emitNodeGrouping(Node** nodeHandle) { void printNode(Node* node) { switch(node->type) { + case NODE_ERROR: + printf("error"); + break; + case NODE_LITERAL: printf("literal:"); printLiteral(node->atomic.literal); diff --git a/source/parser.c b/source/parser.c index 99dec9b..2ff9d68 100644 --- a/source/parser.c +++ b/source/parser.c @@ -424,6 +424,10 @@ static bool calcStaticBinaryArithmetic(Node** nodeHandle) { case OP_MODULO: result = TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) ); break; + + default: + printf("[internal] bad opcode argument passed to calcStaticBinaryArithmetic()"); + return false; } } @@ -450,6 +454,10 @@ static bool calcStaticBinaryArithmetic(Node** nodeHandle) { case OP_DIVISION: result = TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) ); break; + + default: + printf("[internal] bad opcode argument passed to calcStaticBinaryArithmetic()"); + return false; } } diff --git a/source/repl_main.c b/source/repl_main.c index 3194927..5140bb4 100644 --- a/source/repl_main.c +++ b/source/repl_main.c @@ -73,7 +73,7 @@ void runString(char* source) { //get the bytecode dump int size = 0; - char* tb = collateCompiler(&compiler, &size); + unsigned char* tb = collateCompiler(&compiler, &size); //cleanup freeCompiler(&compiler); @@ -131,7 +131,7 @@ void repl() { if (!error) { //get the bytecode dump int size = 0; - char* tb = collateCompiler(&compiler, &size); + unsigned char* tb = collateCompiler(&compiler, &size); // for (int i = 0; i < size; i++) { // printf("%d ", tb[i]); diff --git a/source/token_types.h b/source/token_types.h index 2f04a36..e38b601 100644 --- a/source/token_types.h +++ b/source/token_types.h @@ -30,7 +30,7 @@ typedef enum TokenType { TOKEN_OF, TOKEN_PRINT, TOKEN_RETURN, - TOKEN_USING, + TOKEN_TYPE, TOKEN_VAR, TOKEN_WHILE,