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 <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "list.h"
|
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
#include "opcodes.h"
|
#include "opcodes.h"
|
||||||
#include "symbols_table.h"
|
#include "symbols_table.h"
|
||||||
|
|
||||||
void ParseTokens(List* tokens, SymbolTable** symbolsTable);
|
void ParseTokens(TokenList* tokens, SymbolTable** symbolsTable);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+1
-2
@@ -7,9 +7,8 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
#include "list.h"
|
|
||||||
#include "opcodes.h"
|
#include "opcodes.h"
|
||||||
|
|
||||||
List* GenerateTokenList(const char*);
|
TokenList* GenerateTokenList(const char*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -12,23 +12,11 @@
|
|||||||
|
|
||||||
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
RefUnknown,
|
|
||||||
RefLiteral,
|
|
||||||
RefExpression,
|
|
||||||
RefPointer
|
|
||||||
} ReferenceType;
|
|
||||||
|
|
||||||
typedef struct _symbol {
|
typedef struct _symbol {
|
||||||
char* Name;
|
char* Name;
|
||||||
int Address;
|
int Address;
|
||||||
int Length;
|
int Length;
|
||||||
union {
|
Token* Token;
|
||||||
Token* Mnemonic;
|
|
||||||
struct _symbol* Symbol;
|
|
||||||
unsigned short Number;
|
|
||||||
} Value;
|
|
||||||
ReferenceType Type;
|
|
||||||
} Symbol;
|
} Symbol;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+13
-3
@@ -25,8 +25,6 @@ typedef enum {
|
|||||||
DB,
|
DB,
|
||||||
Include,
|
Include,
|
||||||
Byte
|
Byte
|
||||||
// Load,
|
|
||||||
// Store
|
|
||||||
} Directive;
|
} Directive;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@@ -41,7 +39,7 @@ typedef enum {
|
|||||||
AddressClass = LabelClass | IdentifierClass
|
AddressClass = LabelClass | IdentifierClass
|
||||||
} TokenClass;
|
} TokenClass;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct __token {
|
||||||
char* Lemexe;
|
char* Lemexe;
|
||||||
TokenClass Class;
|
TokenClass Class;
|
||||||
int LineNumber;
|
int LineNumber;
|
||||||
@@ -54,9 +52,21 @@ typedef struct {
|
|||||||
Directive Directive;
|
Directive Directive;
|
||||||
int Number;
|
int Number;
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
|
struct __token* Prev;
|
||||||
|
struct __token* Next;
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Token** content;
|
||||||
|
int size;
|
||||||
|
int capacity;
|
||||||
|
} TokenList;
|
||||||
|
|
||||||
Token* CreateToken(int lineNumber, TokenClass tokenClass);
|
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);
|
void FreeToken(Token* token);
|
||||||
|
|
||||||
#endif
|
#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 <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sysexits.h>
|
#include <sysexits.h>
|
||||||
#include "../includes/list.h"
|
#include "../includes/token.h"
|
||||||
#include "../includes/futil.h"
|
#include "../includes/futil.h"
|
||||||
#include "../includes/scanner.h"
|
#include "../includes/scanner.h"
|
||||||
#include "../includes/parser.h"
|
#include "../includes/parser.h"
|
||||||
|
|
||||||
const char* MagicStartName = "__start";
|
const char* MagicStartName = "__start";
|
||||||
List* LIST;
|
TokenList* LIST;
|
||||||
SymbolTable* Symbols = NULL;
|
SymbolTable* Symbols = NULL;
|
||||||
|
|
||||||
unsigned char mem[128] = {0};
|
unsigned char mem[128] = {0};
|
||||||
|
|||||||
+21
-15
@@ -7,7 +7,7 @@ typedef enum {
|
|||||||
NoOptions = 0, ForwardParser = 1, RemoveExpected = 2
|
NoOptions = 0, ForwardParser = 1, RemoveExpected = 2
|
||||||
} ExpectOptions;
|
} ExpectOptions;
|
||||||
|
|
||||||
List* TokensList;
|
TokenList* TokensList;
|
||||||
int CurrentToken = 0;
|
int CurrentToken = 0;
|
||||||
|
|
||||||
void PrintSymbols(void);
|
void PrintSymbols(void);
|
||||||
@@ -30,7 +30,7 @@ SymbolTable* SymbolsTable;
|
|||||||
//int HeapSize = 0;
|
//int HeapSize = 0;
|
||||||
int PC = 0;
|
int PC = 0;
|
||||||
|
|
||||||
void ParseTokens(List* tokens, SymbolTable** symbols) {
|
void ParseTokens(TokenList* tokens, SymbolTable** symbols) {
|
||||||
if (!tokens) return;
|
if (!tokens) return;
|
||||||
if (!symbols) return;
|
if (!symbols) return;
|
||||||
|
|
||||||
@@ -65,14 +65,16 @@ void ParseTokens(List* tokens, SymbolTable** symbols) {
|
|||||||
// else
|
// else
|
||||||
// {
|
// {
|
||||||
symbol->Address = PC;
|
symbol->Address = PC;
|
||||||
symbol->Type = RefPointer;
|
//symbol->Type = RefPointer;
|
||||||
|
//symbol->Token = t;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
RemoveCurrentToken(); //label
|
RemoveCurrentToken(); //label
|
||||||
|
|
||||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||||
|
|
||||||
symbol->Value.Mnemonic = ExpectMnemonic();
|
//symbol->Value.Mnemonic = ExpectMnemonic();
|
||||||
|
symbol->Token = ExpectMnemonic();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -99,7 +101,7 @@ void HandleAssemblerDirective() {
|
|||||||
if (PeekToken()->Class == CharacterClass) {
|
if (PeekToken()->Class == CharacterClass) {
|
||||||
ExpectCharacterClass(symbol);
|
ExpectCharacterClass(symbol);
|
||||||
|
|
||||||
symbol->Type = RefLiteral;
|
//symbol->Type = RefLiteral;
|
||||||
}
|
}
|
||||||
else if (PeekToken()->Class == NumberClass) {
|
else if (PeekToken()->Class == NumberClass) {
|
||||||
symbol->Length = 2;
|
symbol->Length = 2;
|
||||||
@@ -108,10 +110,10 @@ void HandleAssemblerDirective() {
|
|||||||
|
|
||||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||||
|
|
||||||
symbol->Type = RefLiteral;
|
//symbol->Type = RefLiteral;
|
||||||
}
|
}
|
||||||
else if (PeekToken()->Class == IdentifierClass) {
|
else if (PeekToken()->Class == IdentifierClass) {
|
||||||
symbol->Type = RefExpression;
|
//symbol->Type = RefExpression;
|
||||||
|
|
||||||
symbol = ExpectIdentifier(RemoveExpected, 0);
|
symbol = ExpectIdentifier(RemoveExpected, 0);
|
||||||
symbol->Length = 2;
|
symbol->Length = 2;
|
||||||
@@ -437,6 +439,9 @@ Symbol* ExpectIdentifier(ExpectOptions options, int expectUnresolved) {
|
|||||||
//symbol->Resolved = resolved || symbol->Resolved(symbol);
|
//symbol->Resolved = resolved || symbol->Resolved(symbol);
|
||||||
symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (expectUnresolved) symbol->Token = token;
|
||||||
|
|
||||||
// else {
|
// else {
|
||||||
// symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
// symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
||||||
// }
|
// }
|
||||||
@@ -504,13 +509,16 @@ void PrintSymbols(void) {
|
|||||||
for(int i = 0; i < SymbolsTable->Size; i++) {
|
for(int i = 0; i < SymbolsTable->Size; i++) {
|
||||||
Symbol* symbol = SymbolsTable->Symbols[i];
|
Symbol* symbol = SymbolsTable->Symbols[i];
|
||||||
|
|
||||||
if (symbol->Type != RefPointer)
|
printf("[%s] %s [Width: %d]", SymbolResolved(symbol) == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Length);
|
||||||
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);
|
|
||||||
|
|
||||||
|
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("\n");
|
||||||
}
|
}
|
||||||
printf("-----SYMBOLS-----\n");
|
printf("-----SYMBOLS-----\n");
|
||||||
@@ -546,7 +554,5 @@ void IgnoreParserLine(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RemoveCurrentToken(void) {
|
void RemoveCurrentToken(void) {
|
||||||
RemoveItem(CurrentToken, TokensList);
|
RemoveToken(CurrentToken, TokensList);
|
||||||
|
|
||||||
//if (CurrentToken > 0) CurrentToken--;
|
|
||||||
}
|
}
|
||||||
+9
-9
@@ -21,8 +21,8 @@ Token* ParseNumber(void);
|
|||||||
Token* ParsePunctuation(char);
|
Token* ParsePunctuation(char);
|
||||||
void IgnoreLine(void);
|
void IgnoreLine(void);
|
||||||
|
|
||||||
List* GenerateTokenList(const char* source) {
|
TokenList* GenerateTokenList(const char* source) {
|
||||||
List* tokens = CreateList();
|
TokenList* tokens = CreateTokenList();
|
||||||
Token* token = NULL;
|
Token* token = NULL;
|
||||||
|
|
||||||
if (!tokens) return NULL;
|
if (!tokens) return NULL;
|
||||||
@@ -54,7 +54,7 @@ List* GenerateTokenList(const char* source) {
|
|||||||
|
|
||||||
token = CreateToken(Line, PunctuationClass);
|
token = CreateToken(Line, PunctuationClass);
|
||||||
token->Value.Punctuation = NewLine;
|
token->Value.Punctuation = NewLine;
|
||||||
AddListItem(token, tokens);
|
AddToken(token, tokens);
|
||||||
AdvanceScanner();
|
AdvanceScanner();
|
||||||
Line++;
|
Line++;
|
||||||
break;
|
break;
|
||||||
@@ -69,28 +69,28 @@ List* GenerateTokenList(const char* source) {
|
|||||||
break;
|
break;
|
||||||
case '.': //directive like ".include" or ".db"
|
case '.': //directive like ".include" or ".db"
|
||||||
token = ParseDirective();
|
token = ParseDirective();
|
||||||
if (token) AddListItem(token, tokens);
|
if (token) AddToken(token, tokens);
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
token = ParseString();
|
token = ParseString();
|
||||||
if (token) AddListItem(token, tokens);
|
if (token) AddToken(token, tokens);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (isdigit(c)) {
|
if (isdigit(c)) {
|
||||||
AddListItem(ParseNumber(), tokens);
|
AddToken(ParseNumber(), tokens);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsPunctuation(c)) {
|
if (IsPunctuation(c)) {
|
||||||
AdvanceScanner();
|
AdvanceScanner();
|
||||||
AddListItem(ParsePunctuation(c), tokens);
|
AddToken(ParsePunctuation(c), tokens);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
token = ParseIdentifier();
|
token = ParseIdentifier();
|
||||||
|
|
||||||
if (token) AddListItem(token, tokens);
|
if (token) AddToken(token, tokens);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ List* GenerateTokenList(const char* source) {
|
|||||||
|
|
||||||
token->EndOfFile = 1;
|
token->EndOfFile = 1;
|
||||||
|
|
||||||
AddListItem(token, tokens);
|
AddToken(token, tokens);
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-11
@@ -6,16 +6,18 @@
|
|||||||
int SymbolResolved(Symbol* symbol) {
|
int SymbolResolved(Symbol* symbol) {
|
||||||
if (!symbol) return 0;
|
if (!symbol) return 0;
|
||||||
|
|
||||||
switch (symbol->Type) {
|
return symbol->Token != NULL;
|
||||||
case RefLiteral:
|
|
||||||
return 1;
|
// switch (symbol->Type) {
|
||||||
case RefExpression:
|
// case RefLiteral:
|
||||||
return 0;
|
// return 1;
|
||||||
case RefPointer:
|
// case RefExpression:
|
||||||
return symbol->Value.Mnemonic != 0;
|
// return 0;
|
||||||
default:
|
// case RefPointer:
|
||||||
return 0;
|
// return symbol->Value.Mnemonic != 0;
|
||||||
}
|
// default:
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
SymbolTable* CreateSymbolTable(void){
|
SymbolTable* CreateSymbolTable(void){
|
||||||
@@ -51,7 +53,7 @@ Symbol* CreateSymbol(char* name, int address) {
|
|||||||
|
|
||||||
symbol->Address = address;
|
symbol->Address = address;
|
||||||
symbol->Name = name;
|
symbol->Name = name;
|
||||||
symbol->Type = RefUnknown;
|
//symbol->Type = RefUnknown;
|
||||||
|
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|||||||
+67
@@ -1,5 +1,7 @@
|
|||||||
#include "../includes/token.h"
|
#include "../includes/token.h"
|
||||||
|
|
||||||
|
#define LISTDEFAULTSIZE 32
|
||||||
|
|
||||||
Token* CreateToken(int lineNumber, TokenClass tokenClass) {
|
Token* CreateToken(int lineNumber, TokenClass tokenClass) {
|
||||||
Token* token = calloc(1, sizeof(Token));
|
Token* token = calloc(1, sizeof(Token));
|
||||||
|
|
||||||
@@ -19,3 +21,68 @@ void FreeToken(Token* token) {
|
|||||||
|
|
||||||
free(token);
|
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