Expand list of strings to 2D coordinate

146 views Asked by At

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).

1

There are 1 answers

0
tripl3dogdare On

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!