You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A good refactor would be to abstract stack pushing/popping in compiler 'GenerateTargetTraversal.ts'
from June 2023 discussion:
I agree it's confusing. It might be good to abstract away the stack pushing/popping at some point actually, since it's been the cause of a few bugs in the past 😅
This whole piece of code is very very bad and I would hopefully not write it the same way if I were to do it again lmao. But hopefully this gives some insight into what is actually happening here and what you need to change. By the end of your execution you need to end up with '(value)' on top of the stack, and the rest of the stack being unaffected.
So to recap: this.stack keeps track of what the stack looks like if the compiled bytecode were to be executed up until then. this.pushToStack() pushes to this stack, and this.popFromStack() pops from it.
In the example above, we start with this stack:
1:
When we execute this.emit(hexToBin('a914')), this pushes a value to the stack, so we also call this.pushToStack('(value)') to account for that.
1:
2:
Then we call this.visit(node.parameters[0]), the end result of this "visit" will be a new value on the stack (this is pushed in the call to this.visit().
1: node.parameters[0]
2:
3:
Then we execute this.emit(Op.OP_CAT), this concats the top 2 stack elements together. Because I am lazy I don't call this.popFromStack() here. So we need to remember to call pop 1x later on.
1: + node.parameters[0]
2:
Then we execute this.emit(hexToBin('87')), pushing a value to the stack. We don't call pushToStack, which cancels out last time we didn't call pop.
1:
2: + node.parameters[0]
3:
Then we execute this.emit(Op.OP_CAT), concatenating again. We don't call pop, so we need to remember to call pop 1x later on.
1: + node.parameters[0] +
2:
Then we call this.popFromStack(2) (equivalent to 2x this.popFromStack()) and later outside the big if-statement we call this.pushToStack('(value)'). The first pop is to compensate for the one we skipped earlier, the second pop+push is to replace whatever was on top of the stack with '(value)'.
1:
2:
The text was updated successfully, but these errors were encountered:
A good refactor would be to abstract stack pushing/popping in compiler 'GenerateTargetTraversal.ts'
from June 2023 discussion:
The text was updated successfully, but these errors were encountered: