Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c95d739e16 | ||
|
|
21c2bf046e | ||
|
|
b810d7803c | ||
|
|
9e178ca3da | ||
|
|
82b2136a69 |
@@ -12,11 +12,18 @@
|
||||
|
||||
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
||||
|
||||
typedef struct __token_node {
|
||||
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;
|
||||
|
||||
typedef struct {
|
||||
@@ -26,10 +33,12 @@ typedef struct {
|
||||
} SymbolTable;
|
||||
|
||||
SymbolTable* CreateSymbolTable(void);
|
||||
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 SymbolResolved(const Symbol* symbol, const SymbolTable* table);
|
||||
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;
|
||||
|
||||
+93
-43
@@ -23,9 +23,12 @@ void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options);
|
||||
void ExpectRegister(void);
|
||||
Symbol* ExpectIdentifier(ExpectOptions options, int expectUnresolved);
|
||||
void ExpectCharacterClass(Symbol* symbol);
|
||||
void ExpectLineEndOrFileEnd(ExpectOptions options);
|
||||
Token* ExpectLineEndOrFileEnd(ExpectOptions options);
|
||||
Token* ExpectMathOperator(ExpectOptions options);
|
||||
Token* ExpectMathOperand(ExpectOptions options);
|
||||
|
||||
SymbolTable* SymbolsTable;
|
||||
TokenNode* ParseSymbolExpression(void);
|
||||
|
||||
//int HeapSize = 0;
|
||||
int PC = 0;
|
||||
@@ -56,31 +59,25 @@ void ParseTokens(TokenList* tokens, SymbolTable** symbols) {
|
||||
Symbol* symbol;
|
||||
int found = TryGetSymbol(t->Lemexe, SymbolsTable, &symbol);
|
||||
|
||||
if (found && SymbolResolved(symbol)) {
|
||||
if (found && SymbolResolved(symbol, SymbolsTable)) {
|
||||
fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!found) symbol = AddSymbolToTable(t->Lemexe, PC, SymbolsTable);
|
||||
// else
|
||||
// {
|
||||
symbol->Address = PC;
|
||||
//symbol->Type = RefPointer;
|
||||
//symbol->Token = t;
|
||||
//}
|
||||
|
||||
symbol->Address = PC;
|
||||
|
||||
RemoveCurrentToken(); //label
|
||||
|
||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||
|
||||
//symbol->Value.Mnemonic = ExpectMnemonic();
|
||||
symbol->Token = ExpectMnemonic();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "[Warning] Line %d: Syntax error, expected start of expression, got '%c' [%d].\n", t->LineNumber, t->Value.Punctuation, t->Class);
|
||||
exit(1);
|
||||
//IgnoreParserLine();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -100,30 +97,12 @@ void HandleAssemblerDirective() {
|
||||
|
||||
if (PeekToken()->Class == CharacterClass) {
|
||||
ExpectCharacterClass(symbol);
|
||||
|
||||
//symbol->Type = RefLiteral;
|
||||
}
|
||||
else if (PeekToken()->Class == NumberClass) {
|
||||
else if (PeekToken()->Class == NumberClass || PeekToken()->Class == IdentifierClass) {
|
||||
symbol->Length = 2;
|
||||
|
||||
RemoveCurrentToken(); //Clear the number token.
|
||||
|
||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||
|
||||
//symbol->Type = RefLiteral;
|
||||
symbol->ValueExpression = ParseSymbolExpression();
|
||||
}
|
||||
else if (PeekToken()->Class == IdentifierClass) {
|
||||
//symbol->Type = RefExpression;
|
||||
|
||||
symbol = ExpectIdentifier(RemoveExpected, 0);
|
||||
symbol->Length = 2;
|
||||
|
||||
//TODO: This needs to be expanded to handle expressions, a symbol to symbol reg is not allowed.
|
||||
|
||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||
}
|
||||
|
||||
//HeapSize += symbol->Length;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -430,22 +409,14 @@ Symbol* ExpectIdentifier(ExpectOptions options, int expectUnresolved) {
|
||||
|
||||
int found = TryGetSymbol(token->Lemexe, SymbolsTable, &symbol);
|
||||
|
||||
if (found && SymbolResolved(symbol) && expectUnresolved) {
|
||||
if (found && TryGetSymbolValue(symbol, SymbolsTable, NULL) && expectUnresolved) {
|
||||
fprintf(stderr, "[Line %d] Redefinition of symbol '%s'.\n", token->LineNumber, token->Lemexe);
|
||||
exit(4);
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
//symbol->Resolved = resolved || symbol->Resolved(symbol);
|
||||
symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
||||
}
|
||||
|
||||
if (!found) symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
||||
if (expectUnresolved) symbol->Token = token;
|
||||
|
||||
// else {
|
||||
// symbol = AddSymbolToTable(token->Lemexe, PC, SymbolsTable);
|
||||
// }
|
||||
|
||||
if (options & RemoveExpected) RemoveCurrentToken();
|
||||
if (options & ForwardParser) AdvanceParser();
|
||||
|
||||
@@ -461,6 +432,7 @@ void ExpectCharacterClass(Symbol* symbol) {
|
||||
}
|
||||
|
||||
symbol->Length = strlen(token->Lemexe);
|
||||
symbol->Token = token;
|
||||
|
||||
RemoveCurrentToken(); //Remove the string declared by this DB command.
|
||||
|
||||
@@ -489,10 +461,10 @@ void ExpectCharacterClass(Symbol* symbol) {
|
||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||
}
|
||||
|
||||
void ExpectLineEndOrFileEnd(ExpectOptions options) {
|
||||
Token* ExpectLineEndOrFileEnd(ExpectOptions options) {
|
||||
Token* token = PeekToken();
|
||||
|
||||
if (token->EndOfFile) return;
|
||||
if (token->EndOfFile) return token;
|
||||
|
||||
if (token->Class != PunctuationClass || token->Value.Punctuation != NewLine) {
|
||||
fprintf(stderr, "[Line %d] Syntax error, expected line break.\n", token->LineNumber);
|
||||
@@ -501,6 +473,8 @@ void ExpectLineEndOrFileEnd(ExpectOptions options) {
|
||||
|
||||
if (options & RemoveExpected) RemoveCurrentToken();
|
||||
if (options & ForwardParser) AdvanceParser();
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
void PrintSymbols(void) {
|
||||
@@ -508,8 +482,12 @@ void PrintSymbols(void) {
|
||||
printf("-----SYMBOLS-----\n");
|
||||
for(int i = 0; i < SymbolsTable->Size; i++) {
|
||||
Symbol* symbol = SymbolsTable->Symbols[i];
|
||||
unsigned short value = 0;
|
||||
int resolved = SymbolResolved(symbol, SymbolsTable);
|
||||
|
||||
printf("[%s] %s [Width: %d]", SymbolResolved(symbol) == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Length);
|
||||
if (resolved) TryGetSymbolValue(symbol, SymbolsTable, &value);
|
||||
|
||||
printf("[%s] %s [Value: %d]", resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, value);
|
||||
|
||||
if (symbol->Token && symbol->Token->Class == MnemonicClass)
|
||||
{
|
||||
@@ -556,3 +534,75 @@ void IgnoreParserLine(void) {
|
||||
void RemoveCurrentToken(void) {
|
||||
RemoveToken(CurrentToken, TokensList);
|
||||
}
|
||||
|
||||
TokenNode* ParseSymbolExpression(void) {
|
||||
TokenNode* root = CreateTokenNode(ExpectMathOperand(RemoveExpected));
|
||||
|
||||
while(!ParserAtEnd()) {
|
||||
if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == NewLine) {
|
||||
|
||||
ExpectPuncuation(NewLine, RemoveExpected);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
TokenNode* value = CreateTokenNode(ExpectMathOperand(RemoveExpected));
|
||||
TokenNode* operation = CreateTokenNode(ExpectMathOperator(RemoveExpected));
|
||||
|
||||
operation->Left = root;
|
||||
operation->Right = value;
|
||||
root = operation;
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
Token* ExpectMathOperator(ExpectOptions options) {
|
||||
if (ParserAtEnd()) {
|
||||
fprintf(stderr, "Syntax error, expected math operator but found unexpected end of file.\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
Token* current = PeekToken();
|
||||
|
||||
if (current->Class != PunctuationClass) {
|
||||
fprintf(stderr, "[Line %d] Syntax error, expected math operator.\n", current->LineNumber);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
switch (current->Value.Punctuation) {
|
||||
case Plus:
|
||||
case Minus:
|
||||
case Star:
|
||||
case Slash:
|
||||
case Power:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "[Line %d] Syntax error, expected math operator 2.\n", current->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (options & RemoveExpected) RemoveCurrentToken();
|
||||
if (options & ForwardParser) AdvanceParser();
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
Token* ExpectMathOperand(ExpectOptions options) {
|
||||
if (ParserAtEnd()) {
|
||||
fprintf(stderr, "Syntax error, expected math operand but found unexpected end of file.\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
Token* token = PeekToken();
|
||||
|
||||
if (token->Class != NumberClass && token->Class != IdentifierClass) {
|
||||
fprintf(stderr, "[Line %d] Syntax error, expected a number or identifier.\n", token->LineNumber);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
if (options & RemoveExpected) RemoveCurrentToken();
|
||||
if (options & ForwardParser) AdvanceParser();
|
||||
|
||||
return token;
|
||||
}
|
||||
@@ -328,6 +328,10 @@ int IsPunctuation(char c) {
|
||||
case '(':
|
||||
case ')':
|
||||
case ',':
|
||||
case '-':
|
||||
case '+':
|
||||
case '*':
|
||||
case '/':
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
@@ -366,6 +370,18 @@ Token* ParsePunctuation(char c) {
|
||||
case ',':
|
||||
punctuation = Comma;
|
||||
break;
|
||||
case '-':
|
||||
punctuation = Minus;
|
||||
break;
|
||||
case '+':
|
||||
punctuation = Plus;
|
||||
break;
|
||||
case '*':
|
||||
punctuation = Star;
|
||||
break;
|
||||
case '/':
|
||||
punctuation = Slash;
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
+97
-15
@@ -3,21 +3,16 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int SymbolResolved(Symbol* symbol) {
|
||||
int SymbolResolved(const Symbol* symbol, const SymbolTable* table) {
|
||||
if (!symbol) return 0;
|
||||
|
||||
return symbol->Token != NULL;
|
||||
const Token* token = symbol->Token;
|
||||
|
||||
// switch (symbol->Type) {
|
||||
// case RefLiteral:
|
||||
// return 1;
|
||||
// case RefExpression:
|
||||
// return 0;
|
||||
// case RefPointer:
|
||||
// return symbol->Value.Mnemonic != 0;
|
||||
// default:
|
||||
// return 0;
|
||||
// }
|
||||
if (!token) return 0;
|
||||
|
||||
if (token->Class == LabelClass || token->Class == CharacterClass || token->Class == MnemonicClass) return 1;
|
||||
|
||||
return TryGetSymbolValue(symbol, table, NULL);
|
||||
}
|
||||
|
||||
SymbolTable* CreateSymbolTable(void){
|
||||
@@ -42,7 +37,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 +53,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 +68,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++) {
|
||||
@@ -118,3 +113,90 @@ void FreeSymbol(Symbol* symbol) {
|
||||
|
||||
free(symbol);
|
||||
}
|
||||
|
||||
int TryGetTokenNodeValue(const TokenNode* node, const SymbolTable* table, unsigned short* value);
|
||||
unsigned short DoOp(unsigned short left, unsigned short right, TokenPunctuation op);
|
||||
|
||||
int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short* value) {
|
||||
if (!symbol || !symbol->Token || !table) return 0;
|
||||
|
||||
TokenNode* root = symbol->ValueExpression;
|
||||
Symbol* s = NULL;
|
||||
if (value) *value = 0;
|
||||
|
||||
if (!root || !root->Token) return 0;
|
||||
|
||||
switch (root->Token->Class) {
|
||||
case NumberClass:
|
||||
if (value) *value = root->Token->Value.Number;
|
||||
|
||||
return 1;
|
||||
case IdentifierClass:
|
||||
if (!TryGetSymbol(root->Token->Lemexe, table, &s)) return 0;
|
||||
|
||||
return TryGetTokenNodeValue(s->ValueExpression, table, value);
|
||||
case PunctuationClass:
|
||||
return TryGetTokenNodeValue(root, table, value);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int TryGetTokenNodeValue(const TokenNode* node, const SymbolTable* table, unsigned short* value) {
|
||||
const Token* token = node->Token;
|
||||
Symbol* symbol = { 0 };
|
||||
|
||||
if (token->Class == NumberClass) {
|
||||
if (value) *value = token->Value.Number;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (token->Class == IdentifierClass) {
|
||||
if (!TryGetSymbol(token->Lemexe, table, &symbol)) return 0;
|
||||
|
||||
return TryGetTokenNodeValue(symbol->ValueExpression, table, value);
|
||||
}
|
||||
|
||||
if (token->Class != PunctuationClass) return 0;
|
||||
|
||||
unsigned short left = 0;
|
||||
unsigned short right = 0;
|
||||
|
||||
if (!TryGetTokenNodeValue(node->Left, table, &left)) return 0;
|
||||
if (!TryGetTokenNodeValue(node->Right, table, &right)) return 0;
|
||||
|
||||
if (value) *value = DoOp(left, right, token->Value.Punctuation);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
TokenNode* CreateTokenNode(const Token* token){
|
||||
TokenNode* node = calloc(1, sizeof(TokenNode));
|
||||
|
||||
if (!node) {
|
||||
fprintf(stderr, "Failed to calloc memory for a TokenNode. %s.\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->Token = token;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
unsigned short DoOp(unsigned short left, unsigned short right, TokenPunctuation op) {
|
||||
switch (op) {
|
||||
case Plus:
|
||||
return left + right;
|
||||
case Minus:
|
||||
return left - right;
|
||||
case Star:
|
||||
return left * right;
|
||||
case Slash:
|
||||
return left / right;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -83,6 +83,4 @@ void RemoveToken(int index, TokenList* list) {
|
||||
list->size--;
|
||||
|
||||
list->content[list->size] = NULL;
|
||||
|
||||
FreeToken(token);
|
||||
}
|
||||
Reference in New Issue
Block a user