Initial commit
This commit is contained in:
commit
84e04fbe15
50
java/com/craftinginterpreters/lox/AstPrinter.java
Normal file
50
java/com/craftinginterpreters/lox/AstPrinter.java
Normal file
@ -0,0 +1,50 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
class AstPrinter implements Expr.Visitor<String> {
|
||||
String print(Expr expr) {
|
||||
return expr.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitBinaryExpr(Expr.Binary expr) {
|
||||
return parenthesize(expr.operator.lexeme, expr.left, expr.right);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitGroupingExpr(Expr.Grouping expr) {
|
||||
return parenthesize("group", expr.expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitLiteralExpr(Expr.Literal expr) {
|
||||
if (expr.value == null)
|
||||
return "nil";
|
||||
return expr.value.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitUnaryExpr(Expr.Unary expr) {
|
||||
return parenthesize(expr.operator.lexeme, expr.right);
|
||||
}
|
||||
|
||||
private String parenthesize(String name, Expr... exprs) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
builder.append("(").append(name);
|
||||
for (Expr expr : exprs) {
|
||||
builder.append(" ");
|
||||
builder.append(expr.accept(this));
|
||||
}
|
||||
builder.append(")");
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Expr expression = new Expr.Binary(
|
||||
new Expr.Unary(new Token(TokenType.MINUS, "-", null, 1), new Expr.Literal(123)),
|
||||
new Token(TokenType.STAR, "*", null, 1), new Expr.Grouping(new Expr.Literal(45.67)));
|
||||
|
||||
System.out.println(new AstPrinter().print(expression));
|
||||
}
|
||||
}
|
||||
72
java/com/craftinginterpreters/lox/Expr.java
Normal file
72
java/com/craftinginterpreters/lox/Expr.java
Normal file
@ -0,0 +1,72 @@
|
||||
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);
|
||||
}
|
||||
|
||||
static class Binary extends Expr {
|
||||
Binary(Expr left, Token operator, Expr right) {
|
||||
this.left = left;
|
||||
this.operator = operator;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitBinaryExpr(this);
|
||||
}
|
||||
|
||||
final Expr left;
|
||||
final Token operator;
|
||||
final Expr right;
|
||||
}
|
||||
|
||||
static class Grouping extends Expr {
|
||||
Grouping(Expr expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitGroupingExpr(this);
|
||||
}
|
||||
|
||||
final Expr expression;
|
||||
}
|
||||
|
||||
static class Literal extends Expr {
|
||||
Literal(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitLiteralExpr(this);
|
||||
}
|
||||
|
||||
final Object value;
|
||||
}
|
||||
|
||||
static class Unary extends Expr {
|
||||
Unary(Token operator, Expr right) {
|
||||
this.operator = operator;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitUnaryExpr(this);
|
||||
}
|
||||
|
||||
final Token operator;
|
||||
final Expr right;
|
||||
}
|
||||
|
||||
abstract <R> R accept(Visitor<R> visitor);
|
||||
}
|
||||
66
java/com/craftinginterpreters/lox/Lox.java
Normal file
66
java/com/craftinginterpreters/lox/Lox.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
public class Lox {
|
||||
static boolean hadError = false;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length > 1) {
|
||||
System.out.println("Usage: jlox [script]");
|
||||
System.exit(64);
|
||||
} else if (args.length == 1) {
|
||||
runFile(args[0]);
|
||||
} else {
|
||||
runPrompt();
|
||||
}
|
||||
}
|
||||
|
||||
private static void runFile(String path) throws IOException {
|
||||
byte[] bytes = Files.readAllBytes(Paths.get(path));
|
||||
run(new String(bytes, Charset.defaultCharset()));
|
||||
if (hadError)
|
||||
System.exit(65);
|
||||
}
|
||||
|
||||
private static void runPrompt() throws IOException {
|
||||
InputStreamReader input = new InputStreamReader(System.in);
|
||||
BufferedReader reader = new BufferedReader(input);
|
||||
|
||||
for (;;) {
|
||||
System.out.print("> ");
|
||||
String line = reader.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
run(line);
|
||||
hadError = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void run(String source) {
|
||||
Scanner scanner = new Scanner(source);
|
||||
List<Token> tokens = scanner.scanTokens();
|
||||
|
||||
// For now, just print the tokens.
|
||||
for (Token token : tokens) {
|
||||
System.out.println(token);
|
||||
}
|
||||
}
|
||||
|
||||
static void error(int line, String message) {
|
||||
report(line, "", message);
|
||||
}
|
||||
|
||||
private static void report(int line, String where, String message) {
|
||||
System.err.println(
|
||||
"[line " + line + "] Error" + where + ": " + message);
|
||||
hadError = true;
|
||||
}
|
||||
|
||||
}
|
||||
229
java/com/craftinginterpreters/lox/Scanner.java
Normal file
229
java/com/craftinginterpreters/lox/Scanner.java
Normal file
@ -0,0 +1,229 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.craftinginterpreters.lox.TokenType.*;
|
||||
|
||||
class Scanner {
|
||||
private final String source;
|
||||
private final List<Token> tokens = new ArrayList();
|
||||
private int start = 0;
|
||||
private int current = 0;
|
||||
private int line = 1;
|
||||
private static final Map<String, TokenType> keywords;
|
||||
|
||||
static {
|
||||
keywords = new HashMap<>();
|
||||
keywords.put("and", AND);
|
||||
keywords.put("class", CLASS);
|
||||
keywords.put("else", ELSE);
|
||||
keywords.put("false", FALSE);
|
||||
keywords.put("for", FOR);
|
||||
keywords.put("fun", FUN);
|
||||
keywords.put("if", IF);
|
||||
keywords.put("nil", NIL);
|
||||
keywords.put("or", OR);
|
||||
keywords.put("print", PRINT);
|
||||
keywords.put("return", RETURN);
|
||||
keywords.put("super", SUPER);
|
||||
keywords.put("this", THIS);
|
||||
keywords.put("true", TRUE);
|
||||
keywords.put("var", VAR);
|
||||
keywords.put("while", WHILE);
|
||||
}
|
||||
|
||||
Scanner(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
List<Token> scanTokens() {
|
||||
while (!isAtEnd()) {
|
||||
// We are at the beginning of the next lexeme.
|
||||
start = current;
|
||||
scanToken();
|
||||
}
|
||||
|
||||
tokens.add(new Token(EOF, "", null, line));
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private boolean isAtEnd() {
|
||||
return current >= source.length();
|
||||
}
|
||||
|
||||
private char advance() {
|
||||
return source.charAt(current++);
|
||||
}
|
||||
|
||||
private void addToken(TokenType type) {
|
||||
addToken(type, null);
|
||||
}
|
||||
|
||||
private void addToken(TokenType type, Object literal) {
|
||||
String text = source.substring(start, current);
|
||||
tokens.add(new Token(type, text, literal, line));
|
||||
}
|
||||
|
||||
private void scanToken() {
|
||||
char c = advance();
|
||||
switch (c) {
|
||||
case '(':
|
||||
addToken(LEFT_PAREN);
|
||||
break;
|
||||
case ')':
|
||||
addToken(RIGHT_PAREN);
|
||||
break;
|
||||
case '{':
|
||||
addToken(LEFT_BRACE);
|
||||
break;
|
||||
case '}':
|
||||
addToken(RIGHT_BRACE);
|
||||
break;
|
||||
case ',':
|
||||
addToken(COMMA);
|
||||
break;
|
||||
case '.':
|
||||
addToken(DOT);
|
||||
break;
|
||||
case '-':
|
||||
addToken(MINUS);
|
||||
break;
|
||||
case '+':
|
||||
addToken(PLUS);
|
||||
break;
|
||||
case ';':
|
||||
addToken(SEMICOLON);
|
||||
break;
|
||||
case '*':
|
||||
addToken(STAR);
|
||||
break;
|
||||
case '!':
|
||||
addToken(match('=') ? BANG_EQUAL : BANG);
|
||||
break;
|
||||
case '=':
|
||||
addToken(match('=') ? EQUAL_EQUAL : EQUAL);
|
||||
break;
|
||||
case '<':
|
||||
addToken(match('=') ? LESS_EQUAL : LESS);
|
||||
break;
|
||||
case '>':
|
||||
addToken(match('=') ? GREATER_EQUAL : GREATER);
|
||||
break;
|
||||
case '/':
|
||||
if (match('/')) {
|
||||
// A comment goes until the end of the line.
|
||||
while (peek() != '\n' && !isAtEnd())
|
||||
advance();
|
||||
} else {
|
||||
addToken(SLASH);
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
case '\r':
|
||||
case '\t':
|
||||
// Ignore whitespace.
|
||||
break;
|
||||
case '\n':
|
||||
line++;
|
||||
break;
|
||||
case '"':
|
||||
string();
|
||||
break;
|
||||
default:
|
||||
if (isDigit(c)) {
|
||||
number();
|
||||
} else if (isAlpha(c)) {
|
||||
identifier();
|
||||
} else {
|
||||
Lox.error(line, "Unexpected character.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void identifier() {
|
||||
while (isAlphaNumeric(peek()))
|
||||
advance();
|
||||
|
||||
String text = source.substring(start, current);
|
||||
TokenType type = keywords.get(text);
|
||||
if (type == null)
|
||||
type = IDENTIFIER;
|
||||
addToken(type);
|
||||
}
|
||||
|
||||
private void number() {
|
||||
while (isDigit(peek()))
|
||||
advance();
|
||||
|
||||
// Look for a fractional part.
|
||||
if (peek() == '.' && isDigit(peekNext())) {
|
||||
// Consume the "."
|
||||
advance();
|
||||
|
||||
while (isDigit(peek()))
|
||||
advance();
|
||||
}
|
||||
|
||||
addToken(NUMBER,
|
||||
Double.parseDouble(source.substring(start, current)));
|
||||
}
|
||||
|
||||
private void string() {
|
||||
while (peek() != '"' && !isAtEnd()) {
|
||||
if (peek() == '\n')
|
||||
line++;
|
||||
advance();
|
||||
}
|
||||
|
||||
if (isAtEnd()) {
|
||||
Lox.error(line, "Unterminated string.");
|
||||
return;
|
||||
}
|
||||
|
||||
advance(); // The closing ".
|
||||
|
||||
// Trim the surrounding quotes.
|
||||
String value = source.substring(start + 1, current - 1);
|
||||
addToken(STRING, value);
|
||||
}
|
||||
|
||||
private boolean match(char expected) {
|
||||
if (isAtEnd())
|
||||
return false;
|
||||
if (source.charAt(current) != expected)
|
||||
return false;
|
||||
|
||||
current++;
|
||||
return true;
|
||||
}
|
||||
|
||||
private char peek() {
|
||||
if (isAtEnd())
|
||||
return '\0';
|
||||
return source.charAt(current);
|
||||
}
|
||||
|
||||
private char peekNext() {
|
||||
if (current + 1 >= source.length())
|
||||
return '\0';
|
||||
return source.charAt(current + 1);
|
||||
}
|
||||
|
||||
private boolean isAlpha(char c) {
|
||||
return (c >= 'a' && c <= 'z') ||
|
||||
(c >= 'A' && c <= 'Z') ||
|
||||
c == '_';
|
||||
}
|
||||
|
||||
private boolean isAlphaNumeric(char c) {
|
||||
return isAlpha(c) || isDigit(c);
|
||||
}
|
||||
|
||||
private boolean isDigit(char c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
}
|
||||
19
java/com/craftinginterpreters/lox/Token.java
Normal file
19
java/com/craftinginterpreters/lox/Token.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
class Token {
|
||||
final TokenType type;
|
||||
final String lexeme;
|
||||
final Object literal;
|
||||
final int line;
|
||||
|
||||
Token(TokenType type, String lexeme, Object literal, int line) {
|
||||
this.type = type;
|
||||
this.lexeme = lexeme;
|
||||
this.literal = literal;
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return type + " " + lexeme + " " + literal;
|
||||
}
|
||||
}
|
||||
20
java/com/craftinginterpreters/lox/TokenType.java
Normal file
20
java/com/craftinginterpreters/lox/TokenType.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
enum TokenType {
|
||||
// Single character tokens.
|
||||
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
|
||||
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR,
|
||||
|
||||
// One or two character tokens.
|
||||
BANG, BANG_EQUAL, EQUAL, EQUAL_EQUAL,
|
||||
GREATER, GREATER_EQUAL, LESS, LESS_EQUAL,
|
||||
|
||||
// Literals.
|
||||
IDENTIFIER, STRING, NUMBER,
|
||||
|
||||
// Keywords.
|
||||
AND, CLASS, ELSE, FALSE, FUN, FOR, IF, NIL, OR,
|
||||
PRINT, RETURN, SUPER, THIS, TRUE, VAR, WHILE,
|
||||
|
||||
EOF
|
||||
}
|
||||
93
java/com/craftinginterpreters/tool/GenerateAst.java
Normal file
93
java/com/craftinginterpreters/tool/GenerateAst.java
Normal file
@ -0,0 +1,93 @@
|
||||
package com.craftinginterpreters.tool;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateAst {
|
||||
public static void main(String[] args) throws IOException {
|
||||
// if (args.length != 1) {
|
||||
// System.err.println("Usage: generate_ast <output directory>");
|
||||
// System.exit(64);
|
||||
// }
|
||||
// String outputDir = args[0];
|
||||
String outputDir = "com/craftinginterpreters/lox";
|
||||
defineAst(outputDir, "Expr", Arrays.asList(
|
||||
"Binary : Expr left, Token operator, Expr right",
|
||||
"Grouping : Expr expression",
|
||||
"Literal : Object value",
|
||||
"Unary : Token operator, Expr right"));
|
||||
}
|
||||
|
||||
private static void defineAst(
|
||||
String outputDir, String baseName, List<String> types) throws IOException {
|
||||
String path = outputDir + "/" + baseName + ".java";
|
||||
PrintWriter writer = new PrintWriter(path, "UTF-8");
|
||||
|
||||
writer.println("package com.craftinginterpreters.lox;");
|
||||
writer.println();
|
||||
writer.println("import java.util.List;");
|
||||
writer.println();
|
||||
writer.println("abstract class " + baseName + " {");
|
||||
|
||||
defineVisitor(writer, baseName, types);
|
||||
|
||||
// The AST clasess.
|
||||
for (String type : types) {
|
||||
String className = type.split(":")[0].trim();
|
||||
String fields = type.split(":")[1].trim();
|
||||
defineType(writer, baseName, className, fields);
|
||||
}
|
||||
|
||||
// The base accept() method.
|
||||
writer.println(" abstract <R> R accept(Visitor<R> visitor);");
|
||||
|
||||
writer.println("}");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static void defineVisitor(PrintWriter writer, String baseName, List<String> types) {
|
||||
writer.println(" interface Visitor<R> {");
|
||||
|
||||
for (String type : types) {
|
||||
String typeName = type.split(":")[0].trim();
|
||||
writer.println(" R visit" + typeName + baseName + "(" + typeName + " " + baseName.toLowerCase() + ");");
|
||||
}
|
||||
|
||||
writer.println(" }");
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private static void defineType(PrintWriter writer, String baseName, String className, String fieldList) {
|
||||
writer.println(" static class " + className + " extends " + baseName + " {");
|
||||
|
||||
// Constructor.
|
||||
writer.println(" " + className + "(" + fieldList + ") {");
|
||||
|
||||
// Store parameters in fields.
|
||||
String[] fields = fieldList.split(", ");
|
||||
for (String field : fields) {
|
||||
String name = field.split(" ")[1];
|
||||
writer.println(" this." + name + " = " + name + ";");
|
||||
}
|
||||
|
||||
writer.println(" }");
|
||||
|
||||
// Visitor pattern.
|
||||
writer.println();
|
||||
writer.println(" @Override");
|
||||
writer.println(" <R> R accept(Visitor<R> visitor) {");
|
||||
writer.println(" return visitor.visit" + className + baseName + "(this);");
|
||||
writer.println(" }");
|
||||
|
||||
// Fields.
|
||||
writer.println();
|
||||
for (String field : fields) {
|
||||
writer.println(" final " + field + ";");
|
||||
}
|
||||
|
||||
writer.println(" }");
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user