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

e-mail mgbg25171 mgbg25171 at blueyonder.co.uk
Wed May 16 10:08:52 EDT 2012


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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120516/5912c696/attachment.html>


More information about the Python-list mailing list