Started work on rewriting the Parser.

This commit is contained in:
2022-05-10 21:03:09 +00:00
parent fbef044905
commit 72abf6c4ab
3 changed files with 18 additions and 16 deletions
+16 -15
View File
@@ -1,7 +1,8 @@
#include "../includes/parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}