DEV Community

Cover image for Play: A new programming language that targets Web Assembly, based on Forth

Play: A new programming language that targets Web Assembly, based on Forth

Robin Heggelund Hansen on November 23, 2020

Today marks the first release of Play, a new programming language inspired by Forth. Play might look a lot like Forth, but improves on the languag...
Collapse
 
sarini68 profile image
sarini68

Hi, I'm spending some time in having fun with Play. I have a first question (maybe a lot more after). What happens to values on stack, are they consumed after use? See e.g.

def: main
entry: true
: four-and-five +

What happens to 4 and 5 after they are summed?
It seems to me at the moment not clear how to manage values on the stack
Thanks,
m

Collapse
 
robinheghan profile image
Robin Heggelund Hansen

Great to hear you're enjoying Play!

It depends on the word, really. For + and other math words, the numbers will first be popped/consumed, then the result will be pushed onto the stack. So they're consumed as part of their use.

The type of + is Int Int -- Int. You can read that as: "consume two ints, produce one int."

If you for some reason want to retain values on the stack you can use dup (short for duplicate). So if you want to keep 4 and 5 on stack, but also the sum of them, you can do the following: four-and-five dup dup +. The resulting stack would be: 4 5 9.

Collapse
 
sarini68 profile image
sarini68

Many thanks for your reply!
Is there any way to see all the content of the stack at a given time and not just the last value on top of it? I am also trying to read documentation about Forth to have a clearer idea of the background, best, m

Thread Thread
 
sarini68 profile image
sarini68

If am not wrong, please tell me, I've tried your suggestion and with rotate applied three times I got 4 10 and 5, as if the dup applied to have 2 times 5, and ten is the sum of the two. I can not explain me why, thanks again for your attention! Best, m

Thread Thread
 
robinheghan profile image
Robin Heggelund Hansen • Edited

Is there any way to see all the content of the stack at a given time

Unfortunetly, no. But that's definetly something that can be done in a later release!

as if the dup applied to have 2 times 5

Ahh, I was too quick in my reply and got it wrong. The following would be more correct:

def: main
entry: true
: four-and-five # stack: 4 5
  dup # stack: 4 5 5
  -rotate # stack: 5 5 4
  dup # stack: 5 5 4 4
  -rotate # stack: 5 4 4 5
  swap # stack: 5 4 5 4
  + # 5 4 10
  rotate # stack: 10 5 4
  swap # stack: 10 4 5
  -rotate # stack: 4 5 10
Enter fullscreen mode Exit fullscreen mode

Now, this is quite horrid. This is way too much stack manipulation for my taste. However, in later alpha's there will be more powerful stack manipulation words, like a specific word for doing duplicates of the two top elements on the stack.

Thread Thread
 
sarini68 profile image
sarini68

Many thanks again for your reply. Agree with you about too much stack manipulation. Unfortunately I didn't get the same results you wrote in the comments. I'll try again to better understand if something I did was wrong. Just a last question, I was wondering to use rotate to explore the content of the stack. Does rotate work fine just with three values on the stack?
Best, m

Collapse
 
timosarkar profile image
Timo Sarkar

Hot !