Refactored the parsing code just a touch.

This commit is contained in:
2022-08-24 21:23:14 +00:00
parent 947de1d14c
commit 89ab951c6a
2 changed files with 136 additions and 45 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ int main(int argc, char* args[]) {
// for(int i = 0; i < list->size; 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("*");
// printf("\n");
// }
+135 -44
View File
@@ -1,4 +1,5 @@
#include "../includes/parser.h"
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -7,7 +8,7 @@
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
#endif
#define HEAPTOP HIGHMEMORY
//#define HEAPTOP HIGHMEMORY
typedef struct {
Token* token;
@@ -17,6 +18,7 @@ typedef struct {
Symbol* CreateSymbol(Token*, int);
const List* TokensList;
int CurrentToken = 0;
//int HeapTop = HIGHMEMORY;
void AddSymbol(Token*, int);
void PrintSymbols(void);
@@ -41,67 +43,129 @@ void ParseTokens(List* tokens) {
while(!ParserAtEnd()) {
Token* t = PeekToken();
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) {
ProgramCounter++;
HandleOperation();
}
else if (t->token_class == Directive) {
HandleAssemblerDirective();
}
AdvanceParser();
switch(t->token_class) {
case Directive:
HandleAssemblerDirective();
case Opcode:
ProgramCounter++;
HandleOperation();
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();
DestroyList(SymbolsTable);
printf("Program Counter: %d\n", ProgramCounter);
//printf("Heap Top: %#06X\n", HeapTop);
}
void HandleAssemblerDirective(void) {
Token* token = PeekToken();
AdvanceParser();
if (token->type == DB) {
token = PeekToken();
if (!Expect(1, IDENTIFIER)) {
if (PeekToken()->type == DB) {
AdvanceParser();
Token* identifier = 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) {
AdvanceParser();
AddSymbol(identifier, ProgramCounter);
switch(PeekToken()->type) {
case STRING:
ProgramCounter += strlen(PeekToken()->lexeme);
printf("[INFO] %s = '%s'\n", identifier->lexeme, PeekToken()->lexeme);
if (PeekToken()-> type == COMMA) {
AdvanceParser();
if (PeekToken()-> type != NUMBER) {
fprintf(stderr, "Expected string termination byte on line %d\n", PeekToken()->line);
exit(1);
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 {
// //Setup string and return
// 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 {
// //Add the NULL byte.
// ProgramCounter++;
// return;
// }
// }
// }
// else {
// AddSymbol(token, ProgramCounter);
// }
// }
}
void HandleOperation(void) {
@@ -114,12 +178,16 @@ void HandleOperation(void) {
if (inst->parameter_one == None) return;
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) {
//Encode the register number here.
}
else if (inst->parameter_one == Immediate16) {
ProgramCounter += 2;
}
AdvanceParser();
}
@@ -134,7 +202,11 @@ void HandleOperation(void) {
}
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();
} else {
printf("Failed to match %s (class: %d)\n", PeekToken()->lexeme, PeekToken()->token_class);
@@ -180,6 +252,25 @@ 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++) {