0

I'm attempting to write a Befunge interpreter in Python, and my problem right now is the p command. Basically, I have a list of strings that are each line of the file and I use a 2D coordinate system to keep track of my position. The outer list is y, and the position in each string is x.

The problem is this: The p command pops coordinates and a value out of the stack. It then puts the ASCII character corresponding to the value at that position. Unfortunately, with Python, lists and strings are really hard to expand by index. I need the most efficient way to expand the list and the strings within them to fit the updated data. I can't have it pre-expanded to a certain size for two reasons: theoretically infinite memory allocation (needed for Turing-completeness) and wrapping on the edge of the program if it isn't expanded past that edge (pre-expanded removes the ability to do that efficiently).

TL;DR: I need to find the best/most efficient way to expand a list and all the strings inside it to reach and include a specific 2D coordinate (outer list is y, inner list is x, i.e. prog[y][x]) and I can't have it pre-allocated because of several complicated and hard to explain reasons.

As it seems I was rather unclear, the p command directly modifies the running Befunge code. Thus, I can't store the data outside the program itself, as the program needs to be able to access and run it if necessary (As I have stated, Befunge is weird).

  • Can you give an example? This isn't entirely clear. – Linuxios Aug 25 '15 at 22:04
  • For example, the program `94+5*99p99g,@` should put an "A" at coordinate 9,9 and then get it back out and print it. The problem is when 9,9 is outside the program bounds... I need to expand the list and the strings inside it to fit the updated data. – tripl3dogdare Aug 25 '15 at 22:07
  • Maybe implement some sort of sparse matrix or use a tuple in a dict? You could have `{(9, 9): 'A'}` for the example in your comment. – dawg Aug 25 '15 at 22:10
  • I'm not sure how that helps... Really the only option within the specifications I'm working with is directly modifying the running program. Having it stored separately won't work as it has to be able to then run the `p`ed code if it hits it during execution. – tripl3dogdare Aug 25 '15 at 22:12
  • You need to be more specific then. – dawg Aug 25 '15 at 23:58
  • I'm sorry if I'm a little unclear - Befunge is a rather odd language and I don't really have the time or expertise to explain it in detail. (that's why I put the link to the wiki post) – tripl3dogdare Aug 26 '15 at 00:22

1 Answers1

0

After some deliberation, I decided to use @dawg's idea tupled dicts, but in a slightly different way; basically the entire program is a tuple-key dict, that way the program can be easily modified by coordinate at any time. Thank you for your help!