From 72abf6c4abc2b600b5bd41c54f9acd12eb6d11fa Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 10 May 2022 21:03:09 +0000 Subject: [PATCH] Started work on rewriting the Parser. --- includes/parser.h | 1 + src/opcodes.c | 2 +- src/parser.c | 31 ++++++++++++++++--------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/includes/parser.h b/includes/parser.h index 9db721c..90b7025 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -5,6 +5,7 @@ #include #include "list.h" #include "token.h" +#include "opcodes.h" void ParseTokens(List*); diff --git a/src/opcodes.c b/src/opcodes.c index a84dcba..4c30c85 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -31,7 +31,7 @@ int IsOpcode(const char* text, TokenType* opcode) { for(int i = 0; i < OPCODECOUNT; i++) { if (strcmp(opcodes[i].lexeme, text) == 0) { - *opcode = opcodes[i].op; + if (opcode) *opcode = opcodes[i].op; return 1; } } diff --git a/src/parser.c b/src/parser.c index 4b92693..5af7c6b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,7 +1,8 @@ #include "../includes/parser.h" -#include -#include -#include + +#ifndef HIGHMEMORY +#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF +#endif typedef struct { Token* token; @@ -9,7 +10,7 @@ typedef struct { } Symbol; Symbol* CreateSymbol(Token*, int); -void AddSymbol(Token*); +void AddSymbol(Token*, int); void PrintSymbols(void); Token* GetSymbol(char*); List* SymbolsTable; @@ -24,18 +25,18 @@ void ParseTokens(List* tokens) { switch(t->type) { case IDENTIFIER: case LABEL: - AddSymbol(t); + AddSymbol(t, ProgramCounter); break; case STRING: ProgramCounter += strlen(t->lexeme); break; case NUMBER: - case HEX: - if ((long) t->value < 256) ProgramCounter += 1; - else ProgramCounter += 2; + case HEX: //Number's will be 2 bytes + ProgramCounter += 2; break; - default: - if (t->type > ORG) ProgramCounter += 1; + default: //Instructions are 1 byte wide. + if (IsOpcode(t->lexeme, NULL)) ProgramCounter += 1; + break; } } @@ -49,12 +50,12 @@ void PrintSymbols(void) { printf("-----SYMBOLS-----\n"); for(int i = 0; i < SymbolsTable->size; i++) { Symbol* symbol = SymbolsTable->content[i]; - printf("[%#08X] %s\n", symbol->address, symbol->token->lexeme); + printf("[%#06X] %s\n", symbol->address, symbol->token->lexeme); } printf("-----SYMBOLS-----\n"); } -void AddSymbol(Token* token) { +void AddSymbol(Token* token, int address) { if (!token) return; if (token->type != IDENTIFIER && token->type != LABEL) return; @@ -64,7 +65,7 @@ void AddSymbol(Token* token) { if (strcmp(s->token->lexeme, token->lexeme) == 0) return; } - Symbol* symbol = CreateSymbol(token, ProgramCounter); + Symbol* symbol = CreateSymbol(token, address); AddListItem(symbol, sizeof(Symbol), SymbolsTable); } @@ -75,7 +76,7 @@ Token* GetSymbol(char* name) { return NULL; } -Symbol* CreateSymbol(Token* token, int offset) { +Symbol* CreateSymbol(Token* token, int address) { Symbol* symbol = calloc(1, sizeof(Symbol)); if (!symbol) { @@ -83,7 +84,7 @@ Symbol* CreateSymbol(Token* token, int offset) { } symbol->token = token; - symbol->address = offset; + symbol->address = address; return symbol; } \ No newline at end of file