what do these mean

2012-05-20 Thread e-mail mgbg25171
There's a little forth program written in python here
#http://openbookproject.net/py4fun/forth/forth.py
I'm struggling to understand what these lines mean.

def rJnz (cod,p) : return (cod[p],p+1)[ds.pop()]
def rJz  (cod,p) : return (p+1,cod[p])[ds.pop()==0]

Specifically I'm stuck on what (code[p], followed by p+1 does inside the
brackets
and also what [ds.pop()] abd [ds.pop==0] does afterwards
I do know that
p is an integer
cod[p] is one of a list of functions that are stored in cod
ds.pop() is popping the top of the ds list and
ds.pop() == 0 either means that the result popped is 0 or that there are no
items left in list ds[]

Any help much appreciated
-- 
http://mail.python.org/mailman/listinfo/python-list


what does newP = func(code,p) do?

2012-05-16 Thread e-mail mgbg25171
def execute (code) :
p = 0
while p  len(code) :
func = code[p]
p += 1
newP = func(code,p)
if newP != None :
p = newP

I'm trying to work out what this does

code is a list of function addresses and numbers
What on earth is funct(code,p) doing???

My understanding so far is...
set p to 0
while p is less than the number of elements in list code keep doing what
follows...
set func to the first element in the list
increment the list index p
?
if newP is not null then set the list index p to ?
Can someone please explain in english what ? is
Thank you in anticipation

BTW the link for this stuff is

http://openbookproject.net/py4fun/forth/forth.html
to see the thing in action ie the download link is on the above page

Here's a listing of the output from this proc for the following forth input
i.e. I put some print statements in
but have no idea how
newP set to func( [function rPush at 0x0164F570, 4] , 1 ) i.e.  2
results in 2
and then
newP set to func( [function rAdd at 0x0164F130] , 1 ) i.e.  None
results in None
???


Forth 5 4 + .

1st line of execute__
code =  [function rPush at 0x0164F570, 5]
p =  0
1st line of while__
func = code[ 0 ] i.e.  function rPush at 0x0164F570
incrementing p to  1
newP set to func( [function rPush at 0x0164F570, 5] , 1 ) i.e.  2
p = newP i.e.  2

1st line of execute__
code =  [function rPush at 0x0164F570, 4]
p =  0
1st line of while__
func = code[ 0 ] i.e.  function rPush at 0x0164F570
incrementing p to  1
newP set to func( [function rPush at 0x0164F570, 4] , 1 ) i.e.  2
p = newP i.e.  2

1st line of execute__
code =  [function rAdd at 0x0164F130]
p =  0
1st line of while__
func = code[ 0 ] i.e.  function rAdd at 0x0164F130
incrementing p to  1
newP set to func( [function rAdd at 0x0164F130] , 1 ) i.e.  None

1st line of execute__
code =  [function rDot at 0x0164F430]
p =  0
1st line of while__
func = code[ 0 ] i.e.  function rDot at 0x0164F430
incrementing p to  1
13
newP set to func( [function rDot at 0x0164F430] , 1 ) i.e.  5
None
Forth
-- 
http://mail.python.org/mailman/listinfo/python-list


what does newP = func(code,p) do?

2012-05-16 Thread e-mail mgbg25171
It's been a long time since I did any Python and I've never done that
In C I'm used to storing function ptrs and then having to use some other
constructs to call them.
To be able to store func and then use func to call itself like that threw
me...it's very elegant.
Thank you very much for your very lucid explanation and for taking the time
to provide it.
Best Regards
-- 
http://mail.python.org/mailman/listinfo/python-list