BOAST 0.9994 → 0.9995

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,7 @@
1
1
  module BOAST
2
2
 
3
3
  class Sizet
4
- def self.parens(*args,&block)
5
- return Variable::new(args[0], self, *args[1..-1], &block)
6
- end
4
+ extend BOAST::VarFunctor
7
5
 
8
6
  attr_reader :signed
9
7
  attr_reader :size
@@ -39,9 +37,7 @@ module BOAST
39
37
  end
40
38
 
41
39
  class Real
42
- def self.parens(*args,&block)
43
- return Variable::new(args[0], self, *args[1..-1], &block)
44
- end
40
+ extend BOAST::VarFunctor
45
41
 
46
42
  attr_reader :size
47
43
  attr_reader :signed
@@ -112,9 +108,7 @@ module BOAST
112
108
  end
113
109
 
114
110
  class Int
115
- def self.parens(*args,&block)
116
- return Variable::new(args[0], self, *args[1..-1], &block)
117
- end
111
+ extend BOAST::VarFunctor
118
112
 
119
113
  attr_reader :size
120
114
  attr_reader :signed
@@ -234,10 +228,9 @@ module BOAST
234
228
  end
235
229
 
236
230
  class CStruct
231
+ extend BOAST::VarFunctor
232
+
237
233
  attr_reader :name, :members, :members_array
238
- def self.parens(*args,&block)
239
- return Variable::new(args[0], self, *args[1..-1], &block)
240
- end
241
234
 
242
235
  def initialize(hash={})
243
236
  @name = hash[:type_name]
@@ -250,11 +243,19 @@ module BOAST
250
243
  }
251
244
  end
252
245
 
253
- def decl
246
+ def decl_c
254
247
  return "struct #{@name}" if [C, CL, CUDA].include?( BOAST::get_lang )
248
+ end
249
+
250
+ def decl_fortran
255
251
  return "TYPE(#{@name})" if BOAST::get_lang == FORTRAN
256
252
  end
257
253
 
254
+ def decl
255
+ return self.decl_c if [C, CL, CUDA].include?( BOAST::get_lang )
256
+ return self.decl_fortran if BOAST::get_lang == FORTRAN
257
+ end
258
+
258
259
  def finalize
259
260
  s = ""
260
261
  s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
@@ -262,44 +263,37 @@ module BOAST
262
263
  return s
263
264
  end
264
265
 
265
- def indent
266
- return " "*BOAST::get_indent_level
266
+ def define
267
+ return define_c if [C, CL, CUDA].include?( BOAST::get_lang )
268
+ return define_fortran if BOAST::get_lang == FORTRAN
267
269
  end
268
270
 
269
- def header
270
- return header_c if [C, CL, CUDA].include?( BOAST::get_lang )
271
- return header_fortran if BOAST::get_lang == FORTRAN
272
- raise "Unsupported language!"
273
- end
274
-
275
- def header_c(final = true)
276
- s = ""
277
- s += self.indent if final
278
- s += self.decl + " {\n"
271
+ def define_c
272
+ s = BOAST::indent
273
+ s += self.decl_c + " {"
274
+ BOAST::get_output.puts s
279
275
  @members_array.each { |value|
280
- s+= self.indent if final
281
- s+= " "*BOAST::get_indent_increment + value.decl(false)+";\n"
276
+ value.decl
282
277
  }
283
- s += self.indent if final
278
+ s = BOAST::indent
284
279
  s += "}"
285
- s += self.finalize if final
286
- BOAST::get_output.print s if final
287
- return s
280
+ s += self.finalize
281
+ BOAST::get_output.print s
282
+ return self
288
283
  end
289
284
 
290
- def header_fortran(final = true)
291
- s = ""
292
- s += self.indent if final
285
+ def define_fortran
286
+ s = BOAST::indent
293
287
  s += "TYPE :: #{@name}\n"
294
- members_array.each { |value|
295
- s+= self.indent if final
296
- s+= " "*BOAST::get_indent_increment + value.decl(false)+"\n"
288
+ BOAST::get_output.puts s
289
+ @members_array.each { |value|
290
+ value.decl
297
291
  }
298
- s += self.indent if final
292
+ s = BOAST::indent
299
293
  s += "END TYPE #{@name}"
300
- s += self.finalize if final
301
- BOAST::get_output.print s if final
302
- return s
294
+ s += self.finalize
295
+ BOAST::get_output.print s
296
+ return self
303
297
  end
304
298
 
305
299
  end
@@ -1,14 +1,13 @@
1
1
  module BOAST
2
- def BOAST::Return(value)
2
+
3
+ def self.Return(value)
3
4
  return Expression("return",nil, value)
4
5
  end
5
6
 
6
7
  class Expression
7
8
  include BOAST::Arithmetic
8
-
9
- def self.parens(*args,&block)
10
- return self::new(*args,&block)
11
- end
9
+ include BOAST::Inspectable
10
+ extend BOAST::Functor
12
11
 
13
12
  attr_reader :operator
14
13
  attr_reader :operand1
@@ -18,11 +17,8 @@ module BOAST
18
17
  @operand1 = operand1
19
18
  @operand2 = operand2
20
19
  end
21
- def to_s
22
- self.to_str
23
- end
24
20
 
25
- def Expression.to_str_base(op1, op2, oper, return_type = nil)
21
+ def Expression.to_s_base(op1, op2, oper, return_type = nil)
26
22
  return oper.to_s(op1, op2, return_type) if not oper.kind_of?(String)
27
23
  s = ""
28
24
  if op1 then
@@ -48,13 +44,13 @@ module BOAST
48
44
  op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
49
45
  if op1 and op2 then
50
46
  r_t, oper = BOAST::transition(op1, op2, @operator)
51
- res_exp = BOAST::Expression::to_str_base(op1, op2, oper, r_t)
47
+ res_exp = BOAST::Expression::to_s_base(op1, op2, oper, r_t)
52
48
  return r_t.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
53
49
  elsif op2
54
- res_exp = BOAST::Expression::to_str_base(@operand1, op2, @operator)
50
+ res_exp = BOAST::Expression::to_s_base(@operand1, op2, @operator)
55
51
  return op2.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
56
52
  elsif op1
57
- res_exp = BOAST::Expression::to_str_base(op1, @operand2, @operator)
53
+ res_exp = BOAST::Expression::to_s_base(op1, @operand2, @operator)
58
54
  return op1.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
59
55
  else
60
56
  STDERR.puts "#{@operand1} #{@operand2}"
@@ -62,7 +58,7 @@ module BOAST
62
58
  end
63
59
  end
64
60
 
65
- def to_str
61
+ def to_s
66
62
  op1 = nil
67
63
  op1 = @operand1.to_var if @operand1.respond_to?(:to_var)
68
64
  op2 = nil
@@ -77,16 +73,16 @@ module BOAST
77
73
  op1 = @operand1 if op1.nil?
78
74
  op2 = @operand2 if op2.nil?
79
75
 
80
- return BOAST::Expression::to_str_base(op1, op2, oper, r_t)
76
+ return BOAST::Expression::to_s_base(op1, op2, oper, r_t)
81
77
  end
82
78
 
83
- def print(final=true)
79
+ def print
84
80
  s=""
85
- s += " "*BOAST::get_indent_level if final
86
- s += self.to_str
87
- s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang )
88
- BOAST::get_output.puts s if final
89
- return s
81
+ s += BOAST::indent
82
+ s += self.to_s
83
+ s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
84
+ BOAST::get_output.puts s
85
+ return self
90
86
  end
91
87
  end
92
88
 
data/lib/BOAST/For.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  module BOAST
2
- class For
2
+
3
+ class For < ControlStructure
4
+ include BOAST::Inspectable
5
+ extend BOAST::Functor
6
+
3
7
  attr_reader :iterator
4
8
  attr_reader :begin
5
9
  attr_reader :end
6
10
  attr_reader :step
7
11
 
8
- def self.parens(*args,&block)
9
- return self::new(*args,&block)
10
- end
11
-
12
12
  def initialize(i, b, e, s=1, &block)
13
13
  @iterator = i
14
14
  @begin = b
@@ -16,22 +16,29 @@ module BOAST
16
16
  @step = s
17
17
  @block = block
18
18
  end
19
+
20
+ @@c_strings = {
21
+ :for => '"for (#{i} = #{b}; #{i} <= #{e}; #{i} += #{s}) {"',
22
+ :end => '"}"'
23
+ }
24
+
25
+ @@f_strings = {
26
+ :for => '"do #{i} = #{b}, #{e}, #{s}"',
27
+ :end => '"end do"'
28
+ }
29
+
30
+ @@strings = {
31
+ BOAST::C => @@c_strings,
32
+ BOAST::CL => @@c_strings,
33
+ BOAST::CUDA => @@c_strings,
34
+ BOAST::FORTRAN => @@f_strings
35
+ }
36
+
37
+ eval token_string_generator( * %w{for i b e s})
38
+ eval token_string_generator( * %w{end})
39
+
19
40
  def to_s
20
- self.to_str
21
- end
22
- def to_str
23
- return self.to_str_fortran if BOAST::get_lang == FORTRAN
24
- return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
25
- end
26
- def to_str_fortran
27
- s = ""
28
- s += "do #{@iterator}=#{@begin}, #{@end}"
29
- s += ", #{@step}" if 1 != @step
30
- return s
31
- end
32
- def to_str_c
33
- s = ""
34
- s += "for(#{@iterator}=#{@begin}; #{@iterator}<=#{@end}; #{@iterator}+=#{@step}){"
41
+ s = for_string(@iterator, @begin, @end, @step)
35
42
  return s
36
43
  end
37
44
 
@@ -78,41 +85,33 @@ module BOAST
78
85
  @iterator.constant = nil
79
86
  end
80
87
 
81
- def print(*args)
82
- final = true
88
+ def open
83
89
  s=""
84
- s += " "*BOAST::get_indent_level if final
85
- s += self.to_str
90
+ s += BOAST::indent
91
+ s += self.to_s
92
+ BOAST::get_output.puts s
86
93
  BOAST::increment_indent_level
87
- BOAST::get_output.puts s if final
94
+ return self
95
+ end
96
+
97
+ def print(*args)
98
+ self.open
88
99
  if @block then
89
- s += "\n"
90
100
  @block.call(*args)
91
- s += self.close
101
+ self.close
92
102
  end
93
- return s
103
+ return self
94
104
  end
95
105
 
96
- def close(final=true)
97
- return self.close_fortran(final) if BOAST::get_lang == FORTRAN
98
- return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
99
- end
100
- def close_c(final=true)
101
- s = ""
106
+ def close
102
107
  BOAST::decrement_indent_level
103
- s += " "*BOAST::get_indent_level if final
104
- s += "}"
105
- BOAST::get_output.puts s if final
106
- return s
107
- end
108
- def close_fortran(final=true)
109
108
  s = ""
110
- BOAST::decrement_indent_level
111
- s += " "*BOAST::get_indent_level if final
112
- s += "enddo"
113
- BOAST::get_output.puts s if final
114
- return s
109
+ s += BOAST::indent
110
+ s += end_string
111
+ BOAST::get_output.puts s
112
+ return self
115
113
  end
114
+
116
115
  end
117
116
 
118
117
  end
@@ -2,12 +2,11 @@ module BOAST
2
2
 
3
3
  class FuncCall
4
4
  include BOAST::Arithmetic
5
+ include BOAST::Inspectable
6
+ extend BOAST::Functor
5
7
 
6
8
  @return_type
7
9
  @options
8
- def self.parens(*args,&block)
9
- return self::new(*args,&block)
10
- end
11
10
 
12
11
  attr_reader :func_name
13
12
  attr_reader :args
@@ -36,30 +35,29 @@ module BOAST
36
35
  end
37
36
 
38
37
  def to_s
39
- self.to_str
38
+ return self.to_s_fortran if BOAST::get_lang == FORTRAN
39
+ return self.to_s_c if [C, CL, CUDA].include?( BOAST::get_lang )
40
40
  end
41
41
 
42
- def to_str
43
- return self.to_str_fortran if BOAST::get_lang == FORTRAN
44
- return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
45
- end
46
- def to_str_fortran
42
+ def to_s_fortran
47
43
  s = ""
48
44
  s += @prefix if @prefix
49
45
  s += "#{func_name}(#{@args.join(", ")})"
50
46
  end
51
- def to_str_c
47
+
48
+ def to_s_c
52
49
  s = ""
53
50
  s += @prefix if @prefix
54
51
  s += "#{func_name}(#{@args.join(", ")})"
55
52
  end
56
- def print(final=true)
53
+
54
+ def print
57
55
  s=""
58
- s += " "*BOAST::get_indent_level if final
59
- s += self.to_str
60
- s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang )
61
- BOAST::get_output.puts s if final
62
- return s
56
+ s += BOAST::indent
57
+ s += self.to_s
58
+ s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
59
+ BOAST::get_output.puts s
60
+ return self
63
61
  end
64
62
  end
65
63
 
@@ -0,0 +1,19 @@
1
+ module BOAST
2
+
3
+ module Functor
4
+
5
+ def parens(*args,&block)
6
+ return self::new(*args,&block)
7
+ end
8
+
9
+ end
10
+
11
+ module VarFunctor
12
+
13
+ def parens(*args,&block)
14
+ return Variable::new(args[0], self, *args[1..-1], &block)
15
+ end
16
+
17
+ end
18
+
19
+ end
data/lib/BOAST/If.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  module BOAST
2
2
 
3
- class If
4
- def self.parens(*args,&block)
5
- return self::new(*args,&block)
6
- end
3
+ class If < BOAST::ControlStructure
4
+ include BOAST::Inspectable
5
+ extend BOAST::Functor
7
6
 
8
7
  attr_reader :conditions
8
+
9
9
  def initialize(*conditions, &block)
10
10
  @conditions = []
11
11
  @blocks = []
@@ -27,83 +27,84 @@ module BOAST
27
27
  @blocks.push(conditions.last)
28
28
  end
29
29
  end
30
- def to_s(*args)
31
- self.to_str(*args)
32
- end
33
- def to_str(condition, first= true)
34
- return self.to_str_fortran(condition, first) if BOAST::get_lang == FORTRAN
35
- return self.to_str_c(condition, first) if [C, CL, CUDA].include?( BOAST::get_lang )
36
- end
37
- def to_str_fortran(condition, first)
38
- s = ""
39
- if first then
40
- s += "if ( #{condition} ) then"
41
- else
42
- if condition then
43
- s += "else if ( #{condition} ) then"
44
- else
45
- s += "else"
46
- end
47
- end
48
- return s
49
- end
50
- def to_str_c(condition, first)
30
+
31
+ @@c_strings = {
32
+ :if => '"if (#{cond}) {"',
33
+ :else_if => '"} else if (#{cond}) {"',
34
+ :else => '"} else {"',
35
+ :end => '"}"'
36
+ }
37
+
38
+ @@f_strings = {
39
+ :if => '"if (#{cond}) then"',
40
+ :elsif => '"else if (#{cond}) then"',
41
+ :else => '"else"',
42
+ :end => '"end if"'
43
+ }
44
+
45
+ @@strings = {
46
+ BOAST::C => @@c_strings,
47
+ BOAST::CL => @@c_strings,
48
+ BOAST::CUDA => @@c_strings,
49
+ BOAST::FORTRAN => @@f_strings
50
+ }
51
+
52
+ eval token_string_generator( * %w{if cond} )
53
+ eval token_string_generator( * %w{elsif cond} )
54
+ eval token_string_generator( * %w{else} )
55
+ eval token_string_generator( * %w{end} )
56
+
57
+ def to_s(condition_number = 0)
51
58
  s = ""
52
- if first then
53
- s += "if(#{condition}){"
59
+ if condition_number == 0 then
60
+ s += if_string(@conditions.first)
54
61
  else
55
- if condition then
56
- s += "} else if(#{condition}){"
62
+ if @conditions[condition_number] then
63
+ s += elsif_string(@conditions[condition_number])
57
64
  else
58
- s += "} else {"
65
+ s += else_string
59
66
  end
60
67
  end
61
68
  return s
62
69
  end
63
- def print(*args)
70
+
71
+ def open
64
72
  s=""
65
- s += " "*BOAST::get_indent_level
66
- s += self.to_str(@conditions.first)
67
- BOAST::increment_indent_level
73
+ s += BOAST::indent
74
+ s += self.to_s
68
75
  BOAST::get_output.puts s
76
+ BOAST::increment_indent_level
77
+ return self
78
+ end
79
+
80
+ def print(*args)
69
81
  if @blocks.size > 0 then
70
- if @blocks[0] then
71
- @blocks[0].call(*args)
72
- end
73
- @blocks[1..-1].each_index { |indx|
74
- BOAST::decrement_indent_level
82
+ BOAST::increment_indent_level
83
+ @blocks.each_index { |indx|
84
+ BOAST::decrement_indent_level
75
85
  s=""
76
- s += " "*BOAST::get_indent_level
77
- s += self.to_str(@conditions[1..-1][indx],false)
78
- BOAST::increment_indent_level
86
+ s += BOAST::indent
87
+ s += self.to_s(indx)
79
88
  BOAST::get_output.puts s
80
- @blocks[1..-1][indx].call(*args)
89
+ BOAST::increment_indent_level
90
+ @blocks[indx].call(*args)
81
91
  }
82
92
  self.close
93
+ else
94
+ self.open
83
95
  end
84
96
  return self
85
97
  end
86
- def close(final=true)
87
- return self.close_fortran(final) if BOAST::get_lang == FORTRAN
88
- return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
89
- end
90
- def close_c(final=true)
91
- s = ""
92
- BOAST::decrement_indent_level
93
- s += " "*BOAST::get_indent_level if final
94
- s += "}"
95
- BOAST::get_output.puts s if final
96
- return s
97
- end
98
- def close_fortran(final=true)
98
+
99
+ def close
100
+ BOAST::decrement_indent_level
99
101
  s = ""
100
- BOAST::decrement_indent_level
101
- s += " "*BOAST::get_indent_level if final
102
- s += "end if"
103
- BOAST::get_output.puts s if final
104
- return s
102
+ s += BOAST::indent
103
+ s += end_string
104
+ BOAST::get_output.puts s
105
+ return self
105
106
  end
106
107
 
107
108
  end
108
109
 
109
- end
110
+ end
data/lib/BOAST/Index.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module BOAST
2
2
  class Index < Expression
3
+ include BOAST::Inspectable
3
4
  attr_reader :source
4
5
  attr_reader :indexes
5
6
  def initialize(source, indexes)
@@ -13,18 +14,17 @@ module BOAST
13
14
  end
14
15
 
15
16
  def to_s
16
- self.to_str
17
+ return self.to_s_fortran if BOAST::get_lang == FORTRAN
18
+ return self.to_s_c if [C, CL, CUDA].include?( BOAST::get_lang )
17
19
  end
18
- def to_str
19
- return self.to_str_fortran if BOAST::get_lang == FORTRAN
20
- return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
21
- end
22
- def to_str_fortran
20
+
21
+ def to_s_fortran
23
22
  s = ""
24
23
  s += "#{@source}(#{@indexes.join(", ")})"
25
24
  return s
26
25
  end
27
- def to_str_texture
26
+
27
+ def to_s_texture
28
28
  raise "Unsupported language #{BOAST::get_lang} for texture!" if not [CL, CUDA].include?( BOAST::get_lang )
29
29
  raise "Write is unsupported for textures!" if not ( @source.constant or @source.direction == :in )
30
30
  dim_number = 1
@@ -69,8 +69,9 @@ module BOAST
69
69
  end
70
70
  return s
71
71
  end
72
- def to_str_c
73
- return to_str_texture if @source.texture
72
+
73
+ def to_s_c
74
+ return to_s_texture if @source.texture
74
75
  dim = @source.dimension.first
75
76
  if dim.val2 then
76
77
  start = dim.val1
@@ -110,14 +111,16 @@ module BOAST
110
111
  s = "#{@source}[" + sub + "]"
111
112
  return s
112
113
  end
113
- def print(final=true)
114
+
115
+ def print
114
116
  s=""
115
- s += " "*BOAST::get_indent_level if final
116
- s += self.to_str
117
- s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang )
118
- BOAST::get_output.puts s if final
119
- return s
117
+ s += BOAST::indent
118
+ s += self.to_s
119
+ s += ";" if [C, CL, CUDA].include?( BOAST::get_lang )
120
+ BOAST::get_output.puts s
121
+ return self
120
122
  end
123
+
121
124
  end
122
125
 
123
126
  end
@@ -0,0 +1,28 @@
1
+ module BOAST
2
+
3
+ @@inspect = false
4
+
5
+ def self.inspect?
6
+ return @@inspect
7
+ end
8
+
9
+ def self.inspect=(val)
10
+ @@inspect = val
11
+ end
12
+
13
+ module Inspectable
14
+
15
+ def inspect
16
+ if BOAST::inspect? then
17
+ variables = self.instance_variables.map{ |v|
18
+ instance_variable_get(v) ? "#{v}=#{instance_variable_get(v).inspect}" : nil
19
+ }.reject{ |v| v.nil? }.join(", ")
20
+ "#<#{self.class}:#{(self.object_id<<1).to_s(16)}#{variables == "" ? "" : " #{variables}" }>"
21
+ else
22
+ self.to_s
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end