Minor cleanup.
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
|
||||
void VisitBinary(struct binary);
|
||||
void VisitGrouping(struct grouping);
|
||||
void VisitLiteral(struct literal);
|
||||
void VisitLiteral(Token);
|
||||
void VisitUnary(struct unary);
|
||||
@@ -24,11 +24,6 @@ struct grouping {
|
||||
struct Expr* expression;
|
||||
};
|
||||
|
||||
struct literal {
|
||||
Token* type;
|
||||
void* object;
|
||||
};
|
||||
|
||||
struct unary {
|
||||
Token* op;
|
||||
struct Expr* right;
|
||||
@@ -39,14 +34,14 @@ struct Expr {
|
||||
union ex {
|
||||
struct binary Binary;
|
||||
struct grouping Grouping;
|
||||
struct literal Literal;
|
||||
Token* Literal;
|
||||
struct unary Unary;
|
||||
} expression;
|
||||
};
|
||||
|
||||
void VisitBinary(struct binary);
|
||||
void VisitGrouping(struct grouping);
|
||||
void VisitLiteral(struct literal);
|
||||
void VisitLiteral(Token);
|
||||
void VisitUnary(struct unary);
|
||||
|
||||
#endif
|
||||
@@ -42,6 +42,7 @@ void RunFile(const char* path) {
|
||||
printf("TREE:\n");
|
||||
Expr* tree = GenerateExpressionTree(tokens);
|
||||
PrintExpressionTree(tree);
|
||||
printf("\n");
|
||||
FreeExpressionTree(tree);
|
||||
DestroyTokenList(tokens);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "parser.h"
|
||||
#include "expr.h"
|
||||
#include "token.h"
|
||||
#include <stdio.h>
|
||||
|
||||
Expr* Expression(void);
|
||||
@@ -15,6 +16,7 @@ int ParserAtEnd(void);
|
||||
Token* ParserPeek(void);
|
||||
Token* Previous(void);
|
||||
Token* AdvanceParser(void);
|
||||
void SynchronizeParser(void);
|
||||
|
||||
const TokenList* ListOfTokens;
|
||||
int Current = 0;
|
||||
@@ -117,25 +119,18 @@ Expr* Primary() {
|
||||
expr->type = LITERAL;
|
||||
|
||||
if (Match(3, FALSE, TRUE, NIL)) {
|
||||
expr->expression.Literal.type = ParserPeek();//CreateToken("false", FALSE);
|
||||
expr->expression.Literal = ParserPeek();
|
||||
return expr;
|
||||
}
|
||||
// if (SMatch(1, TRUE)) {
|
||||
// expr->expression.Literal.type = //CreateToken("true", TRUE);
|
||||
// return expr;
|
||||
// }
|
||||
// if (SMatch(1, NIL)) {
|
||||
// expr->expression.Literal.type = //CreateToken("nil", NIL);
|
||||
// return expr;
|
||||
// }
|
||||
|
||||
if (Match(2, Number, String)) {
|
||||
expr->expression.Literal.type = Previous();
|
||||
expr->expression.Literal = Previous();
|
||||
return expr;
|
||||
}
|
||||
|
||||
free(expr);
|
||||
|
||||
if (Match(1, LParen)) {
|
||||
free(expr);
|
||||
expr = Expression();
|
||||
|
||||
Expr* temp = calloc(1, sizeof(Expr));
|
||||
@@ -147,8 +142,7 @@ Expr* Primary() {
|
||||
return temp;
|
||||
}
|
||||
|
||||
free(expr);
|
||||
printf("Failed to match Primary\n");
|
||||
printf("Bad expression\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -190,6 +184,30 @@ Token* AdvanceParser() {
|
||||
return Previous();
|
||||
}
|
||||
|
||||
void SynchronizeParser(void) {
|
||||
AdvanceParser();
|
||||
//Discard tokens until we find a statement boundary, or at least something that looks like one.
|
||||
while(!ParserAtEnd()) {
|
||||
if (Previous()->type == Semicolon) return;
|
||||
|
||||
switch(ParserPeek()->type) {
|
||||
case CLASS:
|
||||
case FOR:
|
||||
case FUN:
|
||||
case IF:
|
||||
case PRINT:
|
||||
case RETURN:
|
||||
case VAR:
|
||||
case WHILE:
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
}
|
||||
|
||||
void PrintExpressionTree(const Expr* tree) {
|
||||
if (!tree) return;
|
||||
|
||||
@@ -209,7 +227,7 @@ void PrintExpressionTree(const Expr* tree) {
|
||||
else if (tree->type == GROUPING) {
|
||||
PrintExpressionTree(tree->expression.Grouping.expression);
|
||||
}
|
||||
else if (tree->type == LITERAL) printf("%s", tree->expression.Literal.type->lexeme);
|
||||
else if (tree->type == LITERAL) printf("%s", tree->expression.Literal->lexeme);
|
||||
}
|
||||
|
||||
void FreeExpressionTree(Expr* tree) {
|
||||
|
||||
Reference in New Issue
Block a user