#!/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 profile = parsed.options.profile || parsed.options.profiler 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 printer = if parsed.options.profiler == "html" RubyProf::GraphHtmlPrinter.new(result) else RubyProf::FlatPrinter.new(result) end printer.print($stdout) end