Class: Rubex::CodeWriter
- Inherits:
-
Object
- Object
- Rubex::CodeWriter
- Defined in:
- lib/rubex/code_writer.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #block(str = "", &block) ⇒ Object
- #c_macro(macro) ⇒ Object
- #colon ⇒ Object
- #declare_carray(type:, c_name:, dimension:, value: nil) ⇒ Object
- #declare_func_ptr(var:) ⇒ Object
- #declare_variable(type:, c_name:) ⇒ Object
- #dedent ⇒ Object
- #header? ⇒ Boolean
- #in_header_guard(guard, &block) ⇒ Object
- #indent ⇒ Object
- #init_variable(lhs:, rhs:) ⇒ Object
-
#initialize(target_name, is_header: false) ⇒ CodeWriter
constructor
A new instance of CodeWriter.
- #lbrace ⇒ Object
- #new_line ⇒ Object (also: #nl)
- #rbrace ⇒ Object
- #to_s ⇒ Object
- #write_c_method_header(type:, c_name:, args: [], static: true) ⇒ Object
-
#write_func_declaration(type:, c_name:, args: [], static: true) ⇒ Object
type - Return type of the method.
- #write_include(file_name) ⇒ Object
- #write_instance_method(klass:, method_name:, method_c_name:) ⇒ Object
- #write_location(location) ⇒ Object
- #write_ruby_method_header(type:, c_name:) ⇒ Object
- #write_singleton_method(klass:, method_name:, method_c_name:) ⇒ Object
Constructor Details
#initialize(target_name, is_header: false) ⇒ CodeWriter
Returns a new instance of CodeWriter.
5 6 7 8 9 10 11 12 |
# File 'lib/rubex/code_writer.rb', line 5 def initialize target_name, is_header: false @code = "/* C extension for #{target_name}.\n"\ "This file in generated by Rubex::Compiler. Do not change!\n"\ "File generation time: #{Time.now}."\ "*/\n\n" @indent = 0 @is_header = is_header end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
3 4 5 |
# File 'lib/rubex/code_writer.rb', line 3 def code @code end |
Instance Method Details
#<<(str) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/rubex/code_writer.rb', line 98 def << str str.each_line do |s| @code << " "*@indent @code << s end end |
#block(str = "", &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rubex/code_writer.rb', line 143 def block str="", &block new_line lbrace indent new_line block.call dedent rbrace @code << str new_line new_line end |
#c_macro(macro) ⇒ Object
48 49 50 51 |
# File 'lib/rubex/code_writer.rb', line 48 def c_macro macro @code << "#define #{macro}" new_line end |
#colon ⇒ Object
43 44 45 46 |
# File 'lib/rubex/code_writer.rb', line 43 def colon @code << ";" new_line end |
#declare_carray(type:, c_name:, dimension:, value: nil) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/rubex/code_writer.rb', line 84 def declare_carray type:, c_name:, dimension:, value: nil stmt = "#{type} #{c_name}[#{dimension}]" stmt << " = {" + value.join(',') + "}" if value stmt << ";" self << stmt nl end |
#declare_func_ptr(var:) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/rubex/code_writer.rb', line 74 def declare_func_ptr var: type = var.type func = type.base_type @code << " "*@indent @code << "#{func.type.to_s} (#{type.ptr_level}#{func.c_name}) " @code << "(" + func.arg_list.map { |e| e.type.to_s }.join(',') + ")" @code << ";" nl end |
#declare_variable(type:, c_name:) ⇒ Object
69 70 71 72 |
# File 'lib/rubex/code_writer.rb', line 69 def declare_variable type:, c_name: @code << " "*@indent + "#{type} #{c_name};" new_line end |
#dedent ⇒ Object
114 115 116 117 |
# File 'lib/rubex/code_writer.rb', line 114 def dedent raise "Cannot dedent, already 0." if @indent == 0 @indent -= 2 end |
#in_header_guard(guard, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rubex/code_writer.rb', line 18 def in_header_guard guard, &block if header? @code << "#ifndef #{guard}\n" @code << "#define #{guard}\n" block.call @code << "#endif\n" else raise "file is not a header file." end end |
#indent ⇒ Object
110 111 112 |
# File 'lib/rubex/code_writer.rb', line 110 def indent @indent += 2 end |
#init_variable(lhs:, rhs:) ⇒ Object
92 93 94 95 96 |
# File 'lib/rubex/code_writer.rb', line 92 def init_variable lhs: , rhs: stat = " "*@indent + "#{lhs} = #{rhs};" @code << stat new_line end |
#lbrace ⇒ Object
135 136 137 |
# File 'lib/rubex/code_writer.rb', line 135 def lbrace @code << (" "*@indent + "{") end |
#new_line ⇒ Object Also known as: nl
105 106 107 |
# File 'lib/rubex/code_writer.rb', line 105 def new_line @code << "\n" end |
#rbrace ⇒ Object
139 140 141 |
# File 'lib/rubex/code_writer.rb', line 139 def rbrace @code << (" "*@indent + "}") end |
#to_s ⇒ Object
131 132 133 |
# File 'lib/rubex/code_writer.rb', line 131 def to_s @code end |
#write_c_method_header(type:, c_name:, args: [], static: true) ⇒ Object
53 54 55 |
# File 'lib/rubex/code_writer.rb', line 53 def write_c_method_header type: , c_name: , args: [], static: true write_func_prototype type, c_name, args, static: static end |
#write_func_declaration(type:, c_name:, args: [], static: true) ⇒ Object
type - Return type of the method. c_name - C Name. args - Array of Arrays containing data type and variable name.
32 33 34 35 36 |
# File 'lib/rubex/code_writer.rb', line 32 def write_func_declaration type:, c_name:, args: [], static: true write_func_prototype type, c_name, args, static: static @code << ";" new_line end |
#write_include(file_name) ⇒ Object
38 39 40 41 |
# File 'lib/rubex/code_writer.rb', line 38 def write_include file_name @code << "#include \"#{file_name}.h\"" new_line end |
#write_instance_method(klass:, method_name:, method_c_name:) ⇒ Object
119 120 121 122 123 |
# File 'lib/rubex/code_writer.rb', line 119 def write_instance_method klass: , method_name: , method_c_name: @code << " "*@indent + "rb_define_method(" + klass + " ,\"" + method_name + "\", " + method_c_name + ", -1);" new_line end |
#write_location(location) ⇒ Object
63 64 65 66 67 |
# File 'lib/rubex/code_writer.rb', line 63 def write_location location new_line self << "/* Rubex file location: #{location} */" new_line end |
#write_ruby_method_header(type:, c_name:) ⇒ Object
57 58 59 60 61 |
# File 'lib/rubex/code_writer.rb', line 57 def write_ruby_method_header type: , c_name: args = [["int", "argc"], ["VALUE*", "argv"], ["VALUE", "#{Rubex::ARG_PREFIX + "self"}"]] write_func_prototype type, c_name, args end |
#write_singleton_method(klass:, method_name:, method_c_name:) ⇒ Object
125 126 127 128 129 |
# File 'lib/rubex/code_writer.rb', line 125 def write_singleton_method klass:, method_name:, method_c_name: @code << " "*@indent + "rb_define_singleton_method(" + klass + " ,\"" + method_name + "\", " + method_c_name + ", -1);" new_line end |