Created
January 19, 2018 06:12
-
-
Save erkyrath/24f0fabf0d65c70f6efca8fbc383ffd1 to your computer and use it in GitHub Desktop.
Example of Inform protected memory array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Test Case" by Andrew Plotkin. | |
The Kitchen is a room. "The Kitchen is a room." | |
The Bathroom is a room. | |
The Bathroom is south of the Kitchen. | |
The rock is a thing. The player carries the rock. | |
The stone is in the Kitchen. | |
Instead of waiting: | |
say "... "; | |
repeat with N running from 0 to 15: | |
let V be protected array entry N; | |
say "[N]=[V]; "; | |
say "."; | |
Instead of touching the rock: | |
set protected array entry 6 to 99; | |
let V be protected array entry 6; | |
say "Set 6 to [V]."; | |
Instead of touching the stone: | |
set protected array entry 12 to 123; | |
let V be protected array entry 12; | |
say "Set 12 to [V]."; | |
Instead of jumping: | |
repeat with N running from 0 to 15: | |
set protected array entry N to 1; | |
say "Set all to 1."; | |
[The code above demonstrates the protected array feature. The code below implements it.] | |
Include (- | |
Constant PROTECT_ARRAY_SIZE 16; | |
Array protect_array --> PROTECT_ARRAY_SIZE; | |
-) after "Global Variables" in "Output.i6t"; | |
Rule for starting the virtual machine (this is the protect memory array rule): | |
define protected array. | |
[You can set or examine any array entry from 0 to 15. If you want more entries, increase PROTECT_ARRAY_SIZE.] | |
To define protected array: (- SetUpProtect(); -). | |
To decide what number is protected array entry (N - number): (- GetProtectEntry(({N})) -); | |
To set protected array entry (N - number) to (V - number): (- SetProtectEntry(({N}), ({V})); -); | |
Include (- | |
[ SetUpProtect len; | |
len = PROTECT_ARRAY_SIZE * WORDSIZE; | |
@protect protect_array len; | |
]; | |
[ GetProtectEntry ix; | |
if (ix < 0 || ix >= PROTECT_ARRAY_SIZE) | |
print_ret "GetProtectEntry error: entry ", ix, " is out of bounds."; | |
return protect_array-->ix; | |
]; | |
[ SetProtectEntry ix val; | |
if (ix < 0 || ix >= PROTECT_ARRAY_SIZE) | |
print_ret "SetProtectEntry error: entry ", ix, " is out of bounds."; | |
protect_array-->ix = val; | |
]; | |
-); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment