Class: Dentaku::AST::Modulo

Inherits:
Arithmetic show all
Defined in:
lib/dentaku/ast/arithmetic.rb

Instance Attribute Summary

Attributes inherited from Operation

#left, #right

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Arithmetic

#type

Methods inherited from Operation

#accept, #display_operator, max_param_count, min_param_count, right_associative?

Methods inherited from Node

#name, #type

Constructor Details

#initialize(left, right = nil) ⇒ Modulo

Returns a new instance of Modulo.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dentaku/ast/arithmetic.rb', line 176

def initialize(left, right = nil)
  if right
    @left  = left
    @right = right
  else
    @right = left
  end

  unless valid_left?
    raise NodeError.new(%i[numeric nil], left.type, :left),
          "#{self.class} requires numeric operands or nil"
  end
  unless valid_right?
    raise NodeError.new(:numeric, right.type, :right),
          "#{self.class} requires numeric operands"
  end
end

Class Method Details

.arityObject



167
168
169
# File 'lib/dentaku/ast/arithmetic.rb', line 167

def self.arity
  @arity
end

.peek(input) ⇒ Object



171
172
173
174
# File 'lib/dentaku/ast/arithmetic.rb', line 171

def self.peek(input)
  @arity = 1
  @arity = 2 if input.length > 1
end

.precedenceObject



218
219
220
# File 'lib/dentaku/ast/arithmetic.rb', line 218

def self.precedence
  20
end

Instance Method Details

#dependencies(context = {}) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/dentaku/ast/arithmetic.rb', line 194

def dependencies(context = {})
  if percent?
    @right.dependencies(context)
  else
    super
  end
end

#operatorObject



214
215
216
# File 'lib/dentaku/ast/arithmetic.rb', line 214

def operator
  :%
end

#percent?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/dentaku/ast/arithmetic.rb', line 202

def percent?
  left.nil?
end

#valid_left?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/dentaku/ast/arithmetic.rb', line 222

def valid_left?
  valid_node?(left) || left.nil?
end

#value(context = {}) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/dentaku/ast/arithmetic.rb', line 206

def value(context = {})
  if percent?
    cast(right.value(context)) * 0.01
  else
    super
  end
end