elikaski / BF-it

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

Add Structs #55

Open NeeEoo opened 3 years ago

NeeEoo commented 3 years ago

Closes #30 Closes #29

I feel like that there is so much that could be improved, so I'm opening this as a draft. If you have any questions about my code, ask me.

Here is the progress:

Some thoughts

The function named get_variable_size_from_token in General.py could use a better name.

Some of the comments in Node.py could be improved

There should be a util in parser that returns True or False if the tokens match. check_next_tokens_are and check_current_tokens_are, assert and raise if it doesn't match. There could be assert_next_tokens_are, assert_current_tokens_are, check_next_tokens_are and check_current_tokens_are. The old functions should be renamed to the assert ones.

Extras

I realized that namedtuple is used wrongly, correcting it makes it not work. Converting the data into a class is better.

The way the array access currently works on arr[5].list, arr.list[2] and arr[5].list[2], is that in the second one it passes the struct object to get_array_index_expression and in the first one it doesn't. The way this works is that it calculates the data size of the array and multiplies the index if it is the first one, and in the last one it adds both.

I made some debug tools: https://gist.github.com/NeeEoo/bf88c058fd48793a00cb53e5554d4500

NeeEoo commented 3 years ago

@elikaski Would you be able to review the current revision?

elikaski commented 3 years ago

@elikaski Would you be able to review the current revision?

Of course I'm currently busy with the switch-case, so after that :) But if you're blocked and can't advance then let me know and I'll switch to this

NeeEoo commented 3 years ago

Continue with the switch-case since that is more closer to being finished than this.

abhra2020-smart commented 3 years ago

Can I help, by any chance? I'm working on a sort of programming language (called FlowLang) which can only support mathematical operations

NeeEoo commented 3 years ago

@abhra2020-smart Sure.

If you don't know how to get my changes, you can get either,

Here are some instructions If you use GitHub Cli:

I'm not sure how to do it in GitHub Desktop.

abhra2020-smart commented 3 years ago

For the implementation of s[2].list[3] , if you change return get_array_index_expression to return (index_expression, index_expression.__sizeof__()*2+3), life's a bit easier. Then you have to change EVERY function call to get_array_index_expression()[0] Also: index_expression.__ sizeof __() returns the size of index_expression (object. __ sizeof __ returns the size of said object) EDIT: Correction: You only need to change the function calls for when you want the first element

NeeEoo commented 3 years ago

@abhra2020-smart I'll try your way with the index expression after i finish fixing a bug.

I also use PyCharm and i use the git tool included in PyCharm, but i make my pull requests with GitHub Cli. GitHub Cli just makes it easier to interact with GitHub. I don't see how that would make you need to use Notepad.

NeeEoo commented 3 years ago

@elikaski I probably need help with Accessing structs in structs. Since that would require a rewrite of the way the .field is represented by the compiler. It should also be able to store an array index.

elikaski commented 3 years ago

@elikaski I probably need help with Accessing structs in structs. Since that would require a rewrite of the way the .field is represented by the compiler. It should also be able to store an array index.

OK Unfortunately I haven't had the time to look at this yet :( I will let you know when I get started

NeeEoo commented 3 years ago

@elikaski Do you know how to update a branch so that it contains the new stuff from master? Would the correct way be to make a pull request that merges master to NeeEoo/add-structs (this pull request), and then i merge that pull request?

elikaski commented 3 years ago

@elikaski Do you know how to update a branch so that it contains the new stuff from master? Would the correct way be to make a pull request that merges master to NeeEoo/add-structs (this pull request), and then i merge that pull request?

I think you can just pull normally and then rebase your commits over the latest master commit

NeeEoo commented 3 years ago

I've read that rebase is bad and that it might cause issues. But i might be wrong.

I'm not sure what the correct git commands are for doing that.

elikaski commented 3 years ago

You can also "git pull" and a merge commit will be created. That will work too Or we could keep this branch separated and merge it with master when it's ready

NeeEoo commented 3 years ago

It's more because there are some bug fixes such as the for loop scope bug which isn't on this branch, and when i test i need to copy the changes from that commit onto my working branch and then when i want to commit i remove that code.

elikaski commented 3 years ago

I merged and made a pull request (not sure that was necessary) Let me know if it helps

NeeEoo commented 3 years ago

I think i did it correctly

NeeEoo commented 3 years ago

Some of the old comments needs to be changed.

When implementing setting structs as a return type, the data type handling would need to be changed since it need to keep track of structs, it would need to store info such as which struct_id it is.

NeeEoo commented 3 years ago

There could be a new library function called divmod which has a return type of a struct with the definition:

struct {
    int ???; // b-a%b
    int mod;
    int div;
}

The function should take 2 parameters.

elikaski commented 3 years ago

There could be a new library function called divmod which has a return type of a struct with the definition:

struct {
    int ???; // b-a%b
    int mod;
    int div;
}

The function should take 2 parameters.

Good idea It will require implementing a mechanism that makes the return value size arbitrary, and not always 1 cell

NeeEoo commented 3 years ago

I have already implemented dynamic return size. The only thing missing is copying of more cells.

abhra2020-smart commented 1 year ago

There could be a new library function called divmod which has a return type of a struct with the definition:

struct {
    int ???; // b-a%b
    int mod;
    int div;
}

The function should take 2 parameters.

Good idea It will require implementing a mechanism that makes the return value size arbitrary, and not always 1 cell

The library part is easy to implement. I've done some research on how compilation works. With the way C (and C++) does it, they use a preprocessor. I've implemented one; look into #74 (I should change it to a draft as it is a bit buggy with strings, but I will fix it after finishing this comment). And the ??? bit I think should be called something along the lines of nearestMultiplebelow.