Class: Str

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) ⇒ Str

Returns a new instance of Str.



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 249

def initialize(val=nil)
    if val != nil
        @internal = val
    end
    @fields = {
        "__as_str" => (NativeFnInternal.new (Proc.new do 
            as_string
        end)),
        "__as_code_str" => (NativeFnInternal.new (Proc.new do
            as_code_string
        end)),
        "__add" => (NativeFnInternal.new (Proc.new do |other|
            add other[0]
        end)),
        "__mul" => (NativeFnInternal.new (Proc.new do |other|
            mul 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
286
287
288
289
290
291
# File 'lib/sdx/vm/datatypes.rb', line 283

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

#as_code_stringObject



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

def as_code_string
    (Str.new @internal.dump)
end

#as_stringObject



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

def as_string
    (Str.new @internal)
end

#mul(other) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/sdx/vm/datatypes.rb', line 293

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