Made use of the Symbols Table, and started the proccess of determining the memory location of opcodes and variables.
This commit is contained in:
+11
-11
@@ -20,21 +20,21 @@ int main(int argc, char* args[]) {
|
|||||||
|
|
||||||
List* list = GenerateTokenList(source_code);
|
List* list = GenerateTokenList(source_code);
|
||||||
|
|
||||||
for(int i = 0; i < list->size; i++) {
|
// for(int i = 0; i < list->size; i++) {
|
||||||
Token* t = (Token*) list->content[i];
|
// Token* t = (Token*) list->content[i];
|
||||||
|
|
||||||
if (t->type == LineEnd) {
|
// if (t->type == LineEnd) {
|
||||||
if (i - 1 >= 0 && ((Token*) list->content[i - 1])->type != LineEnd)
|
// if (i - 1 >= 0 && ((Token*) list->content[i - 1])->type != LineEnd)
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
|
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
printf("[%i] '%s' [%i] ", t->type, t->lexeme, t->token_class);
|
// printf("[%i] '%s' [%i] ", t->type, t->lexeme, t->token_class);
|
||||||
if (t->type == LABEL) printf("* ");
|
// if (t->type == LABEL) printf("* ");
|
||||||
}
|
// }
|
||||||
|
|
||||||
//ParseTokens(list);
|
ParseTokens(list);
|
||||||
|
|
||||||
free(source_code);
|
free(source_code);
|
||||||
}
|
}
|
||||||
+60
-89
@@ -15,7 +15,13 @@ typedef struct {
|
|||||||
int address;
|
int address;
|
||||||
} Symbol;
|
} Symbol;
|
||||||
|
|
||||||
Symbol* CreateSymbol(Token*, int);
|
struct intr {
|
||||||
|
TokenType struction;
|
||||||
|
unsigned int parameter1;
|
||||||
|
unsigned int parameter2;
|
||||||
|
};
|
||||||
|
|
||||||
|
Symbol* CreateSymbol(Token* token, int address);
|
||||||
const List* TokensList;
|
const List* TokensList;
|
||||||
int CurrentToken = 0;
|
int CurrentToken = 0;
|
||||||
//int HeapTop = HIGHMEMORY;
|
//int HeapTop = HIGHMEMORY;
|
||||||
@@ -29,7 +35,7 @@ Token* PeekToken(void);
|
|||||||
int ParserAtEnd(void);
|
int ParserAtEnd(void);
|
||||||
int Expect(int, ...);
|
int Expect(int, ...);
|
||||||
int ExpectTokenClass(int, ...);
|
int ExpectTokenClass(int, ...);
|
||||||
Token* GetSymbol(char*);
|
const Symbol* GetSymbol(char*);
|
||||||
List* SymbolsTable;
|
List* SymbolsTable;
|
||||||
unsigned int ProgramCounter = 0;
|
unsigned int ProgramCounter = 0;
|
||||||
|
|
||||||
@@ -46,41 +52,20 @@ void ParseTokens(List* tokens) {
|
|||||||
switch(t->token_class) {
|
switch(t->token_class) {
|
||||||
case Directive:
|
case Directive:
|
||||||
HandleAssemblerDirective();
|
HandleAssemblerDirective();
|
||||||
|
break;
|
||||||
case Opcode:
|
case Opcode:
|
||||||
ProgramCounter++;
|
ProgramCounter++;
|
||||||
HandleOperation();
|
HandleOperation();
|
||||||
|
break;
|
||||||
|
case Immediate16:
|
||||||
|
ProgramCounter++;
|
||||||
|
AddSymbol(t, ProgramCounter);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
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();
|
PrintSymbols();
|
||||||
@@ -131,41 +116,6 @@ void HandleAssemblerDirective(void) {
|
|||||||
exit(1);
|
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) {
|
void HandleOperation(void) {
|
||||||
@@ -175,23 +125,43 @@ void HandleOperation(void) {
|
|||||||
|
|
||||||
AdvanceParser();
|
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) {
|
if (inst->parameter_one && PeekToken()->token_class > 0) {
|
||||||
//printf("Matched '%s'\n", PeekToken()->lexeme);
|
//printf("Matched '%s'\n", PeekToken()->lexeme);
|
||||||
|
|
||||||
if (inst->parameter_one == Reg) {
|
if (inst->parameter_one & Reg) {
|
||||||
//Encode the register number here.
|
//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;
|
ProgramCounter += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inst->parameter_two == None) return;
|
if (inst->parameter_two == None) {
|
||||||
|
printf("\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (PeekToken()->type == COMMA) {
|
if (PeekToken()->type == COMMA) {
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
@@ -203,7 +173,19 @@ void HandleOperation(void) {
|
|||||||
|
|
||||||
if (inst->parameter_two && PeekToken()->token_class > 0) {
|
if (inst->parameter_two && PeekToken()->token_class > 0) {
|
||||||
//printf("Matched 2 '%s'\n", PeekToken()->lexeme);
|
//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;
|
ProgramCounter += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,6 +194,8 @@ void HandleOperation(void) {
|
|||||||
printf("Failed to match %s (class: %d)\n", PeekToken()->lexeme, PeekToken()->token_class);
|
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("Inst %s (class: %x)\n", inst->lexeme, inst->parameter_two);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int ExpectTokenClass(int count, ...) {
|
int ExpectTokenClass(int count, ...) {
|
||||||
@@ -252,25 +236,6 @@ int Expect(int count, ...) {
|
|||||||
return 0;
|
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) {
|
void PrintSymbols(void) {
|
||||||
printf("-----SYMBOLS-----\n");
|
printf("-----SYMBOLS-----\n");
|
||||||
for(int i = 0; i < SymbolsTable->size; i++) {
|
for(int i = 0; i < SymbolsTable->size; i++) {
|
||||||
@@ -295,9 +260,15 @@ void AddSymbol(Token* token, int address) {
|
|||||||
AddListItem(symbol, sizeof(Symbol), SymbolsTable);
|
AddListItem(symbol, sizeof(Symbol), SymbolsTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
Token* GetSymbol(char* name) {
|
const Symbol* GetSymbol(char* name) {
|
||||||
if (!name) return NULL;
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user