Finish chapter 6

This commit is contained in:
Michael Smith 2022-07-06 14:50:11 +02:00
parent 472a589502
commit bdf3a3db34
3 changed files with 194 additions and 6 deletions

View File

@ -1,12 +1,13 @@
package com.craftinginterpreters.lox;
import java.util.List;
abstract class Expr {
interface Visitor<R> {
R visitBinaryExpr(Binary expr);
R visitGroupingExpr(Grouping expr);
R visitLiteralExpr(Literal expr);
R visitUnaryExpr(Unary expr);
}

View File

@ -46,11 +46,14 @@ public class Lox {
private static void run(String source) {
Scanner scanner = new Scanner(source);
List<Token> tokens = scanner.scanTokens();
Parser parser = new Parser(tokens);
Expr expression = parser.parse();
// For now, just print the tokens.
for (Token token : tokens) {
System.out.println(token);
}
// Stop if there was a syntax error.
if (hadError)
return;
System.out.println(new AstPrinter().print(expression));
}
static void error(int line, String message) {
@ -63,4 +66,11 @@ public class Lox {
hadError = true;
}
static void error(Token token, String message) {
if (token.type == TokenType.EOF) {
report(token.line, " at end", message);
} else {
report(token.line, " at '" + token.lexeme + "'", message);
}
}
}

View File

@ -0,0 +1,177 @@
package com.craftinginterpreters.lox;
import java.util.List;
import static com.craftinginterpreters.lox.TokenType.*;
class Parser {
private static class ParseError extends RuntimeException {
}
private final List<Token> tokens;
private int current = 0;
Parser(List<Token> tokens) {
this.tokens = tokens;
}
Expr parse() {
try {
return expression();
} catch (ParseError error) {
return null;
}
}
private Expr expression() {
return equality();
}
private Expr equality() {
Expr expr = comparison();
while (match(BANG_EQUAL, EQUAL_EQUAL)) {
Token operator = previous();
Expr right = comparison();
expr = new Expr.Binary(expr, operator, right);
}
return expr;
}
private Expr comparison() {
Expr expr = term();
while (match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL)) {
Token operator = previous();
Expr right = term();
expr = new Expr.Binary(expr, operator, right);
}
return expr;
}
private Expr term() {
Expr expr = factor();
while (match(MINUS, PLUS)) {
Token operator = previous();
Expr right = factor();
expr = new Expr.Binary(expr, operator, right);
}
return expr;
}
private Expr factor() {
Expr expr = unary();
while (match(SLASH, STAR)) {
Token operator = previous();
Expr right = unary();
expr = new Expr.Binary(expr, operator, right);
}
return expr;
}
private Expr unary() {
if (match(BANG, MINUS)) {
Token operator = previous();
Expr right = unary();
return new Expr.Unary(operator, right);
}
return primary();
}
private Expr primary() {
if (match(FALSE))
return new Expr.Literal(false);
if (match(TRUE))
return new Expr.Literal(true);
if (match(NIL))
return new Expr.Literal(null);
if (match(NUMBER, STRING)) {
return new Expr.Literal(previous().literal);
}
if (match(LEFT_PAREN)) {
Expr expr = expression();
consume(RIGHT_PAREN, "Expect ')' after expression.");
return new Expr.Grouping(expr);
}
throw error(peek(), "Expect expression.");
}
private boolean match(TokenType... types) {
for (TokenType type : types) {
if (check(type)) {
advance();
return true;
}
}
return false;
}
private Token consume(TokenType type, String message) {
if (check(type))
return advance();
throw error(peek(), message);
}
private boolean check(TokenType type) {
if (isAtEnd())
return false;
return peek().type == type;
}
private Token advance() {
if (!isAtEnd())
current++;
return previous();
}
private boolean isAtEnd() {
return peek().type == EOF;
}
private Token peek() {
return tokens.get(current);
}
private Token previous() {
return tokens.get(current - 1);
}
private ParseError error(Token token, String message) {
Lox.error(token, message);
return new ParseError();
}
private void synchronize() {
advance();
while (!isAtEnd()) {
if (previous().type == SEMICOLON)
return;
switch (peek().type) {
case CLASS:
case FOR:
case FUN:
case IF:
case PRINT:
case RETURN:
case VAR:
case WHILE:
return;
}
advance();
}
}
}