Skip to content

Instantly share code, notes, and snippets.

@allisio
Created April 13, 2020 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allisio/133b21f709ccf5a9694e98a880dedf81 to your computer and use it in GitHub Desktop.
Save allisio/133b21f709ccf5a9694e98a880dedf81 to your computer and use it in GitHub Desktop.
ptls, a forgiving-to-a-fault Pointless interpreter
#!/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
@andrewnc
Copy link

I must be doing something wrong. I got this running, but I enter pointless lines and the repl just hangs. Never showing any outputs.

@allisio
Copy link
Author

allisio commented Apr 15, 2020

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.

@andrewnc
Copy link

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