collapseos/forth/core.fs

132 lines
3.5 KiB
Forth
Raw Normal View History

: H@ HERE @ ;
: -^ SWAP - ;
2020-03-27 15:25:20 -04:00
: [ INTERPRET 1 FLAGS ! ; IMMEDIATE
: ] R> DROP ;
: LIT 34 , ;
2020-03-22 11:56:40 -04:00
: LITS LIT SCPY ;
: LIT< WORD LITS ; IMMEDIATE
2020-03-25 20:06:06 -04:00
: _err LIT< word-not-found (print) ABORT ;
: ' WORD (find) NOT (?br) [ 4 , ] _err ;
: ['] ' LITN ; IMMEDIATE
: COMPILE ' LITN ['] , , ; IMMEDIATE
2020-03-21 16:17:51 -04:00
: [COMPILE] ' , ; IMMEDIATE
: BEGIN H@ ; IMMEDIATE
: AGAIN COMPILE (br) H@ - , ; IMMEDIATE
: UNTIL COMPILE (?br) H@ - , ; IMMEDIATE
2020-03-21 16:27:21 -04:00
: ( BEGIN LIT< ) WORD SCMP NOT UNTIL ; IMMEDIATE
( Hello, hello, krkrkrkr... do you hear me?
Ah, voice at last! Some lines above need comments
BTW: Forth lines limited to 64 cols because of default
input buffer size in Collapse OS
2020-03-16 21:31:43 -04:00
2020-03-25 20:06:06 -04:00
"_": words starting with "_" are meant to be "private",
that is, only used by their immediate surrondings.
LIT: 34 == LIT
COMPILE: Tough one. Get addr of caller word (example above
(br)) and then call LITN on it. )
2020-03-18 16:39:22 -04:00
: +! SWAP OVER @ + SWAP ! ;
: ALLOT HERE +! ;
: IF ( -- a | a: br cell addr )
COMPILE (?br)
H@ ( push a )
2 ALLOT ( br cell allot )
; IMMEDIATE
: THEN ( a -- | a: br cell addr )
DUP H@ -^ SWAP ( a-H a )
!
; IMMEDIATE
: ELSE ( a1 -- a2 | a1: IF cell a2: ELSE cell )
COMPILE (br)
2 ALLOT
DUP H@ -^ SWAP ( a-H a )
!
H@ 2 - ( push a. -2 for allot offset )
; IMMEDIATE
2020-03-22 11:49:09 -04:00
: CREATE
2020-03-27 15:25:20 -04:00
(entry) ( empty header with name )
11 ( 11 == cellWord )
2020-03-27 15:25:20 -04:00
, ( write it )
2020-03-22 11:49:09 -04:00
;
2020-03-30 19:01:28 -04:00
( We run this when we're in an entry creation context. Many
things we need to do.
1. Change the code link to doesWord
2. Leave 2 bytes for regular cell variable.
3. Write down RS' RTOS to entry.
4. exit parent definition
)
: DOES>
( Overwrite cellWord in CURRENT )
2020-03-31 21:46:52 -04:00
( 43 == doesWord )
43 CURRENT @ !
2020-03-30 19:01:28 -04:00
( When we have a DOES>, we forcefully place HERE to 4
bytes after CURRENT. This allows a DOES word to use ","
and "C," without messing everything up. )
CURRENT @ 4 + HERE !
( HERE points to where we should write R> )
R> ,
( We're done. Because we've popped RS, we'll exit parent
definition )
;
: VARIABLE CREATE 2 ALLOT ;
2020-03-30 19:01:28 -04:00
: CONSTANT CREATE , DOES> @ ;
2020-03-16 22:36:29 -04:00
: / /MOD SWAP DROP ;
: MOD /MOD DROP ;
( In addition to pushing H@ this compiles 2 >R so that loop
2020-03-21 17:21:01 -04:00
variables are sent to PS at runtime )
: DO
COMPILE SWAP COMPILE >R COMPILE >R
H@
; IMMEDIATE
2020-03-21 17:21:01 -04:00
( One could think that we should have a sub word to avoid all
these COMPILE, but we can't because otherwise it messes with
the RS )
: LOOP
COMPILE R> 1 LITN COMPILE + COMPILE DUP COMPILE >R
COMPILE I' COMPILE = COMPILE (?br)
H@ - ,
COMPILE R> COMPILE DROP COMPILE R> COMPILE DROP
; IMMEDIATE
2020-03-25 08:39:44 -04:00
( WARNING: there are no limit checks. We must be cautious, in
core code, not to create more than SYSV_BUFSIZE/2 sys vars.
Also: SYSV shouldn't be used during runtime: SYSVNXT won't
point at the right place. It should only be used during
stage1 compilation. This is why this word is not documented
in dictionary.txt )
: (sysv)
2020-03-30 14:19:47 -04:00
( Get new sysv addr )
( RAM+46 (2e) == SYSVNXT )
46 RAM+ @
CONSTANT
2020-03-30 14:19:47 -04:00
( increase current sysv counter )
2 46 RAM+ +!
2020-03-25 08:39:44 -04:00
;
2020-03-30 21:48:56 -04:00
( Set up initial SYSVNXT value, which is 2 bytes after its
own address )
46 RAM+ DUP 2 + SWAP !
2020-03-30 21:48:56 -04:00
: ."
LIT
BEGIN
C< DUP ( c c )
( 34 is ASCII for " )
DUP 34 = IF DROP DROP 0 0 THEN
C,
0 = UNTIL
COMPILE (print)
; IMMEDIATE
: ABORT" [COMPILE] ." COMPILE ABORT ; IMMEDIATE