From 23f0b340d36ac858cb6f5a0b411728a576e1d935 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 4 Mar 2016 20:21:31 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20REPL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- program.own | 2 +- src/com/annimon/ownlang/Main.java | 50 +++++++++++++++++++- src/com/annimon/ownlang/lib/modules/std.java | 6 +-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/program.own b/program.own index d49a542..e0406c4 100644 --- a/program.own +++ b/program.own @@ -242,4 +242,4 @@ println 1 :: 2 :: 3 println "\u042a" -include "visitor.own" \ No newline at end of file +include "visitor.own" diff --git a/src/com/annimon/ownlang/Main.java b/src/com/annimon/ownlang/Main.java index eb15b65..62e7720 100644 --- a/src/com/annimon/ownlang/Main.java +++ b/src/com/annimon/ownlang/Main.java @@ -1,5 +1,6 @@ package com.annimon.ownlang; +import com.annimon.ownlang.exceptions.LexerException; import com.annimon.ownlang.parser.Lexer; import com.annimon.ownlang.parser.Parser; import com.annimon.ownlang.parser.SourceLoader; @@ -9,22 +10,26 @@ import com.annimon.ownlang.parser.visitors.AssignValidator; import com.annimon.ownlang.parser.visitors.FunctionAdder; import java.io.IOException; import java.util.List; +import java.util.Scanner; import java.util.concurrent.TimeUnit; /** * @author aNNiMON */ public final class Main { + + private static final String VERSION = "1.1.0"; public static void main(String[] args) throws IOException { if (args.length == 0) { try { run(SourceLoader.readSource("program.own"), true, true, true); } 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" + " options:\n" + " -f, --file [input] Run program file. Required.\n" + + " -r, --repl Enter to a REPL mode\n" + " -a, --showast Show AST of program\n" + " -t, --showtokens Show lexical tokens\n" + " -m, --showtime Show elapsed time of parsing and execution"); @@ -51,6 +56,11 @@ public final class Main { showMeasurements = true; break; + case "-r": + case "--repl": + repl(); + return; + case "-f": case "--file": 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 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(); + } } diff --git a/src/com/annimon/ownlang/lib/modules/std.java b/src/com/annimon/ownlang/lib/modules/std.java index 7fceb41..d304764 100644 --- a/src/com/annimon/ownlang/lib/modules/std.java +++ b/src/com/annimon/ownlang/lib/modules/std.java @@ -39,8 +39,8 @@ public final class std implements Module { Functions.set("newarray", new std_newarray()); Functions.set("sort", new std_sort()); Functions.set("arrayCombine", new std_arrayCombine()); - Functions.set("arrayKeyExists ", new std_arrayKeyExists()); - Functions.set("arrayKeys ", new std_arrayKeys()); - Functions.set("arrayValues ", new std_arrayValues()); + Functions.set("arrayKeyExists", new std_arrayKeyExists()); + Functions.set("arrayKeys", new std_arrayKeys()); + Functions.set("arrayValues", new std_arrayValues()); } }