Добавлен REPL

This commit is contained in:
Victor 2016-03-04 20:21:31 +02:00
parent c67060a7f3
commit 23f0b340d3
3 changed files with 53 additions and 5 deletions

View File

@ -1,5 +1,6 @@
package com.annimon.ownlang; package com.annimon.ownlang;
import com.annimon.ownlang.exceptions.LexerException;
import com.annimon.ownlang.parser.Lexer; import com.annimon.ownlang.parser.Lexer;
import com.annimon.ownlang.parser.Parser; import com.annimon.ownlang.parser.Parser;
import com.annimon.ownlang.parser.SourceLoader; import com.annimon.ownlang.parser.SourceLoader;
@ -9,6 +10,7 @@ import com.annimon.ownlang.parser.visitors.AssignValidator;
import com.annimon.ownlang.parser.visitors.FunctionAdder; import com.annimon.ownlang.parser.visitors.FunctionAdder;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -16,15 +18,18 @@ import java.util.concurrent.TimeUnit;
*/ */
public final class Main { public final class Main {
private static final String VERSION = "1.1.0";
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
if (args.length == 0) { if (args.length == 0) {
try { try {
run(SourceLoader.readSource("program.own"), true, true, true); run(SourceLoader.readSource("program.own"), true, true, true);
} catch (IOException ioe) { } catch (IOException ioe) {
System.out.println("OwnLang version 1.1.0\n\n" + System.out.println("OwnLang version " + VERSION + "\n\n" +
"Usage: ownlang [options]\n" + "Usage: ownlang [options]\n" +
" options:\n" + " options:\n" +
" -f, --file [input] Run program file. Required.\n" + " -f, --file [input] Run program file. Required.\n" +
" -r, --repl Enter to a REPL mode\n" +
" -a, --showast Show AST of program\n" + " -a, --showast Show AST of program\n" +
" -t, --showtokens Show lexical tokens\n" + " -t, --showtokens Show lexical tokens\n" +
" -m, --showtime Show elapsed time of parsing and execution"); " -m, --showtime Show elapsed time of parsing and execution");
@ -51,6 +56,11 @@ public final class Main {
showMeasurements = true; showMeasurements = true;
break; break;
case "-r":
case "--repl":
repl();
return;
case "-f": case "-f":
case "--file": case "--file":
if (i + 1 < args.length) { if (i + 1 < args.length) {
@ -106,4 +116,42 @@ public final class Main {
} }
} }
} }
private static void repl() {
final StringBuilder buffer = new StringBuilder();
final Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to OwnLang " + VERSION + " REPL\n"
+ "Type in expressions to have them evaluated.\n"
+ "Type :reset to clear buffer.\n"
+ "Type :exit to exit REPL.");
while (true) {
System.out.print((buffer.length() == 0) ? "\n> " : " ");
if (!scanner.hasNextLine()) break;
final String line = scanner.nextLine();
if (":exit".equalsIgnoreCase(line)) break;
if (":reset".equalsIgnoreCase(line)) {
buffer.setLength(0);
continue;
}
buffer.append(line).append(System.lineSeparator());
try {
final List<Token> tokens = Lexer.tokenize(buffer.toString());
final Parser parser = new Parser(tokens);
final Statement program = parser.parse();
if (parser.getParseErrors().hasErrors()) {
continue;
}
program.execute();
} catch (LexerException lex) {
continue;
} catch (Exception ex) {
Console.handleException(Thread.currentThread(), ex);
}
buffer.setLength(0);
}
scanner.close();
}
} }