Added a length attribute to the symbol.
This commit is contained in:
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
typedef struct {
|
||||
Token* token;
|
||||
//unsigned char terminatingByte;
|
||||
int length;
|
||||
int address;
|
||||
int resolved;
|
||||
} Symbol;
|
||||
|
||||
+20
-43
@@ -4,20 +4,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct intr {
|
||||
TokenType struction;
|
||||
unsigned int parameter1;
|
||||
unsigned int parameter2;
|
||||
};
|
||||
|
||||
Symbol* CreateSymbol(Token* token, int address, int resolved);
|
||||
Symbol* CreateSymbol(Token* token, int length, int address, int resolved);
|
||||
const List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
//int HeapStart = 4; // zero indexed, used for declared variables.
|
||||
//int BinaryEnd = 4; // Zero indexed (binary starts with a 4 byte header)
|
||||
|
||||
IROpcode* CreateIROpcode(TokenType mnemonic);
|
||||
Symbol* AddSymbol(Token* token, int address, int resolved);
|
||||
Symbol* AddSymbol(Token* token, int length, int address, int resolved);
|
||||
void PrintSymbols(void);
|
||||
void HandleOperation(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
@@ -25,7 +19,6 @@ void AdvanceParser(void);
|
||||
Token* PeekToken(void);
|
||||
int ParserAtEnd(void);
|
||||
int Expect(int, ...);
|
||||
int ExpectTokenClass(int, ...);
|
||||
Symbol* GetSymbol(char*);
|
||||
//List* SymbolsTable;
|
||||
unsigned int HeapTop = 4;
|
||||
@@ -58,7 +51,7 @@ IRState* ParseTokens(List* tokens) {
|
||||
case Address:
|
||||
if (t->type == LABEL) {
|
||||
printf("%s: \n", t->lexeme);
|
||||
AddSymbol(t, ProgramCounter, 1);
|
||||
AddSymbol(t, 2, ProgramCounter, 1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -73,7 +66,7 @@ IRState* ParseTokens(List* tokens) {
|
||||
//DestroyList(SymbolsTable);
|
||||
|
||||
//memcpy(&Heap[HeapTop], Memory, ProgramCounter);
|
||||
int totalSize = HeapTop + ProgramCounter;
|
||||
//int totalSize = HeapTop + ProgramCounter;
|
||||
// Heap[0] = 2 >> HeapTop & 0xFF;
|
||||
// Heap[1] = HeapTop & 0xFF;
|
||||
// Heap[2] = 2 >> totalSize & 0xFF;
|
||||
@@ -102,7 +95,7 @@ void HandleAssemblerDirective(void) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Symbol* symbol = AddSymbol(identifier, HeapTop, 1);
|
||||
Symbol* symbol = AddSymbol(identifier, 0, HeapTop, 1);
|
||||
|
||||
identifier = PeekToken();
|
||||
|
||||
@@ -117,6 +110,7 @@ void HandleAssemblerDirective(void) {
|
||||
|
||||
//ProgramCounter += strlen(PeekToken()->lexeme);
|
||||
HeapTop += strlen(identifier->lexeme);
|
||||
symbol->length = strlen(identifier->lexeme);
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
@@ -129,10 +123,11 @@ void HandleAssemblerDirective(void) {
|
||||
}
|
||||
else {
|
||||
//Hideous...
|
||||
int oldLength = strlen(identifier->lexeme);
|
||||
identifier->lexeme = realloc(identifier->lexeme, oldLength + 2);
|
||||
identifier->value = identifier->lexeme;
|
||||
identifier->lexeme[oldLength + 1] = '\0';//*PeekToken()->lexeme & 0x0F;
|
||||
// int oldLength = strlen(identifier->lexeme);
|
||||
// identifier->lexeme = realloc(identifier->lexeme, oldLength + 2);
|
||||
// identifier->value = identifier->lexeme;
|
||||
// identifier->lexeme[oldLength + 1] = '\0';//*PeekToken()->lexeme & 0x0F;
|
||||
symbol->length++; //Basically, read in the NULL byte at the end of the string.
|
||||
HeapTop++;
|
||||
return;
|
||||
}
|
||||
@@ -169,7 +164,7 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
|
||||
Symbol* symbol = GetSymbol(token->lexeme);
|
||||
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(token, 0, 0);
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(token, 2, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;//CreateSymbol(token, symbol->address, symbol->resolved);
|
||||
}
|
||||
else {
|
||||
@@ -194,7 +189,7 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
// fprintf(stderr, "[Error] Line %d: %s is undefined.\n", token->line, token->lexeme);
|
||||
// exit(1);
|
||||
// }
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(token, 0, 0);//CreateSymbol(token, 0, 0);
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(token, 2, 0, 0);//CreateSymbol(token, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;//CreateSymbol(token, symbol->address, symbol->resolved);
|
||||
|
||||
// Memory[ProgramCounter - 1] |= 0x10;//0b00010000;
|
||||
@@ -274,7 +269,7 @@ void HandleOperation(void) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||
opcode->ParameterOneType = Address;
|
||||
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 0, 0);
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 2, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;
|
||||
// if (!symbol) {
|
||||
// fprintf(stderr, "[Error] %s is undefined.\n", PeekToken()->lexeme);
|
||||
@@ -346,7 +341,7 @@ void HandleOperation(void) {
|
||||
// fprintf(stderr, "[Error] Line %d: %s is undefined.\n", PeekToken()->line, PeekToken()->lexeme);
|
||||
// exit(1);
|
||||
// }
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 0, 0);
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 2, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;
|
||||
//Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
||||
//Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||
@@ -371,7 +366,7 @@ void HandleOperation(void) {
|
||||
// fprintf(stderr, "[Error] Line %d: %s is undefined.\n", PeekToken()->line, PeekToken()->lexeme);
|
||||
// exit(1);
|
||||
// }
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 0, 0);
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 2, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;
|
||||
|
||||
// Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
||||
@@ -431,25 +426,6 @@ void HandleOperation(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
int ExpectTokenClass(int count, ...) {
|
||||
va_list list;
|
||||
Token* token = PeekToken();
|
||||
|
||||
va_start(list, count);
|
||||
|
||||
for(int i = 0; i < count; i++) {
|
||||
if (va_arg(list, TokenClass) == token->token_class) {
|
||||
va_end(list);
|
||||
AdvanceParser();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
va_end(list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Expect(int count, ...) {
|
||||
va_list list;
|
||||
Token* token = PeekToken();
|
||||
@@ -478,7 +454,7 @@ void PrintSymbols(void) {
|
||||
printf("-----SYMBOLS-----\n");
|
||||
}
|
||||
|
||||
Symbol* AddSymbol(Token* token, int address, int resolved) {
|
||||
Symbol* AddSymbol(Token* token, int length, int address, int resolved) {
|
||||
if (!token) return NULL;
|
||||
if (token->type != IDENTIFIER && token->type != LABEL) return NULL;
|
||||
|
||||
@@ -492,11 +468,12 @@ Symbol* AddSymbol(Token* token, int address, int resolved) {
|
||||
}
|
||||
s->resolved = resolved;
|
||||
s->address = address;
|
||||
s->length = length;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
Symbol* symbol = CreateSymbol(token, address, resolved);
|
||||
Symbol* symbol = CreateSymbol(token, length, address, resolved);
|
||||
|
||||
AddListItem(symbol, sizeof(Symbol), MachineState.SymbolsTable);
|
||||
|
||||
@@ -515,7 +492,7 @@ Symbol* GetSymbol(char* name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Symbol* CreateSymbol(Token* token, int address, int resolved) {
|
||||
Symbol* CreateSymbol(Token* token, int length, int address, int resolved) {
|
||||
Symbol* symbol = calloc(1, sizeof(Symbol));
|
||||
|
||||
if (!symbol) return NULL;
|
||||
|
||||
Reference in New Issue
Block a user