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