elikaski / BF-it

A C-like language to Brainfuck compiler, written in Python
MIT License
120 stars 11 forks source link

Add better array initialization #22

Closed NeeEoo closed 3 years ago

NeeEoo commented 3 years ago

Currently if you want to set an array to have specific values you need to do this

int array[5];
array[0] = 40;
array[1] = 63;
array[2] = 15;
array[3] = 33;
array[4] = 76;

it would be better to be able to do this

int array[5] = {40, 63, 15, 33, 76};

or

int array[5];
array = {40, 63, 15, 33, 76};

(unsure if this is valid c)

The resulting code would probably look like this

[-]>[-]+++++[-<++++++++>]
>[-]+++++++[-<+++++++++>]
+++++++++++++++>[-]
+>[-]++++[-<++++++++>]
+>[-]+++++[-<+++++++++++++++>]

This should also support multi-dimensional arrays (This might be very hard to implement) Example:

void main() {
    int array[2][5];
    int idx = readchar() - 48;

    if(idx == 0) {
        array[0] = {40, 63, 15, 33, 76};
        array[1] = {27, 122, 75, 23, 64};
    }
    if(idx == 1) {
        array = {
            {87, 45, 23, 12, 11},
            {1, 0, 9, 2, 12}
        };
    }
    if(idx == 2) {
        array = {
            13, 4, 98, 4, 7,
            22, 85, 43, 11, 52
        };
    }
}