Created
February 25, 2015 06:27
-
-
Save Chouser/20f114d9c53a647f2c12 to your computer and use it in GitHub Desktop.
Self-modifying forth
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
: site-counter ( literal-int -- int ) | |
here cell - { addr } \ store in 'addr' the address of the literal int preceding this in the user's code | |
postpone dup \ compile code to ...dup the int at the top of the data stack | |
postpone 1+ \ ...increment it | |
addr postpone literal \ ...push the above 'addr' onto the data stack | |
postpone ! \ ...copy the incremented value INTO THE USERS CODE | |
; immediate \ do all this at compile time | |
\ Now for a "normal" word defintion | |
: bar ( -- ) | |
0 site-counter \ push zero onto the data stack and attach a counter | |
100 site-counter \ push 100 onto the data stack and attach a different counter to it | |
. . \ print the two values on the stack | |
; | |
bar | |
\=> 100 0 ok | |
bar | |
\=> 101 1 ok | |
bar | |
\=> 102 2 ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment