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
+5 -5
View File
@@ -22,7 +22,7 @@ typedef enum {
LABEL,
NUMBER,
//Keywords
DB, ORG,
DB, ORG,
//Opcodes
COPY, ADD, SUB, JZ, INT, YLD, RET,
CMP, IN, OUT,
@@ -31,10 +31,10 @@ typedef enum {
} TokenType;
typedef enum {
None,
Reg,
Immediate16,
Opcode
None = 0,
Reg = 1,
Immediate16 = 2,
Opcode = 4
} TokenClass;
typedef struct {
+59 -20
View File
@@ -1,5 +1,6 @@
#include "../includes/parser.h"
#include <stdarg.h>
#include <stdlib.h>
#ifndef HIGHMEMORY
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
@@ -16,9 +17,10 @@ int CurrentToken = 0;
void AddSymbol(Token*, int);
void PrintSymbols(void);
void HandleOperation(Token*);
void HandleOperation(void);
void AdvanceParser(void);
Token* PeekToken(void);
int ParserAtEnd(void);
int Expect(int, ...);
Token* GetSymbol(char*);
List* SymbolsTable;
@@ -31,8 +33,8 @@ void ParseTokens(List* tokens) {
SymbolsTable = CreateList();
for(int i = 0; i < tokens->size; i++){
Token* t = tokens->content[i];
while(!ParserAtEnd()) {
Token* t = PeekToken();
switch(t->type) {
case IDENTIFIER:
@@ -48,7 +50,10 @@ void ParseTokens(List* tokens) {
default: //Instructions are 1 byte wide.
if (t->token_class == Opcode) {
ProgramCounter++;
HandleOperation();
}
AdvanceParser();
break;
}
@@ -59,32 +64,50 @@ void ParseTokens(List* tokens) {
printf("Program Counter: %d\n", ProgramCounter);
}
void HandleOperation(Token* token) {
const Instruction* inst = GetOpcodeDetails(token->type);
void HandleOperation(void) {
const Instruction* inst = GetOpcodeDetails(PeekToken()->type);
if (!inst) return;
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;
}
if (inst->parameter_one && PeekToken()->token_class == 1) {
printf("Matched '%s'\n", PeekToken()->lexeme);
AdvanceParser();
}
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) {
printf("-----SYMBOLS-----\n");
for(int i = 0; i < SymbolsTable->size; i++) {
@@ -124,4 +147,20 @@ Symbol* CreateSymbol(Token* token, int address) {
symbol->address = address;
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];
}