90 lines
2.1 KiB
C
90 lines
2.1 KiB
C
#include "../includes/parser.h"
|
|
|
|
#ifndef HIGHMEMORY
|
|
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
|
|
#endif
|
|
|
|
typedef struct {
|
|
Token* token;
|
|
int address;
|
|
} Symbol;
|
|
|
|
Symbol* CreateSymbol(Token*, int);
|
|
void AddSymbol(Token*, int);
|
|
void PrintSymbols(void);
|
|
Token* GetSymbol(char*);
|
|
List* SymbolsTable;
|
|
unsigned int ProgramCounter = 0;
|
|
|
|
void ParseTokens(List* tokens) {
|
|
SymbolsTable = CreateList();
|
|
|
|
for(int i = 0; i < tokens->size; i++){
|
|
Token* t = tokens->content[i];
|
|
|
|
switch(t->type) {
|
|
case IDENTIFIER:
|
|
case LABEL:
|
|
AddSymbol(t, ProgramCounter);
|
|
break;
|
|
case STRING:
|
|
ProgramCounter += strlen(t->lexeme);
|
|
break;
|
|
case NUMBER:
|
|
case HEX: //Number's will be 2 bytes
|
|
ProgramCounter += 2;
|
|
break;
|
|
default: //Instructions are 1 byte wide.
|
|
if (IsOpcode(t->lexeme, NULL)) ProgramCounter += 1;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
PrintSymbols();
|
|
DestroyList(SymbolsTable);
|
|
printf("Program Counter: %d\n", ProgramCounter);
|
|
}
|
|
|
|
void PrintSymbols(void) {
|
|
printf("-----SYMBOLS-----\n");
|
|
for(int i = 0; i < SymbolsTable->size; i++) {
|
|
Symbol* symbol = SymbolsTable->content[i];
|
|
printf("[%#06X] %s\n", symbol->address, symbol->token->lexeme);
|
|
}
|
|
printf("-----SYMBOLS-----\n");
|
|
}
|
|
|
|
void AddSymbol(Token* token, int address) {
|
|
if (!token) return;
|
|
if (token->type != IDENTIFIER && token->type != LABEL) return;
|
|
|
|
for(int i = 0; i < SymbolsTable->size; i++) {
|
|
Symbol* s = SymbolsTable->content[i];
|
|
|
|
if (strcmp(s->token->lexeme, token->lexeme) == 0) return;
|
|
}
|
|
|
|
Symbol* symbol = CreateSymbol(token, address);
|
|
|
|
AddListItem(symbol, sizeof(Symbol), SymbolsTable);
|
|
}
|
|
|
|
Token* GetSymbol(char* name) {
|
|
if (!name) return NULL;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
Symbol* CreateSymbol(Token* token, int address) {
|
|
Symbol* symbol = calloc(1, sizeof(Symbol));
|
|
|
|
if (!symbol) {
|
|
return NULL;
|
|
}
|
|
|
|
symbol->token = token;
|
|
symbol->address = address;
|
|
|
|
return symbol;
|
|
} |