-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.fth
More file actions
121 lines (89 loc) · 2.51 KB
/
bootstrap.fth
File metadata and controls
121 lines (89 loc) · 2.51 KB
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
113
114
115
116
117
118
119
120
121
// Dictionary Helpers
// ------------------
: is-immediate? cell + c@ 1 & ;
: nt>xt cell + 1 + dup c@ + 1 + aligned ;
: xt>cfa @ ;
: xt>pf cell + ;
: cells cell * ;
: ' parse-name find-nt nt>xt ;
: ['] immediate lit [ ' lit , ] , ' , ;
: postpone immediate ['] lit , ' , ['] , , ;
: here dp @ ;
: allot dp @ + dp ! ;
: abort restart ;
// Conditionals And Looping Constructs
// -----------------------------------
: ref< here ;
: <ref here - , ;
: ref> here 0 , ;
: >ref here over - swap ! ;
: if immediate postpone 0branch ref> ;
: else immediate postpone branch ref> swap >ref ;
: then immediate >ref ;
: begin immediate ref< ;
: again immediate postpone branch <ref ;
: until immediate postpone 0branch <ref ;
: while immediate postpone 0branch ref> ;
: repeat immediate swap postpone branch <ref >ref ;
// Extended Stack Manipulation
// ---------------------------
: 2dup over over ;
: 2drop drop drop ;
: -rot swap >r swap r> ;
: rot >r swap r> swap ;
// Helpers
// -------
: hex 16 base ! ;
: decimal 10 base ! ;
: true 1 ;
: false 0 ;
: = - 0= ;
: / /mod swap drop ;
: min 2dup > if swap then drop ;
// Memory Access Helpers
// ---------------------
: +! swap over @ + swap ! ;
: !+ over ! cell + ;
: @+ dup @ swap cell + swap ;
// Character Memory Access
// -----------------------
: c!+ over c! 1 + ;
: c!- over c! 1 - ;
: c@+ dup c@ swap 1 + swap ;
: cmove begin dup while >r over c@ over c! 1 + swap 1 + swap r> 1 - repeat drop drop drop ;
: creverse
2dup + 1 - swap 2 /
begin dup while
1 - >r
2dup
c@ swap
c@ swap
-rot
c!-
-rot
c!+
swap
r>
repeat
drop drop drop
;
// Complex Word Defining
// ---------------------
: docol, ['] docol xt>cfa , ;
: create header docol, postpone rel ref> postpone exit postpone exit >ref latest @ get-current ! ;
: xt>here xt>pf cell + dup @ + ;
: (does) latest @ nt>xt xt>pf 2 cells + ! ;
: does> immediate postpone rel ref> postpone (does) postpone exit >ref docol, ;
: constant create , does> @ ;
: variable create 0 , ;
: defer create postpone exit does> @ execute ;
: deferaddr xt>here ;
: is ' deferaddr ! ;
: defer! deferaddr ! ;
: defer@ deferaddr @ ;
: marker create get-current @ , here cell + , does> dup @ get-current ! cell + @ dp ! ;
// Wordlist Helpers
// ----------------
: wordlist here 0 , ;
: get-order #order @ context over 1 - cells + swap begin dup while >r dup @ swap cell - r> 1 - repeat 2drop #order @ ;
: set-order dup #order ! context swap begin dup while >r swap over ! cell + r> 1 - repeat 2drop ;