Parser is now mostly setup for the 'peek and advance' pattern. Still having infinite loop issues somewhere though.
This commit is contained in:
+4
-4
@@ -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 {
|
||||||
|
|||||||
+56
-17
@@ -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,31 +64,49 @@ 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);
|
||||||
|
AdvanceParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Expect(int count, ...) {
|
if (PeekToken()->type == COMMA) {
|
||||||
va_list list;
|
AdvanceParser();
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Expected comma at line %d\n", PeekToken()->line);
|
||||||
|
exit(666);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
va_start(list, count);
|
if (inst->parameter_two && PeekToken()->token_class == 1) {
|
||||||
|
printf("Matched 2 '%s'\n", PeekToken()->lexeme);
|
||||||
for(int i = 0; i < count; i++) {
|
AdvanceParser();
|
||||||
if (va_arg(list, TokenClass) == PeekToken()->token_class) {
|
|
||||||
va_end(list);
|
|
||||||
//advance
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
va_end(list);
|
// int Expect(int count, ...) {
|
||||||
|
// va_list list;
|
||||||
|
|
||||||
return 0;
|
// 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");
|
||||||
@@ -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];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user