Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Structs #55

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

Add Structs #55

wants to merge 6 commits into from

Conversation

NeeEoo
Copy link
Contributor

@NeeEoo NeeEoo commented Apr 5, 2021

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:

  • Add structs
  • Allow accessing structs in structs
  • Be able to use s[2].list[3]. Tip: Make it so get_array_index_expression returns (data_size*(2) + 3)
  • Refactor the assignment expression code
  • Add Array Initialization for fields
    • s.list = {1,2,3,4};
    • s[2].list = {1,2,3,4};
  • Add Struct Initialization? struct point p = { 1, 2 } and struct point p = { .x = 1, .y = 2 } (This might be to hard)
  • Making a copy of a struct variable
  • Make function return variable size dynamic
  • Allow making functions with a struct as a return type
  • Add sizeof
  • Add sizeof of int, char, ... (sizeof(int))
  • Make it so you can define a struct variable as global
  • Add more comments
  • Add example files (Do this last)

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
Copy link
Contributor Author

NeeEoo commented Apr 5, 2021

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

@elikaski
Copy link
Owner

elikaski commented Apr 5, 2021

@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
Copy link
Contributor Author

NeeEoo commented Apr 5, 2021

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

@abhra2020-smart
Copy link
Contributor

abhra2020-smart commented Apr 6, 2021

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

@NeeEoo
Copy link
Contributor Author

NeeEoo commented Apr 6, 2021

@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:

  • Fork my fork of this repo (https://github.com/NeeEoo/BF-it)
  • Type gh repo clone abhra2020-smart/BF-it BF-it-NeeEoo in a terminal
  • Type git checkout add-structs in a terminal
  • Make changes
  • Commit (Don't push)
  • Type gh pr create in a terminal

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

@abhra2020-smart
Copy link
Contributor

abhra2020-smart commented Apr 7, 2021

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
Copy link
Contributor Author

NeeEoo commented Apr 7, 2021

@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
Copy link
Contributor Author

NeeEoo commented Apr 10, 2021

@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
Copy link
Owner

@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
Copy link
Contributor Author

NeeEoo commented Apr 17, 2021

@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
Copy link
Owner

@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
Copy link
Contributor Author

NeeEoo commented Apr 17, 2021

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
Copy link
Owner

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
Copy link
Contributor Author

NeeEoo commented Apr 17, 2021

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
Copy link
Owner

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

@NeeEoo
Copy link
Contributor Author

NeeEoo commented Apr 17, 2021

I think i did it correctly

@NeeEoo
Copy link
Contributor Author

NeeEoo commented Apr 17, 2021

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
Copy link
Contributor Author

NeeEoo commented Apr 28, 2021

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
Copy link
Owner

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
Copy link
Contributor Author

NeeEoo commented Apr 29, 2021

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

@abhra2020-smart
Copy link
Contributor

abhra2020-smart commented Dec 28, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support structs Exclude the return_value from the output if the function return type is void
3 participants