Page 2 of 2

Re: Any usable scripting language for PB?

Posted: Sat Apr 28, 2012 10:11 am
by Danilo
Zach 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/Stack_machine
- 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()
You see the same commands (PUSH, ADD, SUB, MUL, DIV) if you start Beispiel2.pb in EvaluateExpression.zip

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.

Re: Any usable scripting language for PB?

Posted: Sat Apr 28, 2012 9:05 pm
by Zach
Thanks guys, interesting

Re: Any usable scripting language for PB?

Posted: Wed May 02, 2012 1:05 am
by void
In my case, I'm doing lightweight pseudo-threads, so to all primitives I pass a single argument of a *VMState, which includes information about its data and call stacks, variable contexts, among other things.

This adds a little to execution overhead over a 'purer' forth model, but I gain a lot of flexibility in usage models. And it's still plenty fast, even though I'm only using a single OS thread (and thus not making use of other cores) so far.

The result, if I ever manage to complete it, will be a 'managed' forth-alike to be embedded in the 2d game engine environment I'm designing.