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

Ian Kelly ian.g.kelly at gmail.com
Wed May 16 11:32:41 EDT 2012


On Wed, May 16, 2012 at 8:08 AM, e-mail mgbg25171
<mgbg25171 at blueyonder.co.uk> wrote:
> 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???

code represents the bytecode of the Forth program being executed.  It
contains functions that represent operations and the data arguments used by
those operations.  p is the program counter, keeping track of which
opcode should be executed next.  func is the operation function that
was pulled from code at the current program counter.  func(code, p)
calls that function, passing in both the entire bytecode, and the
index into the bytecode at which the operation can find its data (if
any).  If the function returns a value, it means that the program
counter should be updated -- either to skip past a data element used
for the operation just executed, or to perform a jump to somewhere
else in the bytecode.  If the program counter ever falls off the end
of the bytecode, then the program terminates.



More information about the Python-list mailing list