X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=sys.f;h=6e7c8539bcdd67ea720b7faf5c2f15c8781b0880;hb=61b721d365efaaae4d5ebb8d13972faa697be54b;hp=604d87aa75e71f116f1177d048e18c4028a5b4b6;hpb=f05a9a424e2d5cfc15f55cee82f503d7b4c1e5a9;p=rrq%2Fjonasforth.git diff --git a/sys.f b/sys.f index 604d87a..6e7c853 100644 --- a/sys.f +++ b/sys.f @@ -10,10 +10,6 @@ EXIT [ EXIT [ IMMEDIATE -: / /MOD DROP ; -: MOD /MOD SWAP DROP ; -: NEG 0 SWAP - ; - : IF IMMEDIATE ' 0BRANCH , HERE @ @@ -37,48 +33,77 @@ EXIT [ HERE @ ; +: AGAIN IMMEDIATE + ' BRANCH , + HERE @ - , ; + +: ( IMMEDIATE + BEGIN + READ-WORD + 1 = IF + C@ 41 = IF + EXIT + THEN + ELSE + DROP + THEN + AGAIN ; ( Yay! We now have comments! ) + : UNTIL IMMEDIATE ' 0BRANCH , HERE @ - , ; -: FIB - 0 1 - 0 - BEGIN - ROT - DUP ROT + - ROT ROT +( Compile a literal value into the current word. ) +: LIT, IMMEDIATE ( x -- ) + ' LIT , , ; - 1 + - DUP 4 PICK = UNTIL - DROP SWAP DROP SWAP DROP -; +: / /MOD DROP ; +: MOD /MOD SWAP DROP ; +: NEG 0 SWAP - ; : C, HERE @ C! HERE @ 1 + HERE ! ; -: OVER - SWAP DUP ROT ; +: OVER ( a b -- a b a ) SWAP DUP ROT ; -: STORE-STRING +( An alternative comment syntax. Reads until the end of the line. ) +: \ IMMEDIATE + BEGIN + KEY + 10 = UNTIL ; + +\ So far, S" has only worked in immediate mode, which is backwards -- actually, +\ the main use-case of this is as a compile-time word. Let's fix that. +: S" IMMEDIATE + ' LITSTRING , + HERE @ 0 C, \ We will put the length here + 0 + BEGIN + 1 + + KEY DUP C, + 34 = UNTIL + \ Remove final " + HERE @ 1 - HERE ! + 1 - + SWAP C! ; + +( Compile the given string into the current word directly. ) +: STORE-STRING ( str len -- ) BEGIN OVER C@ C, SWAP 1 + SWAP 1 - DUP 0 = UNTIL DROP DROP ; -S" HELLO-ADDR" CREATE -S" Hello!" DUP ROT -STORE-STRING -: HELLO - ' HELLO-ADDR LIT [ , ] TELL NEWLINE ; +: NEWLINE 10 EMIT ; +: SPACE 32 EMIT ; -HELLO +( Read a number from standard input. ) +: READ-NUMBER READ-WORD PARSE-NUMBER ; -S" 10 FIB = " TELL -10 FIB .U -S" (Expected: 59)" TELL NEWLINE +: RESTART S" Ready." TELL NEWLINE ; +RESTART