From 89ab951c6ae7f5a623474876d1c17b4e0a40e373 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 24 Aug 2022 21:23:14 +0000 Subject: [PATCH] Refactored the parsing code just a touch. --- src/main.c | 2 +- src/parser.c | 179 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 136 insertions(+), 45 deletions(-) diff --git a/src/main.c b/src/main.c index 60831c1..536d731 100644 --- a/src/main.c +++ b/src/main.c @@ -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"); // } diff --git a/src/parser.c b/src/parser.c index 0d1d9f7..c51fba6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,4 +1,5 @@ #include "../includes/parser.h" +#include #include #include #include @@ -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++) {