Code: Select all
' Dimension an array to hold all objects and their keys and values.
DIM _OBJECTS(100, 100)
' Metamethod to add a new record to database.
' RETURNS the new index
DEF __newindex
IF ._OBJECTS(0, 0) < 100 THEN
.OBJECTS(0, 0) += 1 : RETURN .OBJECTS(0, 0)
END IF
END DEF
' Metamethod to find the index of a key on object (where it's stored).
' RETURNS The index of the key or -1 if not found.
DEF __keyindex(index, key)
t = (STR SPLIT __table(index), ",") : i = -1
FOR k = 0 TO ((SIZE OF t) - 1)
IF (STR LEFT t(k), 1) = " " THEN t(k) = STR RIGHT t(k), (STR LEN t(k)) - 1
r = (STR RPL t(k), " = ", "=") : d = (STR SPLIT r, "=")
IF d(0) = key THEN
i = (k + 1) : BREAK
END IF
NEXT
RETURN i
END DEF
' Metamethod to assign starting values to keys.
DEF __init(index)
v = STR SPLIT __table(index), ","
FOR i = 0 TO ((SIZE OF v) - 1)
IF (STR LEFT v(i), 1) = " " THEN v(i) = STR RIGHT v(i), (STR LEN v(i)) - 1
r = (STR RPL v(i), " = ", "=") : d = (STR SPLIT r, "=") : g = 0
IF (STR LEFT d(1), 1) = "'" AND (STR RIGHT d(1), 1) = "'" THEN
g = STR MID d(1), 2, (STR LEN d(1)) - 2
ELSE
g = STR NUM d(1)
END IF
rawset(index, d(0), g)
NEXT
END DEF
' Metamethod to rawget an objects table
DEF __table(index) = ._OBJECTS(index, 0)
' Method to create a new object.
' table table of keys assigned to the object, it can be the index of
' another object to use it's table instead.
' RETURNS the new object's index
DEF $(table)
index = __newindex
IF ._OBJECTS(index 0) = 0 THEN
IF (TYPE OF table) = 2 THEN ._OBJECTS(index 0) = table ELSE ._OBJECTS(index 0) = __table(table)
__init(index) : RETURN index
END IF
END DEF
' Method to assign values to an object's key.
' index position of the object in the database
' key key to assign
' value value to assign to key
DEF rawset(index, key, value)
i = __keyindex(index, key)
IF i > -1 THEN ._OBJECTS(index, i) = value
END DEF
' Method to rawget the value of a key in an object.
' index position of the object in the database
' table table to rawget key index
' key key to assign
' RETURNS If the key exists returns its value, else returns 0
DEF rawget(index, key)
i = __keyindex(index, key)
IF i = -1 THEN RETURN 0 ELSE RETURN ._OBJECTS(index, i)
END DEF
object1 = $("var1 = 'love', var2 = 'foo', var3 = 10")
object2 = $(object1)
rawset(object1, "var1", "ken")
PRINT rawget(object2, "var1")
PRINT rawget(object1, "var1")