Divided up library code.
[forth.jl.git] / src / lib_6_variables.4th
1 \ Constants and Variables
2
3 : CONSTANT
4         CREATE ,
5 DOES>   @
6 ;
7
8 : ALLOT         ( n -- )
9         H +!         ( adds n to H, after this the old value of H is still on the stack )
10 ;
11
12 : VARIABLE
13         CREATE
14         1 CELLS ALLOT   ( allocate 1 cell of memory, push the pointer to this memory )
15 ;
16
17 : VALUE         ( n -- )
18         CREATE ,
19 DOES>   @
20 ;
21
22 : TO IMMEDIATE  ( n -- )
23         BL WORD         ( get the name of the value )
24         FIND DROP       ( look it up in the dictionary )
25         >BODY           ( get a pointer to the first data field (the 'LIT') )
26         STATE @ IF      ( compiling? )
27                 ['] LIT ,         ( compile LIT )
28                 ,               ( compile the address of the value )
29                 ['] ! ,           ( compile ! )
30         ELSE            ( immediate mode )
31                 !               ( update it straightaway )
32         THEN
33 ;
34
35 ( x +TO VAL adds x to VAL )
36 : +TO IMMEDIATE
37         BL WORD         ( get the name of the value )
38         FIND DROP       ( look it up in the dictionary )
39         >BODY           ( get a pointer to the first data field (the 'LIT') )
40         STATE @ IF      ( compiling? )
41                 ['] LIT ,         ( compile LIT )
42                 ,               ( compile the address of the value )
43                 ['] +! ,          ( compile +! )
44         ELSE            ( immediate mode )
45                 +!              ( update it straightaway )
46         THEN
47 ;
48
49 ( Fill u ints, starting at a, with the value b )
50 : FILL          ( a u b -- )
51         -ROT OVER + SWAP ?DO
52                 DUP I !
53         LOOP
54         DROP
55 ;
56
57 : ERASE         ( a u -- )
58         0 FILL
59 ;