fuzzballcat / milliForth

A FORTH in 340 bytes — the smallest real programming language ever as of yet.
MIT License
476 stars 22 forks source link

drop issue #9

Closed tromp closed 10 months ago

tromp commented 10 months ago

: drop dup - + ;

implements (a b -- a) rather than (b -- ) it would fail on a stack containing only 1 element.

mitchblank commented 10 months ago

I don't see how it would cause a problem. It would just harmlessly add zero to the memory location above the stack.

tromp commented 10 months ago

The memory location above the stack could be special memory that should not be read or written to.

It would be safer to zero the element to be dropped and add it to some byte read from a safe location and then written back there.

mitchblank commented 10 months ago

Sure, if this was some production C++ code then an out-of-bounds memory read would obviously be an issue. I think everybody can understand that.

In the context of a code-golfed assembly program I think it's safe to assume that the stack is in its explicitly-placed location.

That said, I suspect...

: drop sp@ ! ;

...would work fine too.

tromp commented 10 months ago

Oh; that's nice! Just overwrite the item to be dropped with itself first.

I like it.