forked from organix/pijFORTHos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jonesforth.f
112 lines (111 loc) · 3.4 KB
/
jonesforth.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
\ Annotation has been removed from this file to expedite processing.
\ See the files in the /annexia/ for a full Literate Code tutorial, it's great!
: '\n' 10 ;
: BL 32 ;
: ':' [ CHAR : ] LITERAL ;
: ';' [ CHAR ; ] LITERAL ;
: '(' [ CHAR ( ] LITERAL ;
: ')' [ CHAR ) ] LITERAL ;
: '"' [ CHAR " ] LITERAL ;
: 'A' [ CHAR A ] LITERAL ;
: '0' [ CHAR 0 ] LITERAL ;
: '-' [ CHAR - ] LITERAL ;
: '.' [ CHAR . ] LITERAL ;
: ( IMMEDIATE 1 BEGIN KEY DUP '(' = IF DROP 1+ ELSE ')' = IF 1- THEN THEN DUP 0= UNTIL DROP ;
: SPACES ( n -- ) BEGIN DUP 0> WHILE SPACE 1- REPEAT DROP ;
: WITHIN -ROT OVER <= IF > IF TRUE ELSE FALSE THEN ELSE 2DROP FALSE THEN ;
: ALIGNED ( c-addr -- a-addr ) 3 + 3 INVERT AND ;
: ALIGN HERE @ ALIGNED HERE ! ;
: C, HERE @ C! 1 HERE +! ;
: S" IMMEDIATE ( -- addr len )
STATE @ IF
' LITS , HERE @ 0 ,
BEGIN KEY DUP '"'
<> WHILE C, REPEAT
DROP DUP HERE @ SWAP - 4- SWAP ! ALIGN
ELSE
HERE @
BEGIN KEY DUP '"'
<> WHILE OVER C! 1+ REPEAT
DROP HERE @ - HERE @ SWAP
THEN
;
: ." IMMEDIATE ( -- )
STATE @ IF
[COMPILE] S" ' TELL ,
ELSE
BEGIN KEY DUP '"' = IF DROP EXIT THEN EMIT AGAIN
THEN
;
: DICT WORD FIND ;
: VALUE ( n -- ) WORD CREATE DOCOL , ' LIT , , ' EXIT , ;
: TO IMMEDIATE ( n -- )
DICT >DFA 4+
STATE @ IF ' LIT , , ' ! , ELSE ! THEN
;
: +TO IMMEDIATE
DICT >DFA 4+
STATE @ IF ' LIT , , ' +! , ELSE +! THEN
;
: ID. 4+ COUNT F_LENMASK AND BEGIN DUP 0> WHILE SWAP COUNT EMIT SWAP 1- REPEAT 2DROP ;
: ?HIDDEN 4+ C@ F_HIDDEN AND ;
: ?IMMEDIATE 4+ C@ F_IMMED AND ;
: WORDS LATEST @ BEGIN ?DUP WHILE DUP ?HIDDEN NOT IF DUP ID. SPACE THEN @ REPEAT CR ;
: FORGET DICT DUP @ LATEST ! HERE ! ;
: CFA> LATEST @ BEGIN ?DUP WHILE 2DUP SWAP < IF NIP EXIT THEN @ REPEAT DROP 0 ;
: SEE
DICT HERE @ LATEST @
BEGIN 2 PICK OVER <> WHILE NIP DUP @ REPEAT
DROP SWAP ':' EMIT SPACE DUP ID. SPACE
DUP ?IMMEDIATE IF ." IMMEDIATE " THEN
>DFA BEGIN 2DUP
> WHILE DUP @ CASE
' LIT OF 4 + DUP @ . ENDOF
' LITS OF [ CHAR S ] LITERAL EMIT '"' EMIT SPACE
4 + DUP @ SWAP 4 + SWAP 2DUP TELL '"' EMIT SPACE + ALIGNED 4 -
ENDOF
' 0BRANCH OF ." 0BRANCH ( " 4 + DUP @ . ." ) " ENDOF
' BRANCH OF ." BRANCH ( " 4 + DUP @ . ." ) " ENDOF
' ' OF [ CHAR ' ] LITERAL EMIT SPACE 4 + DUP @ CFA> ID. SPACE ENDOF
' EXIT OF 2DUP 4 + <> IF ." EXIT " THEN ENDOF
DUP CFA> ID. SPACE
ENDCASE 4 + REPEAT
';' EMIT CR 2DROP
;
: :NONAME 0 0 CREATE HERE @ DOCOL , ] ;
: ['] IMMEDIATE ' LIT , ;
: EXCEPTION-MARKER RDROP 0 ;
: CATCH ( xt -- exn? ) DSP@ 4+ >R ' EXCEPTION-MARKER 4+ >R EXECUTE ;
: THROW ( n -- ) ?DUP IF
RSP@ BEGIN DUP R0 4-
< WHILE DUP @ ' EXCEPTION-MARKER 4+
= IF 4+ RSP! DUP DUP DUP R> 4- SWAP OVER ! DSP! EXIT THEN
4+ REPEAT DROP
CASE
0 1- OF ." ABORTED" CR ENDOF
." UNCAUGHT THROW " DUP . CR
ENDCASE QUIT THEN
;
: ABORT ( -- ) 0 1- THROW ;
: PRINT-STACK-TRACE
RSP@ BEGIN DUP R0 4-
< WHILE DUP @ CASE
' EXCEPTION-MARKER 4+ OF ." CATCH ( DSP=" 4+ DUP @ U. ." ) " ENDOF
DUP CFA> ?DUP IF 2DUP ID. [ CHAR + ] LITERAL EMIT SWAP >DFA 4+ - . THEN
ENDCASE 4+ REPEAT DROP CR
;
: BINARY ( -- ) 2 BASE ! ;
: OCTAL ( -- ) 8 BASE ! ;
: 2# BASE @ 2 BASE ! WORD NUMBER DROP SWAP BASE ! ;
: 8# BASE @ 8 BASE ! WORD NUMBER DROP SWAP BASE ! ;
: # ( b -- n ) BASE @ SWAP BASE ! WORD NUMBER DROP SWAP BASE ! ;
: UNUSED ( -- n ) PAD HERE @ - 4/ ;
: WELCOME
S" TEST-MODE" FIND NOT IF
." JONESFORTH VERSION " VERSION . CR
UNUSED . ." CELLS REMAINING" CR
." OK "
THEN
;
WELCOME
HIDE WELCOME