Made use of the Symbols Table, and started the proccess of determining the memory location of opcodes and variables.
This commit is contained in:
+60
-89
@@ -15,7 +15,13 @@ typedef struct {
|
||||
int address;
|
||||
} Symbol;
|
||||
|
||||
Symbol* CreateSymbol(Token*, int);
|
||||
struct intr {
|
||||
TokenType struction;
|
||||
unsigned int parameter1;
|
||||
unsigned int parameter2;
|
||||
};
|
||||
|
||||
Symbol* CreateSymbol(Token* token, int address);
|
||||
const List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
//int HeapTop = HIGHMEMORY;
|
||||
@@ -29,7 +35,7 @@ Token* PeekToken(void);
|
||||
int ParserAtEnd(void);
|
||||
int Expect(int, ...);
|
||||
int ExpectTokenClass(int, ...);
|
||||
Token* GetSymbol(char*);
|
||||
const Symbol* GetSymbol(char*);
|
||||
List* SymbolsTable;
|
||||
unsigned int ProgramCounter = 0;
|
||||
|
||||
@@ -46,41 +52,20 @@ void ParseTokens(List* tokens) {
|
||||
switch(t->token_class) {
|
||||
case Directive:
|
||||
HandleAssemblerDirective();
|
||||
break;
|
||||
case Opcode:
|
||||
ProgramCounter++;
|
||||
HandleOperation();
|
||||
break;
|
||||
case Immediate16:
|
||||
ProgramCounter++;
|
||||
AddSymbol(t, ProgramCounter);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
// switch(t->type) {
|
||||
// case IDENTIFIER:
|
||||
// case LABEL:
|
||||
// AddSymbol(t, ProgramCounter);
|
||||
// AdvanceParser();
|
||||
// break;
|
||||
// // case STRING:
|
||||
// // ProgramCounter += strlen(t->lexeme);
|
||||
// // break;
|
||||
// //case NUMBER: //Number's will be 2 bytes
|
||||
// // ProgramCounter += 2;
|
||||
// // break;
|
||||
// default: //Instructions are 1 byte wide.
|
||||
// if (t->token_class == Opcode) {
|
||||
// //printf("One: %s\n", t->lexeme);
|
||||
// ProgramCounter++;
|
||||
// HandleOperation();
|
||||
// }
|
||||
// else if (t->token_class == Directive) {
|
||||
// HandleAssemblerDirective();
|
||||
// }
|
||||
|
||||
// AdvanceParser();
|
||||
|
||||
// break;
|
||||
// }
|
||||
}
|
||||
|
||||
PrintSymbols();
|
||||
@@ -131,41 +116,6 @@ void HandleAssemblerDirective(void) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// if (token->type == DB) {
|
||||
// token = PeekToken();
|
||||
// if (!Expect(1, IDENTIFIER)) {
|
||||
// fprintf(stderr, "Expected identifier on line %d\n", PeekToken()->line);
|
||||
// exit(1);
|
||||
// }
|
||||
// //This should be done only after the directive is parsed fully.
|
||||
// //AddSymbol(token, HEAPTOP);
|
||||
|
||||
// if (PeekToken()-> type == STRING) {
|
||||
|
||||
// AddSymbol(token, ProgramCounter);
|
||||
|
||||
// ProgramCounter += strlen(PeekToken()->lexeme);
|
||||
|
||||
// AdvanceParser();
|
||||
|
||||
// if (PeekToken()-> type == COMMA) {
|
||||
// AdvanceParser();
|
||||
// if (PeekToken()-> type != NUMBER) {
|
||||
// fprintf(stderr, "Expected string termination byte on line %d\n", PeekToken()->line);
|
||||
// exit(1);
|
||||
// }
|
||||
// else {
|
||||
// //Add the NULL byte.
|
||||
// ProgramCounter++;
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// AddSymbol(token, ProgramCounter);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void HandleOperation(void) {
|
||||
@@ -175,23 +125,43 @@ void HandleOperation(void) {
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
if (inst->parameter_one == None) return;
|
||||
printf("[%#05X] %s ", ProgramCounter, inst->lexeme);
|
||||
|
||||
if (inst->parameter_one == None) {
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (inst->parameter_one && PeekToken()->token_class > 0) {
|
||||
//printf("Matched '%s'\n", PeekToken()->lexeme);
|
||||
|
||||
if (inst->parameter_one == Reg) {
|
||||
if (inst->parameter_one & Reg) {
|
||||
//Encode the register number here.
|
||||
printf("%s ", PeekToken()->lexeme);
|
||||
}
|
||||
else if (inst->parameter_one & Immediate16) {
|
||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||
|
||||
if (!symbol) {
|
||||
fprintf(stderr, "[Error] %s is undefined.\n", PeekToken()->lexeme);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("[%s@%#04X] ", PeekToken()->lexeme, symbol->address);
|
||||
}
|
||||
else if (PeekToken()->type == NUMBER) printf("%s ", PeekToken()->lexeme);
|
||||
|
||||
else if (inst->parameter_one == Immediate16) {
|
||||
ProgramCounter += 2;
|
||||
}
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
|
||||
if (inst->parameter_two == None) return;
|
||||
if (inst->parameter_two == None) {
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (PeekToken()->type == COMMA) {
|
||||
AdvanceParser();
|
||||
@@ -203,7 +173,19 @@ void HandleOperation(void) {
|
||||
|
||||
if (inst->parameter_two && PeekToken()->token_class > 0) {
|
||||
//printf("Matched 2 '%s'\n", PeekToken()->lexeme);
|
||||
if (inst->parameter_one == Immediate16) {
|
||||
if (inst->parameter_two & Immediate16) {
|
||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||
|
||||
if (!symbol) {
|
||||
fprintf(stderr, "[Error] %s is undefined.\n", PeekToken()->lexeme);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("[%s@%#04X] ", PeekToken()->lexeme, symbol->address);
|
||||
}
|
||||
else if (PeekToken()->type == NUMBER) printf("%s ", PeekToken()->lexeme);
|
||||
|
||||
ProgramCounter += 2;
|
||||
}
|
||||
|
||||
@@ -212,6 +194,8 @@ void HandleOperation(void) {
|
||||
printf("Failed to match %s (class: %d)\n", PeekToken()->lexeme, PeekToken()->token_class);
|
||||
printf("Inst %s (class: %x)\n", inst->lexeme, inst->parameter_two);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int ExpectTokenClass(int count, ...) {
|
||||
@@ -252,25 +236,6 @@ int Expect(int count, ...) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Require(int count, ...) {
|
||||
va_list tokenTypes;
|
||||
Token* token = PeekToken();
|
||||
|
||||
va_start(tokenTypes, count);
|
||||
|
||||
for(int i = 0; i < count; i++) {
|
||||
if (va_arg(tokenTypes, TokenType) == token->type) {
|
||||
va_end(tokenTypes);
|
||||
AdvanceParser();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
va_end(tokenTypes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PrintSymbols(void) {
|
||||
printf("-----SYMBOLS-----\n");
|
||||
for(int i = 0; i < SymbolsTable->size; i++) {
|
||||
@@ -295,9 +260,15 @@ void AddSymbol(Token* token, int address) {
|
||||
AddListItem(symbol, sizeof(Symbol), SymbolsTable);
|
||||
}
|
||||
|
||||
Token* GetSymbol(char* name) {
|
||||
const Symbol* GetSymbol(char* name) {
|
||||
if (!name) return NULL;
|
||||
|
||||
for(int i = 0; i < SymbolsTable->size; i++) {
|
||||
const Symbol* s = SymbolsTable->content[i];
|
||||
|
||||
if (strcmp(s->token->lexeme, name) == 0) return s;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user