|
1 |
| -// Generated automatically from src/module/repl.wren. Do not edit. |
| 1 | +// Generated automatically from ./src/module/repl.wren. Do not edit. |
2 | 2 | static const char* replModuleSource =
|
3 | 3 | "import \"meta\" for Meta\n"
|
4 |
| -"import \"io\" for Stdin, Stdout\n" |
| 4 | +"import \"io\" for File, Stdin, Stdout\n" |
5 | 5 | "import \"os\" for Platform\n"
|
6 | 6 | "\n"
|
7 | 7 | "/// Abstract base class for the REPL. Manages the input line and history, but\n"
|
@@ -68,7 +68,9 @@ static const char* replModuleSource =
|
68 | 68 | " // TODO: Handle ESC 0 sequences.\n"
|
69 | 69 | " }\n"
|
70 | 70 | " } else if (byte == Chars.carriageReturn) {\n"
|
71 |
| -" executeInput()\n" |
| 71 | +" var next = executeInput()\n" |
| 72 | +" if (next == Chars.ctrlD) return true\n" |
| 73 | +" if (next != null) handleChar(next)\n" |
72 | 74 | " } else if (byte == Chars.delete) {\n"
|
73 | 75 | " deleteLeft()\n"
|
74 | 76 | " } else if (byte >= Chars.space && byte <= Chars.tilde) {\n"
|
@@ -168,6 +170,11 @@ static const char* replModuleSource =
|
168 | 170 | "\n"
|
169 | 171 | " System.print()\n"
|
170 | 172 | "\n"
|
| 173 | +" if (Command.isCommand(input)) {\n" |
| 174 | +" var parts = Command.split(input)\n" |
| 175 | +" return executeCommand(parts[0], parts[1...parts.count])\n" |
| 176 | +" }\n" |
| 177 | +"\n" |
171 | 178 | " // Guess if it looks like a statement or expression. If it looks like an\n"
|
172 | 179 | " // expression, we try to print the result.\n"
|
173 | 180 | " var token = lexFirst(input)\n"
|
@@ -210,6 +217,41 @@ static const char* replModuleSource =
|
210 | 217 | " }\n"
|
211 | 218 | " }\n"
|
212 | 219 | "\n"
|
| 220 | +" executeCommand(command, arguments) {\n" |
| 221 | +" if (command == Command.clear) {\n" |
| 222 | +" return Chars.ctrlL\n" |
| 223 | +" }\n" |
| 224 | +"\n" |
| 225 | +" if (command == Command.exit) {\n" |
| 226 | +" return Chars.ctrlD\n" |
| 227 | +" }\n" |
| 228 | +"\n" |
| 229 | +" if (command == Command.help) {\n" |
| 230 | +" System.print(Command.help())\n" |
| 231 | +" return\n" |
| 232 | +" }\n" |
| 233 | +"\n" |
| 234 | +" if (command == Command.save) {\n" |
| 235 | +" var path = !arguments.isEmpty ? arguments[0] : \"\"\n" |
| 236 | +" var fiber = Fiber.new {\n" |
| 237 | +" if (path.isEmpty) Fiber.abort(\"missing filename\")\n" |
| 238 | +" File.create(path) {|file|\n" |
| 239 | +" var code = _history.where {|line| !Command.isCommand(line) }\n" |
| 240 | +" for (line in code) {\n" |
| 241 | +" file.writeBytes(line + \"\n\")\n" |
| 242 | +" }\n" |
| 243 | +" file.writeBytes(line + \"\n\")\n" |
| 244 | +" System.print(\"Session saved to: %(path)\")\n" |
| 245 | +" }\n" |
| 246 | +" }\n" |
| 247 | +" fiber.try()\n" |
| 248 | +" if (fiber.error != null) System.print(\"Failed to save: %(fiber.error)\")\n" |
| 249 | +" return\n" |
| 250 | +" }\n" |
| 251 | +"\n" |
| 252 | +" System.print(\"Invalid REPL command: %(command)\")\n" |
| 253 | +" }\n" |
| 254 | +"\n" |
213 | 255 | " lex(line, includeWhitespace) {\n"
|
214 | 256 | " var lexer = Lexer.new(line)\n"
|
215 | 257 | " var tokens = []\n"
|
@@ -389,6 +431,43 @@ static const char* replModuleSource =
|
389 | 431 | " }\n"
|
390 | 432 | "}\n"
|
391 | 433 | "\n"
|
| 434 | +"/// REPL commands.\n" |
| 435 | +"class Command {\n" |
| 436 | +" static clear { \".clear\" }\n" |
| 437 | +" static exit { \".exit\" }\n" |
| 438 | +" static help { \".help\" }\n" |
| 439 | +" static save { \".save\" }\n" |
| 440 | +"\n" |
| 441 | +" static isCommand(str) {\n" |
| 442 | +" return str.trim().startsWith(\".\")\n" |
| 443 | +" }\n" |
| 444 | +"\n" |
| 445 | +" static split(str) {\n" |
| 446 | +" return str.trim().split(\" \").where {|part| !part.isEmpty }.toList\n" |
| 447 | +" }\n" |
| 448 | +"\n" |
| 449 | +" static help() {\n" |
| 450 | +" var max = Fn.new {|a, b| a > b ? a : b }\n" |
| 451 | +" var width = COMMANDS.map {|entry|\n" |
| 452 | +" var command = entry[0]\n" |
| 453 | +" return command.count\n" |
| 454 | +" }.reduce(max)\n" |
| 455 | +" return COMMANDS.map {|entry|\n" |
| 456 | +" var command = entry[0]\n" |
| 457 | +" var description = entry[1]\n" |
| 458 | +" while (command.count < width) command = command + \" \"\n" |
| 459 | +" return command + \" \" + description\n" |
| 460 | +" }.join(\"\n\")\n" |
| 461 | +" }\n" |
| 462 | +"}\n" |
| 463 | +"\n" |
| 464 | +"var COMMANDS = [\n" |
| 465 | +" [Command.clear, \"Clear screen\"],\n" |
| 466 | +" [Command.exit, \"Exit the repl\"],\n" |
| 467 | +" [Command.help, \"Print this help message\"],\n" |
| 468 | +" [Command.save, \"Save all evaluated commands in this REPL session to a file\"]\n" |
| 469 | +"]\n" |
| 470 | +"\n" |
392 | 471 | "/// ANSI color escape sequences.\n"
|
393 | 472 | "class Color {\n"
|
394 | 473 | " static none { \"\x1b[0m\" }\n"
|
|
0 commit comments