From 67d25d837ac55f28a366c0a3b262e439a6e75fc3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 19 Aug 2017 12:15:28 +0200 Subject: Add AmForth --- amforth-6.5/common/lib/local.frt | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 amforth-6.5/common/lib/local.frt (limited to 'amforth-6.5/common/lib/local.frt') 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 -- cgit v1.2.3