Guest User

Untitled

a guest
Apr 27th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. First, the header describing the library.
  2.  
  3. ::
  4.  
  5. ( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
  6. ( do ... until )
  7. ( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
  8. ( Value n is taken from the stack and stored to a virtual )
  9. ( variable. When this number is equal to the TOS at the time )
  10. ( until is executed, the loop terminates. )
  11. ( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
  12.  
  13. Start a private namespace.
  14.  
  15. ::
  16.  
  17. {{
  18.  
  19. Two definitions are defined. First, **skipped-nop**:
  20.  
  21. ::
  22.  
  23. : skipped-nop ( - ) 8 , here 2 + , 0 , ;
  24.  
  25. Taking this piece by piece:
  26.  
  27. ::
  28.  
  29. 8 , ::: JUMP opcode [ngaro]
  30. here 2 + , ::: Target for jump. Skip over the next cell.
  31. 0 , ::: NOP opcode
  32.  
  33. Next is **virtual-var**
  34.  
  35. ::
  36.  
  37. : virtual-var ( R: -a C: -aa )
  38. here dup 1-
  39. dup literal, ;
  40.  
  41. Again, breaking it down:
  42.  
  43. ::
  44.  
  45. here dup 1- ::: Get address of the previous cell
  46. dup literal, ::: Compile it as a literal, and leave a
  47. ::: copy on the stack for later use
  48.  
  49. The rest of the words will be visible in the dictionary:
  50.  
  51. ::
  52.  
  53. ---reveal---
  54.  
  55. Now on to the main words. First up **do**:
  56.  
  57. ::
  58.  
  59. : do ( R: nn-n? C: -aa )
  60. ` 2dup ` >if
  61. skipped-nop
  62. virtual-var ` ! ; compile-only
  63.  
  64. This is a compiler macro (**compile-only**), and will lay down some code in any definition using it.
  65.  
  66.  
  67. ::
  68.  
  69. ` 2dup ` >if ::: Compile a call to 2dup and execute >if when the macro is run
  70. skipped-nop ::: Call skipped-nop to lay down a jump and virtual variable
  71. virtual-var ` ! ::: Get a reference to the virtual variable and store TOS to it
  72.  
  73. The word **until** is also a compiler macro:
  74.  
  75. ::
  76.  
  77. : until ( R: ?n- C: aa- )
  78. ` dup literal, ` @
  79. ` > ` if swap 8 , 3 + ,
  80. ` then ` drop
  81. ` else ` 2drop ` then ; compile-only
  82.  
  83. Breaking **until** down:
  84.  
  85. ::
  86.  
  87. ` dup :::
  88. literal, ` @ :::
  89. ` > ` if :::
  90. swap 8 , 3 + , :::
  91. ` then ` drop :::
  92. ` else ` 2drop :::
  93. ` then :::
  94.  
  95. Finally, close out the private namespace, leaving only the public words visible:
  96.  
  97. ::
  98.  
  99. }}
Add Comment
Please, Sign In to add comment