class Object

Public Instance Methods

command_line_options() click to toggle source
# File lib/bam.rb, line 28
def command_line_options

  # Add the read switch before any solitary Boogie files
  require_relative 'bpl/passes/utility/reading.rb'
  require_relative 'bpl/passes/utility/writing.rb'

  def file_switch?(x)
    (Bpl::Reading.switch[:args] + Bpl::Writing.switch[:args]).
    any? {|a| /#{a}/ =~ x}
  end

  ARGV.each_index do |idx|
    next if idx > 0 && file_switch?(ARGV[idx-1])
    next unless File.extname(ARGV[idx]) == '.bpl'
    ARGV.insert(idx, Bpl::Reading.switch[:args].first)
  end

  OptionParser.new do |opts|

    opts.banner = "Usage: #{File.basename $0} [options] FILE(s)"

    opts.separator ""
    opts.separator "Basic options:"

    opts.on("-h", "--help", "Show this message") do |v|
      puts opts
      exit
    end

    opts.on("--version", "Show version") do
      puts "#{File.basename $0} version #{BAM::VERSION || "??"}"
      exit
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely? (default #{$verbose})") do |v|
      $verbose = v
      $quiet = !v
    end

    opts.on("-q", "--[no-]quiet", "Run quietly? (default #{$quiet})") do |q|
      $quiet = q
      $verbose = !q
    end

    opts.on("-w", "--[no-]warnings", "Show warnings? (default #{$show_warnings})") do |w|
      $show_warnings = w
    end

    opts.on("-k", "--[no-]keep-files", "Keep intermediate files? (default #{$keep})") do |v|
      $keep = v
    end

    categories = {}
    root = File.expand_path(File.dirname(__FILE__))
    Dir.glob(File.join(root,'bpl','passes','**','*/')).each do |dir|
      Dir.glob(File.join(dir, "*.rb")).each do |lib|
        require_relative lib
        name = File.basename(lib,'.rb')
        klass = "Bpl::#{name.classify}"
        @passes[name.to_sym] = Object.const_get(klass)
        categories[File.basename(dir)] ||= Set.new
        categories[File.basename(dir)] << name.to_sym
      end
    end

    categories.each do |cat, passes|
      opts.separator ""
      opts.separator "#{cat} passes:"
      passes.each do |name|
        sw = @passes[name].switch
        opts.on(*sw[:args]) do |*args|
          opts = {}
          sw[:blk].call(
            Enumerator::Yielder.new {|k,v| opts[k] = v},
            *args
          ) if sw[:blk]
          @stages.push([name, opts])
        end
        @passes[name].flags.each do |f|
          opts.on(*f[:args]) do |*args|
            f[:blk].call(
              Enumerator::Yielder.new{|k,v| @passes[name].option k, v},
              *args
            )
          end
        end
      end
    end

    opts.separator ""
  end
end
file_switch?(x) click to toggle source
# File lib/bam.rb, line 34
def file_switch?(x)
  (Bpl::Reading.switch[:args] + Bpl::Writing.switch[:args]).
  any? {|a| /#{a}/ =~ x}
end
get_block(file, line) click to toggle source
# File lib/bpl/ast/trace.rb, line 92
def get_block(file, line)
  lines = File.read(file).lines.drop(line)
  lines.take_while{|l| l !~ /^  (goto|return)/} +
  [lines.detect{|l| l =~ /^  (goto|return)/}]
end
get_proc(file, line) click to toggle source
# File lib/bpl/ast/trace.rb, line 88
def get_proc(file, line)
  File.read(file).lines.take(line).grep(/\bprocedure\b/).last.sub(/[^\(\)]*\s+([^ \(\){}:]+)\s*\(.*/,'\1').strip
end
source_file_options(files) click to toggle source
# File lib/bam.rb, line 14
def source_file_options(files)
  # parse @bam-options comments in the source file(s) for additional options
  opts = []
  files.each do |f|
    next unless File.exist?(f)
    File.readlines(f).grep(/@bam-options (.*)/) do |line|
      line.gsub(/.* @bam-options (.*)/,'\1').split.reverse.each do |arg|
        opts << arg
      end
    end
  end
  opts
end
timed(desc = nil) { || ... } click to toggle source
# File lib/bam/prelude.rb, line 133
def timed(desc = nil)
  time = Time.now
  res = yield if block_given?
  time = (Time.now - time).round(2)
  info "#{desc} took #{time}s." if $verbose && desc
  res
end