elikaski / BF-it

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

Support structs #30

Open abhra2020-smart opened 3 years ago

NeeEoo commented 3 years ago

OO is very hard to make in brainfuck. So I don't think that this is feasible yet.

elikaski commented 3 years ago

In this implementation there are no pointers (including function pointers), so making it OO is not feasible I'm afraid. But I do think that adding structs is a decent idea.

NeeEoo commented 3 years ago

The structs must have the size known. So when copying it to a variable or parameter, it would make enough space for the struct. Then when copying the struct, it copies every cell of the struct.

The best way to start implementing structs is to make the function/variable compiler have a cell size value. So it can adjust the space between every variable's location on the tape.

When accessing/writing a value from a struct it would point directly to that cell, since the name can't change.

struct Example {
    char message[5];
    bool working;
};
struct Example test;
test.working = true;

Would just write the value 1 to the 6th cell of the variable.

NeeEoo commented 3 years ago

Could you assign me to this? I want to try to implement this.

NeeEoo commented 3 years ago

What would you recommend to represent ID DOT ID (test.working) in the Node.py?

elikaski commented 3 years ago

I think it should be similar to NodeArrayGetElement and NodeArraySetElement (Maybe names should be NodeStructSet and NodeStructGet or something similar) The class should hold the token ID of the struct (e.g. token of "test"), and token ID of the field name (e.g. token of "working") There should be a function (maybe in General.py?) that has input of these two tokens, and an output of index of the cell in which the field value should be Then use get_copy_to_offset_code to do the reading/writing, or something similar

We should also consider whether or not we allow arrays of structs. E.g. arr[i].working=1;, as this might be tricky

NeeEoo commented 3 years ago

Arrays with structs is planned. Structs in structs is also planned.

I also came up with an idea that the pointer () could make it inherit the same id from that id map. So `int test(struct Example aaaa)` would make aaaa point to the same variable as that was passed in the parameters. The pointers wouldn't have all the functionality but it would have some.

NeeEoo commented 3 years ago

I might need to add more Node types such as NodeStructSetArrayElement and NodeStructGetArrayElement. Would this be okay, or should i try to find a way to integrate it into the pre existing nodes?

NeeEoo commented 3 years ago

Nevermind, it would probably be more messy to add new nodes

elikaski commented 3 years ago

I might need to add more Node types such as NodeStructSetArrayElement and NodeStructGetArrayElement. Would this be okay, or should i try to find a way to integrate it into the pre existing nodes?

It's your decision :) In my opinion it would be best to use existing code as much as possible, so I would say defining Struct Nodes that will use the Array Nodes If it's too difficult or theres a good reason to define new Nodes then that would be OK too

NeeEoo commented 3 years ago

Ok i got arrays in structs working without adding any extra nodes. I also fixed a small bug that didn't do anything