25.06.2012, 07:06
Here's an interesting little experiment with custom operators allowing you to do this:
Note, though, this only works for assignments. Other things like ++, --, /, * doesn't work. Though they could be added just as easy.
Source:
pawn Код:
new variable = 200;
new Pointer:ptr = PointerToVar(variable);
ptr = 123;
printf("%d", variable);
// Output: 123
Source:
pawn Код:
// Operator called when an untagged variable is assigned to a value tagged with Pointer
stock Pointer:operator=(value) {
static addr;
// Get return address
#emit LOAD.S.alt 4
// Add COD
#emit LCTRL 0
#emit ADD
#emit MOVE.alt
// Subtract DAT
#emit LCTRL 1
#emit SUB.alt
// Read the first opcode's parameter
#emit ADD.C 4
#emit STOR.pri addr
#emit LREF.pri addr
#emit STOR.pri addr
// Is this address relative to the previous frame?
if (addr <= 0) {
// Add the previous frame to make it absolute
#emit LOAD.S.pri 0
#emit LOAD.alt addr
#emit ADD
// Load it and store it
#emit LOAD.I
#emit STOR.pri addr
} else {
// Load it and store it
#emit LOAD.pri addr
#emit LOAD.I
#emit STOR.pri addr
}
// Store the new value there
if (addr) {
#emit LOAD.S.pri value
#emit LOAD.alt addr
#emit STOR.I
}
// Add 8 to the return address (to skip the assignment)
#emit LOAD.S.pri 4
#emit ADD.C 8
#emit STOR.S.pri 4
// Return the same value the pointer was
return Pointer:0;
}
stock Pointer:PointerToVar(...) {
#emit LOAD.S.pri 12
#emit RETN
return Pointer:0;
}