chochain / eforth

eForth in C/C++ - multi-platform (Linux,Windows,ESP32,WASM)
10 stars 1 forks source link

Bug in 'alloc' ? #1

Closed wboeke closed 2 months ago

wboeke commented 2 months ago

Hello, Something goes wrong in the following code:

here .
create val 8 allot
here .

The value of the 2nd 'here' should be 8 bigger than the value of the 1st 'here'. But the difference appears to be only 1. Could you have a look at this? Maybe it's a good idea to give some demo programs illustrating more advanced forth code, like create ... does> ... and array@ ? The provided test programs are nice but rather basic.

chochain commented 2 months ago

Hello Mr. Boeke, thank you for taking interest in eForth.

  1. The reason 'here' incremented by 1 is due to the current decision/experiment. Dynamic array is chosen for parameter memory, also stacks and dictionary. See ~/src/ceforth.h for details. This departs from the classic Forths which use memory blocks. So, 'here' is no more the memory address but an index to the new dictionary entry. Any new colon word, including create, will increment here by just 1. A bad consequence is that I don't know how to do 'dump' now! However, 'see' does work.
  2. I took your suggestion and added some demos that illustrate most of the 'basic' features. Please pull my latest check-in and review the Build & Run section (i.e. ~/tests/lessons420.txt)
  3. Will work on adding the advanced.
wboeke commented 2 months ago

Hello Chochain, Thanks for the reply. I tried your lessons420 and it works perfectly. However I'm not very smart, so I need a simple example. Here is a demo for an array:

\ forth:
\ : inc cells ;
\ eforth:
: inc 2* ;
create arr 10 inc allot
136 arr !
arr @ .
137 arr 2 inc + !
arr 2 inc + @ .

Thus there are 2 inc's, one for standard forth, one for eforth. The assignment of 136 works okay, but the assignment of 137 to the 2nd array member causes a crash in eforth. In your example you are using a normal variable. Maybe that is needed in eforth?

chochain commented 2 months ago

Ah, dynamic array is at fault again. Since it's no more a continuous block of bytes, accessing an array element needs a new word.

So, try whether these work (in fact, that's how I do string look up in lessens420 itself, too).

create arr 10 allot    \ this gets you 10 elements
see arr                    \ shows you the array with 10 zeros
136 arr !                  \ treat arr as a regular variable (or array with only 1 element)
see arr                    \ shows 136 taking the arr[0]
137 arr 2 array!       \ this should fill the arr[2]
see arr
arr 2 array@           \ this gets you the arr[2] back

Unfortunately, eForth is still pretty fragile against these memory exception. It crashes. By setting CC_DEBUG to 1 in config.h can catch some of them. In the mean time, thank you very much for your feedback and do enjoy if you can. Chochain

BTW, I like the 'th' used by 4tH, a dialect of Forth, as the array access word, i.e. 137 arr 2 th ! and arr 2 th @. I might do just that.

wboeke commented 2 months ago

Thanks, it works!