-
Notifications
You must be signed in to change notification settings - Fork 45
/
stdlib.frt
302 lines (238 loc) · 5.26 KB
/
stdlib.frt
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
: IMMEDIATE last_word @ cfa 1 - dup c@ 1 or swap c! ;
: rot >r swap r> swap ;
: -rot swap >r swap r> ;
: over >r dup r> swap ;
: 2dup over over ;
: <> = not ;
: <= 2dup < -rot = lor ;
: > <= not ;
: >= < not ;
: cell% 8 ;
: cells cell% * ;
: KB 1024 * ;
: MB KB KB ;
: allot dp @ swap over + dp ! ;
: begin here ; IMMEDIATE
: again ' branch , , ; IMMEDIATE
: if ' 0branch , here 0 , ; IMMEDIATE
: else ' branch , here 0 , swap here swap ! ; IMMEDIATE
: then here swap ! ; IMMEDIATE
: endif ' then execute ; IMMEDIATE
: repeat here ; IMMEDIATE
: until ' 0branch , , ; IMMEDIATE
: for
' swap ,
' >r ,
' >r ,
here ' r> ,
' r> ,
' 2dup ,
' >r ,
' >r ,
' < ,
' 0branch ,
here 0 ,
swap ; IMMEDIATE
: endfor
' r> ,
' lit , 1 ,
' + ,
' >r ,
' branch ,
, here swap !
' r> ,
' drop ,
' r> ,
' drop ,
; IMMEDIATE
: do ' swap , ' >r , ' >r , here ; IMMEDIATE
: loop
' r> ,
' lit , 1 ,
' + ,
' dup ,
' r@ ,
' < ,
' not ,
' swap ,
' >r ,
' 0branch , ,
' r> ,
' drop ,
' r> ,
' drop ,
; IMMEDIATE
: sys-read-no 0 ;
: sys-write-no 1 ;
: sys-read >r >r >r sys-read-no r> r> r> 0 0 0 syscall drop ;
: sys-write >r >r >r sys-write-no r> r> r> 0 0 0 syscall drop ;
: readc@ in_fd @ swap 1 sys-read ;
: readc inbuf readc@ drop inbuf c@ ;
: ( repeat readc 41 - not until ; IMMEDIATE
( Now we can define comments :)
: 2drop drop drop ;
: 2over >r >r dup r> swap r> swap ;
: case 0 ; IMMEDIATE
: of ' over , ' = , ' if execute ' drop , ; IMMEDIATE
: endof ' else execute ; IMMEDIATE
: endcase ' drop , dup if repeat ' then execute dup not until drop then ; IMMEDIATE
( num from to -- 1/0)
: in-range rot swap over >= -rot <= land ;
( 1 if we are compiling )
: compiling state @ ;
: compnumber compiling if ' lit , , then ;
( -- input character's code )
: .' readc compnumber ; IMMEDIATE
: readce readc dup .' \ = if
readc dup .' n = if
drop drop 10
else
drop drop 0
then
then
;
: cr 10 emit ;
: QUOTE 34 ;
: _"
compiling if
' branch , here 0 , here
repeat
readc dup 34 =
if
drop
0 c, ( null terminator )
( label_to_link string_start )
swap
( string_start label_to_link )
here swap !
( string_start )
' lit , , 1
else c, 0
then
until
else
repeat
readce dup 34 = if drop 1 else emit 0 then
until
then ; IMMEDIATE
: " compiling if
' branch , here 0 , here
repeat
readce dup 34 =
if
drop
0 c, ( null terminator )
( label_to_link string_start )
swap
( string_start label_to_link )
here swap !
( string_start )
' lit , , 1
else c, 0
then
until
else
repeat
readce dup 34 = if drop 1 else emit 0 then
until
then ; IMMEDIATE
: g"
dp @
repeat
readce dup 34 = if
drop 0 1 allot c! 1
else 1 allot c! 0 then
until
compiling if
' lit , ,
then ; IMMEDIATE
: ." ' " execute compiling if ' prints , then ; IMMEDIATE
: read-digit readc dup .' 0 .' 9 in-range if .' 0 - else drop -1 then ;
: read-hex-digit
readc dup .' 0 .' 9 in-range if
.' 0 -
else dup .' a .' f in-range if
.' a - 10 +
else dup .' A .' F in-range if
.' A - 10 +
else
drop -1 then
then
then ;
: read-oct-digit
readc dup .' 0 .' 7 in-range if
.' 0 -
else
drop -1
then ;
: 08x 0
repeat
read-oct-digit dup -1 = if
else
swap 8 * swap +
0
then
until
compnumber
; IMMEDIATE
( adds hexadecimal literals )
: 0x 0
repeat
read-hex-digit dup -1 = if
else
swap 16 * swap +
0
then
until
compnumber
; IMMEDIATE
( File I/O )
: O_APPEND 0x 400 ;
: O_CREAT 0x 40 ;
: O_TRUNC 0x 200 ;
: O_RDWR 0x 2 ;
: O_WRONLY 0x 1 ;
: O_RDONLY 0x 0 ;
: sys-open-no 2 ;
: sys-open >r >r >r sys-open-no r> r> r> 0 0 0 syscall drop ;
: sys-close-no 3 ;
: sys-close >r sys-close-no r> 0 0 0 0 0 syscall drop ;
: file-create O_RDWR O_CREAT O_TRUNC or or 08x 700 sys-open ;
: file-open-append O_APPEND O_RDWR O_CREAT or or 08x 700 sys-open ;
: file-open-read O_RDONLY 08x 700 sys-open ;
: file-close sys-close drop ;
( fd string - )
: file-print count sys-write ;
: include
inbuf word drop
inbuf file-open-append >r
( place descriptor on top of data stack and interpret it )
r@ interpret-fd
r@ file-close
r> drop ;
: global inbuf word drop 0 inbuf create ' docol @ , ' lit , cell% allot , ' exit , ;
: constant inbuf word drop 0 inbuf create ' docol @ , ' lit , , ' exit , ;
( structures )
: struct 0 ;
: field over inbuf word drop 0 inbuf create ' docol @ , ' lit , , ' + , ' exit , + ;
: end-struct constant ;
include diagnostics.frt
include doc-engine.frt
include documentation.frt
16 MB ( heap size )
include heap.frt
drop
include string.frt
include hash.frt
: enum 0 repeat
inbuf word drop dup
0 inbuf create ' docol @ , ' lit , , ' exit ,
1 +
" end"
inbuf string-eq until drop ;
include recursion.frt
include runtime-meta.frt
include managed-string.frt
." Forthress -- a tiny Forth from scratch > (c) Igor Zhirkov 2017-2018 " cr
include fib.frt
include native.frt