Class: BOAST::Expression

Inherits:
Object show all
Defined in:
lib/BOAST/Algorithm.rb

Direct Known Subclasses

Index

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, operand1, operand2) ⇒ Expression

Returns a new instance of Expression.



210
211
212
213
214
# File 'lib/BOAST/Algorithm.rb', line 210

def initialize(operator, operand1, operand2)
  @operator = operator
  @operand1 = operand1
  @operand2 = operand2
end

Instance Attribute Details

#operand1Object (readonly)

Returns the value of attribute operand1.



208
209
210
# File 'lib/BOAST/Algorithm.rb', line 208

def operand1
  @operand1
end

#operand2Object (readonly)

Returns the value of attribute operand2.



209
210
211
# File 'lib/BOAST/Algorithm.rb', line 209

def operand2
  @operand2
end

#operatorObject (readonly)

Returns the value of attribute operator.



207
208
209
# File 'lib/BOAST/Algorithm.rb', line 207

def operator
  @operator
end

Class Method Details

.parens(*args, &block) ⇒ Object



203
204
205
# File 'lib/BOAST/Algorithm.rb', line 203

def self.parens(*args,&block)
  return self::new(*args,&block)
end

.to_str_base(op1, op2, oper, return_type = nil) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/BOAST/Algorithm.rb', line 271

def Expression.to_str_base(op1, op2, oper, return_type = nil)
  return oper.to_s(op1,op2, return_type) if not oper.kind_of?(String)
  s = ""
  if op1 then
    s += "(" if (oper == "*" or oper == "/") 
    s += op1.to_s
    s += ")" if (oper == "*" or oper == "/") 
  end        
  s += " " unless oper == "++" or oper == "."
  s += oper 
  s += " " unless oper == "." or oper == "&" or ( oper == "*" and op1.nil? )
  if op2 then
    s += "(" if (oper == "*" or oper == "/" or oper == "-") 
    s += op2.to_s
    s += ")" if (oper == "*" or oper == "/" or oper == "-") 
  end
  return s
end

Instance Method Details

#!Object



263
264
265
# File 'lib/BOAST/Algorithm.rb', line 263

def !
  return Expression::new(BOAST::Not,nil,self)
end

#*(x) ⇒ Object



243
244
245
# File 'lib/BOAST/Algorithm.rb', line 243

def *(x)
  return Expression::new(BOAST::Multiplication,self,x)
end

#+(x) ⇒ Object



227
228
229
# File 'lib/BOAST/Algorithm.rb', line 227

def +(x)
  return Expression::new(BOAST::Addition,self,x)
end

#-(x) ⇒ Object



259
260
261
# File 'lib/BOAST/Algorithm.rb', line 259

def -(x)
  return Expression::new(BOAST::Substraction,self,x)
end

#-@Object



267
268
269
# File 'lib/BOAST/Algorithm.rb', line 267

def -@
  return Expression::new(BOAST::Minus,nil,self)
end

#/(x) ⇒ Object



247
248
249
# File 'lib/BOAST/Algorithm.rb', line 247

def /(x)
  return Expression::new(BOAST::Division,self,x)
end

#<(x) ⇒ Object



235
236
237
# File 'lib/BOAST/Algorithm.rb', line 235

def <(x)
  return Expression::new("<",self,x)
end

#==(x) ⇒ Object



223
224
225
# File 'lib/BOAST/Algorithm.rb', line 223

def ==(x)
  return Expression::new("==",self,x)
end

#===(x) ⇒ Object



219
220
221
# File 'lib/BOAST/Algorithm.rb', line 219

def ===(x)
  return Expression::new(BOAST::Affectation,self,x)
end

#>(x) ⇒ Object



231
232
233
# File 'lib/BOAST/Algorithm.rb', line 231

def >(x)
  return Expression::new(">",self,x)
end

#>=(x) ⇒ Object



239
240
241
# File 'lib/BOAST/Algorithm.rb', line 239

def >=(x)
  return Expression::new(">=",self,x)
end

#dereferenceObject



251
252
253
# File 'lib/BOAST/Algorithm.rb', line 251

def dereference
  return Expression::new("*",nil,self)
end


329
330
331
332
333
334
335
336
# File 'lib/BOAST/Algorithm.rb', line 329

def print(final=true)
  s=""
  s += " "*BOAST::get_indent_level if final
  s += self.to_str
  s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang ) 
  BOAST::get_output.puts s if final
  return s
end

#struct_reference(x) ⇒ Object



255
256
257
# File 'lib/BOAST/Algorithm.rb', line 255

def struct_reference(x)
  return Expression::new(".",self,x)
end

#to_sObject



215
216
217
# File 'lib/BOAST/Algorithm.rb', line 215

def to_s
  self.to_str
end

#to_strObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/BOAST/Algorithm.rb', line 311

def to_str
  op1 = nil
  op1 = @operand1.to_var if @operand1.respond_to?(:to_var)
  op2 = nil
  op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
  r_t = nil
  if op1 and op2 then
    r_t, oper = BOAST::transition(op1, op2, @operator)
  else
    oper = @operator
  end

  op1 = @operand1 if op1.nil?
  op2 = @operand2 if op2.nil?

  return BOAST::Expression::to_str_base(op1, op2, oper, r_t)
end

#to_varObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/BOAST/Algorithm.rb', line 290

def to_var
  op1 = nil
  op1 = @operand1.to_var if @operand1.respond_to?(:to_var)
  op2 = nil
  op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
  if op1 and op2 then
    r_t, oper = BOAST::transition(op1, op2, @operator)
    res_exp = BOAST::Expression::to_str_base(op1, op2, oper, r_t)
    return r_t.copy(res_exp, :const => nil, :constant => nil)
  elsif op2
    res_exp = BOAST::Expression::to_str_base(@operand1, op2, @operator)
    return op2.copy(res_exp, :const => nil, :constant => nil)
  elsif op1
    res_exp = BOAST::Expression::to_str_base(op1, @operand2, @operator)
    return op1.copy(res_exp, :const => nil, :constant => nil)
  else
    STDERR.puts "#{@operand1} #{@operand2}"
    raise "Expression on no operand!"
  end
end