Also, how big relative to each other?
I'm trying to make a brainfuck interpreter for an Arduino. I know this has been done before, but I'm adding things such as an Adafruit Display and code-writing ability.
Because of this, the code is quite long, especially since I'm storing both code and tape in arrays:
char code[] = {};
byte tape[] = {};
Memory should consist of at least 30000 cells, some existing brainfuck programs do need more so this should be configurable or unbounded.
I know I'm never going to fit 30000 cells into a Uno, but it seems like dynamic memory is the way to get the most space out of it. Any way of doing this appreciated.
Currently, char code[512] = {};
and byte tape[1024] = {};
uses "1,807 bytes (88%) of dynamic memory", but that's just the interpreter sketch. I'll need to squash some more code in there too. The code is here for anyone who is interested.
You can implement it using a map
object, instead of creating a limited array. Use the indexes as keys and the value in that cell as a value (delete it if it equal to zero, and create a new entry in the map if the value of that cell now rises to 1 or more, or decreases to less than zero.
That way you store only the values that are not 0 (saves memory and allocation considerations), and you have a theoretically unlimited tape (more pure implementation).
operation map
----------------------------
#code_start <>
+ <0 : 1>
+ <0 : 2>
- <0 : 1>
- <>
- <0 : -1>
>+ <0 : -1, 1 : 1>
<<- <-1 : -1, 0 : -1, 1 : 1>
Maps can be used by including #include <map>
and created by std::map<int, int> map_name
or whatever types you want to use.
See documentation here: http://www.cplusplus.com/reference/map/map/