diff --git a/expr.c b/expr.c index ac33163..42515bd 100644 --- a/expr.c +++ b/expr.c @@ -2,5 +2,5 @@ void VisitBinary(struct binary); void VisitGrouping(struct grouping); -void VisitLiteral(struct literal); +void VisitLiteral(Token); void VisitUnary(struct unary); \ No newline at end of file diff --git a/expr.h b/expr.h index eee6a95..ba0ff14 100644 --- a/expr.h +++ b/expr.h @@ -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 \ No newline at end of file diff --git a/lox.c b/lox.c index ac02ef8..8ea9ed5 100644 --- a/lox.c +++ b/lox.c @@ -42,6 +42,7 @@ void RunFile(const char* path) { printf("TREE:\n"); Expr* tree = GenerateExpressionTree(tokens); PrintExpressionTree(tree); + printf("\n"); FreeExpressionTree(tree); DestroyTokenList(tokens); } diff --git a/parser.c b/parser.c index 99f58c7..0080194 100644 --- a/parser.c +++ b/parser.c @@ -1,5 +1,6 @@ #include "parser.h" #include "expr.h" +#include "token.h" #include 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) { diff --git a/token.h b/token.h index 059872f..a9ead21 100644 --- a/token.h +++ b/token.h @@ -43,7 +43,6 @@ typedef struct { TokenType type; const char* lexeme; int line; - int length; } Token; extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];