Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created February 25, 2015 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Chouser/20f114d9c53a647f2c12 to your computer and use it in GitHub Desktop.
Save Chouser/20f114d9c53a647f2c12 to your computer and use it in GitHub Desktop.
Self-modifying forth
: 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