Class: SFRP::Command

Inherits:
Struct
  • Object
show all
Defined in:
lib/sfrp/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ccObject

Returns the value of attribute cc



5
6
7
# File 'lib/sfrp/command.rb', line 5

def cc
  @cc
end

#error_classObject

Returns the value of attribute error_class



5
6
7
# File 'lib/sfrp/command.rb', line 5

def error_class
  @error_class
end

#include_pathsObject

Returns the value of attribute include_paths



5
6
7
# File 'lib/sfrp/command.rb', line 5

def include_paths
  @include_paths
end

#main_fileObject

Returns the value of attribute main_file



5
6
7
# File 'lib/sfrp/command.rb', line 5

def main_file
  @main_file
end

#out_dirObject

Returns the value of attribute out_dir



5
6
7
# File 'lib/sfrp/command.rb', line 5

def out_dir
  @out_dir
end

#show_filesObject

Returns the value of attribute show_files



5
6
7
# File 'lib/sfrp/command.rb', line 5

def show_files
  @show_files
end

Class Method Details

.extract_argv(com, argv) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sfrp/command.rb', line 15

def self.extract_argv(com, argv)
  opt_parser = OptionParser.new do |parse|
    desc = 'show paths of the .c file'
    parse.on('--show-files', desc) do
      com.show_files = true
    end
    desc = 'specify output directory (default is ./output)'
    parse.on('--out=DIR_NAME', desc) do |dir|
      com.out_dir = './' + dir.gsub(/\/$/, '')
    end
    desc = 'add include path (multi specification is allowed)'
    parse.on('--include=PATH', desc) do |path|
      com.include_paths << path
    end
    desc = 'only print error (do not generate compiled file)'
    parse.on('--only-print-error', desc) do
      com.out_dir = nil
    end
    desc = 'print error class instead of error message'
    parse.on('--error-class', desc) do
      com.error_class = true
    end
    desc = 'make binary by given command'
    parse.on('--build=CC', desc) do |cc|
      com.cc = cc
    end
  end
  args_without_option = opt_parser.parse(argv)
  case args_without_option.size
  when 0
    nil
  when 1
    com.main_file = args_without_option[0].gsub(/\.sfrp$/, '')
  else
    STDERR.puts 'invalid target specification'
    exit(1)
  end
rescue OptionParser::InvalidOption => error
  puts error.message
  exit(1)
end

.from_argv(argv, include_path) ⇒ Object



8
9
10
11
12
13
# File 'lib/sfrp/command.rb', line 8

def self.from_argv(argv, include_path)
  com = new('Main', './output', false, ['.'], false, nil)
  extract_argv(com, argv)
  com[:include_paths] << include_path
  com
end

Instance Method Details

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sfrp/command.rb', line 57

def run
  outs = Compiler.new(main_file, include_paths).compile(out_dir)
  out_paths = outs.map { |o| out_dir + '/' + o + '.c' }
  STDOUT.puts out_paths.join("\n") if show_files
  STDERR.print `#{cc} -o #{main_file} #{out_paths.join(' ')}` if cc
rescue SFRP::CompileError => cerr
  text = error_class ? cerr.class.to_s : cerr.message
  if out_dir == nil
    STDOUT.puts text
    exit(0)
  else
    STDERR.puts text
    exit(1)
  end
end