Minor cleanup.

This commit is contained in:
2022-03-03 17:12:01 +00:00
parent d039a26c16
commit caf4f02428
5 changed files with 36 additions and 23 deletions
+1 -1
View File
@@ -2,5 +2,5 @@
void VisitBinary(struct binary);
void VisitGrouping(struct grouping);
void VisitLiteral(struct literal);
void VisitLiteral(Token);
void VisitUnary(struct unary);
+2 -7
View File
@@ -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
+1
View File
@@ -42,6 +42,7 @@ void RunFile(const char* path) {
printf("TREE:\n");
Expr* tree = GenerateExpressionTree(tokens);
PrintExpressionTree(tree);
printf("\n");
FreeExpressionTree(tree);
DestroyTokenList(tokens);
}
+32 -14
View File
@@ -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) {
-1
View File
@@ -43,7 +43,6 @@ typedef struct {
TokenType type;
const char* lexeme;
int line;
int length;
} Token;
extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];