tsoding / porth

It's like Forth but in Python
629 stars 50 forks source link

Stack pointer operation (enables putting putd in `std.porth`) #117

Closed ap29600 closed 2 years ago

ap29600 commented 2 years ago

A simple operation that pushes a pointer to the element that is currenlty on the top of the stack.

With this functionality it's possible to allocate a temporary buffer on the stack by repeatedly pushing 0, and this enables to implement putd without a static buffer in memory, so putd can now be in the standard library.

As a sidenote, building gol.porth will now fail due to the macro name collision.

There is one drawback, and that is that this will create a discrepancy between simulation and compilation, as there is no easy way to implement this in python I think. Assuming that the stack is contiguous in memory in the bootstrapped version, there should really be no problem after that.

mjdr commented 2 years ago

Stack should be used for storing values that size know in compile time. ref introduces value aliasing and makes static analysis useless

Domkeykong commented 2 years ago

you can alternatively call the mmap syscall and get some memory, this is obviously a bit overkill but unless there isnt any malloc functionality this is the only way to not clobber any memory. Also don forget to call munmap

ap29600 commented 2 years ago

yep, makes sense to me.

mjdr commented 2 years ago

here another way you can implement putd without mem

126