From 4af2527806686233b193e4915c875a0a8353dc92 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 7 Aug 2023 01:18:01 -0500 Subject: [PATCH] Updated the structure of symbols and tokens to hopefully help make computing symbol values easier. --- includes/list.h | 22 ------------- includes/parser.h | 3 +- includes/scanner.h | 3 +- includes/symbols_table.h | 14 +-------- includes/token.h | 16 ++++++++-- src/list.c | 61 ------------------------------------ src/main.c | 4 +-- src/parser.c | 36 ++++++++++++--------- src/scanner.c | 18 +++++------ src/symbols_table.c | 24 +++++++------- src/token.c | 67 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 128 insertions(+), 140 deletions(-) delete mode 100644 includes/list.h delete mode 100644 src/list.c diff --git a/includes/list.h b/includes/list.h deleted file mode 100644 index 24490e6..0000000 --- a/includes/list.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef LIST_H -#define LIST_H - -#include -#include -#include -#include - -#define LISTDEFAULTSIZE 4 - -typedef struct { - void** content; - int size; - int capacity; -} List; - -List* CreateList(void); -int AddListItem(void *, List *); -void RemoveItem(int index, List * list); -void DestroyList(List*); - -#endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index c508224..62a6789 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,11 +6,10 @@ #include #include #include -#include "list.h" #include "token.h" #include "opcodes.h" #include "symbols_table.h" -void ParseTokens(List* tokens, SymbolTable** symbolsTable); +void ParseTokens(TokenList* tokens, SymbolTable** symbolsTable); #endif \ No newline at end of file diff --git a/includes/scanner.h b/includes/scanner.h index 5b59df1..5e5c8fd 100644 --- a/includes/scanner.h +++ b/includes/scanner.h @@ -7,9 +7,8 @@ #include #include "stdlib.h" #include "token.h" -#include "list.h" #include "opcodes.h" -List* GenerateTokenList(const char*); +TokenList* GenerateTokenList(const char*); #endif \ No newline at end of file diff --git a/includes/symbols_table.h b/includes/symbols_table.h index 9010354..84c353b 100644 --- a/includes/symbols_table.h +++ b/includes/symbols_table.h @@ -12,23 +12,11 @@ #define SYMBOLSTABLE_DEFAULT_CAPACITY 128 -typedef enum { - RefUnknown, - RefLiteral, - RefExpression, - RefPointer -} ReferenceType; - typedef struct _symbol { char* Name; int Address; int Length; - union { - Token* Mnemonic; - struct _symbol* Symbol; - unsigned short Number; - } Value; - ReferenceType Type; + Token* Token; } Symbol; typedef struct { diff --git a/includes/token.h b/includes/token.h index 4022b03..6d3345d 100644 --- a/includes/token.h +++ b/includes/token.h @@ -25,8 +25,6 @@ typedef enum { DB, Include, Byte - // Load, - // Store } Directive; typedef enum { @@ -41,7 +39,7 @@ typedef enum { AddressClass = LabelClass | IdentifierClass } TokenClass; -typedef struct { +typedef struct __token { char* Lemexe; TokenClass Class; int LineNumber; @@ -54,9 +52,21 @@ typedef struct { Directive Directive; int Number; } Value; + + struct __token* Prev; + struct __token* Next; } Token; +typedef struct { + Token** content; + int size; + int capacity; +} TokenList; + Token* CreateToken(int lineNumber, TokenClass tokenClass); +TokenList* CreateTokenList(void); +int AddToken(Token* token, TokenList* list); +void RemoveToken(int index, TokenList* list); void FreeToken(Token* token); #endif \ No newline at end of file diff --git a/src/list.c b/src/list.c deleted file mode 100644 index e3219a9..0000000 --- a/src/list.c +++ /dev/null @@ -1,61 +0,0 @@ -#include "../includes/list.h" -#include -#include - -List* CreateList() { - List *new = malloc(sizeof(List)); - - if (!new) { - fprintf(stderr, "Failed to malloc() for new new List.\n"); - return NULL; - } - - new->content = calloc(LISTDEFAULTSIZE, sizeof(void*)); - - if (!new->content) { - fprintf(stderr, "Failed to malloc() memory for List contents.\n"); - free(new); - return NULL; - } - - new->size = 0; - new->capacity = LISTDEFAULTSIZE; - - return new; -} - -int AddListItem(void *item, List* list) { - if (!list || !item) return -1; - - if (list->capacity < list->size + 1) { - void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2); - //Note: realloc will free list->root if it succeeds. - if (!ptr) { - fprintf(stderr, "Failed to resize array with realloc() (%d bytes).\n", list->capacity * 2); - return -1; - } - - list->content = ptr; - list->capacity *= 2; - } - - list->content[list->size] = item; - list->size++; - - return 0; -} - -void RemoveItem(int index, List * list) { - memmove(&list->content[index], &list->content[index + 1], (list->size - index) * sizeof(void*)); - - list->size--; - - list->content[list->size] = NULL; -} - -void DestroyList(List* list) { - if (!list) return; - - free(list->content); - free(list); -} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 957a23b..8e896ed 100644 --- a/src/main.c +++ b/src/main.c @@ -3,13 +3,13 @@ #include #include #include -#include "../includes/list.h" +#include "../includes/token.h" #include "../includes/futil.h" #include "../includes/scanner.h" #include "../includes/parser.h" const char* MagicStartName = "__start"; -List* LIST; +TokenList* LIST; SymbolTable* Symbols = NULL; unsigned char mem[128] = {0}; diff --git a/src/parser.c b/src/parser.c index 50054e2..3179817 100644 --- a/src/parser.c +++ b/src/parser.c @@ -7,7 +7,7 @@ typedef enum { NoOptions = 0, ForwardParser = 1, RemoveExpected = 2 } ExpectOptions; -List* TokensList; +TokenList* TokensList; int CurrentToken = 0; void PrintSymbols(void); @@ -30,7 +30,7 @@ SymbolTable* SymbolsTable; //int HeapSize = 0; int PC = 0; -void ParseTokens(List* tokens, SymbolTable** symbols) { +void ParseTokens(TokenList* tokens, SymbolTable** symbols) { if (!tokens) return; if (!symbols) return; @@ -65,14 +65,16 @@ void ParseTokens(List* tokens, SymbolTable** symbols) { // else // { symbol->Address = PC; - symbol->Type = RefPointer; + //symbol->Type = RefPointer; + //symbol->Token = t; //} RemoveCurrentToken(); //label ExpectLineEndOrFileEnd(RemoveExpected); - symbol->Value.Mnemonic = ExpectMnemonic(); + //symbol->Value.Mnemonic = ExpectMnemonic(); + symbol->Token = ExpectMnemonic(); } break; default: @@ -99,7 +101,7 @@ void HandleAssemblerDirective() { if (PeekToken()->Class == CharacterClass) { ExpectCharacterClass(symbol); - symbol->Type = RefLiteral; + //symbol->Type = RefLiteral; } else if (PeekToken()->Class == NumberClass) { symbol->Length = 2; @@ -108,10 +110,10 @@ void HandleAssemblerDirective() { ExpectLineEndOrFileEnd(RemoveExpected); - symbol->Type = RefLiteral; + //symbol->Type = RefLiteral; } else if (PeekToken()->Class == IdentifierClass) { - symbol->Type = RefExpression; + //symbol->Type = RefExpression; symbol = ExpectIdentifier(RemoveExpected, 0); symbol->Length = 2; @@ -437,6 +439,9 @@ Symbol* ExpectIdentifier(ExpectOptions options, int expectUnresolved) { //symbol->Resolved = resolved || symbol->Resolved(symbol); symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable); } + + if (expectUnresolved) symbol->Token = token; + // else { // symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable); // } @@ -504,13 +509,16 @@ void PrintSymbols(void) { for(int i = 0; i < SymbolsTable->Size; i++) { Symbol* symbol = SymbolsTable->Symbols[i]; - if (symbol->Type != RefPointer) - printf("[%s] %s [%d] [Width: %d] <%d>", SymbolResolved(symbol) == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length, symbol->Type); - else { - GetMnemonicText(symbol->Value.Mnemonic->Value.Mnemonic, mn); - printf("[%s] %s [%d] [Width: %d] -> [%s] Line %d <%d>", SymbolResolved(symbol) == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length, mn, symbol->Value.Mnemonic->LineNumber, symbol->Type); + printf("[%s] %s [Width: %d]", SymbolResolved(symbol) == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Length); + if (symbol->Token && symbol->Token->Class == MnemonicClass) + { + GetMnemonicText(symbol->Token->Value.Mnemonic, mn); + printf(" -> [%s]", mn); } + + if (symbol->Token) printf(" Line %d", symbol->Token->LineNumber); + printf("\n"); } printf("-----SYMBOLS-----\n"); @@ -546,7 +554,5 @@ void IgnoreParserLine(void) { } void RemoveCurrentToken(void) { - RemoveItem(CurrentToken, TokensList); - - //if (CurrentToken > 0) CurrentToken--; + RemoveToken(CurrentToken, TokensList); } \ No newline at end of file diff --git a/src/scanner.c b/src/scanner.c index da11532..8dd8346 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -21,8 +21,8 @@ Token* ParseNumber(void); Token* ParsePunctuation(char); void IgnoreLine(void); -List* GenerateTokenList(const char* source) { - List* tokens = CreateList(); +TokenList* GenerateTokenList(const char* source) { + TokenList* tokens = CreateTokenList(); Token* token = NULL; if (!tokens) return NULL; @@ -54,7 +54,7 @@ List* GenerateTokenList(const char* source) { token = CreateToken(Line, PunctuationClass); token->Value.Punctuation = NewLine; - AddListItem(token, tokens); + AddToken(token, tokens); AdvanceScanner(); Line++; break; @@ -69,28 +69,28 @@ List* GenerateTokenList(const char* source) { break; case '.': //directive like ".include" or ".db" token = ParseDirective(); - if (token) AddListItem(token, tokens); + if (token) AddToken(token, tokens); break; case '"': token = ParseString(); - if (token) AddListItem(token, tokens); + if (token) AddToken(token, tokens); break; default: if (isdigit(c)) { - AddListItem(ParseNumber(), tokens); + AddToken(ParseNumber(), tokens); break; } if (IsPunctuation(c)) { AdvanceScanner(); - AddListItem(ParsePunctuation(c), tokens); + AddToken(ParsePunctuation(c), tokens); break; } token = ParseIdentifier(); - if (token) AddListItem(token, tokens); + if (token) AddToken(token, tokens); break; } @@ -111,7 +111,7 @@ List* GenerateTokenList(const char* source) { token->EndOfFile = 1; - AddListItem(token, tokens); + AddToken(token, tokens); return tokens; } diff --git a/src/symbols_table.c b/src/symbols_table.c index f697d7e..d9ac537 100644 --- a/src/symbols_table.c +++ b/src/symbols_table.c @@ -6,16 +6,18 @@ int SymbolResolved(Symbol* symbol) { if (!symbol) return 0; - switch (symbol->Type) { - case RefLiteral: - return 1; - case RefExpression: - return 0; - case RefPointer: - return symbol->Value.Mnemonic != 0; - default: - return 0; - } + return symbol->Token != NULL; + + // switch (symbol->Type) { + // case RefLiteral: + // return 1; + // case RefExpression: + // return 0; + // case RefPointer: + // return symbol->Value.Mnemonic != 0; + // default: + // return 0; + // } } SymbolTable* CreateSymbolTable(void){ @@ -51,7 +53,7 @@ Symbol* CreateSymbol(char* name, int address) { symbol->Address = address; symbol->Name = name; - symbol->Type = RefUnknown; + //symbol->Type = RefUnknown; return symbol; } diff --git a/src/token.c b/src/token.c index bd9c49f..c95557b 100644 --- a/src/token.c +++ b/src/token.c @@ -1,5 +1,7 @@ #include "../includes/token.h" +#define LISTDEFAULTSIZE 32 + Token* CreateToken(int lineNumber, TokenClass tokenClass) { Token* token = calloc(1, sizeof(Token)); @@ -18,4 +20,69 @@ void FreeToken(Token* token) { if (!token) return; free(token); +} + +TokenList* CreateTokenList(void) { + TokenList *new = malloc(sizeof(TokenList)); + + if (!new) { + fprintf(stderr, "Failed to malloc() for new new List.\n"); + return NULL; + } + + new->content = calloc(LISTDEFAULTSIZE, sizeof(void*)); + + if (!new->content) { + fprintf(stderr, "Failed to malloc() memory for List contents.\n"); + free(new); + return NULL; + } + + new->size = 0; + new->capacity = LISTDEFAULTSIZE; + + return new; +} + +int AddToken(Token* token, TokenList* list) { + if (!list || !token) return 0; + + if (list->capacity < list->size + 1) { + void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2); + //Note: realloc will free list->root if it succeeds. + if (!ptr) { + fprintf(stderr, "Failed to resize array with realloc() (%d bytes).\n", list->capacity * 2); + return 0; + } + + list->content = ptr; + list->capacity *= 2; + } + + if (list->size > 0) + { + Token* prev = list->content[list->size - 1]; + + token->Prev = prev; + prev->Next = token; + } + + list->content[list->size] = token; + list->size++; + + return 1; +} + +void RemoveToken(int index, TokenList* list) { + Token* token = list->content[index]; + + if (token->Prev) token->Prev->Next = token->Next; + + memmove(&list->content[index], &list->content[index + 1], (list->size - index) * sizeof(Token*)); + + list->size--; + + list->content[list->size] = NULL; + + FreeToken(token); } \ No newline at end of file