JohnEarnest / ok

An open-source interpreter for the K5 programming language.
MIT License
587 stars 72 forks source link

readline-based REPL #23

Closed ngn closed 9 years ago

ngn commented 9 years ago

Please reject if you aren't happy with any of the above.

JohnEarnest commented 9 years ago

Hmm. I'm a little torn- removing support for stdio breaks the circle.k, tictac.k and hangman.k examples, but on the other hand they never would have worked in the real k5. Node support for stdio also seems to be quite flaky and unstable across versions, so going with what the readline module is capable of ought to be a better way to ensure this codebase still works in the future. Overall this does seem like it could be a usability improvement.

Does this handle file arguments? k5 executes scripts and then drops the user into a prompt, and the old oK repl would process the files and immediately quit. I think the latter behavior is more convenient for testing but either is acceptable:

je@kbox:~$ echo "+/2 7 9" > test.k
je@kbox:~$ cat test.k
+/2 7 9
je@kbox:~$ ./k test.k 
18
 \\
je@kbox:~$

Additionally, could you modify your pull request to remove the aforementioned examples and update the command-line mode section of the readme to reflect the new CLI repl, removing reference to stdio? Feel free to use different/more interesting examples provided they are reasonably short.

ngn commented 9 years ago

I can implement writing to stdout without problems, that would save circle.k. Reading is the one that causes trouble.

readline is not concerned with cmd line args. They are available as process.argv, so implementing k's behaviour is possible.

ngn commented 9 years ago

I noticed a problem with setIO(). I've put read in slots 0 1 and write in 2 3 4 5. This is what happens:

expression     callback called
`    0: "bb"   write
`a   0: "bb"   write
`aa  0: "bb"   write
""   0: "bb"   read
"a"  0: "bb"   write
"aa" 0: "bb"   read

Am I doing something wrong?

JohnEarnest commented 9 years ago

Sounds like a bug on my end. I'll have a look.

JohnEarnest commented 9 years ago

Should be corrected now- the source of the problem appears to have been a parser bug in which IO verbs were being handled as if they were part of a parenthesized subexpression in certain cases, forcing them to parse as their monadic forms. My bad!

ngn commented 9 years ago

Now it can load .k files passed as command line arguments. I used the readFileSync() technique from the original repl.js to implement monadic `0:`` when not running interactively.

JohnEarnest commented 9 years ago

Looks great- thank you!