aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/common/lib/local.frt
blob: 94cf86d2d9e36ffc34e3698ffda21fd37b54dec4 (plain)
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
\ trivial local

\ there is exactly one local called X
\ it is not initialized upon entry
\ it works like a local should do:
\ get the content by calling X, assign
\ a new value with TO

\ separate local stack

\ max. call depth 10
#10 cells constant l-sizee

\ the local stack pointer and the stack itself
l-size cell+ buffer: lsp

\ initialize l-stackpointer, call it
\ in e.g. turnkey prior to use!
: l-init lsp l-size + lsp ! ;

\ general stack access, unsued
: l@ lsp @ @ ;
: l! lsp @ ! ;
: l-free   1 cells lsp +! ;
: l-alloc -1 cells lsp +! ;
: >l l! l-alloc ;
: l> l-free l@  ;

: local@ negate lsp @ + @ ;
: local! negate lsp @ + ! ;

\ define a local by its offset
\ relative to the local stack pointer
: local ( offset "name" -- )
    (value) ,
    ['] local@ ,
    ['] local! ,
;

\ should be smarter, it should
\ check whether X is used at all
\ and allocate the local stack
\ only if needed.
: : : l-alloc ;
: ; l-free postpone ; ; immediate

\ globally define a label for the first
\ local variable. The X is a global name
\ but has local content. If using more, 
\ add a l-alloc/l-free pair in the : and ;
\ definitions above.

0 local X

\ test cases
\ l-init
\ : test1 to X X . ;
\ 1 test1
\ -> 1
\ : test2 1 test1 to X X . ;
\ 2 test2
\ -> 1 2 
\