diff --git a/src/ir/ir.c b/src/ir/ir.c index dc33288..2b2c818 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -64,7 +64,12 @@ IR_OUTPUT* parseIR(AST_TREE_BRANCH* node) { out->blockCount = 0; out->blocks = malloc(sizeof(IR_BASIC_BLOCK*) * out->allocatedBlockCount); out->map = createHashmap(512,200); - + + out->blocks[out->blockCount] = malloc(sizeof(IR_BASIC_BLOCK)); + out->blocks[out->blockCount]->instructions = NULL; + out->blocks[out->blockCount]->instructionCount = 0; + out->blocks[out->blockCount]->allocatedSize = 0; + while(node != NULL) { switch(node->type) { case AST_TYPE_FUNCTION_DECLARATION: diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c index bff5aeb..1a98b3b 100644 --- a/src/ir/irs/functions.c +++ b/src/ir/irs/functions.c @@ -42,10 +42,12 @@ void parseFunction(IR_OUTPUT* out, AST_FUNCTION_DEC* node) { hashPut(out->map, hash, func); - out->blocks[out->blockCount] = malloc(sizeof(IR_BASIC_BLOCK)); - out->blocks[out->blockCount]->instructions = NULL; - out->blocks[out->blockCount]->instructionCount = 0; - out->blocks[out->blockCount]->allocatedSize = 0; + if(out->blockCount != 0) { + out->blocks[out->blockCount] = malloc(sizeof(IR_BASIC_BLOCK)); + out->blocks[out->blockCount]->instructions = NULL; + out->blocks[out->blockCount]->instructionCount = 0; + out->blocks[out->blockCount]->allocatedSize = 0; + } AST_TREE_BRANCH* branch = (AST_TREE_BRANCH*) node->body; while(branch->next != NULL) { diff --git a/src/ir/irs/values.c b/src/ir/irs/values.c index f79d1a7..950ad58 100644 --- a/src/ir/irs/values.c +++ b/src/ir/irs/values.c @@ -9,6 +9,8 @@ #include "../../parser/structs/values.h" #include "../../parser/structs/tree.h" +#include "../../lib/types.h" + /** * Parses the value into the buffer. * @param buff the byte buffer. @@ -19,15 +21,42 @@ void parseValue(void** buff, int index, void* value) { if(((AST_TREE_BRANCH*)value)->type == AST_TYPE_VALUE) { AST_VALUE* val = (AST_VALUE*)value; - if(val->valueType[0] == 0x01) { //int32 - int num = atoi(val->value); + switch(*val->valueType) { + case INT32: + int num = atoi(val->value); + buff[index] = malloc(4); + + ((unsigned char*)buff[index])[0] = (num >> 24) & 0xFF; + ((unsigned char*)buff[index])[1] = (num >> 16) & 0xFF; + ((unsigned char*)buff[index])[2] = (num >> 8) & 0xFF; + ((unsigned char*)buff[index])[3] = num & 0xFF; + + break; + + case INT24: + num = atoi(val->value); + buff[index] = malloc(3); + + ((unsigned char*)buff[index])[0] = (num >> 24) & 0xFF; + ((unsigned char*)buff[index])[1] = (num >> 16) & 0xFF; + ((unsigned char*)buff[index])[2] = (num >> 8) & 0xFF; + break; - buff[index] = malloc(4); + case INT16: + num = atoi(val->value); + buff[index] = malloc(2); + + ((unsigned char*)buff[index])[0] = (num >> 24) & 0xFF; + ((unsigned char*)buff[index])[1] = (num >> 16) & 0xFF; + break; + + case INT8: + num = atoi(val->value); + buff[index] = malloc(1); + + ((unsigned char*)buff[index])[0] = (num >> 24) & 0xFF; + break; - ((unsigned char*)buff[index])[0] = (num >> 24) & 0xFF; - ((unsigned char*)buff[index])[1] = (num >> 16) & 0xFF; - ((unsigned char*)buff[index])[2] = (num >> 8) & 0xFF; - ((unsigned char*)buff[index])[3] = num & 0xFF; } } } @@ -37,6 +66,16 @@ void parseValue(void** buff, int index, void* value) { * @param type the type's byte indentifier. */ int getValueSize(unsigned char type) { - if(type == 0x01) return 4; // int32 type -> 4 bytes + switch(type) { + case INT32: + return 32; + case INT24: + return 24; + case INT16: + return 16; + case INT8: + return 8; + + } return 0; } diff --git a/src/ir/irs/variables.c b/src/ir/irs/variables.c index fcd7b99..c35e6d5 100644 --- a/src/ir/irs/variables.c +++ b/src/ir/irs/variables.c @@ -12,6 +12,8 @@ #include "../ir.h" +#include "../../lib/types.h" + /** * Parses a variable declaration. * @param block the IR basic block to append to. @@ -19,7 +21,24 @@ */ void parseVariableDeclaration(IR_BASIC_BLOCK* block, AST_VARIABLE_DEC* node) { int allocSize = 0; - if(node->type[0] == 0x01) allocSize = 32; // int32 + + switch(node->type[0]) { + case INT32: + allocSize = 32; + break; + case INT24: + allocSize = 24; + break; + case INT16: + allocSize = 16; + break; + case INT8: + allocSize = 8; + break; + default: + allocSize = 0; + break; + } void** params = malloc(sizeof(void*) * 2); diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 1bdebec..72fac0d 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -112,6 +112,15 @@ LEXER_RESULT runLexer(char* string, int size) { else if(strcmp(buff, "int32") == 0) { pushToken(&result, TYPE_INT32); } + else if(strcmp(buff, "int24") == 0) { + pushToken(&result, TYPE_INT24); + } + else if(strcmp(buff, "int16") == 0) { + pushToken(&result, TYPE_INT16); + } + else if(strcmp(buff, "int8") == 0) { + pushToken(&result, TYPE_INT8); + } else { pushToken(&result, KEYWORD); result.tokens[result.size - 1].value = buff; diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h index e21bdce..15391f6 100644 --- a/src/lexer/tokens.h +++ b/src/lexer/tokens.h @@ -24,7 +24,10 @@ typedef enum { NONE, MATH_OP, - TYPE_INT32 + TYPE_INT32, + TYPE_INT24, + TYPE_INT16, + TYPE_INT8 } TOKEN_TYPE; /** diff --git a/src/lib/types.h b/src/lib/types.h new file mode 100644 index 0000000..24b0ad0 --- /dev/null +++ b/src/lib/types.h @@ -0,0 +1,20 @@ +/** + * Standart Quickfall types. + */ + +#ifndef LIB_TYPES_H +#define LIB_TYPES_H + +typedef enum LIB_TYPES { + + VOID = 0x00, + + // Numeric types + INT32 = 0x01, + INT24 = 0x02, + INT16 = 0x03, + INT8 = 0x04 + +} LIB_TYPES; + +#endif \ No newline at end of file diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c index 317f59d..26ab697 100644 --- a/src/parser/asts/values.c +++ b/src/parser/asts/values.c @@ -11,23 +11,31 @@ #include "../../lexer/lexer.h" +#include "../../lib/types.h" + /** * Parses an AST value on the lexer result. * @param result the Lexer result. * @param index the index of the start of the parsing. + * @param exceptedType The excepted type of token. */ -AST_VALUE* parseASTValue(LEXER_RESULT result, int index) { +AST_VALUE* parseASTValue(LEXER_RESULT result, int index, LIB_TYPES exceptedType) { AST_VALUE* value = malloc(sizeof(AST_VALUE)); value->astType = AST_TYPE_VALUE; - switch(result.tokens[index].type) { - case NUMBER: + switch(exceptedType) { + case INT32: + case INT24: + case INT16: + case INT8: + if(result.tokens[index].type != NUMBER) { + printf("Excepted number as value!\n"); + return NULL; + } + value->valueType = malloc(1); - value->valueType[0] = 0x01; //i32 + value->valueType[0] = exceptedType; break; - default: - printf("Error: Couldn't parse token %d as a value!\n", result.tokens[index].type); - return value; } value->endingIndex = index + 1; @@ -40,15 +48,16 @@ AST_VALUE* parseASTValue(LEXER_RESULT result, int index) { * Parses an actual expression as value instead of just one token. * @param result the Lexer result. * @param index the index of the start of the parsing. + * @param exceptedType The excepted type of token. */ -void* parseValueGroup(LEXER_RESULT result, int index) { +void* parseValueGroup(LEXER_RESULT result, int index, LIB_TYPES exceptedType) { switch(result.tokens[index].type) { case NUMBER: if(result.tokens[index + 1].type == MATH_OP) { //todo: parse math op. } - return parseASTValue(result, index); + return parseASTValue(result, index, exceptedType); break; default: printf("Error: couldn't parse value token group!\n"); diff --git a/src/parser/asts/values.h b/src/parser/asts/values.h index 0c96b4e..a95b08d 100644 --- a/src/parser/asts/values.h +++ b/src/parser/asts/values.h @@ -6,6 +6,8 @@ #include "../../lexer/lexer.h" +#include "../../lib/types.h" + #ifndef VALUES_AST_H #define VALUES_AST_H @@ -13,14 +15,16 @@ * Parses an AST value on the lexer result. * @param result the Lexer result. * @param index the index of the start of the parsing. + * @param exceptedType The excepted type of token. */ -AST_VALUE* parseASTValue(LEXER_RESULT result, int index); +AST_VALUE* parseASTValue(LEXER_RESULT result, int index, LIB_TYPES exceptedType); /** * Parses an actual expression as value instead of just one token. * @param result the Lexer result. * @param index the index of the start of the parsing. + * @param exceptedType The excepted type of token. */ -void* parseValueGroup(LEXER_RESULT result, int index); +void* parseValueGroup(LEXER_RESULT result, int index, LIB_TYPES exceptedType); #endif \ No newline at end of file diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 77e4dfb..edc5e0a 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -26,11 +26,16 @@ AST_VARIABLE_DEC* parseASTVariableDeclaration(LEXER_RESULT result, int index) { switch(result.tokens[index].type) { case TYPE_INT32: - var->type[0] = 0x01; + case TYPE_INT24: + case TYPE_INT16: + case TYPE_INT8: + var->type[0] = (result.tokens[index].type - TYPE_INT32) + 0x01; break; + case VAR: var->type[0] = 0x00; break; + default: printf("Error: Disallowed token as variable type!\n"); return NULL; @@ -44,14 +49,14 @@ AST_VARIABLE_DEC* parseASTVariableDeclaration(LEXER_RESULT result, int index) { var->name = result.tokens[index + 1].value; if(result.tokens[index + 2].type == DECLARE) { - void* value = parseValueGroup(result, index + 3); + void* value = parseValueGroup(result, index + 3, var->type[0]); if(value == NULL) { printf("Error: Couldn't parse variable value group!\n"); return NULL; } - var->endingIndex = ((AST_TREE_BRANCH*)value)->endingIndex; + var->endingIndex = ((AST_TREE_BRANCH*)value)->endingIndex - 1; var->value = value; } @@ -71,7 +76,7 @@ AST_VARIABLE_MOD* parseVariableModification(LEXER_RESULT result, int index) { mod->name = result.tokens[index].value; - void* value = parseValueGroup(result, index + 2); + void* value = parseValueGroup(result, index + 2, 0x01); // todo: change later if(value == NULL) { printf("Error: Couldn't parse variable value group in redefinition!\n"); @@ -79,7 +84,7 @@ AST_VARIABLE_MOD* parseVariableModification(LEXER_RESULT result, int index) { } mod->value = value; - mod->endingIndex = ((AST_TREE_BRANCH*)value)->endingIndex; + mod->endingIndex = ((AST_TREE_BRANCH*)value)->endingIndex - 1; return mod; } \ No newline at end of file diff --git a/src/parser/parser.c b/src/parser/parser.c index be7ae81..533f1fa 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -35,6 +35,9 @@ void* parseRoot(LEXER_RESULT result, int startingIndex, AST_TYPE type) { switch(t.type) { case TYPE_INT32: + case TYPE_INT24: + case TYPE_INT16: + case TYPE_INT8: case VAR: void* node = parseASTVariableDeclaration(result, i); if(node != NULL) { @@ -76,6 +79,7 @@ void* parseRoot(LEXER_RESULT result, int startingIndex, AST_TYPE type) { case BRACKETS_CLOSE: if(type == AST_TYPE_FUNC_ROOT) { + curr->next = NULL; root->endingIndex = i; return root; }