Added statements to the Parser, currently just the Print and Expression statements. 8.1 isn't quite done yet, maybe half way there.
This commit is contained in:
+33
-1
@@ -1,5 +1,6 @@
|
||||
#include "parser.h"
|
||||
#include "expr.h"
|
||||
#include "statement.h"
|
||||
#include "token.h"
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -10,6 +11,9 @@ Expr* Term(void);
|
||||
Expr* Factor(void);
|
||||
Expr* Unary(void);
|
||||
Expr* Primary(void);
|
||||
Stmt* Statement(void);
|
||||
Stmt* PrintStatement(void);
|
||||
Stmt* ExpressionStatement(void);
|
||||
int Match(int, ...);
|
||||
int Check(TokenType);
|
||||
int ParserAtEnd(void);
|
||||
@@ -32,6 +36,34 @@ Expr* Expression() {
|
||||
return Equality();
|
||||
}
|
||||
|
||||
Stmt* Statement(void) {
|
||||
if (Match(1, PRINT)) return PrintStatement();
|
||||
|
||||
return ExpressionStatement();
|
||||
}
|
||||
|
||||
Stmt* PrintStatement(void) {
|
||||
Expr* value = Expression();
|
||||
|
||||
if (!Match(1, Semicolon)) fprintf(stderr, "Expected ';' after value\n");
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
return CreateStatement(value, STMT_Print);
|
||||
}
|
||||
|
||||
Stmt* ExpressionStatement(void) {
|
||||
Expr* expr = Expression();
|
||||
|
||||
if (!Match(1, Semicolon)) fprintf(stderr, "Expected ';' after expression\n");
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
return CreateStatement(expr, STMT_Expression);
|
||||
}
|
||||
|
||||
Stmt* ExpressionStatement(void);
|
||||
|
||||
Expr* Equality() {
|
||||
Expr* expr = Comparison();
|
||||
|
||||
@@ -146,7 +178,7 @@ Expr* Primary() {
|
||||
return temp;
|
||||
}
|
||||
|
||||
printf("Bad expression\n");
|
||||
fprintf(stderr, "Bad expression, this should be unreachable.\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user