Class: CAS::CLib

Inherits:
Hash
  • Object
show all
Defined in:
lib/Mr.CAS/c-opt.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ CLib

Returns a new instance of CLib.

Raises:

  • (ArgumentError)


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

.create(name, &blk) ⇒ Object



36
37
38
39
40
# File 'lib/Mr.CAS/c-opt.rb', line 36

def self.create(name, &blk)
  a = CLib.new(name)
  a.instance_eval(&blk)
  return a
end

Instance Method Details

#as_doubleObject



61
# File 'lib/Mr.CAS/c-opt.rb', line 61

def as_double;   @type = "double";   end

#as_floatObject



62
# File 'lib/Mr.CAS/c-opt.rb', line 62

def as_float;    @type = "float";    end

#as_intObject



63
# File 'lib/Mr.CAS/c-opt.rb', line 63

def as_int;      @type = "int";      end

#as_long_intObject



64
# File 'lib/Mr.CAS/c-opt.rb', line 64

def as_long_int; @type = "long int"; end

#define(k, v) ⇒ Object

Raises:

  • (ArgumentError)


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

#headerObject



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

Raises:

  • (ArgumentError)


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

#sourceObject



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