Skip to content

Instantly share code, notes, and snippets.

@brandondrew
Created September 28, 2018 20:37
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 brandondrew/18d57de632a5f553f90738521d07a0db to your computer and use it in GitHub Desktop.
Save brandondrew/18d57de632a5f553f90738521d07a0db to your computer and use it in GitHub Desktop.
require 'readline'
require 'parser/current'
# TODO: detect version of Ruby being used, and require the appropriate parser
# require 'parser/rubyXY'
# opt into newer AST format since we're not maintaining backward compatibility with old formats!
Parser::Builders::Default.emit_lambda = true
Parser::Builders::Default.emit_procarg0 = true
Parser::Builders::Default.emit_encoding = true
Parser::Builders::Default.emit_index = true
### Our Unicode Characters
THE_END = "\u0004"
LINE_BREAK = "\u0085"
### Let's exit gracefully ###
def goodbye(newline: true)
puts if newline
puts "Exiting Optic Ruby Parser."
end
Signal.trap("INT") { goodbye; exit }
Signal.trap("TERM") { goodbye; exit }
# for testing:
#puts "Your process ID is #{Process.pid}"
# READ
while input = Readline.readline(">: ", true)
break if input == nil # handle ⌃D
input.chomp!
break if ["exit", "quit", ":q"].include? input
# EVAL (actually: parse into AST)
ast = Parser::CurrentRuby.parse(input)
# PRINT
puts ast.to_s.gsub(/\s+/,' ') + THE_END
end
# LOOP
goodbye(newline: false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment