#!/usr/bin/env ruby # frozen_string_literal: true require_relative "../lib/code-ruby" require "dorian/arguments" parsed = Dorian::Arguments.parse( input: { type: :string, alias: :i }, parse: { type: :boolean, alias: :p }, profile: :boolean, profiler: :string, timeout: { type: :integer, alias: :t }, version: { type: :boolean, alias: :v }, help: { type: :boolean, alias: :h } ) abort Code::Version.to_s if parsed.options.version abort parsed.help if parsed.options.help input = parsed.options.input.to_s input = File.read(input) if File.exist?(input) input += parsed.arguments.join(" ") input += parsed.files.map { |file| File.read(file) }.join abort parsed.help if input.empty? profile = parsed.options.profile || !parsed.options.profiler.empty? require "ruby-prof" if profile RubyProf.start if profile if parsed.options.parse pp Code::Parser.parse(input).to_raw else print( Code.evaluate( input, output: $stdout, error: $stderr, timeout: parsed.options.timeout ) ) end if profile result = RubyProf.stop if parsed.options.profiler == "html" printer = RubyProf::GraphHtmlPrinter.new(result) printer.print($stdout) else printer = RubyProf::FlatPrinter.new(result) printer.print($stdout) end end