by Insanity · 7y ago · 0 comments
(loop(print(eval(read)))
to have a REPL. (Just reverse the letters, easy enough to remember).That to me is elegance. It's simple yet powerful, and just 4 words really.
while(1) {eval(fgets(STDIN));echo "\n";};
I then tried it on the command line like this: php -r 'while(1) {eval(fgets(STDIN));echo "\n";};';
Hurray, it prompted me for input! So I typed: for ($i=0;$i<10;$i++) echo $i;
Which got me: 0123456789
So far so good.I wondered: Can we now run the repl in the repl? So I typed:
while(1) {eval(fgets(STDIN));echo "\n";};
It kept prompting me for input. Am I in a REPL in a REPL now? I typed: echo "Hello from a REPL in a REPL!";
And the reply was: Hello from a REPL in a REPL!
I'm not totally sure if I believe it though.To test whether your logic is working properly, make your repl print out a different character when it prompts for input. For example, the toplevel repl can print out "> " whereas the inner repl prints out "repl> "
For bonus points, your repl should exit if you type Ctrl-D. That way you can go from the inner repl to the outer repl, and from there it should exit your program.
"I am in a REPL in a REPL in a REPL,
Running all my code in nested loops..."
I'm not saying the acronym came before or after the LISP code :P