Class: RubimCode::UserVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/rubimc.rb

Direct Known Subclasses

LoopCounter, UserClass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = "undefined") ⇒ UserVariable

attr_accessor :level # ToDo: для указания области видимости



25
26
27
# File 'lib/rubimc.rb', line 25

def initialize(name, type = "undefined")
  @name, @type = name.to_s, type.to_s
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/rubimc.rb', line 23

def name
  @name
end

#typeObject

Returns the value of attribute type.



23
24
25
# File 'lib/rubimc.rb', line 23

def type
  @type
end

Instance Method Details

#!=(val) ⇒ Object



91
# File 'lib/rubimc.rb', line 91

def !=(val); common_operator(val, __method__); end

#!@Object



86
# File 'lib/rubimc.rb', line 86

def !@; common_operator(nil, __method__, unary: true); end

#%(val) ⇒ Object



77
# File 'lib/rubimc.rb', line 77

def %(val); common_operator(val, __method__); end

#&(val) ⇒ Object

Binary Operators:



98
# File 'lib/rubimc.rb', line 98

def &(val); common_operator(val, __method__); end

#*(val) ⇒ Object



75
# File 'lib/rubimc.rb', line 75

def *(val); common_operator(val, __method__); end

#**(val) ⇒ Object



78
# File 'lib/rubimc.rb', line 78

def **(val);common_operator(val, __method__); end

#+(val) ⇒ Object

Arithmetic Operators:



73
# File 'lib/rubimc.rb', line 73

def +(val); common_operator(val, __method__); end

#+@Object



85
# File 'lib/rubimc.rb', line 85

def +@; common_operator(nil, __method__, unary: true); end

#-(val) ⇒ Object



74
# File 'lib/rubimc.rb', line 74

def -(val); common_operator(val, __method__); end

#-@Object

Unary Operators:



84
# File 'lib/rubimc.rb', line 84

def -@; common_operator(nil, __method__, unary: true); end

#/(val) ⇒ Object



76
# File 'lib/rubimc.rb', line 76

def /(val); common_operator(val, __method__); end

#<(val) ⇒ Object



92
# File 'lib/rubimc.rb', line 92

def  <(val); common_operator(val, __method__); end

#<<(val) ⇒ Object



101
# File 'lib/rubimc.rb', line 101

def <<(val); common_operator(val, __method__); end

#<=(val) ⇒ Object



94
# File 'lib/rubimc.rb', line 94

def <=(val); common_operator(val, __method__); end

#==(val) ⇒ Object

Comparison Operators:



90
# File 'lib/rubimc.rb', line 90

def ==(val); common_operator(val, __method__); end

#>(val) ⇒ Object



93
# File 'lib/rubimc.rb', line 93

def  >(val); common_operator(val, __method__); end

#>=(val) ⇒ Object



95
# File 'lib/rubimc.rb', line 95

def >=(val); common_operator(val, __method__); end

#>>(val) ⇒ Object



102
# File 'lib/rubimc.rb', line 102

def >>(val); common_operator(val, __method__); end

#^(val) ⇒ Object



100
# File 'lib/rubimc.rb', line 100

def ^(val); common_operator(val, __method__); end

#c_assign=(val) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rubimc.rb', line 47

def c_assign=(val)
  RubimCode::Isolator.permit!(self)
  RubimCode::Isolator.permit!(val)

  RubimCode.perror "Undefined variable or method" if val.nil?
  RubimCode.perror "Wrong match types" unless val.is_a? UserVariable

  RubimCode.pout "#{self.name} = #{val};"
end

#common_operator(val, operator_sym, **options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubimc.rb', line 57

def common_operator(val, operator_sym, **options)
  if not val.class.respond_to? :to_s
    RubimCode.perror "Conversion of variable #{val} is impossible. Method 'to_s' not found"
  else 
    if options[:unary]
      RubimCode::Isolator.permit!(self)
      UserVariable.new(operator_sym.to_s[0] + self.name, 'expression')
    else
      RubimCode::Isolator.permit!(self)
      RubimCode::Isolator.permit!(val)
      UserVariable.new(self.name + operator_sym.to_s + val.to_s, 'expression')
    end
  end
end

#times {|n| ... } ⇒ Object

Range-operators “..” and “…” ToDo: is it need? or use Enumerator?

Yields:

  • (n)


123
124
125
126
127
128
129
130
# File 'lib/rubimc.rb', line 123

def times
  n = LoopCounter.new
  RubimCode.pout ("for (int #{n}=0; #{n}<#{self}; #{n}++) {")
  RubimCode.level += 1
  yield(n)
  RubimCode.pout ("}")
  RubimCode.level -= 1
end

#to_boolObject



37
38
39
40
41
# File 'lib/rubimc.rb', line 37

def to_bool
  return true   if self.name == true   || self.name =~ (/(true|t|yes|y|1)$/i)
  return false  if self.name == false  || self.name.blank? || self.name =~ (/(false|f|no|n|0)$/i)
  RubimCode.perror "Can not convert variable #{self} to boolean"
end

#to_iObject



33
34
35
# File 'lib/rubimc.rb', line 33

def to_i
  self.name.to_i
end

#to_rubimObject



43
44
45
# File 'lib/rubimc.rb', line 43

def to_rubim
  self
end

#to_sObject



29
30
31
# File 'lib/rubimc.rb', line 29

def to_s
  "(" + self.name + ")"
end

#|(val) ⇒ Object



99
# File 'lib/rubimc.rb', line 99

def |(val); common_operator(val, __method__); end

#~@Object



87
# File 'lib/rubimc.rb', line 87

def ~@; common_operator(nil, __method__, unary: true); end