Re: Any usable scripting language for PB?
Posted: Sat Apr 28, 2012 10:11 am
- http://en.wikipedia.org/wiki/Stack_machineZach wrote:I've never attempt anything like stack-based programming.. I'd probably suck at it, but at the same time I'd be interested in any newbie type links on the subject.
- http://en.wikipedia.org/wiki/P-code_machine
Simple example:
Code: Select all
;
; the stack
;
Global NewList Stack.i()
Procedure PUSH(value)
;Debug "PUSH "+Str(value)
AddElement(Stack())
Stack() = value
EndProcedure
Procedure POP()
value = Stack()
;Debug "POP "+Str(value)
DeleteElement(Stack())
ProcedureReturn value
EndProcedure
;
; machine commands
;
Procedure OUTPUT()
Debug "OUTPUT: "+Str( POP() )
EndProcedure
Procedure ADD()
;Debug "ADD"
value2 = POP()
value1 = POP()
PUSH( value1 + value2 )
EndProcedure
Procedure SUB()
;Debug "SUB"
value2 = POP()
value1 = POP()
PUSH( value1 - value2 )
EndProcedure
Procedure MUL()
;Debug "MUL"
value2 = POP()
value1 = POP()
PUSH( value1 * value2 )
EndProcedure
Procedure DIV()
;Debug "DIV"
value2 = POP()
value1 = POP()
PUSH( value1 / value2 )
EndProcedure
;
; examples
;
; program: OUTPUT 4 * 9 + 2
PUSH(4)
PUSH(9)
MUL()
PUSH(2)
ADD()
OUTPUT()
Debug "-----"
; program: OUTPUT 18 * 27 + 36 / 6
PUSH(18)
PUSH(27)
MUL()
PUSH(36)
PUSH(6)
DIV()
ADD()
OUTPUT()
Debug "-----"
; program: OUTPUT 1 + 2 + 3 * 4 - 5
PUSH(1)
PUSH(2)
ADD()
PUSH(3)
PUSH(4)
MUL()
ADD()
PUSH(5)
SUB()
OUTPUT()
PUSH puts something on top of the stack
POP removes something from the top of the stack
Commands like ADD,SUB,MUL,DIV each take (POP) the 2 topmost stack values and PUSH the result back on top of the stack.
OUTPUT takes the top of the stack and outputs it. Use it for displaying the result of the expression.