Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.3KB

  1. : H@ HERE @ ;
  2. : IMMEDIATE
  3. CURRENT @ 1 -
  4. DUP C@ 128 OR SWAP C!
  5. ;
  6. : [ INTERPRET 1 FLAGS ! ; IMMEDIATE
  7. : ] R> DROP ;
  8. : LITS 34 , SCPY ;
  9. : LIT< WORD LITS ; IMMEDIATE
  10. : LITA 36 , , ;
  11. : '
  12. WORD (find) (?br) [ 4 , ] EXIT
  13. LIT< (wnf) (find) DROP EXECUTE
  14. ;
  15. : ['] ' LITA ; IMMEDIATE
  16. : COMPILE ' LITA ['] , , ; IMMEDIATE
  17. : [COMPILE] ' , ; IMMEDIATE
  18. : BEGIN H@ ; IMMEDIATE
  19. : AGAIN COMPILE (br) H@ - , ; IMMEDIATE
  20. : UNTIL COMPILE (?br) H@ - , ; IMMEDIATE
  21. : ( BEGIN LIT< ) WORD SCMP NOT UNTIL ; IMMEDIATE
  22. ( Hello, hello, krkrkrkr... do you hear me?
  23. Ah, voice at last! Some lines above need comments
  24. BTW: Forth lines limited to 64 cols because of default
  25. input buffer size in Collapse OS
  26. "_": words starting with "_" are meant to be "private",
  27. that is, only used by their immediate surrondings.
  28. LITS: 34 == litWord
  29. LITA: 36 == addrWord
  30. COMPILE: Tough one. Get addr of caller word (example above
  31. (br)) and then call LITA on it. )
  32. : +! SWAP OVER @ + SWAP ! ;
  33. : -^ SWAP - ;
  34. : ALLOT HERE +! ;
  35. : IF ( -- a | a: br cell addr )
  36. COMPILE (?br)
  37. H@ ( push a )
  38. 2 ALLOT ( br cell allot )
  39. ; IMMEDIATE
  40. : THEN ( a -- | a: br cell addr )
  41. DUP H@ -^ SWAP ( a-H a )
  42. !
  43. ; IMMEDIATE
  44. : ELSE ( a1 -- a2 | a1: IF cell a2: ELSE cell )
  45. COMPILE (br)
  46. 2 ALLOT
  47. DUP H@ -^ SWAP ( a-H a )
  48. !
  49. H@ 2 - ( push a. -2 for allot offset )
  50. ; IMMEDIATE
  51. : CREATE
  52. (entry) ( empty header with name )
  53. 11 ( 11 == cellWord )
  54. , ( write it )
  55. ;
  56. ( We run this when we're in an entry creation context. Many
  57. things we need to do.
  58. 1. Change the code link to doesWord
  59. 2. Leave 2 bytes for regular cell variable.
  60. 3. Write down RS' RTOS to entry.
  61. 4. exit parent definition
  62. )
  63. : DOES>
  64. ( Overwrite cellWord in CURRENT )
  65. ( 43 == doesWord )
  66. 43 CURRENT @ !
  67. ( When we have a DOES>, we forcefully place HERE to 4
  68. bytes after CURRENT. This allows a DOES word to use ","
  69. and "C," without messing everything up. )
  70. CURRENT @ 4 + HERE !
  71. ( HERE points to where we should write R> )
  72. R> ,
  73. ( We're done. Because we've popped RS, we'll exit parent
  74. definition )
  75. ;
  76. : VARIABLE CREATE 2 ALLOT ;
  77. : CONSTANT CREATE , DOES> @ ;
  78. : / /MOD SWAP DROP ;
  79. : MOD /MOD DROP ;
  80. ( In addition to pushing H@ this compiles 2 >R so that loop
  81. variables are sent to PS at runtime )
  82. : DO
  83. COMPILE SWAP COMPILE >R COMPILE >R
  84. H@
  85. ; IMMEDIATE
  86. ( One could think that we should have a sub word to avoid all
  87. these COMPILE, but we can't because otherwise it messes with
  88. the RS )
  89. : LOOP
  90. COMPILE R> 1 LITN COMPILE + COMPILE DUP COMPILE >R
  91. COMPILE I' COMPILE = COMPILE (?br)
  92. H@ - ,
  93. COMPILE R> COMPILE DROP COMPILE R> COMPILE DROP
  94. ; IMMEDIATE
  95. ( a1 a2 u -- )
  96. : MOVE
  97. ( u ) 0 DO
  98. SWAP DUP I + C@ ( a2 a1 x )
  99. ROT SWAP OVER I + ( a1 a2 x a2 )
  100. C! ( a1 a2 )
  101. LOOP
  102. 2DROP
  103. ;
  104. : DELW
  105. 1 - 0 SWAP C!
  106. ;
  107. : PREV
  108. 3 - DUP @ ( a o )
  109. - ( a-o )
  110. ;
  111. : WHLEN
  112. 1 - C@ ( name len field )
  113. 127 AND ( 0x7f. remove IMMEDIATE flag )
  114. 3 + ( fixed header len )
  115. ;
  116. : FORGET
  117. ' DUP ( w w )
  118. ( HERE must be at the end of prev's word, that is, at the
  119. beginning of w. )
  120. DUP WHLEN - HERE ! ( w )
  121. PREV CURRENT !
  122. ;