It is currently Sun May 19, 2013 6:24 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Any usable scripting language for PB?
PostPosted: Sat Apr 28, 2012 10:11 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 1290
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:
;
; 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.


Top
 Profile  
 
 Post subject: Re: Any usable scripting language for PB?
PostPosted: Sat Apr 28, 2012 9:05 pm 
Offline
Addict
Addict

Joined: Sun Dec 12, 2010 12:36 am
Posts: 1284
Location: Waterloo, WI - USA
Thanks guys, interesting

_________________
Image


Top
 Profile  
 
 Post subject: Re: Any usable scripting language for PB?
PostPosted: Wed May 02, 2012 1:05 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Aug 27, 2011 9:50 pm
Posts: 107
Location: Washington, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2

All times are UTC + 1 hour


Who is online

Users browsing this forum: Exabot [Bot], woki and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye