[Tutor] Does Python have pointers?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Oct 1 22:43:45 CEST 2004



On Fri, 1 Oct 2004, Mark Kels wrote:

> I want to make an interpreter for BF (more info -
> http://home.wxs.nl/~faase009/Ha_BF.html) in Python.
> To do it I guess I need to use pointers

Hi Mark,


Let's see... the BF language appears to have only eight commands:


"""
'>' : move the memory pointer to the next cell,

'<' : move the memory pointer to the previous cell,

'+' : increment the memory cell under the memory pointer,

'-' : decrement the memory cell under the memory pointer,

',' : fills the memory cell under the memory pointer with the ASCII value
of next character from the input,

'.' : writes the contents of the memory cell under the memory pointer as a
character with the corresponding ASCII value,

'[' : moves to the command following the matching ']', if the memory cell
under the memory pointer is zero, and

']' : moves to the command following the matching '[', if the memory cell
under the memory pointer is not zero.
"""


I'm guessing that you need some kind of thing that represents the memory
cells of the BF virtual machine.  We can represent the memory cells as a
list:

    computer_memory = [0] * 1024

The "memory pointer" here can be a simple index into that computer memory,
initially positioned at 0.

    memory_pointer = 0


Does this make sense so far?  Pointers are a general concept: they're not
hardcoded to a particular language.  Here, we use an index as a "pointer"
into our computer memory.


Hope this helps!



More information about the Tutor mailing list