Class: LazyPP::Unit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prog, file, opts = {}) ⇒ Unit

Returns a new instance of Unit.



6
7
8
9
10
# File 'lib/readable-cpp/program.rb', line 6

def initialize prog, file, opts={}
  super prog, file, nil, nil, nil, nil
  @read,@scanned = false,false
  @opts={gen_header: false, build: true}.merge opts
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents

Returns:

  • (Object)

    the current value of contents



4
5
6
# File 'lib/readable-cpp/program.rb', line 4

def contents
  @contents
end

#cppObject

Returns the value of attribute cpp.



5
6
7
# File 'lib/readable-cpp/program.rb', line 5

def cpp
  @cpp
end

#fileObject

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



4
5
6
# File 'lib/readable-cpp/program.rb', line 4

def file
  @file
end

#programObject

Returns the value of attribute program

Returns:

  • (Object)

    the current value of program



4
5
6
# File 'lib/readable-cpp/program.rb', line 4

def program
  @program
end

#raw_treeObject

Returns the value of attribute raw_tree

Returns:

  • (Object)

    the current value of raw_tree



4
5
6
# File 'lib/readable-cpp/program.rb', line 4

def raw_tree
  @raw_tree
end

#timeObject

Returns the value of attribute time

Returns:

  • (Object)

    the current value of time



4
5
6
# File 'lib/readable-cpp/program.rb', line 4

def time
  @time
end

#treeObject

Returns the value of attribute tree

Returns:

  • (Object)

    the current value of tree



4
5
6
# File 'lib/readable-cpp/program.rb', line 4

def tree
  @tree
end

Instance Method Details

#basenameObject



41
42
43
# File 'lib/readable-cpp/program.rb', line 41

def basename
  File.basename(file, '.lpp')
end

#build?Boolean

Returns:

  • (Boolean)


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

def build?; @opts[:build] end

#debugger_required!Object



13
14
15
# File 'lib/readable-cpp/program.rb', line 13

def debugger_required!
  @opts[:debugger_required?] = true
end

#debugger_required?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/readable-cpp/program.rb', line 16

def debugger_required?
  @opts[:debugger_required?]
end

#gen_headerObject



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

def gen_header; @opts[:gen_header] end

#gen_header=(val) ⇒ Object



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

def gen_header= val; @opts[:gen_header]= val end

#outnameObject



32
33
34
# File 'lib/readable-cpp/program.rb', line 32

def outname
  basename + '.cpp'
end

#readObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/readable-cpp/program.rb', line 20

def read
  return if @read
  warn "reading "+file
  #self.contents = File.read(file)
  self.contents = ERB.new(File.read(file)).result(binding)
  t = program.parse_str(self.contents)
  self.tree, self.raw_tree = t
  self.time = Time.now
  @read = true

  self.tree
end

#scanObject



35
36
37
38
39
40
# File 'lib/readable-cpp/program.rb', line 35

def scan
  return if @scanned
  warn "scanning "+file
  tree && tree.scan(program)
  @scanned = true
end

#to_cpp(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/readable-cpp/program.rb', line 63

def to_cpp(*args)
  @cpp ||= (
    if gen_header ##filter out top-level declarations so they only appear in the header
      tree.to_cpp(*args) { |n| !(n.is_a?(VarDeclInitializer) || n.is_a?(VarDeclSimple)) }
    else
      tree.to_cpp(*args) 
    end
  )
end

#to_hpp(*args) ⇒ Object



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

def to_hpp(*args); @hpp ||= tree.to_hpp(*args) end

#writeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/readable-cpp/program.rb', line 44

def write
  if gen_header
    warn "writing "+(n= basename+'.hpp')
    open(n, 'w+') do |f|
      f.puts "#pragma once"
      # f.puts "#ifndef __HEADER_#{basename}"
      # f.puts "#define __HEADER_#{basename}"
      f.puts to_hpp(RenderState.new(program: program, gen_header: gen_header))
      # f.puts "#endif"
    end
  end
  warn 'writing '+outname
  open(outname, 'w+') do |f| 
    if gen_header
      f.puts "#include \"#{basename+'.hpp'}\""
    end
    f.puts to_cpp(RenderState.new(program: program, gen_header: gen_header))
  end
end