aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/common/lib/local.frt
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-19 12:15:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-19 12:15:28 +0200
commit67d25d837ac55f28a366c0a3b262e439a6e75fc3 (patch)
treedf7715c7724c5935ab87c807f3b8b4ef529315e3 /amforth-6.5/common/lib/local.frt
parente0d6784e89dba33226c0edb815bb974486fa7c48 (diff)
Add AmForth
Diffstat (limited to 'amforth-6.5/common/lib/local.frt')
-rw-r--r--amforth-6.5/common/lib/local.frt63
1 files changed, 63 insertions, 0 deletions
diff --git a/amforth-6.5/common/lib/local.frt b/amforth-6.5/common/lib/local.frt
new file mode 100644
index 0000000..94cf86d
--- /dev/null
+++ b/amforth-6.5/common/lib/local.frt
@@ -0,0 +1,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
+\ \ No newline at end of file