Refactored the parsing code just a touch.
This commit is contained in:
+1
-1
@@ -23,7 +23,7 @@ int main(int argc, char* args[]) {
|
|||||||
// 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];
|
||||||
|
|
||||||
// printf("[%i] '%s'", t->type, t->lexeme);
|
// printf("[%i] '%s' [%i]", t->type, t->lexeme, t->token_class);
|
||||||
// if (t->type == LABEL) printf("*");
|
// if (t->type == LABEL) printf("*");
|
||||||
// printf("\n");
|
// printf("\n");
|
||||||
// }
|
// }
|
||||||
|
|||||||
+124
-33
@@ -1,4 +1,5 @@
|
|||||||
#include "../includes/parser.h"
|
#include "../includes/parser.h"
|
||||||
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -7,7 +8,7 @@
|
|||||||
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
|
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define HEAPTOP HIGHMEMORY
|
//#define HEAPTOP HIGHMEMORY
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Token* token;
|
Token* token;
|
||||||
@@ -17,6 +18,7 @@ typedef struct {
|
|||||||
Symbol* CreateSymbol(Token*, int);
|
Symbol* CreateSymbol(Token*, int);
|
||||||
const List* TokensList;
|
const List* TokensList;
|
||||||
int CurrentToken = 0;
|
int CurrentToken = 0;
|
||||||
|
//int HeapTop = HIGHMEMORY;
|
||||||
|
|
||||||
void AddSymbol(Token*, int);
|
void AddSymbol(Token*, int);
|
||||||
void PrintSymbols(void);
|
void PrintSymbols(void);
|
||||||
@@ -41,67 +43,129 @@ void ParseTokens(List* tokens) {
|
|||||||
while(!ParserAtEnd()) {
|
while(!ParserAtEnd()) {
|
||||||
Token* t = PeekToken();
|
Token* t = PeekToken();
|
||||||
|
|
||||||
switch(t->type) {
|
switch(t->token_class) {
|
||||||
case IDENTIFIER:
|
case Directive:
|
||||||
case LABEL:
|
HandleAssemblerDirective();
|
||||||
AddSymbol(t, ProgramCounter);
|
case Opcode:
|
||||||
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) {
|
|
||||||
ProgramCounter++;
|
ProgramCounter++;
|
||||||
HandleOperation();
|
HandleOperation();
|
||||||
}
|
default:
|
||||||
else if (t->token_class == Directive) {
|
break;
|
||||||
HandleAssemblerDirective();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
break;
|
// 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();
|
||||||
DestroyList(SymbolsTable);
|
DestroyList(SymbolsTable);
|
||||||
printf("Program Counter: %d\n", ProgramCounter);
|
printf("Program Counter: %d\n", ProgramCounter);
|
||||||
|
//printf("Heap Top: %#06X\n", HeapTop);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleAssemblerDirective(void) {
|
void HandleAssemblerDirective(void) {
|
||||||
Token* token = PeekToken();
|
|
||||||
|
if (PeekToken()->type == DB) {
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
if (token->type == DB) {
|
Token* identifier = PeekToken();
|
||||||
token = PeekToken();
|
|
||||||
if (!Expect(1, IDENTIFIER)) {
|
if (!Expect(1, IDENTIFIER)) {
|
||||||
fprintf(stderr, "Expected identifier on line %d\n", PeekToken()->line);
|
fprintf(stderr, "Expected identifier on line %d\n", PeekToken()->line);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
//This should be done only after the directive is parsed fully.
|
|
||||||
AddSymbol(token, HEAPTOP);
|
|
||||||
|
|
||||||
if (PeekToken()-> type == STRING) {
|
AddSymbol(identifier, ProgramCounter);
|
||||||
|
|
||||||
|
switch(PeekToken()->type) {
|
||||||
|
case STRING:
|
||||||
|
ProgramCounter += strlen(PeekToken()->lexeme);
|
||||||
|
|
||||||
|
printf("[INFO] %s = '%s'\n", identifier->lexeme, PeekToken()->lexeme);
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
if (PeekToken()-> type == COMMA) {
|
if (PeekToken()->type == COMMA) {
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
if (PeekToken()-> type != NUMBER) {
|
if (PeekToken()-> type != NUMBER) {
|
||||||
fprintf(stderr, "Expected string termination byte on line %d\n", PeekToken()->line);
|
fprintf(stderr, "Expected string termination byte on line %d\n", PeekToken()->line);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
//Add the NULL byte.
|
||||||
|
ProgramCounter++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case NUMBER:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "[Line: %d] Expected either a number or string after identifier '%s'.\n", PeekToken()->line, identifier->lexeme);
|
||||||
|
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 {
|
// else {
|
||||||
// //Setup string and return
|
// //Add the NULL byte.
|
||||||
|
// ProgramCounter++;
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// else {
|
||||||
|
// AddSymbol(token, ProgramCounter);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleOperation(void) {
|
void HandleOperation(void) {
|
||||||
@@ -114,12 +178,16 @@ void HandleOperation(void) {
|
|||||||
if (inst->parameter_one == None) return;
|
if (inst->parameter_one == None) 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.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (inst->parameter_one == Immediate16) {
|
||||||
|
ProgramCounter += 2;
|
||||||
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +202,11 @@ 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) {
|
||||||
|
ProgramCounter += 2;
|
||||||
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
} else {
|
} else {
|
||||||
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);
|
||||||
@@ -180,6 +252,25 @@ 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++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user