Build a quick draft of how the symbol value should be computed.
This commit is contained in:
@@ -13,16 +13,16 @@
|
||||
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
||||
|
||||
typedef struct __token_node {
|
||||
Token* Token;
|
||||
const Token* Token;
|
||||
struct __token_node* Left;
|
||||
struct __token_node* Right;
|
||||
} TokenNode;
|
||||
|
||||
typedef struct _symbol {
|
||||
char* Name;
|
||||
const char* Name;
|
||||
int Address;
|
||||
int Length;
|
||||
Token* Token;
|
||||
const Token* Token;
|
||||
TokenNode* ValueExpression;
|
||||
} Symbol;
|
||||
|
||||
@@ -33,12 +33,12 @@ typedef struct {
|
||||
} SymbolTable;
|
||||
|
||||
SymbolTable* CreateSymbolTable(void);
|
||||
TokenNode* CreateTokenNode(Token* token);
|
||||
int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol);
|
||||
Symbol* AddSymbolToTable(char* name, int address, SymbolTable* table);
|
||||
TokenNode* CreateTokenNode(const Token* token);
|
||||
int TryGetSymbol(const char* name, const SymbolTable* table, Symbol** outSymbol);
|
||||
Symbol* AddSymbolToTable(const char* name, int address, SymbolTable* table);
|
||||
void FreeSymbolTable(SymbolTable* table);
|
||||
void FreeSymbol(Symbol* symbol);
|
||||
int SymbolResolved(Symbol* symbol);
|
||||
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short** value);
|
||||
int SymbolResolved(const Symbol* symbol);
|
||||
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short* value);
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -50,7 +50,7 @@ typedef struct __token {
|
||||
Registers Register;
|
||||
Mnemonic Mnemonic;
|
||||
Directive Directive;
|
||||
int Number;
|
||||
unsigned short Number;
|
||||
} Value;
|
||||
|
||||
struct __token* Prev;
|
||||
|
||||
+39
-11
@@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int SymbolResolved(Symbol* symbol) {
|
||||
int SymbolResolved(const Symbol* symbol) {
|
||||
if (!symbol) return 0;
|
||||
|
||||
return symbol->Token != NULL;
|
||||
@@ -42,7 +42,7 @@ SymbolTable* CreateSymbolTable(void){
|
||||
return table;
|
||||
}
|
||||
|
||||
Symbol* CreateSymbol(char* name, int address) {
|
||||
Symbol* CreateSymbol(const char* name, int address) {
|
||||
Symbol* symbol = calloc(1, sizeof(Symbol));
|
||||
|
||||
if (!symbol) {
|
||||
@@ -58,7 +58,7 @@ Symbol* CreateSymbol(char* name, int address) {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) {
|
||||
int TryGetSymbol(const char* name, const SymbolTable* table, Symbol** outSymbol) {
|
||||
*outSymbol = NULL;
|
||||
|
||||
if (!name || !table) return 0;
|
||||
@@ -73,7 +73,7 @@ int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Symbol* AddSymbolToTable(char* name, int address, SymbolTable* table) {
|
||||
Symbol* AddSymbolToTable(const char* name, int address, SymbolTable* table) {
|
||||
//if (!name || !value || !table || length == 0) return NULL;
|
||||
|
||||
for(int i = 0; i < table->Size; i++) {
|
||||
@@ -119,24 +119,52 @@ void FreeSymbol(Symbol* symbol) {
|
||||
free(symbol);
|
||||
}
|
||||
|
||||
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short** value) {
|
||||
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short* value) {
|
||||
if (!symbol || !symbol->Token || !table) return 0;
|
||||
if (symbol->Token->Class == CharacterClass) return 0;
|
||||
|
||||
unsigned short acc = 0;
|
||||
Token* current = symbol->Token->Next;
|
||||
unsigned short left = 0;
|
||||
unsigned short right = 0;
|
||||
|
||||
TokenNode* root = symbol->ValueExpression;
|
||||
Symbol* s = NULL;
|
||||
|
||||
for(;;) {
|
||||
if (current->Class == PunctuationClass && current->Value.Punctuation == NewLine) break;
|
||||
Token* next = current->Next;
|
||||
if (!root || !root->Token) return 0;
|
||||
|
||||
switch (root->Token->Class) {
|
||||
case NumberClass:
|
||||
*value = root->Token->Value.Number;
|
||||
|
||||
return 1;
|
||||
break;
|
||||
case LabelClass:
|
||||
case IdentifierClass:
|
||||
if (!TryGetSymbol(root->Token->Lemexe, table, &s)) return 0;
|
||||
|
||||
if (!TryGetSymbolValue(s, table, value)) return 0;
|
||||
|
||||
return 1;
|
||||
break;
|
||||
case PunctuationClass: //TODO: ()'s need to be handled
|
||||
//check for math operator
|
||||
if (!TryGetSymbol(root->Left->Token->Lemexe, table, &s)) return 0;
|
||||
|
||||
if (!TryGetSymbolValue(s, table, &left)) return 0;
|
||||
|
||||
if (!TryGetSymbol(root->Right->Token->Lemexe, table, &s)) return 0;
|
||||
|
||||
if (!TryGetSymbolValue(s, table, &right)) return 0;
|
||||
|
||||
*value = left + right; //TODO: actually check what op we need to do.
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
TokenNode* CreateTokenNode(Token* token){
|
||||
TokenNode* CreateTokenNode(const Token* token){
|
||||
TokenNode* node = calloc(1, sizeof(TokenNode));
|
||||
|
||||
if (!node) {
|
||||
|
||||
Reference in New Issue
Block a user