6

If i got it right, Michelson is a stack based language.

I've read the following example about stack based languages:

3 4 5 * + = 23

how does Michelson know, that it has to mutiplicate 4 * 5 before adding 3?

Just to explain how i think:

The multiplicator comes first so i would think: 3*4+5 = 17

Why is that wrong?

  • 1
    I tend to think this question is less a Tezos/Michelson specific question and more a general question about postfix notation and stack evaluation ? – Ezy Jan 31 '19 at 13:16
10

We can simulate the program in the following way. We write the stack between [...] and then the operations. We use three different operations push <int>, * and +.

push puts an integer at the top of the stack, * pops two elements from the stack and pushes their product, and + pops two elements from the stack and pushes their sum.

Your program starts with an empty stack [] and some operations:

[], push 3, push 4, push 5, *, +
-> (evaluation of push 3)
[3], push 4, push 5, *, +
-> (evaluation of push 4)
[3, 4], push 5, *, +
-> (evaluation of push 5)
[3, 4, 5], *, +
-> (evaluation of *)
[3, 20], +
-> (evaluation of +)
[23]

So the result of your computation is 23.

9

'*' and '+', in this context, are not the infix operators you are used to. Imagine reading left to right and stacking up the numbers as you go. When you encounter + or * you replace the top two elements of the stack with the result of the operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.