Parser is now mostly setup for the 'peek and advance' pattern. Still having infinite loop issues somewhere though.

This commit is contained in:
2022-05-12 21:30:02 +00:00
parent 72525959f6
commit 2de3538d80
2 changed files with 64 additions and 25 deletions
+4 -4
View File
@@ -31,10 +31,10 @@ typedef enum {
} TokenType; } TokenType;
typedef enum { typedef enum {
None, None = 0,
Reg, Reg = 1,
Immediate16, Immediate16 = 2,
Opcode Opcode = 4
} TokenClass; } TokenClass;
typedef struct { typedef struct {
+59 -20
View File
@@ -1,5 +1,6 @@
#include "../includes/parser.h" #include "../includes/parser.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h>
#ifndef HIGHMEMORY #ifndef HIGHMEMORY
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF #define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
@@ -16,9 +17,10 @@ int CurrentToken = 0;
void AddSymbol(Token*, int); void AddSymbol(Token*, int);
void PrintSymbols(void); void PrintSymbols(void);
void HandleOperation(Token*); void HandleOperation(void);
void AdvanceParser(void); void AdvanceParser(void);
Token* PeekToken(void); Token* PeekToken(void);
int ParserAtEnd(void);
int Expect(int, ...); int Expect(int, ...);
Token* GetSymbol(char*); Token* GetSymbol(char*);
List* SymbolsTable; List* SymbolsTable;
@@ -31,8 +33,8 @@ void ParseTokens(List* tokens) {
SymbolsTable = CreateList(); SymbolsTable = CreateList();
for(int i = 0; i < tokens->size; i++){ while(!ParserAtEnd()) {
Token* t = tokens->content[i]; Token* t = PeekToken();
switch(t->type) { switch(t->type) {
case IDENTIFIER: case IDENTIFIER:
@@ -48,8 +50,11 @@ void ParseTokens(List* tokens) {
default: //Instructions are 1 byte wide. default: //Instructions are 1 byte wide.
if (t->token_class == Opcode) { if (t->token_class == Opcode) {
ProgramCounter++; ProgramCounter++;
HandleOperation();
} }
AdvanceParser();
break; break;
} }
} }
@@ -59,32 +64,50 @@ void ParseTokens(List* tokens) {
printf("Program Counter: %d\n", ProgramCounter); printf("Program Counter: %d\n", ProgramCounter);
} }
void HandleOperation(Token* token) { void HandleOperation(void) {
const Instruction* inst = GetOpcodeDetails(token->type); const Instruction* inst = GetOpcodeDetails(PeekToken()->type);
if (!inst) return; if (!inst) return;
AdvanceParser();
} if (inst->parameter_one && PeekToken()->token_class == 1) {
printf("Matched '%s'\n", PeekToken()->lexeme);
int Expect(int count, ...) { AdvanceParser();
va_list list;
va_start(list, count);
for(int i = 0; i < count; i++) {
if (va_arg(list, TokenClass) == PeekToken()->token_class) {
va_end(list);
//advance
return 1;
}
} }
va_end(list); if (PeekToken()->type == COMMA) {
AdvanceParser();
} else {
fprintf(stderr, "Expected comma at line %d\n", PeekToken()->line);
exit(666);
return;
}
return 0; if (inst->parameter_two && PeekToken()->token_class == 1) {
printf("Matched 2 '%s'\n", PeekToken()->lexeme);
AdvanceParser();
}
} }
// int Expect(int count, ...) {
// va_list list;
// va_start(list, count);
// for(int i = 0; i < count; i++) {
// if (va_arg(list, TokenClass) == PeekToken()->token_class) {
// va_end(list);
// //advance
// return 1;
// }
// }
// va_end(list);
// return 0;
// }
void PrintSymbols(void) { 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++) {
@@ -125,3 +148,19 @@ Symbol* CreateSymbol(Token* token, int address) {
return symbol; return symbol;
} }
void AdvanceParser(void) {
if (ParserAtEnd()) return;
CurrentToken++;
}
int ParserAtEnd(void) {
if (CurrentToken >= TokensList->size) return 1;
return 0;
}
Token* PeekToken(void) {
return TokensList->content[CurrentToken];
}