Class: RubimCode::Printer

Inherits:
Object show all
Defined in:
lib/rubimc/printer.rb

Overview

RubimCode class

Constant Summary collapse

@@instance_vars_cc =
[]
@@pout_destination =
:default

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sandboxObject

Returns the value of attribute sandbox.



76
77
78
# File 'lib/rubimc/printer.rb', line 76

def sandbox
  @sandbox
end

Class Method Details

.code_typeObject



100
101
102
103
104
105
106
107
108
# File 'lib/rubimc/printer.rb', line 100

def self.code_type
  if not Controllers.all.empty?
    "avr-gcc" 
  elsif Controllers.all.empty? and eval("self.private_methods.include? :main")
    "gcc"
  else
    RubimCode.perror "Can not to define type of code"
  end
end

.generate_ccObject



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
# File 'lib/rubimc/printer.rb', line 169

def self.generate_cc
  if Controllers.all.count > 1
    RubimCode.perror "In current version in one file you can define only one Controller Class"
  end

  if self.code_type == "avr-gcc" # if compile program for MCU
    Controllers.all.each do |mcu_class|
      print_layout(:before_main) do
        mcu_class.mcu_layout if mcu_class.respond_to? :mcu_layout
      end
      mcu = mcu_class.new # print initialize section
      print_main_loop {mcu.main_loop} # print body of main loop
      print_layout(:after_main) 
      RubimCode::Interrupts.print()
      RubimCode::Printer.print_instance_vars()
    end # each Controllers.all

  elsif self.code_type == "gcc" # if compile clear-C program
    if Controllers.all.empty? and eval("self.private_methods.include? :main")
      print_layout(:before_main)
      eval("main(RubimCode::CC_ARGS.new)") # execute user`s method :main (CC_ARGS - helper for C agruments argc/argv)
      print_layout(:after_main)
    end
  end
end

.instance_vars_ccObject



80
81
82
# File 'lib/rubimc/printer.rb', line 80

def self.instance_vars_cc
  @@instance_vars_cc
end

.mcu_typeObject



110
111
112
# File 'lib/rubimc/printer.rb', line 110

def self.mcu_type
  code_type == "avr-gcc" ? Controllers.all.first::MCU_NAME : "undefined"
end

.pout_destinationObject



85
86
87
# File 'lib/rubimc/printer.rb', line 85

def self.pout_destination
  @@pout_destination
end

.pout_destination=(dest) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubimc/printer.rb', line 88

def self.pout_destination=(dest)
  if dest.nil?
    perror "Wrong parameter for method #{__method__}. Set destination string"
  end

  if dest.class.name == "String" or dest.in? [:default, :h_file] # dest.is_a?(String) not work...WTF
    @@pout_destination = dest
  else
    perror "Wrong parameter for method #{__method__}. Only string variable or ':default' value is permit as a parameters"
  end
end


159
160
161
162
163
164
165
166
167
# File 'lib/rubimc/printer.rb', line 159

def self.print_instance_vars
  RubimCode::Printer.pout_destination = :h_file
  @@instance_vars_cc.each do |var|
    if var.is_a? RubimCode::UserVariable
      RubimCode.pout "#{var.type} #{var.name};"
    end
  end
  RubimCode::Printer.pout_destination = :default
end


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rubimc/printer.rb', line 114

def self.print_layout(position, &mcu_layout)
  return if RubimCode::Printer.sandbox == true # don`t print output in sandbox
  h_name = File.basename(ARGV[0], '.c') + '.h'
  if position == :before_main
    RubimCode::Printer.pout_destination = :h_file
    RubimCode.pout "/**************************************************************"
    RubimCode.pout " * This code was generated by RubimC micro-framework"
    RubimCode.pout " * Include file for \"#{ARGV[0]}\""
    RubimCode.pout " **************************************************************/"

    RubimCode::Printer.pout_destination = :default
    RubimCode.pout "/**************************************************************"
    RubimCode.pout " * This code was generated by RubimC micro-framework"
    RubimCode.pout " * RubimC version: #{RubimCode::VERSION}"
    RubimCode.pout " * RubimC author: Evgeny Danilov"
    RubimCode.pout " * File created at #{Time.now}"
    RubimCode.pout " **************************************************************/"
    RubimCode.pout
    RubimCode.pout "#include <stdbool.h>"
    RubimCode.pout "#include <stdio.h>"
    RubimCode.pout
    yield if block_given? # print includes for current MCU (see mcu libraries)
    RubimCode.pout
    RubimCode.pout "#include \"#{h_name}\""
    RubimCode.pout
    RubimCode.pout "int main(int argc, char *argv[]) {"
    RubimCode.level += 1
  else
    RubimCode.pout
    RubimCode.pout "return 1;"
    RubimCode.level -= 1
    RubimCode.pout "}"
  end
end


149
150
151
152
153
154
155
156
157
# File 'lib/rubimc/printer.rb', line 149

def self.print_main_loop
  RubimCode.pout
  RubimCode.pout "// === Main Infinite Loop === //"
  RubimCode.pout "while (true) {"
    RubimCode.level += 1
    yield # print body of main loop
    RubimCode.level -= 1
  RubimCode.pout"} // end main loop"
end