Skip to content

Commit 23f0b34

Browse files
committed
Добавлен REPL
1 parent c67060a commit 23f0b34

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

program.own

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,4 @@ println 1 :: 2 :: 3
242242

243243
println "\u042a"
244244

245-
include "visitor.own"
245+
include "visitor.own"

src/com/annimon/ownlang/Main.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.annimon.ownlang;
22

3+
import com.annimon.ownlang.exceptions.LexerException;
34
import com.annimon.ownlang.parser.Lexer;
45
import com.annimon.ownlang.parser.Parser;
56
import com.annimon.ownlang.parser.SourceLoader;
@@ -9,22 +10,26 @@
910
import com.annimon.ownlang.parser.visitors.FunctionAdder;
1011
import java.io.IOException;
1112
import java.util.List;
13+
import java.util.Scanner;
1214
import java.util.concurrent.TimeUnit;
1315

1416
/**
1517
* @author aNNiMON
1618
*/
1719
public final class Main {
20+
21+
private static final String VERSION = "1.1.0";
1822

1923
public static void main(String[] args) throws IOException {
2024
if (args.length == 0) {
2125
try {
2226
run(SourceLoader.readSource("program.own"), true, true, true);
2327
} catch (IOException ioe) {
24-
System.out.println("OwnLang version 1.1.0\n\n" +
28+
System.out.println("OwnLang version " + VERSION + "\n\n" +
2529
"Usage: ownlang [options]\n" +
2630
" options:\n" +
2731
" -f, --file [input] Run program file. Required.\n" +
32+
" -r, --repl Enter to a REPL mode\n" +
2833
" -a, --showast Show AST of program\n" +
2934
" -t, --showtokens Show lexical tokens\n" +
3035
" -m, --showtime Show elapsed time of parsing and execution");
@@ -51,6 +56,11 @@ public static void main(String[] args) throws IOException {
5156
showMeasurements = true;
5257
break;
5358

59+
case "-r":
60+
case "--repl":
61+
repl();
62+
return;
63+
5464
case "-f":
5565
case "--file":
5666
if (i + 1 < args.length) {
@@ -106,4 +116,42 @@ private static void run(String input, boolean showTokens, boolean showAst, boole
106116
}
107117
}
108118
}
119+
120+
private static void repl() {
121+
final StringBuilder buffer = new StringBuilder();
122+
final Scanner scanner = new Scanner(System.in);
123+
System.out.println("Welcome to OwnLang " + VERSION + " REPL\n"
124+
+ "Type in expressions to have them evaluated.\n"
125+
+ "Type :reset to clear buffer.\n"
126+
+ "Type :exit to exit REPL.");
127+
while (true) {
128+
System.out.print((buffer.length() == 0) ? "\n> " : " ");
129+
130+
if (!scanner.hasNextLine()) break;
131+
132+
final String line = scanner.nextLine();
133+
if (":exit".equalsIgnoreCase(line)) break;
134+
if (":reset".equalsIgnoreCase(line)) {
135+
buffer.setLength(0);
136+
continue;
137+
}
138+
139+
buffer.append(line).append(System.lineSeparator());
140+
try {
141+
final List<Token> tokens = Lexer.tokenize(buffer.toString());
142+
final Parser parser = new Parser(tokens);
143+
final Statement program = parser.parse();
144+
if (parser.getParseErrors().hasErrors()) {
145+
continue;
146+
}
147+
program.execute();
148+
} catch (LexerException lex) {
149+
continue;
150+
} catch (Exception ex) {
151+
Console.handleException(Thread.currentThread(), ex);
152+
}
153+
buffer.setLength(0);
154+
}
155+
scanner.close();
156+
}
109157
}

src/com/annimon/ownlang/lib/modules/std.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public void init() {
3939
Functions.set("newarray", new std_newarray());
4040
Functions.set("sort", new std_sort());
4141
Functions.set("arrayCombine", new std_arrayCombine());
42-
Functions.set("arrayKeyExists ", new std_arrayKeyExists());
43-
Functions.set("arrayKeys ", new std_arrayKeys());
44-
Functions.set("arrayValues ", new std_arrayValues());
42+
Functions.set("arrayKeyExists", new std_arrayKeyExists());
43+
Functions.set("arrayKeys", new std_arrayKeys());
44+
Functions.set("arrayValues", new std_arrayValues());
4545
}
4646
}

0 commit comments

Comments
 (0)