Created
April 13, 2020 08:36
-
-
Save allisio/133b21f709ccf5a9694e98a880dedf81 to your computer and use it in GitHub Desktop.
ptls, a forgiving-to-a-fault Pointless interpreter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'open3' | |
require 'tempfile' | |
lines = [] | |
buffer = [] | |
def wrap line | |
# (Naively) assume any line containing an equals | |
# sign is a definition and return it unmodified. | |
line[/=/] ? line : "output = println(#{line})" | |
end | |
while (print ' ' * buffer.size; line = gets&.strip) | |
# Rudimentary continuation handling. | |
buffer << line and next if line[/=$/] | |
line.prepend buffer.join | |
# Wrap non-definitions so we can see their evaluations. | |
lines << wrap(line) | |
# Write all lines seen so far to a named file for Pointless's sake. | |
tmp = Tempfile.new.path | |
File.write tmp, lines.join("\n") | |
# Then cross our fingers and execute them. | |
out, err, res = Open3.capture3 "pointless #{tmp}" | |
# Forget non-definitions. | |
lines.pop unless line['='] | |
if res.success? | |
# Display the evaluation if we were successful. | |
puts out | |
else | |
# If not, remove the offending definition if it | |
# would otherwise be a show-stopper and press on. | |
lines.pop if line['='] && err[/(Parser|Tokenizer) Error:/] | |
end | |
buffer.clear | |
end |
That's probably to do with the fact that I haven't yet made it show the value of expressions immediately after their definitions. You have to explicitly request them, which is obviously rather unpleasant. I put together a brief demonstration of how ptls
"functions" at the moment. Please let me know if interacting with it more along those lines still doesn't work on your end.
That works great. Silly me, I had forgotten to change the system call to point to my installation of pointless
. This is cool. Great work. For posterity, I had to gem install
the two required packages and add a ;
to line 27 in the quotes to force open3 to use the system shell.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I must be doing something wrong. I got this running, but I enter
pointless
lines and the repl just hangs. Never showing any outputs.