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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/sdx/vm/datatypes.rb', line 222

def initialize(val=nil)
    if val != nil
        @internal = val
    end
    @fields = {
        "__as_string" => (NativeFnInternal.new (Proc.new do
            as_string
        end)),
        "__as_code_string" => (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



283
284
285
# File 'lib/sdx/vm/datatypes.rb', line 283

def add(other)
    Num.new @internal + other.internal
end

#as_boolObject



279
280
281
# File 'lib/sdx/vm/datatypes.rb', line 279

def as_bool
    Bool.new true
end

#as_stringObject



275
276
277
# File 'lib/sdx/vm/datatypes.rb', line 275

def as_string
    Str.new @internal.to_s
end

#div(other) ⇒ Object



295
296
297
# File 'lib/sdx/vm/datatypes.rb', line 295

def div(other)
    Num.new @internal / other.internal
end

#ge(other) ⇒ Object



319
320
321
# File 'lib/sdx/vm/datatypes.rb', line 319

def ge(other)
    Bool.new @internal >= other.internal
end

#gt(other) ⇒ Object



311
312
313
# File 'lib/sdx/vm/datatypes.rb', line 311

def gt(other)
    Bool.new @internal > other.internal
end

#le(other) ⇒ Object



315
316
317
# File 'lib/sdx/vm/datatypes.rb', line 315

def le(other)
    Bool.new @internal <= other.internal
end

#lt(other) ⇒ Object



307
308
309
# File 'lib/sdx/vm/datatypes.rb', line 307

def lt(other)
    Bool.new @internal < other.internal
end

#mod(other) ⇒ Object



299
300
301
# File 'lib/sdx/vm/datatypes.rb', line 299

def mod(other)
    Num.new @internal % other.internal
end

#mul(other) ⇒ Object



291
292
293
# File 'lib/sdx/vm/datatypes.rb', line 291

def mul(other)
    Num.new @internal * other.internal
end

#pow(other) ⇒ Object



303
304
305
# File 'lib/sdx/vm/datatypes.rb', line 303

def pow(other)
    Num.new @internal ** other.internal
end

#sub(other) ⇒ Object



287
288
289
# File 'lib/sdx/vm/datatypes.rb', line 287

def sub(other)
    Num.new @internal - other.internal
end