-
Notifications
You must be signed in to change notification settings - Fork 1
/
manipulators.spf
398 lines (373 loc) · 24.4 KB
/
manipulators.spf
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
: SPDROP ( p*n n --) P+A DROP ; \ убрать со стека n байтов
: SPMOVE ( p*n addr n --) $ 4 B=aP D=A D+@P L1: $ -4 Da C=@B @D=C $ 4 Ba $ 4 A-# L1 J0<> 2DROP ; \ скопировать n байтов стека в память
USER-CREATE xtarr 1024 USER-ALLOT \ массив для именованных символом исполняемых токенов с префиксом v
USER-CREATE prarr 1024 USER-ALLOT \ массив для именованных символом параметров с префиксом p
: odin 1 ;
: kilobyte 1024 ;
: megabyte 1048576 ;
\ доп. слова - двойная разрядность
: DOR ( d1 d2 -- d3) D=@P $ 8 Pa $ -4 A|@P @P|D ;
: DXOR ( d1 d2 -- d3) D=@P $ 8 Pa $ -4 A^@P @P^D ;
: DAND ( d1 d2 -- d3) D=@P $ 8 Pa $ -4 A&@P @P&D ;
: DINVERT ( d1 -- d2 ) @P~ A~ ;
: DLSHIFT ( d n -- 'd ) $ 4 D=@P $ 20 C=# C-A D>> C=A @P<< $ 4 @P<< @P|D DROP ;
: DRSHIFT ( d n -- 'd ) D=@P $ 20 C=# C-A D<< C=A @P>> $ 4 @P>> $ 4 @P|D DROP ;
: D0<> ( d -- f ) D0= INVERT ;
\ доп. слова - +- константа в памяти
: 1-! ( addr --) @A-- DROP ;
: !+ ( addr n --) D=@P @D+A 2DROP ;
: -! ( n addr --) D=@P @A-D 2DROP ;
: !- ( addr n --) D=@P @D-A 2DROP ;
\ доп. слова - дин. память
: hAlloc ALLOCATE THROW ;
: hFree FREE THROW ;
\ доп.слова - циклы со счетчиком
M: aDO OVER + SWAP ?DO ; \ макрос ( BOUND ?DO )
\ доп.слова - компиляция
\ лямбда-конструкция
I: LAMBDA{ ( -- res xt ) 0 BRANCH, >MARK HERE ;
I: }LAMBDA ( res xt -- ) RET, >R >RESOLVE1 R> LIT, ;
\ доп. слова - вывод в консоль
\ печать числа в поле из n пробелов, после числа пробелов нет
: .BL ( n -- ) >R 0 <# #S #> R> OVER - 0 MAX DUP IF 0 DO BL EMIT LOOP ELSE DROP THEN TYPE ;
: .HEX HEX U. DECIMAL ;
: BINARY 0x2 BASE ! ;
: .BIN BINARY U. DECIMAL ;
: .BLH HEX .BL DECIMAL ;
: .BLB BINARY .BL DECIMAL ;
: .0H HEX .0 DECIMAL ;
: .0B BINARY .0 DECIMAL ;
\ доп. слова - арифметика
: MOD/ ( n1 n2 -- rez rest ) /MOD SWAP ;
\ доп. слова - ветвления
M: IFNOT 0= IF ;
\ доп. слова - сравнения
: >= 1- > ;
: <= 1+ < ;
: 0>= 1- 0> ;
: 0<= 1+ 0< ;
REQUIRE CHOOSE ~AF\LIB\RANDOM.F
\ доп. слова - работа с курсором в консоли
WINAPI: SetConsoleCursorPosition KERNEL32.DLL
WINAPI: GetConsoleScreenBufferInfo KERNEL32.DLL
: SET-CURSOR ( x y -- ) \ установка курсора в консоли, 0 0 - левый верхний угол
65536 * + H-STDOUT SetConsoleCursorPosition ERR THROW ;
: GET-CURSOR ( -- x y ) \ дать координаты курсора в консоли
22 csbi] csbi H-STDOUT GetConsoleScreenBufferInfo DROP csbi 4 + DUP W@ SWAP 2+ W@ ;
\ доп. слова - работа с битами
: NB? ( n N -- b) A-- C=A\@P 1Ac<< $ 1 bA&b# NIP ; \ дать значение бита N в числе n
: NB1 ( n N -- n~) A-- C=A\@P1 DROP ; \ установить бит N в числе n
: NB0 ( n N -- n~) A-- C=A\@P0 DROP ; \ cброcить бит N в числе n
: NB~ ( n N -- n~) A-- C=A\@P~ DROP ; \ инвертировать бит N в числе n
: NLB ( n -- Nlb) A=L\A A++ ; \ дать номер самого младшего единичного бита
: NHB ( A -- Nhb) A=H\A A++ ; \ дать номер самого старшего единичного бита
: LB0 ( n -- n~) A-- D=L\A C=D\A0 ; \ сбросить самый младший единичный бит
: HB0 ( n -- n~) A-- D=H\A C=D\A0 ; \ сбросить самый старший единичный бит
: NB=? ( n1 n2 N -- f) A-- B^B D=@P $ 4 D^@P D~ C=A\D 1Bc<< A=B NIP NIP ; \ сравнить N-ые биты двух чисел
: QB1 ( n -- sb) C^C L1: $ 0 A=#? L2 J= D=L\A C++ C=D\A0 L1 JMP L2: A=C ; \ число единичных бит
: MLB ( n -- mlb) B^B L2: $ 0 A=#? L1 J0= B++ C=A 1C>> A&C L2 JMP L1: A=B ; \ макс. длина вподряд стоящих единичных бит
: SWBM ( n M -- n~ ) D^D S^S B=L\A C=H\A C=B\@P 1Dc<< C=C\@P 1Sc<< D^S \ обмен 2 битов по маске
L1 J= C=B\@P~ C=C\@P~ L1: DROP ;
\ доп. слова - работа с файлами
\ файл по имени anf unf скопировать в хип ah uh
: FILE->HEAP ( anf unf -- ah uh )
R/O OPEN-FILE THROW >R
R@ FILE-SIZE THROW DROP >R \ двойное в одинарное
R@ ALLOCATE THROW DUP
R> R@ READ-FILE THROW
R> CLOSE-FILE THROW
;
USER-VALUE XSP
: NOTFOUND ( A U -- ) u! a! 0. f! beg! 40 buf] XSP sp! 0. flb! sm! 0. sr! Lb! 0. f0! f9! 0. f/! f|! 0 f\! 0. flen! nz!
u 1 > is flen
a C@ is nz
nz '\' = nz '|' = nz '/' = OR OR IF nz is sr '0' is nz 1 ELSE \ заголовок из одного из разделителей \ | / эквивалентен заголовку 0\ 0| 0/ соответственно
a 1+ C@ is sr 0 THEN is flb
nz '0' = is f0 \ f0 - заголовок 0
nz '0' - 1 10 WITHIN is f9 \ f9 - заголовок от 1 до 9
sr '/' = is f/ \ f/ - разделитель / - стек убрать в новый буфер, 0/ - завести новый буфер
sr '|' = is f| \ f| - разделитель | - стек скопировать в текущий буфер
sr '\' = is f\ \ f\ - разделитель \ - стек убрать в текущий буфер, 0\ - новый буфер не заводить
flen f0 f9 OR AND f/ f| OR f\ OR AND 0=
IF a u NOTFOUND EXIT
ELSE sr
CASE
'/' OF flb 0= IF 40 ALLOCATE THROW is sp sp TO XSP THEN ENDOF
'\' OF sp 0= IF buf is sp THEN ENDOF
'|' OF sp 0= IF buf is sp THEN ENDOF
ENDCASE
f9 IF nz '0' - 4 * is Lb sp LIT, Lb LIT, ` SPMOVE f/ f\ OR IF Lb LIT, ` SPDROP THEN THEN a u + a flb 0= IF 2+ ELSE 1+ THEN
?DO I C@ '[' <> I C@ ']' <> AND f 0= AND
IF I C@
CASE
\ арифметика
'+' OF ` + 0 ENDOF ( n1 n2 -- n1+n2 )
'-' OF ` - 0 ENDOF ( n1 n2 -- n1-n2 )
'*' OF ` * 0 ENDOF ( n1 n2 -- n1*n2 )
'/' OF ` / 0 ENDOF ( n1 n2 -- n1/n2 )
'%' OF ` MOD 0 ENDOF ( n1 n2 -- n )
'n' OF ` NEGATE 0 ENDOF ( n -- -n)
'a' OF ` ABS 0 ENDOF ( n -- |n| )
'i' OF ` 1+ 0 ENDOF ( n -- n+1 )
\ логика и сдвиги
'|' OF ` OR 0 ENDOF ( n1 n2 -- n1|n2 )
'^' OF ` XOR 0 ENDOF ( n1 n2 -- n1^n2 )
'&' OF ` AND 0 ENDOF ( n1 n2 -- n1&n2 )
'~' OF ` INVERT 0 ENDOF ( n -- ~n)
'r' OF ` RSHIFT 0 ENDOF ( n1 n2 -- n )
'l' OF ` LSHIFT 0 ENDOF ( n1 n2 -- n )
\ стек
'd' OF ` DUP 0 ENDOF ( n -- n n )
'x' OF ` DROP 0 ENDOF ( n -- )
'P' OF ` DEPTH 0 ENDOF ( -- n )
\ память
'!' OF ` ! 0 ENDOF
'@' OF ` @ 0 ENDOF
'b' OF ` C@ 0 ENDOF
'w' OF ` C! 0 ENDOF
'V' OF ` MOVE 0 ENDOF
'g' OF ` CELL 0 ENDOF
'F' OF ` FILL 0 ENDOF ( a u n -- )
'q' OF ` COMPARE 0 ENDOF ( a1 u1 a2 u2 -- n )
's' OF ` SEARCH 0 ENDOF ( a1 u1 a2 u2 -- a3 u3 t/f )
'h' OF ` hAlloc 0 ENDOF ( n -- addr )
'f' OF ` hFree 0 ENDOF ( addr -- )
',' OF ` LITERAL 0 ENDOF
\ сравнения
'=' OF ` = 0 ENDOF
'>' OF ` > 0 ENDOF
'<' OF ` < 0 ENDOF
'z' OF ` 0<> 0 ENDOF
'Z' OF ` 0= 0 ENDOF
'M' OF ` MAX 0 ENDOF
'm' OF ` MIN 0 ENDOF
'H' OF ` WITHIN 0 ENDOF
\ ветвления
'?' OF ` IF 0 ENDOF
'e' OF ` ELSE 0 ENDOF
't' OF ` THEN 0 ENDOF
'Y' OF ` TRUE 0 ENDOF
'y' OF ` odin 0 ENDOF
\ циклы со счетчиком
'G' OF ` aDO 0 ENDOF
'D' OF ` ?DO 0 ENDOF
'L' OF ` LOOP 0 ENDOF
'N' OF ` +LOOP 0 ENDOF
'I' OF ` I 0 ENDOF
'J' OF ` J 0 ENDOF
'Q' OF ` LEAVE 0 ENDOF
\ циклы
'B' OF ` BEGIN 0 ENDOF
'O' OF ` AGAIN 0 ENDOF
'U' OF ` UNTIL 0 ENDOF
'W' OF ` WHILE 0 ENDOF
'R' OF ` REPEAT 0 ENDOF
';' OF ` EXIT 0 ENDOF
\ выбор по целому
'C' OF ` CASE 0 ENDOF
'E' OF ` ENDCASE 0 ENDOF
'(' OF ` OF 0 ENDOF
')' OF ` ENDOF 0 ENDOF
\ ввод
'A' OF ` ACCEPT 0 ENDOF ( c-addr +n1 -- +n2 )
'#' OF ` >NUMBER 0 ENDOF ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 )
'k' OF ` KEY 0 ENDOF ( -- char )
'K' OF ` EKEY 0 ENDOF ( -- u )
\ вывод
'.' OF ` . 0 ENDOF
'c' OF ` EMIT 0 ENDOF ( char -- )
'S' OF ` SPACES 0 ENDOF ( n -- )
'T' OF ` TYPE 0 ENDOF ( a u -- )
'\' OF ` CR 0 ENDOF
\ поиск в словаре
'$' OF ` SFIND 0 ENDOF ( addr u -- addr u 0 | xt 1 | xt -1 )
\ исполнение
'{' OF ` LAMBDA{ 0 ENDOF
'}' OF ` }LAMBDA 0 ENDOF
'X' OF ` EXECUTE 0 ENDOF
\ обработка исключений
'0' OF ` FALSE 0 ENDOF
\ инкремент содержимого памяти по адресу
'j' OF ` 1+! 0 ENDOF ( addr -- )
1 SWAP ENDCASE
IF I C@ \ DUP DUP DUP DUP DUP DUP DUP '`' <> SWAP ''' <> AND SWAP '"' <> AND SWAP ':' <> AND SWAP '_' <> AND SWAP 'p' <> AND SWAP 'v' <> AND
is sm sm '`' <> sm ''' <> AND sm '"' <> AND sm ':' <> AND sm '_' <> AND sm 'p' <> AND sm 'v' <> AND
IF sm '0' - is sm sm . sm 0 10 WITHIN
IF sm 0= ." zifra"
IF sm LIT, ." null"
ELSE sm LIT, ` 1- ` CELLS sp LIT, ` + ` @ ." cell" THEN
THEN \ параметры на стеке
ELSE DROP I C@ '`' = I C@ 'v' = OR I C@ 'p' = OR
IF I C@ 'v' = I C@ 'p' = OR
IF I 1+ C@ LIT, ` CELLS I C@ 'v' = IF xtarr ELSE prarr THEN LIT, ` + ` @ I C@ 'v' = IF ` EXECUTE THEN
ELSE
I 1+ C@ '0' - DUP 1 10 WITHIN
IF LIT,
ELSE '0' +
CASE
\ односимвольные операторы c префиксом ` - двойная разрядность
\ арифметика
'+' OF ` D+ ENDOF
'-' OF ` D- ENDOF
'n' OF ` DNEGATE ENDOF
'a' OF ` DABS ENDOF
\ логика и сдвиги
'|' OF ` DOR ENDOF
'^' OF ` DXOR ENDOF
'&' OF ` DAND ENDOF
'~' OF ` DINVERT ENDOF
'l' OF ` DLSHIFT ENDOF
'r' OF ` DRSHIFT ENDOF
\ сравнения
'>' OF ` D> ENDOF
'<' OF ` D< ENDOF
'=' OF ` D= ENDOF
'Z' OF ` D0= ENDOF
'z' OF ` D0<> ENDOF
\ стек
'd' OF ` 2DUP ENDOF
'x' OF ` 2DROP ENDOF
\ память
'b' OF ` W@ ENDOF
'w' OF ` W! ENDOF
'@' OF ` 2@ ENDOF
'!' OF ` 2! ENDOF
\ вывод
'.' OF ` D. ENDOF
\ односимвольные операторы c префиксом ` - с действием противоположным действию аналогичных односимвольных без префикса
\ декремент на стеке и в памяти
'i' OF ` 1- ENDOF \ декремент на стеке
'j' OF ` 1-! ENDOF \ декремент в памяти
\ ветвления
'?' OF ` IFNOT ENDOF
ENDCASE
THEN
THEN
ELSE I C@ ''' = I C@ ':' = I C@ '_' = OR OR
IF
I 1+ C@ '0' - DUP 1 10 WITHIN
IF LIT, ` 1- ` CELLS sp LIT, ` + I C@ ':' = I C@ '_' = OR IF ` ! THEN \ присвоение, например, `2:3 аналогично `2'3! \ или 5'6! аналогично 5:6
ELSE
I C@ ':' = I C@ '_' = OR
IF '0' + LIT, ` CELLS I C@ '_' = IF xtarr ELSE prarr THEN LIT, ` + ` !
ELSE DROP
I 1+ W@
CASE \ двухсимвольные операторы (с префиксом ')
\ арифметика
'/%' OF ` /MOD ENDOF
'%/' OF ` MOD/ ENDOF
\ сравнения
'>=' OF ` >= ENDOF
'<=' OF ` <= ENDOF
'<>' OF ` <> ENDOF
'0>' OF ` 0> ENDOF
'0<' OF ` 0< ENDOF
\ память
'+!' OF ` +! ENDOF ( n addr -- )
'-!' OF ` -! ENDOF ( n addr -- )
'!+' OF ` !+ ENDOF ( addr n -- )
'!-' OF ` !- ENDOF ( addr n -- )
\ память в хипе
'ha' OF ` ALLOCATE ENDOF
'hf' OF ` FREE ENDOF
'hr' OF ` RESIZE ENDOF
'ht' OF ` THROW ENDOF
\ преобразование разрядности
'DS' OF ` D>S ENDOF
'SD' OF ` S>D ENDOF
\ константы
'KB' OF ` kilobyte ENDOF
'MB' OF ` megabyte ENDOF
\ курсор в консоли
'cs' OF ` SET-CURSOR ENDOF ( x y -- )
'c?' OF ` GET-CURSOR ENDOF ( -- x y )
\ клавиатура
'k?' OF ` KEY? ENDOF ( -- flag )
'K?' OF ` EKEY? ENDOF ( -- flag )
'Kc' OF ` EKEY>CHAR ENDOF ( u -- u false | char true )
'Ks' OF ` EKEY>SCAN ENDOF ( u -- scan flag )
\ вывод чисел
'.0' OF ` .0 ENDOF ( n -- )
'..' OF ` .BL ENDOF ( n -- )
'.h' OF ` .HEX ENDOF ( n -- )
'.b' OF ` .BIN ENDOF ( n -- )
\ входной поток
'ev' OF ` EVALUATE ENDOF ( a u -- )
\ файлы
'f;' OF ` CLOSE-FILE ENDOF ( fileid -- ior )
'f:' OF ` CREATE-FILE ENDOF ( c-addr u fam -- fileid ior )
'F:' OF ` CREATE-FILE-SHARED ENDOF ( c-addr u fam -- fileid ior )
'Fo' OF ` OPEN-FILE-SHARED ENDOF ( c-addr u fam -- fileid ior )
'fx' OF ` DELETE-FILE ENDOF ( c-addr u -- ior )
'fp' OF ` FILE-POSITION ENDOF ( fileid -- ud ior )
'fs' OF ` FILE-SIZE ENDOF ( fileid -- ud ior )
'fo' OF ` OPEN-FILE ENDOF ( c-addr u fam -- fileid ior )
'fr' OF ` READ-FILE ENDOF ( c-addr u1 fileid -- u2 ior )
'fP' OF ` REPOSITION-FILE ENDOF ( ud fileid -- ior )
'lr' OF ` READ-LINE ENDOF ( c-addr u1 fileid -- u2 flag ior )
'fw' OF ` WRITE-FILE ENDOF ( c-addr u fileid -- ior )
'fS' OF ` RESIZE-FILE ENDOF ( ud fileid -- ior )
'lw' OF ` WRITE-LINE ENDOF ( c-addr u fileid -- ior )
'ff' OF ` FLUSH-FILE ENDOF ( fileid -- ior )
'fe' OF ` FILE-EXIST ENDOF ( addr u -- f )
'fE' OF ` FILE-EXISTS ENDOF ( addr u -- f )
'fh' OF ` FILE->HEAP ENDOF ( c-addr u -- c-addr1 u1 )
\ многозадачность
'ts' OF ` START ENDOF
't|' OF ` SUSPEND ENDOF
'tr' OF ` RESUME ENDOF
't;' OF ` STOP ENDOF
'tp' OF ` PAUSE ENDOF
'tx' OF ` TERMINATE ENDOF
'ti' OF ` THREAD-ID ENDOF
\ работа с битами
'b?' OF ` NB? ENDOF
'bs' OF ` NB1 ENDOF
'bc' OF ` NB0 ENDOF
'b~' OF ` NB~ ENDOF
'bl' OF ` NLB ENDOF
'bh' OF ` NHB ENDOF
'qb' OF ` QB1 ENDOF
ENDCASE 1 R@ + RP@ !
THEN
THEN
ELSE I 1+ @ 0xFFFFFF AND
\ трехсимвольные операторы (с префиксом ")
CASE
\ сравнения
'0>=' OF ` 0>= ENDOF
'0<=' OF ` 0<= ENDOF
\ работа с битами
'b=?' OF ` NB=? ENDOF
'blc' OF ` LB0 ENDOF
'bhc' OF ` HB0 ENDOF
'bsm' OF ` SWBM ENDOF
'bMl' OF ` MLB ENDOF
\ основание системы счисления
'hex' OF ` HEX ENDOF
'dec' OF ` DECIMAL ENDOF
'bin' OF ` BINARY ENDOF
\ вывод чисел
'.0b' OF ` .0B ENDOF ( n -- )
'..b' OF ` .BLB ENDOF ( n -- )
'.0h' OF ` .0H ENDOF ( n -- )
'..h' OF ` .BLH ENDOF ( n -- )
\ случайный выбор числа из диапазона от 0 до n-1
'cho' OF ` CHOOSE ENDOF ( n -- 0|1|2|..|n-1 )
'rnd' OF ` RANDOMIZE ENDOF
ENDCASE 2 R@ + RP@ !
THEN
THEN 1 R@ + RP@ !
THEN
THEN
ELSE
I C@ CASE
'[' OF I 1+ is beg 1 is f ENDOF \ числа, слова и лок. слова
']' OF 0 is f beg I beg - SFIND DUP 0= ( a u 0 -1 | xt 1 0 | xt -1 0 )
IF DROP 2DUP NUMBER? 0= IF 2DROP lvoc 2SWAP lsearch DROP + 1+ @ COMPILE, ELSE DUP 0= IF DROP LIT, ELSE DLIT, THEN 2DROP THEN
ELSE 1 = IF EXECUTE ELSE COMPILE, THEN
THEN ENDOF
ENDCASE
THEN
LOOP
THEN ;