1
1
package com .annimon .ownlang ;
2
2
3
+ import com .annimon .ownlang .exceptions .LexerException ;
3
4
import com .annimon .ownlang .parser .Lexer ;
4
5
import com .annimon .ownlang .parser .Parser ;
5
6
import com .annimon .ownlang .parser .SourceLoader ;
9
10
import com .annimon .ownlang .parser .visitors .FunctionAdder ;
10
11
import java .io .IOException ;
11
12
import java .util .List ;
13
+ import java .util .Scanner ;
12
14
import java .util .concurrent .TimeUnit ;
13
15
14
16
/**
15
17
* @author aNNiMON
16
18
*/
17
19
public final class Main {
20
+
21
+ private static final String VERSION = "1.1.0" ;
18
22
19
23
public static void main (String [] args ) throws IOException {
20
24
if (args .length == 0 ) {
21
25
try {
22
26
run (SourceLoader .readSource ("program.own" ), true , true , true );
23
27
} catch (IOException ioe ) {
24
- System .out .println ("OwnLang version 1.1.0 \n \n " +
28
+ System .out .println ("OwnLang version " + VERSION + " \n \n " +
25
29
"Usage: ownlang [options]\n " +
26
30
" options:\n " +
27
31
" -f, --file [input] Run program file. Required.\n " +
32
+ " -r, --repl Enter to a REPL mode\n " +
28
33
" -a, --showast Show AST of program\n " +
29
34
" -t, --showtokens Show lexical tokens\n " +
30
35
" -m, --showtime Show elapsed time of parsing and execution" );
@@ -51,6 +56,11 @@ public static void main(String[] args) throws IOException {
51
56
showMeasurements = true ;
52
57
break ;
53
58
59
+ case "-r" :
60
+ case "--repl" :
61
+ repl ();
62
+ return ;
63
+
54
64
case "-f" :
55
65
case "--file" :
56
66
if (i + 1 < args .length ) {
@@ -106,4 +116,42 @@ private static void run(String input, boolean showTokens, boolean showAst, boole
106
116
}
107
117
}
108
118
}
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
+ }
109
157
}
0 commit comments