diff --git a/includes/lexer.h b/includes/lexer.h index 84b1f12..f6521c2 100644 --- a/includes/lexer.h +++ b/includes/lexer.h @@ -23,7 +23,8 @@ typedef enum { TK_Hex, TK_Opcode, TK_Register, - TK_Label + TK_Label, + TK_Directive } TokenType; typedef struct { diff --git a/misc/test.asm b/misc/test.asm index ee42c60..1b71088 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -1,12 +1,13 @@ ; comments are ignored ; This is a very simple test file for an assemblier. -.db msg "Hello, world!" +.org 0x100 +.db msg "Hello, world!", 0 copy r1, msg copy r2, 0 -.loop: +loop: cmp r1, 0 - jz .loop + jz loop add r1, 1 mov r1, 5 ; move the immediate value 5 into r1 add r1,5 ; add 5 into r1 diff --git a/src/lexer.c b/src/lexer.c index 6724d21..6bb64d6 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -45,6 +45,9 @@ List* GenerateTokensFromFile(const char* file_path) { 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); } @@ -70,6 +73,8 @@ Token* GetNextToken(char *string) { int base = 0; while (strlen(string_token) != 0) { + + if (string_token[0] == '.') return CreateToken(TK_Directive, string_token); if (TokenIsNumeric(string_token, &base)) { if (base == 10) return CreateToken(TK_Number, string_token); diff --git a/src/main.c b/src/main.c index 7c748ef..45e8962 100644 --- a/src/main.c +++ b/src/main.c @@ -20,118 +20,6 @@ int main(int argc, char* args[]) { printf("Input files required.\n"); return -1; } - // char* test = "user_id"; - // printf("Key for '%s' is %d for size %d\n", test, GetKeyIndex(test, HASHTABLEDEFAULTSIZE), HASHTABLEDEFAULTSIZE); - GenerateTokensFromFile(args[1]); - - // for (int i = 1; i < argc; i++) { - // input_files[i - 1] = args[i]; - // print_file(args[i]); - // } - - //print_file(args[1]); - // struct test { - // int SomeValue; - // char* SomeText; - // }; - // struct test* thing = malloc(sizeof(struct test)); - // thing->SomeText = calloc(1, 16); - // List *list = CreateList(); - - // for (int i = 0; i < 64; i++) { - // sprintf(thing->SomeText, "%d", i); - // thing->SomeValue = i; - // AddListItem(thing, sizeof(struct test), list); - - // if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - - // printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->content[list->size - 1])->SomeValue, ((struct test*) list->content[list->size - 1])->SomeText); - // } - - // //PrintList(list); - // free(thing->SomeText); - // free(thing); - //free(list); -} - -// void print_file(char* file_path) { -// FILE *file; -// char* line = NULL; -// size_t len = 0; -// ssize_t bytes_read; - -// file = fopen(file_path, "r"); - -// if (!file) { -// printf("Failed to open '%s' for reading.\n", file_path); -// return; -// } - -// while((bytes_read = getline(&line, &len, file)) != -1) { -// List* list = get_strings(line); - -// if (!list) continue; -// if (list->size == 0) continue; - -// // Instruction *inst = malloc(sizeof(Instruction)); -// // inst->parameters = malloc(sizeof(char*) * 2); - -// // if (strcmp(list->root[0], "mov") == 0) { -// // inst->opcode = "mov"; -// // inst->count = 2; -// // inst->parameters[0] = list->root[1]; -// // inst->parameters[1] = list->root[2]; - -// // printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]); -// // } - -// // free(inst->parameters); -// // free(inst); - -// //PrintList(list); -// DestroyList(list); -// } - -// fclose(file); - -// if (line) free(line); -// } - -// List* get_strings(const char* line) { -// if (strlen(line) == 0 ) return NULL; -// if (line[0] == ';') return NULL; //If the line starts with a semi-colon its just a comment, so ignore it. - -// List *list = CreateList(); -// unsigned long lineLength = strlen(line); -// char* currentWord = malloc(lineLength + 1); -// int index = 0; - -// for (int i = 0; i < lineLength; i++) { -// if (line[i] == ' ' || line[i] == ',') { -// if (index > 0) { -// currentWord[index] = '\0'; -// PushListItem(currentWord, strlen(currentWord) + 1, list); -// } -// index = 0; -// continue; -// } - -// if (line[i] == ';' || i + 1 == lineLength) { -// if (index > 0) { -// if (line[i] != ';') { -// currentWord[index] = line[i]; -// index++; -// } -// currentWord[index] = '\0'; -// PushListItem(currentWord, strlen(currentWord) + 1, list); -// } -// break; -// } - -// currentWord[index] = line[i]; -// index++; -// } - -// return list; -// } \ No newline at end of file + GenerateTokensFromFile(args[1]); +} \ No newline at end of file diff --git a/src/tokenizer.c b/src/tokenizer.c index c9902fd..a8d4c1f 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -5,35 +5,30 @@ char* SplitOnBasicGrammar(char*, unsigned long *); void GetStringLiteral(char*, unsigned long, char**, unsigned long*); static char* string; -static unsigned long position; +static unsigned long current_position; char* GetToken(char* line) { if (line) { string = line; - position = 0; + current_position = 0; } - return SplitOnBasicGrammar(string, &position); + return SplitOnBasicGrammar(string, ¤t_position); } char* PeekNextToken() { if (!string) return NULL; if (strlen(string) == 0) return NULL; - unsigned long pos = position; + unsigned long pos = current_position; return SplitOnBasicGrammar(string, &pos); } char* SplitOnBasicGrammar(char* line, unsigned long *string_index) { - // if (line) { - // string = line; - // position = 0; - // } unsigned long length = strlen(string); char* token = calloc(1, length + 1); - //int parsing_string = 0; for (int i = 0; *string_index < length; i++, (*string_index)++) {