Updated the structure of symbols and tokens to hopefully help make computing symbol values easier.
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
#ifndef LIST_H
|
||||
#define LIST_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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
|
||||
+1
-2
@@ -6,11 +6,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#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
|
||||
+1
-2
@@ -7,9 +7,8 @@
|
||||
#include <errno.h>
|
||||
#include "stdlib.h"
|
||||
#include "token.h"
|
||||
#include "list.h"
|
||||
#include "opcodes.h"
|
||||
|
||||
List* GenerateTokenList(const char*);
|
||||
TokenList* GenerateTokenList(const char*);
|
||||
|
||||
#endif
|
||||
@@ -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 {
|
||||
|
||||
+13
-3
@@ -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
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
#include "../includes/list.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
||||
+2
-2
@@ -3,13 +3,13 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sysexits.h>
|
||||
#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};
|
||||
|
||||
+21
-15
@@ -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);
|
||||
}
|
||||
+9
-9
@@ -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;
|
||||
}
|
||||
|
||||
+13
-11
@@ -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;
|
||||
}
|
||||
|
||||
+67
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user