Class: LazyPP::Program

Inherits:
Object show all
Defined in:
lib/readable-cpp/program.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Program

Returns a new instance of Program.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/readable-cpp/program.rb', line 77

def initialize opts={}
  opts = {dir: Dir.pwd}.merge opts
  @working_dir = opts[:dir]
  @parser = Parser.new
  @pkgs = Set.new
  @handlers, @settings, @files, @scheduled = {}, {}, {}, {}
  @build_dir = 'build'
  @handlers[ImportStmt] = proc { |stmt|
    warn "** Package added: #{stmt.p.name}" unless @pkgs.include?(stmt.p)
    @pkgs.add stmt.p
  }
  @handlers[IncludeStmt] = proc { |inc|
    if inc.type == :local 
      if inc.file =~ /\.lpp/
        warn "** lpp included #{inc.file}"
        u = add_file inc.file.to_s
        inc.file = u.outname
      elsif inc.file =~ /\.lhh/
        warn "** lpp will generate headers: #{inc.file}"
        u = add_file File.basename(inc.file.to_s,'.lhh')+'.lpp', gen_header: true, build: true
        inc.file = u.basename+'.hpp'
      end
    end
  }
end

Instance Attribute Details

#build_dirObject

Returns the value of attribute build_dir.



76
77
78
# File 'lib/readable-cpp/program.rb', line 76

def build_dir
  @build_dir
end

#filesObject (readonly)

Returns the value of attribute files.



76
77
78
# File 'lib/readable-cpp/program.rb', line 76

def files
  @files
end

#parserObject (readonly)

Returns the value of attribute parser.



76
77
78
# File 'lib/readable-cpp/program.rb', line 76

def parser
  @parser
end

#raw_treeObject (readonly)

Returns the value of attribute raw_tree.



76
77
78
# File 'lib/readable-cpp/program.rb', line 76

def raw_tree
  @raw_tree
end

#treeObject (readonly)

Returns the value of attribute tree.



76
77
78
# File 'lib/readable-cpp/program.rb', line 76

def tree
  @tree
end

Instance Method Details

#[](key) ⇒ Object



159
# File 'lib/readable-cpp/program.rb', line 159

def [](key); @tree[key] end

#add_file(file, opts = {}) ⇒ Object



110
111
112
113
114
115
# File 'lib/readable-cpp/program.rb', line 110

def add_file file, opts={}
  unless f =(@files[file] || @scheduled[file])
    @scheduled[file] = Unit.new(self, file, opts) 
  else; f
  end
end

#buildscriptsObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/readable-cpp/program.rb', line 197

def buildscripts
  #objs = @files.map{|k,v| v.basename + '.o' }
  cpps = @files.map{|k,v| v.outname }
  linker_opts = @pkgs.map { |p| 
    p.linker or nil 
  }.compact.join' '
  compile_opts = @pkgs.map { |p| p.compile_opts }.compact.join(' ')
  compile_opts += ' -std=gnu++0x' if cpp0x?
  ( warn "debugger enabled (-g)"
    compile_opts.prepend '-g '
  ) if @files.values.any?(&:debugger_required?)

  # "#!/bin/sh \n" +
  # cpps.map{|f|"g++ -c #{f} #{compile_opts} &&\n"}.join +
  # "g++ #{objs.join ' '} -o #{@files.first[1].basename} #{linker_opts} \n"
  "#!/bin/sh\n" \
  "g++ -o #{@files.first[1].basename} #{compile_opts} #{cpps.join' '}  #{linker_opts}\n"
end

#clearObject



140
# File 'lib/readable-cpp/program.rb', line 140

def clear; @files = {} end

#cpp0x?Boolean

Returns:

  • (Boolean)


104
# File 'lib/readable-cpp/program.rb', line 104

def cpp0x?; @settings['c++0x'] end

#notify(msg) ⇒ Object



155
156
157
# File 'lib/readable-cpp/program.rb', line 155

def notify msg
  @handlers[msg.class] && @handlers[msg.class].(msg)
end

#parseObject



117
118
119
# File 'lib/readable-cpp/program.rb', line 117

def parse
  parse!
end

#parse_str(str) ⇒ Object



142
143
144
145
146
147
# File 'lib/readable-cpp/program.rb', line 142

def parse_str str
  raw_tree = @parser.parse_with_debug(str),#, reporter: Parslet::ErrorReporter::Deepest.new)
  [Transform.apply(raw_tree), raw_tree]
rescue Parslet::ParseFailed => error
  puts error.cause.ascii_tree
end


149
150
151
152
153
# File 'lib/readable-cpp/program.rb', line 149

def print_trees
  @files.each do |f, u|
    ap [f, u.raw_tree]
  end
end

#rule(r) ⇒ Object



120
121
122
# File 'lib/readable-cpp/program.rb', line 120

def rule r
  parser.send(r.to_s.downcase)
end

#set_cpp0xObject



103
# File 'lib/readable-cpp/program.rb', line 103

def set_cpp0x; @settings['c++0x'] ||= (warn "** C++0x enabled"; true) end

#to_cppObject



161
162
163
164
165
166
167
# File 'lib/readable-cpp/program.rb', line 161

def to_cpp; 
  @files.map { |f, u|
    #puts "#{f}:\n", u.to_cpp(RenderState.new program: self)
    "#{f}:\n#{u.to_cpp(RenderState.new program: self)}"
  }.join("\n")
  #@tree.to_cpp(RenderState.new(program: self)) end
end

#write(build = false) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/readable-cpp/program.rb', line 169

def write build = false
  return if @files.any? {|f, u| u.tree.nil?}
  set_cpp0x if @pkgs.any? { |p| p.cpp0x? }
  FileUtils.mkdir_p build_dir
  workingdir = Dir.pwd
  Dir.chdir build_dir do
    warn 'Writing...'
    @files.each_value {|u| u.write }
    open(buildscript = "build.#{outname = @files[@files.keys.first].basename}.sh", 'w+') do |f|
      f.puts buildscripts
    end

    if system 'chmod +x '<< buildscript
      if build
        warn 'Building...'
        if system './' << buildscript
          FileUtils.mv outname, File.join(workingdir, outname) unless workingdir == build_dir
        end
      end
    end

    unless $?.success?
      puts ':-(' 
    else
      puts 'Great success!'
    end
  end
end