SamCoVT / TaliForth2

A Subroutine Threaded Code (STC) ANSI-like Forth for the 65c02
Other
29 stars 5 forks source link

Show return stack trace on underflow #36

Closed patricksurry closed 6 months ago

patricksurry commented 6 months ago

Resolves #34

Dumps the return stack prior to an underflow message, which changes to 'Data stack underflow' instead of just 'Stack underflow'. All tests still pass.

Also corrects a couple of minor typos I noticed and adds a python environment.yml suitable for conda or micromamba to create an env for running tests.

...
Tali Forth 2 comes with absolutely NO WARRANTY
Type 'bye' to exit
drop Return stack: 30 8D 42 8F 11 D8 B0 80
Data stack underflow
patricksurry commented 6 months ago

i haven't been using wordlists, so this simple helper seems to work for decoding the return stack. As Sam notes the return stack might also contain user data for loop control and explicit r> etc. But usually the first pair of bytes on the return stack is the return address for an underflow check jsr in a native word, so writing above example as a low-endian address we do $8d30 trace>name.

This doesn't work for addresses in tali internals (tho symbol table is your friend there). Similarly if you have inlined native words you'll find the containing word rather than the actual native word triggering the error but that is often enough. The disasm and dump words are also useful to see what's happening and locate a dictionary header.

: trace>name ( addr -- nt )
    \ find largest nt prior to addr
    >r 0 latestnt begin
        ?dup while
        2dup u< over r@ u< and if
            nip dup
        then
        2 + @
    repeat
    r> drop
    dup if
        dup u. dup wordsize u. dup name>string type space cr
    then
;
patricksurry commented 6 months ago

lmk if this looks better