Class: Num

Inherits:
DataType show all
Defined in:
lib/sdx/vm/datatypes.rb

Instance Attribute Summary

Attributes inherited from DataType

#fields, #internal

Instance Method Summary collapse

Constructor Details

#initialize(val = nil) ⇒ Num

Returns a new instance of Num.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/sdx/vm/datatypes.rb', line 305

def initialize(val=nil)
    if val != nil
        @internal = BigDecimal val
    end
    @fields = {
        "__as_str" => (NativeFnInternal.new (Proc.new do
            as_string
        end)),
        "__as_code_str" => (NativeFnInternal.new (Proc.new do
            as_string
        end)),
        "__as_bool" => (NativeFnInternal.new (Proc.new do
            as_bool
        end)),
        "__add" => (NativeFnInternal.new (Proc.new do |other|
            add other[0]
        end)),
        "__sub" => (NativeFnInternal.new (Proc.new do |other|
            sub other[0]
        end)),
        "__mul" => (NativeFnInternal.new (Proc.new do |other|
            mul other[0]
        end)),
        "__div" => (NativeFnInternal.new (Proc.new do |other|
            div other[0]
        end)),
        "__mod" => (NativeFnInternal.new (Proc.new do |other|
            mod other[0]
        end)),
        "__pow" => (NativeFnInternal.new (Proc.new do |other|
            pow other[0]
        end)),
        "__lt" => (NativeFnInternal.new (Proc.new do |other|
            lt other[0]
        end)),
        "__gt" => (NativeFnInternal.new (Proc.new do |other|
            gt other[0]
        end)),
        "__le" => (NativeFnInternal.new (Proc.new do |other|
            le other[0]
        end)),
        "__ge" => (NativeFnInternal.new (Proc.new do |other|
            ge other[0]
        end)),
        "__eq" => (NativeFnInternal.new (lambda do |other|
            Bool.new @internal == other[0].internal
        end)),
        "__neq" => (NativeFnInternal.new (lambda do |other|
            Bool.new @internal != other[0].internal
        end))
    }
end

Instance Method Details

#add(other) ⇒ Object



366
367
368
369
370
371
372
373
374
375
# File 'lib/sdx/vm/datatypes.rb', line 366

def add(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num + on #{other.class}"
        return nil
    end
    Num.new @internal + other.internal
end

#as_boolObject



362
363
364
# File 'lib/sdx/vm/datatypes.rb', line 362

def as_bool
    Bool.new true
end

#as_stringObject



358
359
360
# File 'lib/sdx/vm/datatypes.rb', line 358

def as_string
    Str.new (@internal.to_s "F")
end

#div(other) ⇒ Object



399
400
401
402
403
404
405
406
407
408
# File 'lib/sdx/vm/datatypes.rb', line 399

def div(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num / on #{other.class}"
        return nil
    end
    Num.new @internal / other.internal
end

#ge(other) ⇒ Object



465
466
467
468
469
470
471
472
473
474
# File 'lib/sdx/vm/datatypes.rb', line 465

def ge(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num >= on #{other.class}"
        return nil
    end
    Bool.new @internal >= other.internal
end

#gt(other) ⇒ Object



443
444
445
446
447
448
449
450
451
452
# File 'lib/sdx/vm/datatypes.rb', line 443

def gt(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num > on #{other.class}"
        return nil
    end
    Bool.new @internal > other.internal
end

#le(other) ⇒ Object



454
455
456
457
458
459
460
461
462
463
# File 'lib/sdx/vm/datatypes.rb', line 454

def le(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num <= on #{other.class}"
        return nil
    end
    Bool.new @internal <= other.internal
end

#lt(other) ⇒ Object



432
433
434
435
436
437
438
439
440
441
# File 'lib/sdx/vm/datatypes.rb', line 432

def lt(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num < on #{other.class}"
        return nil
    end
    Bool.new @internal < other.internal
end

#mod(other) ⇒ Object



410
411
412
413
414
415
416
417
418
419
# File 'lib/sdx/vm/datatypes.rb', line 410

def mod(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num % on #{other.class}"
        return nil
    end
    Num.new @internal % other.internal
end

#mul(other) ⇒ Object



388
389
390
391
392
393
394
395
396
397
# File 'lib/sdx/vm/datatypes.rb', line 388

def mul(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num * on #{other.class}"
        return nil
    end
    Num.new @internal * other.internal
end

#pow(other) ⇒ Object



421
422
423
424
425
426
427
428
429
430
# File 'lib/sdx/vm/datatypes.rb', line 421

def pow(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num ^ on #{other.class}"
        return nil
    end
    Num.new @internal ** other.internal
end

#sub(other) ⇒ Object



377
378
379
380
381
382
383
384
385
386
# File 'lib/sdx/vm/datatypes.rb', line 377

def sub(other)
    case other
    when Num
    when Int
    else
        error "Cannot use Num - on #{other.class}"
        return nil
    end
    Num.new @internal - other.internal
end