Any usable scripting language for PB?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Any usable scripting language for PB?

Post 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.
Zach
Addict
Addict
Posts: 1676
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Any usable scripting language for PB?

Post by Zach »

Thanks guys, interesting
void
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2011 9:50 pm
Location: Washington, USA

Re: Any usable scripting language for PB?

Post 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.
Post Reply