Class: IntegerVal

Inherits:
BaseVal show all
Defined in:
lib/tecsgen/core/value.rb

Overview

IntegerVal: 整数値を扱うクラス

Instance Method Summary collapse

Methods inherited from BaseVal

#unsupport

Methods inherited from Node

#cdl_error, #cdl_error2, #cdl_error3, #cdl_info, #cdl_info2, #cdl_warning, #cdl_warning2, #get_locale, #locale_str, #set_locale

Constructor Details

#initialize(val, str = nil, sign = :SIGNED, size = :NORMAL) ⇒ IntegerVal

@size

Symbol: :NORMAL | :SHORT | :LONG | :LONGLONG



226
227
228
229
230
231
232
# File 'lib/tecsgen/core/value.rb', line 226

def initialize(val, str = nil, sign = :SIGNED, size = :NORMAL)
  super()
  @val = val.to_i
  @str = str
  @sign = sign
  @size = size
end

Instance Method Details

#%(val) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/tecsgen/core/value.rb', line 276

def %(val)
  if val.is_a? FloatVal
    v2 = val.to_f   # to_f を2回評価しない
    if v2 == 0.0
      cdl_error("V1011 % : divieded by zero")
      return FloatVal.new(1.0)
    end
    return FloatVal.new(@val.to_f % v2)
  else
    v2 = val.to_i   # to_i を2回評価しない
    if v2 == 0
      cdl_error("V1012 % : divieded by zero")
      return IntegerVal.new(1)
    end
    return IntegerVal.new(@val % v2)
  end
end

#&(val) ⇒ Object



366
367
368
# File 'lib/tecsgen/core/value.rb', line 366

def &(val)
  IntegerVal.new(@val & val.to_i)
end

#*(val) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/tecsgen/core/value.rb', line 250

def *(val)
  if val.is_a? FloatVal
    return FloatVal.new(@val.to_f * val.to_f)
  else
    return IntegerVal.new(@val * val.to_i)
  end
end

#+(val) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/tecsgen/core/value.rb', line 294

def +(val)
  if val.is_a? FloatVal
    return FloatVal.new(@val.to_f + val.to_f)
  else
    return IntegerVal.new(@val + val.to_i)
  end
end

#+@Object



242
243
244
# File 'lib/tecsgen/core/value.rb', line 242

def +@
  self
end

#-(val) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/tecsgen/core/value.rb', line 302

def -(val)
  if val.is_a? FloatVal
    return FloatVal.new(@val.to_f - val.to_f)
  else
    return IntegerVal.new(@val - val.to_i)
  end
end

#-@Object



238
239
240
# File 'lib/tecsgen/core/value.rb', line 238

def -@
  IntegerVal.new(- @val)
end

#/(val) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/tecsgen/core/value.rb', line 258

def /(val)
  if val.is_a? FloatVal
    v2 = val.to_f   # to_f を2回評価しない
    if v2 == 0.0
      cdl_error("V1009 / : divieded by zero")
      return FloatVal.new(1.0)
    end
    return FloatVal.new(@val.to_f / v2)
  else
    v2 = val.to_i   # to_i を2回評価しない
    if v2 == 0
      cdl_error("V1010 / : divieded by zero")
      return IntegerVal.new(1)
    end
    return IntegerVal.new(@val / v2)
  end
end

#<(val) ⇒ Object



326
327
328
329
330
331
332
# File 'lib/tecsgen/core/value.rb', line 326

def <(val)
  if val.is_a? FloatVal
    return BoolVal.new(@val.to_f < val.to_f)
  else
    return BoolVal.new(@val < val.to_i)
  end
end

#<<(val) ⇒ Object



310
311
312
# File 'lib/tecsgen/core/value.rb', line 310

def <<(val)
  return IntegerVal.new(@val << val.to_i)
end

#<=(val) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/tecsgen/core/value.rb', line 342

def <=(val)
  if val.is_a? FloatVal
    return BoolVal.new(@val.to_f <= val.to_f)
  else
    return BoolVal.new(@val <= val.to_i)
  end
end

#>(val) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/tecsgen/core/value.rb', line 318

def >(val)
  if val.is_a? FloatVal
    return BoolVal.new(@val.to_f > val.to_f)
  else
    return BoolVal.new(@val > val.to_i)
  end
end

#>=(val) ⇒ Object



334
335
336
337
338
339
340
# File 'lib/tecsgen/core/value.rb', line 334

def >=(val)
  if val.is_a? FloatVal
    return BoolVal.new(@val.to_f >= val.to_f)
  else
    return BoolVal.new(@val >= val.to_i)
  end
end

#>>(val) ⇒ Object



314
315
316
# File 'lib/tecsgen/core/value.rb', line 314

def >>(val)
  return IntegerVal.new(@val >> val.to_i)
end

#^(val) ⇒ Object



370
371
372
# File 'lib/tecsgen/core/value.rb', line 370

def ^(val)
  IntegerVal.new(@val ^ val.to_i)
end

#cast(type) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/tecsgen/core/value.rb', line 386

def cast(type)
  t = type.get_original_type # typedef の元を得る
  if t.is_a? IntType
    val = t.check_and_clip(@val, :IntType)
    return IntegerVal.new(val)
  elsif t.is_a? FloatType
    return FloatVal.new(@val)
  elsif t.is_a? PtrType
    return PointerVal.new(@val, type)
  elsif t.is_a? BoolType
    return BoolVal.new(@val.to_b)
  else
    cdl_error("V1013 integer value cannot cast to $1", type.class)
    return nil
  end
end

#eq(val) ⇒ Object

val



350
351
352
353
354
355
356
# File 'lib/tecsgen/core/value.rb', line 350

def eq(val) # == val
  if val.is_a? FloatVal
    return BoolVal.new(@val.to_f == val.to_f)
  else
    return BoolVal.new(@val == val.to_i)
  end
end

#lAND(val) ⇒ Object

&& val



378
379
380
# File 'lib/tecsgen/core/value.rb', line 378

def lAND(val)  # && val
  BoolVal.new(self.to_b && val.to_b)
end

#lOR(val) ⇒ Object

|| val



382
383
384
# File 'lib/tecsgen/core/value.rb', line 382

def lOR(val)   # || val
  BoolVal.new(self.to_b || val.to_b)
end

#neq(val) ⇒ Object

!= val



358
359
360
361
362
363
364
# File 'lib/tecsgen/core/value.rb', line 358

def neq(val) # != val
  if val.is_a? FloatVal
    return BoolVal.new(@val.to_f != val.to_f)
  else
    return BoolVal.new(@val != val.to_i)
  end
end

#notObject

!



246
247
248
# File 'lib/tecsgen/core/value.rb', line 246

def not # !
  BoolVal.new(self.to_b)
end

#to_bObject



411
412
413
# File 'lib/tecsgen/core/value.rb', line 411

def to_b
  @val != 0
end

#to_fObject



419
420
421
# File 'lib/tecsgen/core/value.rb', line 419

def to_f
  @val.to_f
end

#to_iObject



415
416
417
# File 'lib/tecsgen/core/value.rb', line 415

def to_i
  @val
end

#to_sObject



403
404
405
406
407
408
409
# File 'lib/tecsgen/core/value.rb', line 403

def to_s
  if @str
    @str
  else
    @val.to_s
  end
end

#|(val) ⇒ Object



374
375
376
# File 'lib/tecsgen/core/value.rb', line 374

def |(val)
  IntegerVal.new(@val | val.to_i)
end

#~@Object



234
235
236
# File 'lib/tecsgen/core/value.rb', line 234

def ~@
  IntegerVal.new(~ @val)
end