diff --git a/includes/lexer.h b/includes/lexer.h index f6521c2..b5527b9 100644 --- a/includes/lexer.h +++ b/includes/lexer.h @@ -23,8 +23,9 @@ typedef enum { TK_Hex, TK_Opcode, TK_Register, - TK_Label, - TK_Directive + TK_Directive, + TK_Symbol, + TK_Comma } TokenType; typedef struct { diff --git a/src/lexer.c b/src/lexer.c index 6bb64d6..9acc781 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -13,6 +13,7 @@ List* GenerateTokensFromFile(const char* file_path) { char* line = NULL; size_t len = 0; ssize_t bytes_read; + List* tokens = CreateList(); file = fopen(file_path, "r"); @@ -21,39 +22,14 @@ List* GenerateTokensFromFile(const char* file_path) { return NULL; } - //char* context;// = malloc(sizeof(char *)); - while((bytes_read = getline(&line, &len, file)) != -1) { Token* token = GetNextToken(line); while (token) { if (!token) break; - - if(token->type == TK_Number) { - printf("Found number: '%s'\n", token->value); - } - else if (token->type == TK_Hex) { - printf("Hex number: '%s'\n", token->value); - } - else if (token->type == TK_Opcode) { - printf("Found op: '%s'\n", token->value); - } - else if (token->type == TK_Register) { - printf("Found Reg: '%s'\n", token->value); - } - else if(token->type == TK_Label) { - printf("Label: '%s'\n", token->value); - } - else if (token->type == TK_Directive) { - printf("Directive '%s'\n", token->value); - } - else { - printf("Found text: '%s'\n", token->value); - } - - free(token->value); - free(token); + + AddListItem(token, sizeof(Token), tokens); token = GetNextToken(NULL); } @@ -65,7 +41,7 @@ List* GenerateTokensFromFile(const char* file_path) { if (line) free(line); - return NULL; + return tokens; } Token* GetNextToken(char *string) { @@ -81,32 +57,27 @@ Token* GetNextToken(char *string) { if (base == 16) return CreateToken(TK_Hex, string_token); } - //if (strcmp(string_token, ":") == 0) return CreateToken(TK_Colon, string_token); - //if (strcmp(string_token, ",") == 0) return CreateToken(TK_Comma, string_token); - if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token); - if (IsRegister(string_token)) return CreateToken(TK_Register, string_token); + char* next = PeekNextToken(); - // char* next = SplitOnBasicGrammar(NULL); - - // if (next && strcmp(next, ":") == 0) { - // free(next); - // return CreateToken(TK_Label, string_token); - // } - - // if (next) free(next); - - if (strcmp(".db", string_token) == 0) { - char* next = PeekNextToken(); - if (next) { - printf("String variable name: '%s'\n", next); + if (next) { + if (strcmp(":", next) == 0) { free(next); + free(GetToken(NULL)); + return CreateToken(TK_Symbol, string_token); } + + free(next); } + if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token); + if (IsRegister(string_token)) return CreateToken(TK_Register, string_token); + if (strcmp(",", string_token) == 0) return CreateToken(TK_Comma, string_token); return CreateToken(TK_String, string_token); } + free(string_token); + return NULL; } diff --git a/src/main.c b/src/main.c index 45e8962..f3effae 100644 --- a/src/main.c +++ b/src/main.c @@ -21,5 +21,15 @@ int main(int argc, char* args[]) { return -1; } - GenerateTokensFromFile(args[1]); + List* tokens = GenerateTokensFromFile(args[1]); + + for (int i = 0; i < tokens->size; i++) { + Token* t = (Token*) tokens->content[i]; + + printf("Token Type: '%d' Value: '%s'\n", t->type, t->value); + + free(t->value); + } + + DestroyList(tokens); } \ No newline at end of file