Minor cleanup.
This commit is contained in:
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
void VisitBinary(struct binary);
|
void VisitBinary(struct binary);
|
||||||
void VisitGrouping(struct grouping);
|
void VisitGrouping(struct grouping);
|
||||||
void VisitLiteral(struct literal);
|
void VisitLiteral(Token);
|
||||||
void VisitUnary(struct unary);
|
void VisitUnary(struct unary);
|
||||||
@@ -24,11 +24,6 @@ struct grouping {
|
|||||||
struct Expr* expression;
|
struct Expr* expression;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct literal {
|
|
||||||
Token* type;
|
|
||||||
void* object;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct unary {
|
struct unary {
|
||||||
Token* op;
|
Token* op;
|
||||||
struct Expr* right;
|
struct Expr* right;
|
||||||
@@ -39,14 +34,14 @@ struct Expr {
|
|||||||
union ex {
|
union ex {
|
||||||
struct binary Binary;
|
struct binary Binary;
|
||||||
struct grouping Grouping;
|
struct grouping Grouping;
|
||||||
struct literal Literal;
|
Token* Literal;
|
||||||
struct unary Unary;
|
struct unary Unary;
|
||||||
} expression;
|
} expression;
|
||||||
};
|
};
|
||||||
|
|
||||||
void VisitBinary(struct binary);
|
void VisitBinary(struct binary);
|
||||||
void VisitGrouping(struct grouping);
|
void VisitGrouping(struct grouping);
|
||||||
void VisitLiteral(struct literal);
|
void VisitLiteral(Token);
|
||||||
void VisitUnary(struct unary);
|
void VisitUnary(struct unary);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -42,6 +42,7 @@ void RunFile(const char* path) {
|
|||||||
printf("TREE:\n");
|
printf("TREE:\n");
|
||||||
Expr* tree = GenerateExpressionTree(tokens);
|
Expr* tree = GenerateExpressionTree(tokens);
|
||||||
PrintExpressionTree(tree);
|
PrintExpressionTree(tree);
|
||||||
|
printf("\n");
|
||||||
FreeExpressionTree(tree);
|
FreeExpressionTree(tree);
|
||||||
DestroyTokenList(tokens);
|
DestroyTokenList(tokens);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
|
#include "token.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
Expr* Expression(void);
|
Expr* Expression(void);
|
||||||
@@ -15,6 +16,7 @@ int ParserAtEnd(void);
|
|||||||
Token* ParserPeek(void);
|
Token* ParserPeek(void);
|
||||||
Token* Previous(void);
|
Token* Previous(void);
|
||||||
Token* AdvanceParser(void);
|
Token* AdvanceParser(void);
|
||||||
|
void SynchronizeParser(void);
|
||||||
|
|
||||||
const TokenList* ListOfTokens;
|
const TokenList* ListOfTokens;
|
||||||
int Current = 0;
|
int Current = 0;
|
||||||
@@ -117,25 +119,18 @@ Expr* Primary() {
|
|||||||
expr->type = LITERAL;
|
expr->type = LITERAL;
|
||||||
|
|
||||||
if (Match(3, FALSE, TRUE, NIL)) {
|
if (Match(3, FALSE, TRUE, NIL)) {
|
||||||
expr->expression.Literal.type = ParserPeek();//CreateToken("false", FALSE);
|
expr->expression.Literal = ParserPeek();
|
||||||
return expr;
|
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)) {
|
if (Match(2, Number, String)) {
|
||||||
expr->expression.Literal.type = Previous();
|
expr->expression.Literal = Previous();
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(expr);
|
||||||
|
|
||||||
if (Match(1, LParen)) {
|
if (Match(1, LParen)) {
|
||||||
free(expr);
|
|
||||||
expr = Expression();
|
expr = Expression();
|
||||||
|
|
||||||
Expr* temp = calloc(1, sizeof(Expr));
|
Expr* temp = calloc(1, sizeof(Expr));
|
||||||
@@ -147,8 +142,7 @@ Expr* Primary() {
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(expr);
|
printf("Bad expression\n");
|
||||||
printf("Failed to match Primary\n");
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -190,6 +184,30 @@ Token* AdvanceParser() {
|
|||||||
return Previous();
|
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) {
|
void PrintExpressionTree(const Expr* tree) {
|
||||||
if (!tree) return;
|
if (!tree) return;
|
||||||
|
|
||||||
@@ -209,7 +227,7 @@ void PrintExpressionTree(const Expr* tree) {
|
|||||||
else if (tree->type == GROUPING) {
|
else if (tree->type == GROUPING) {
|
||||||
PrintExpressionTree(tree->expression.Grouping.expression);
|
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) {
|
void FreeExpressionTree(Expr* tree) {
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ typedef struct {
|
|||||||
TokenType type;
|
TokenType type;
|
||||||
const char* lexeme;
|
const char* lexeme;
|
||||||
int line;
|
int line;
|
||||||
int length;
|
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];
|
extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];
|
||||||
|
|||||||
Reference in New Issue
Block a user