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
+1
View File
@@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "list.h" #include "list.h"
#include "token.h" #include "token.h"
#include "opcodes.h"
void ParseTokens(List*); void ParseTokens(List*);
+1 -1
View File
@@ -31,7 +31,7 @@ int IsOpcode(const char* text, TokenType* opcode) {
for(int i = 0; i < OPCODECOUNT; i++) { for(int i = 0; i < OPCODECOUNT; i++) {
if (strcmp(opcodes[i].lexeme, text) == 0) { if (strcmp(opcodes[i].lexeme, text) == 0) {
*opcode = opcodes[i].op; if (opcode) *opcode = opcodes[i].op;
return 1; return 1;
} }
} }
+16 -15
View File
@@ -1,7 +1,8 @@
#include "../includes/parser.h" #include "../includes/parser.h"
#include <stdio.h>
#include <stdlib.h> #ifndef HIGHMEMORY
#include <string.h> #define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
#endif
typedef struct { typedef struct {
Token* token; Token* token;
@@ -9,7 +10,7 @@ typedef struct {
} Symbol; } Symbol;
Symbol* CreateSymbol(Token*, int); Symbol* CreateSymbol(Token*, int);
void AddSymbol(Token*); void AddSymbol(Token*, int);
void PrintSymbols(void); void PrintSymbols(void);
Token* GetSymbol(char*); Token* GetSymbol(char*);
List* SymbolsTable; List* SymbolsTable;
@@ -24,18 +25,18 @@ void ParseTokens(List* tokens) {
switch(t->type) { switch(t->type) {
case IDENTIFIER: case IDENTIFIER:
case LABEL: case LABEL:
AddSymbol(t); AddSymbol(t, ProgramCounter);
break; break;
case STRING: case STRING:
ProgramCounter += strlen(t->lexeme); ProgramCounter += strlen(t->lexeme);
break; break;
case NUMBER: case NUMBER:
case HEX: case HEX: //Number's will be 2 bytes
if ((long) t->value < 256) ProgramCounter += 1; ProgramCounter += 2;
else ProgramCounter += 2;
break; break;
default: default: //Instructions are 1 byte wide.
if (t->type > ORG) ProgramCounter += 1; if (IsOpcode(t->lexeme, NULL)) ProgramCounter += 1;
break; break;
} }
} }
@@ -49,12 +50,12 @@ 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++) {
Symbol* symbol = SymbolsTable->content[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"); printf("-----SYMBOLS-----\n");
} }
void AddSymbol(Token* token) { void AddSymbol(Token* token, int address) {
if (!token) return; if (!token) return;
if (token->type != IDENTIFIER && token->type != LABEL) 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; if (strcmp(s->token->lexeme, token->lexeme) == 0) return;
} }
Symbol* symbol = CreateSymbol(token, ProgramCounter); Symbol* symbol = CreateSymbol(token, address);
AddListItem(symbol, sizeof(Symbol), SymbolsTable); AddListItem(symbol, sizeof(Symbol), SymbolsTable);
} }
@@ -75,7 +76,7 @@ Token* GetSymbol(char* name) {
return NULL; return NULL;
} }
Symbol* CreateSymbol(Token* token, int offset) { Symbol* CreateSymbol(Token* token, int address) {
Symbol* symbol = calloc(1, sizeof(Symbol)); Symbol* symbol = calloc(1, sizeof(Symbol));
if (!symbol) { if (!symbol) {
@@ -83,7 +84,7 @@ Symbol* CreateSymbol(Token* token, int offset) {
} }
symbol->token = token; symbol->token = token;
symbol->address = offset; symbol->address = address;
return symbol; return symbol;
} }