Class: BOAST::Expression

Inherits:
Object
  • Object
show all
Extended by:
Functor
Includes:
Arithmetic, Inspectable, PrivateStateAccessor, TypeTransition
Defined in:
lib/BOAST/Expression.rb

Direct Known Subclasses

Index

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Functor

extended

Methods included from TypeTransition

#get_transition, #set_transition, #transition

Methods included from Inspectable

#inspect

Methods included from Arithmetic

#!, #!=, #*, #+, #-, #-@, #/, #<, #<=, #==, #===, #>, #>=, #address, #and, #components, #dereference, #or

Methods included from PrivateStateAccessor

private_boolean_state_accessor, private_state_accessor

Constructor Details

#initialize(operator, operand1, operand2) ⇒ Expression

Returns a new instance of Expression.



19
20
21
22
23
# File 'lib/BOAST/Expression.rb', line 19

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

Instance Attribute Details

#operand1Object (readonly)

Returns the value of attribute operand1.



17
18
19
# File 'lib/BOAST/Expression.rb', line 17

def operand1
  @operand1
end

#operand2Object (readonly)

Returns the value of attribute operand2.



18
19
20
# File 'lib/BOAST/Expression.rb', line 18

def operand2
  @operand2
end

#operatorObject (readonly)

Returns the value of attribute operator.



16
17
18
# File 'lib/BOAST/Expression.rb', line 16

def operator
  @operator
end

Instance Method Details

#prObject



83
84
85
86
87
88
89
90
# File 'lib/BOAST/Expression.rb', line 83

def pr
  s=""
  s += indent
  s += to_s
  s += ";" if [C, CL, CUDA].include?( lang ) 
  output.puts s
  return self
end

#to_sObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/BOAST/Expression.rb', line 65

def to_s
  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 = transition(op1, op2, @operator)
  else
    oper = @operator
  end

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

  return to_s_base(op1, op2, oper, r_t)
end

#to_s_base(op1, op2, oper, return_type = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/BOAST/Expression.rb', line 25

def to_s_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 unless ( oper == "&" and lang == FORTRAN )
  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

#to_varObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/BOAST/Expression.rb', line 44

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 = transition(op1, op2, @operator)
    res_exp = to_s_base(op1, op2, oper, r_t)
    return r_t.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
  elsif op2
    res_exp = to_s_base(@operand1, op2, @operator)
    return op2.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
  elsif op1
    res_exp = to_s_base(op1, @operand2, @operator)
    return op1.copy(res_exp, :const => nil, :constant => nil, :direction => nil, :dir => nil)
  else
    STDERR.puts "#{@operand1} #{@operand2}"
    raise "Expression on no operand!"
  end
end