<commit_before>require "readline"

module BabelBridge
class Shell
  attr_accessor :parser
  def initialize(parser)
    @parser = parser
  end

  def evaluate(parse_tree_node)
    parse_tree_node.evaluate
  rescue Exception => e
    @stderr.puts "Error evaluating parse tree: #{e}\n  "+e.backtrace.join("\n  ")
  end

  # if block is provided, successful parsers are yield to block
  # Otherwise, succuessful parsers are sent the "eval" method
  def start(options={},&block)
    @stdout = options[:stdout] || $stdout
    @stderr = options[:stdout] || @stdout
    @stdin = options[:stdin] || $stdin
    while line = @stdin == $stdin ? Readline.readline("> ", true) : @stdin.gets
      ret = parser.parse line.strip
      if ret
        if block
          yield ret
        else
          @stdout.puts " => #{evaluate(ret).inspect}"
        end
      else
        @stderr.puts parser.parser_failure_info
      end
    end
  end
end
end<commit_msg>if the nodes don't support "evaluate", just show their parse tree<commit_after>require "readline"

module BabelBridge
class Shell
  attr_accessor :parser
  def initialize(parser)
    @parser = parser
  end

  def evaluate(parse_tree_node)
    if parse_tree_node.respond_to? :evaluate
      parse_tree_node.evaluate
    else
      parse_tree_node
    end
  rescue Exception => e
    @stderr.puts "Error evaluating parse tree: #{e}\n  "+e.backtrace.join("\n  ")+"\nParse Tree:\n"+parse_tree_node.inspect
  end

  # if block is provided, successful parsers are yield to block
  # Otherwise, succuessful parsers are sent the "eval" method
  def start(options={},&block)
    @stdout = options[:stdout] || $stdout
    @stderr = options[:stdout] || @stdout
    @stdin = options[:stdin] || $stdin
    while line = @stdin == $stdin ? Readline.readline("> ", true) : @stdin.gets
      ret = parser.parse line.strip
      if ret
        if block
          yield ret
        else
          @stdout.puts " => #{evaluate(ret).inspect}"
        end
      else
        @stderr.puts parser.parser_failure_info
      end
    end
  end
end
end
{'fraction_non_alphanumeric': 0.10686653771760155, 'fraction_numerical': 0.0, 'mean_word_length': 2.8314814814814815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2426736', 'n_tokens_mistral': 659, 'n_tokens_neox': 641, 'n_words': 185}