mhx
2024-12-19 15:22:39 UTC
Reply
Permalinkif it is easy to understand what it does, especially the word MAIN3.
Can it be written in a completely standard way?
What I need is a word that is passed parameters that define an index
into an array that is not allotted yet. So neither the index nor the
base address
can be pre-computed. When the word executes (after much more code which
finally sets up the data), it should access the array to fetch a byte at
the indexed position.
The way I've set it up, the user of the program edits a file to allocate
the data and define a function that does the fetching.
If your Forth lacks >S S> S , leave them away or replace with >R R> R@
FORTH> MAIN3
one, two, three = 1 2 3 \ 1 2 3
result = 123
MAIN3 compiled a noname $013454C0 ok
FORTH> $013454C0 idis
$013454C0 : [trashed]
$013454CA movzx rbx, $01345086 byte-offset
$013454D1 push rbx
$013454D2 ;
FORTH> $01345086 c@ . 123 ok
-marcel
---
\ in THIS file
CREATE myinsert
," ] CR DUP . R@ . S . one . two . three . one two * three * DUP TO
result ["
: MAIN0 ( -- )
1 2 3 0 LOCALS| result three two one |
4 >S 5 >R 6
[ myinsert COUNT EVALUATE ] ( compile time, locals accessible )
CR ." myinsert compiled `one two * three * DUP TO result` "
CR ." stack = " . ." \ 6"
CR ." R> = " R> . ." \ 5"
CR ." S> = " S> . ." \ 4"
CR ." one, two, three = " one . two . three . ." \ 1 2 3"
CR ." MAIN0 does `one two * three *` = " 0DEC.R
." , result = " result . ;
\ in file inside1.frt
\ ] CR DUP . R@ . S . one . two . three . one two * three * DUP TO
result [
: MAIN1 ( -- )
1 2 3 0 LOCALS| result three two one |
4 >S 5 >R 6
[ S" inside1.frt" INCLUDED ] ( at compile time, locals accessible )
CR ." inside1.frt compiled `one two * three * DUP TO result` "
CR ." stack = " . ." \ 6"
CR ." R> = " R> . ." \ 5"
CR ." S> = " S> . ." \ 4"
CR ." one, two, three = " one . two . three . ." \ 1 2 3"
CR ." MAIN1 does `one two * three *` = " 0DEC.R
." , result = " result . ;
\ in file inside2.frt
\ CR .( I see ) DUP . .( on the stack, ) S .
\ .( on the S-stack, locals one/two/three = ) OVER . 2 PICK . 3 PICK