37
37
#include < LibJS/Runtime/Object.h>
38
38
#include < LibJS/Runtime/PrimitiveString.h>
39
39
#include < LibJS/Runtime/Value.h>
40
+ #include < LibLineEdit/LineEditor.h>
40
41
#include < stdio.h>
41
42
42
43
bool dump_ast = false ;
44
+ static LineEditor editor {};
43
45
44
46
String read_next_piece ()
45
47
{
46
48
StringBuilder piece;
47
49
int level = 0 ;
50
+ StringBuilder prompt_builder;
48
51
49
52
do {
50
- if (level == 0 )
51
- fprintf (stderr, " > " );
52
- else
53
- fprintf (stderr, " .%*c" , 4 * level + 1 , ' ' );
54
-
55
- char * line = nullptr ;
56
- size_t allocated_size = 0 ;
57
- ssize_t nread = getline (&line, &allocated_size, stdin);
58
- if (nread < 0 ) {
59
- if (errno == 0 ) {
60
- // Explicit EOF; stop reading. Print a newline though, to make
61
- // the next prompt (or the shell prompt) appear on the next
62
- // line.
63
- fprintf (stderr, " \n " );
64
- break ;
65
- } else {
66
- perror (" getline" );
67
- exit (1 );
68
- }
69
- }
53
+ prompt_builder.clear ();
54
+ prompt_builder.append (" > " );
55
+ for (auto i = 0 ; i < level; ++i)
56
+ prompt_builder.append (" " );
57
+
58
+ String line = editor.get_line (prompt_builder.build ());
70
59
71
60
piece.append (line);
72
61
auto lexer = JS::Lexer (line);
@@ -87,8 +76,6 @@ String read_next_piece()
87
76
break ;
88
77
}
89
78
}
90
-
91
- free (line);
92
79
} while (level > 0 );
93
80
94
81
return piece.to_string ();
@@ -207,6 +194,7 @@ int main(int argc, char** argv)
207
194
interpreter.global_object ().put (" global" , &interpreter.global_object ());
208
195
209
196
if (script_path == nullptr ) {
197
+ editor.initialize ();
210
198
repl (interpreter);
211
199
} else {
212
200
auto file = Core::File::construct (script_path);
0 commit comments