Further refactoring done. The Parser runs without crashing, however it's output is still to be checked and verified.

This commit is contained in:
2022-03-02 21:24:58 +00:00
parent e20ccb8cfb
commit 221d575c16
6 changed files with 85 additions and 64 deletions
+41 -30
View File
@@ -7,13 +7,12 @@ Expr* Term(void);
Expr* Factor(void);
Expr* Unary(void);
Expr* Primary(void);
int SMatch(int, ...);
int Match(int, ...);
int Check(TokenType);
int SIsAtEnd(void);
Token* SPeek(void);
int ParserAtEnd(void);
Token* ParserPeek(void);
Token* Previous(void);
Token* SAdvance(void);
//Token* CreateToken(char*, TokenType);
Token* AdvanceParser(void);
const TokenList* ListOfTokens;
int Current = 0;
@@ -32,7 +31,7 @@ Expr* Expression() {
Expr* Equality() {
Expr* expr = Comparison();
while(SMatch(2, Bang_Equal, Equal_Equal)) {
while(Match(2, Bang_Equal, Equal_Equal)) {
Token* operator = Previous();
Expr* right = Comparison();
Expr* temp = calloc(1, sizeof(Expr));
@@ -49,7 +48,7 @@ Expr* Equality() {
Expr* Comparison() {
Expr* expr = Term();
while(SMatch(4, Greater, Greater_Equal, Less, Less_Equal)) {
while(Match(4, Greater, Greater_Equal, Less, Less_Equal)) {
Token* operator = Previous();
Expr* right = Term();
Expr* temp = calloc(1, sizeof(Expr));
@@ -66,7 +65,7 @@ Expr* Comparison() {
Expr* Term() {
Expr* expr = Factor();
while(SMatch(2, Minus, Plus)) {
while(Match(2, Minus, Plus)) {
Token* operator = Previous();
Expr* right = Factor();
Expr* temp = calloc(1, sizeof(Expr));
@@ -83,7 +82,7 @@ Expr* Term() {
Expr* Factor() {
Expr* expr = Unary();
while(SMatch(2, Slash, Star)) {
while(Match(2, Slash, Star)) {
Token* operator = Previous();
Expr* right = Unary();
Expr* temp = calloc(1, sizeof(Expr));
@@ -98,7 +97,7 @@ Expr* Factor() {
}
Expr* Unary() {
if (SMatch(2, Bang, Minus)) {
if (Match(2, Bang, Minus)) {
Token* operator = Previous();
Expr* right = Unary();
Expr* expr = calloc(1, sizeof(Expr));
@@ -115,8 +114,8 @@ Expr* Primary() {
Expr* expr = calloc(1, sizeof(Expr));
expr->type = LITERAL;
if (SMatch(3, FALSE, TRUE, NIL)) {
expr->expression.Literal.type = SPeek();//CreateToken("false", FALSE);
if (Match(3, FALSE, TRUE, NIL)) {
expr->expression.Literal.type = ParserPeek();//CreateToken("false", FALSE);
return expr;
}
// if (SMatch(1, TRUE)) {
@@ -128,12 +127,12 @@ Expr* Primary() {
// return expr;
// }
if (SMatch(2, Number, String)) {
if (Match(2, Number, String)) {
expr->expression.Literal.type = Previous();
return expr;
}
if (SMatch(1, LParen)) {
if (Match(1, LParen)) {
free(expr);
expr = Expression();
//Consume(RParen, "Expect ')' after expression.");
@@ -144,13 +143,13 @@ Expr* Primary() {
return expr;
}
int SMatch(int count, ...) {
int Match(int count, ...) {
va_list list;
va_start(list, count);
for(int i = 0; i < count; i++) {
if(Check(va_arg(list, TokenType))) {
SAdvance();
AdvanceParser();
return 1;
}
}
@@ -159,15 +158,15 @@ int SMatch(int count, ...) {
}
int Check(TokenType type) {
if (SIsAtEnd()) return 0;
return SPeek()->type == type;
if (ParserAtEnd()) return 0;
return ParserPeek()->type == type;
}
int SIsAtEnd() {
return SPeek()->type == EndOF;
int ParserAtEnd() {
return ParserPeek()->type == EndOF;
}
Token* SPeek() {
Token* ParserPeek() {
return ListOfTokens->tokens[Current];
}
@@ -175,17 +174,29 @@ Token* Previous() {
return ListOfTokens->tokens[Current - 1];
}
Token* SAdvance() {
if (!SIsAtEnd()) Current++;
Token* AdvanceParser() {
if (!ParserAtEnd()) Current++;
return Previous();
}
// Token* CreateToken(char* lexeme, TokenType type) {
// Token* token = calloc(1, sizeof(Token));
// token->type = type;
// token->lexeme = lexeme;
// token->length = strlen(lexeme);
void PrintExpressionTree(const Expr* tree) {
// return token;
// }
}
void FreeExpressionTree(Expr* tree) {
if (!tree) return;
if (tree->type == BINARY) {
FreeExpressionTree(tree->expression.Binary.left);
FreeExpressionTree(tree->expression.Binary.right);
//FreeToken(tree->expression.Binary.op);
}
else if (tree->type == UNARY) {
FreeExpressionTree(tree->expression.Unary.right);
//FreeToken(tree->expression.Unary.op);
}
//else if (tree->type == LITERAL) FreeToken(tree->expression.Literal.type);
free(tree);
}