-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex.src
58 lines (46 loc) · 1.75 KB
/
ex.src
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
\ -----------------------------------------------------------------------------------------------
\ This is an example stand-alone forth program.
\ The program starts a word named "main".
\ If "main" is not found, it starts with the last word defined.
\ -----------------------------------------------------------------------------------------------
.VARIABLE. (HERE)
.VARIABLE. (LAST)
.VARIABLE. BASE
.VARIABLE. STATE
.VARIABLE. (MEM_SZ)
.VARIABLE. >IN
.VARIABLE. #NEG
.VARIABLE. #LEN
.VARIABLE. INPUT-FP
.VARIABLE. T1 \ These are free to use, but since any word
.VARIABLE. T2 \ might use one or more of them, there is no
.VARIABLE. T3 \ guarantee that the value won't change across words.
.EndOfVariables. \ marks the end of the variables
: CELL 4 ; .INLINE.
: LAST (LAST) FETCH ; .INLINE.
: HERE (HERE) FETCH ; .INLINE.
: , HERE STORE HERE CELL ADD (HERE) STORE ;
: C, HERE CSTORE HERE INC (HERE) STORE ;
: JMP 8 C, ;
: JMPZ 9 C, ;
: JMPNZ 10 C, ;
: RET 12 C, ;
: ?COMPILING STATE FETCH ; .INLINE.
: IF ?COMPILING .IF JMPZ HERE 0 , .THEN ; .IMMEDIATE.
\ ELSE ?COMPILING .IF JMP HERE SWAP 0 , HERE SWAP STORE .THEN ; .IMMEDIATE.
: THEN ?COMPILING .IF HERE SWAP STORE .THEN ; .IMMEDIATE.
: BEGIN ?COMPILING IF HERE THEN ; .IMMEDIATE.
: AGAIN ?COMPILING IF JMP , THEN ; .IMMEDIATE.
\ : WHILE ?COMPILING IF JMPNZ , THEN ; .IMMEDIATE.
: COUNT DBGDOTS DUP INC SWAP CFETCH ;
: TYPE RET
BEGIN
DUP 0 EQ IF
DROP DROP RET
THEN
SWAP DUP CFETCH EMIT INC
SWAP DEC
AGAIN ;
\ **********************************************************************************
: main S" Hello, world!" COUNT TYPE BYE ;
: catch-error S" ** ERROR occurred, exiting **" CT BYE ;