Class: CAS::CLib
- Inherits:
-
Hash
- Object
- Hash
- CAS::CLib
- Defined in:
- lib/Mr.CAS/c-opt.rb
Class Method Summary collapse
Instance Method Summary collapse
- #as_double ⇒ Object
- #as_float ⇒ Object
- #as_int ⇒ Object
- #as_long_int ⇒ Object
- #define(k, v) ⇒ Object
- #header ⇒ Object
- #implements_as(name, op) ⇒ Object
- #include(lib) ⇒ Object
- #include_local(lib) ⇒ Object
- #include_type(type, lib) ⇒ Object
-
#initialize(name) ⇒ CLib
constructor
A new instance of CLib.
- #source ⇒ Object
- #undefine(k) ⇒ Object
Constructor Details
#initialize(name) ⇒ CLib
Returns a new instance of CLib.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/Mr.CAS/c-opt.rb', line 42 def initialize(name) raise ArgumentError, "Name for the library undefined" unless name.is_a? String @name = name @define = {} @include = {} @include[:std] = [] @include[:local] = [] @type = "double" # Default definitions # self.define "M_PI", Math::PI # self.define "M_INFINITY","HUGE_VAL" # self.define "M_E", Math::E # self.define "M_EPSILON", 1E-16 # Default inclusions self.include "math.h" end |
Class Method Details
Instance Method Details
#as_double ⇒ Object
61 |
# File 'lib/Mr.CAS/c-opt.rb', line 61 def as_double; @type = "double"; end |
#as_float ⇒ Object
62 |
# File 'lib/Mr.CAS/c-opt.rb', line 62 def as_float; @type = "float"; end |
#as_int ⇒ Object
63 |
# File 'lib/Mr.CAS/c-opt.rb', line 63 def as_int; @type = "int"; end |
#as_long_int ⇒ Object
64 |
# File 'lib/Mr.CAS/c-opt.rb', line 64 def as_long_int; @type = "long int"; end |
#define(k, v) ⇒ Object
66 67 68 69 70 |
# File 'lib/Mr.CAS/c-opt.rb', line 66 def define(k, v) raise ArgumentError, "k must be a String, received a #{k.class}" unless k.is_a? String @define[k] = v.to_s @define end |
#header ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/Mr.CAS/c-opt.rb', line 87 def header <<-TO_HEADER // Header file for library: #{@name}.c #ifndef #{@name}_H #define #{@name}_H // Standard Libraries #{ @include[:std].map { |e| "#include <#{e}>" }.join("\n") } // Local Libraries #{ @include[:local].map { |e| "#include \"#{e}\"" }.join("\n") } // Definitions #{ @define.map { |k, v| "#define #{k} #{v}" }.join("\n") } // Functions #{ self.keys.map do |fname| "#{@type} #{fname}(#{ self[fname].args.map { |x| "#{@type} #{x.name}" }.join(", ")});" end.join("\n") } #endif // #{@name}_H TO_HEADER end |
#implements_as(name, op) ⇒ Object
85 |
# File 'lib/Mr.CAS/c-opt.rb', line 85 def implements_as(name, op); self[name] = op; end |
#include(lib) ⇒ Object
82 |
# File 'lib/Mr.CAS/c-opt.rb', line 82 def include(lib); self.include_type(:std, lib); end |
#include_local(lib) ⇒ Object
83 |
# File 'lib/Mr.CAS/c-opt.rb', line 83 def include_local(lib); self.include_type(:local, lib); end |
#include_type(type, lib) ⇒ Object
77 78 79 80 81 |
# File 'lib/Mr.CAS/c-opt.rb', line 77 def include_type(type, lib) raise ArgumentError, "type must be a Symbol (:std, :local), received #{type}" unless [:std, :local].include? type raise ArgumentError, "lib must be a String, received a #{lib.class}" unless lib.is_a? String @include[type] << lib unless @include[type].include? lib end |
#source ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/Mr.CAS/c-opt.rb', line 121
:def]};"}.join("\n")}
return #{c_op[-1][1][:var]};
}
NEWFUNCTION
functions << nf
end
<<-TO_SOURCE
// Source file for library: #{@name}.c
#include "#{@name}.h"
#{functions.join("\n")}
// end of #{@name}.c
TO_SOURCE
end
|
#undefine(k) ⇒ Object
72 73 74 75 |
# File 'lib/Mr.CAS/c-opt.rb', line 72 def undefine(k) @define.delete k return @define end |