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
|
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
||||||
|
|
||||||
typedef struct __token_node {
|
typedef struct __token_node {
|
||||||
Token* Token;
|
const Token* Token;
|
||||||
struct __token_node* Left;
|
struct __token_node* Left;
|
||||||
struct __token_node* Right;
|
struct __token_node* Right;
|
||||||
} TokenNode;
|
} TokenNode;
|
||||||
|
|
||||||
typedef struct _symbol {
|
typedef struct _symbol {
|
||||||
char* Name;
|
const char* Name;
|
||||||
int Address;
|
int Address;
|
||||||
int Length;
|
int Length;
|
||||||
Token* Token;
|
const Token* Token;
|
||||||
TokenNode* ValueExpression;
|
TokenNode* ValueExpression;
|
||||||
} Symbol;
|
} Symbol;
|
||||||
|
|
||||||
@@ -33,12 +33,12 @@ typedef struct {
|
|||||||
} SymbolTable;
|
} SymbolTable;
|
||||||
|
|
||||||
SymbolTable* CreateSymbolTable(void);
|
SymbolTable* CreateSymbolTable(void);
|
||||||
TokenNode* CreateTokenNode(Token* token);
|
TokenNode* CreateTokenNode(const Token* token);
|
||||||
int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol);
|
int TryGetSymbol(const char* name, const SymbolTable* table, Symbol** outSymbol);
|
||||||
Symbol* AddSymbolToTable(char* name, int address, SymbolTable* table);
|
Symbol* AddSymbolToTable(const char* name, int address, SymbolTable* table);
|
||||||
void FreeSymbolTable(SymbolTable* table);
|
void FreeSymbolTable(SymbolTable* table);
|
||||||
void FreeSymbol(Symbol* symbol);
|
void FreeSymbol(Symbol* symbol);
|
||||||
int SymbolResolved(Symbol* symbol);
|
int SymbolResolved(const Symbol* symbol);
|
||||||
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short** value);
|
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short* value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+1
-1
@@ -50,7 +50,7 @@ typedef struct __token {
|
|||||||
Registers Register;
|
Registers Register;
|
||||||
Mnemonic Mnemonic;
|
Mnemonic Mnemonic;
|
||||||
Directive Directive;
|
Directive Directive;
|
||||||
int Number;
|
unsigned short Number;
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
struct __token* Prev;
|
struct __token* Prev;
|
||||||
|
|||||||
+39
-11
@@ -3,7 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int SymbolResolved(Symbol* symbol) {
|
int SymbolResolved(const Symbol* symbol) {
|
||||||
if (!symbol) return 0;
|
if (!symbol) return 0;
|
||||||
|
|
||||||
return symbol->Token != NULL;
|
return symbol->Token != NULL;
|
||||||
@@ -42,7 +42,7 @@ SymbolTable* CreateSymbolTable(void){
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
Symbol* CreateSymbol(char* name, int address) {
|
Symbol* CreateSymbol(const char* name, int address) {
|
||||||
Symbol* symbol = calloc(1, sizeof(Symbol));
|
Symbol* symbol = calloc(1, sizeof(Symbol));
|
||||||
|
|
||||||
if (!symbol) {
|
if (!symbol) {
|
||||||
@@ -58,7 +58,7 @@ Symbol* CreateSymbol(char* name, int address) {
|
|||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) {
|
int TryGetSymbol(const char* name, const SymbolTable* table, Symbol** outSymbol) {
|
||||||
*outSymbol = NULL;
|
*outSymbol = NULL;
|
||||||
|
|
||||||
if (!name || !table) return 0;
|
if (!name || !table) return 0;
|
||||||
@@ -73,7 +73,7 @@ int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) {
|
|||||||
return 0;
|
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;
|
//if (!name || !value || !table || length == 0) return NULL;
|
||||||
|
|
||||||
for(int i = 0; i < table->Size; i++) {
|
for(int i = 0; i < table->Size; i++) {
|
||||||
@@ -119,24 +119,52 @@ void FreeSymbol(Symbol* symbol) {
|
|||||||
free(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 || !symbol->Token || !table) return 0;
|
||||||
if (symbol->Token->Class == CharacterClass) return 0;
|
if (symbol->Token->Class == CharacterClass) return 0;
|
||||||
|
|
||||||
unsigned short acc = 0;
|
unsigned short left = 0;
|
||||||
Token* current = symbol->Token->Next;
|
unsigned short right = 0;
|
||||||
|
|
||||||
for(;;) {
|
TokenNode* root = symbol->ValueExpression;
|
||||||
if (current->Class == PunctuationClass && current->Value.Punctuation == NewLine) break;
|
Symbol* s = NULL;
|
||||||
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenNode* CreateTokenNode(Token* token){
|
TokenNode* CreateTokenNode(const Token* token){
|
||||||
TokenNode* node = calloc(1, sizeof(TokenNode));
|
TokenNode* node = calloc(1, sizeof(TokenNode));
|
||||||
|
|
||||||
if (!node) {
|
if (!node) {
|
||||||
|
|||||||
Reference in New Issue
Block a user