Initial commit, introduced Scanner
This commit is contained in:
39
.gitignore
vendored
Normal file
39
.gitignore
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
.kotlin
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
34
pom.xml
Normal file
34
pom.xml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.craftinginterpreters.lox</groupId>
|
||||||
|
<artifactId>jlox</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>25</maven.compiler.source>
|
||||||
|
<maven.compiler.target>25</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.4.1</version>
|
||||||
|
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.craftinginterpreters.lox.Lox</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
66
src/main/java/com/craftinginterpreters/lox/Lox.java
Normal file
66
src/main/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;
|
||||||
|
|
||||||
|
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||||
|
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||||
|
public class Lox {
|
||||||
|
private static boolean hadError = false;
|
||||||
|
|
||||||
|
static void main(String[] args) throws IOException {
|
||||||
|
if (args.length > 1){
|
||||||
|
System.exit(64);
|
||||||
|
}
|
||||||
|
if (args.length == 1){ // Filepath
|
||||||
|
runFile(args[0]);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
runPrompt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void runFile(String filePath) throws IOException {
|
||||||
|
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
|
||||||
|
run(new String(bytes, Charset.defaultCharset()));
|
||||||
|
if(hadError) System.exit(65);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void runPrompt() throws IOException {
|
||||||
|
InputStreamReader stream = new InputStreamReader(System.in);
|
||||||
|
BufferedReader reader = new BufferedReader(stream);
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
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 (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;
|
||||||
|
}
|
||||||
|
}
|
||||||
202
src/main/java/com/craftinginterpreters/lox/Scanner.java
Normal file
202
src/main/java/com/craftinginterpreters/lox/Scanner.java
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
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 = 0;
|
||||||
|
|
||||||
|
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 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.");
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The closing ".
|
||||||
|
advance();
|
||||||
|
|
||||||
|
// 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';
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/main/java/com/craftinginterpreters/lox/Token.java
Normal file
19
src/main/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;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/main/java/com/craftinginterpreters/lox/TokenType.java
Normal file
22
src/main/java/com/craftinginterpreters/lox/TokenType.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user