Created
September 28, 2018 20:37
-
-
Save brandondrew/18d57de632a5f553f90738521d07a0db to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) | |
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