-
Notifications
You must be signed in to change notification settings - Fork 56
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
Variable intialization at declare time #908
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase on top of recent main
branch.
711fc8a
to
566c46e
Compare
I did some cleaning and spotted a bug (see the fresh test case): checking of the LHS should occur before the RHS is added to the environment. |
To solve this case, I think we will need to call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just add a changelog entry and this will be ready.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice, I approve it.
If you don't want to do the small suggested changes, I will merge it as it is.
compiler/src/syntax.ml
Outdated
@@ -214,7 +214,15 @@ type align = [`Align | `NoAlign] | |||
|
|||
type plvals = annotations L.located option * plvalue list | |||
|
|||
type vardecls = pstotype * pident list | |||
|
|||
type vardecl = InitVarDecl of pident * pexpr | NotInitVarDecl of pident |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the definition
type vardecl = pident * pexpr option
will be more convenient.
You can do the change if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my first type definition (in fact I am not sure why I changed it). I will change it back
I’ve done the small changes you suggested. I let Alexandre decide whether he wants to change the data-type. |
One is for the left-hand-side, the other for the right-hand-side. This allows non-recursive declarations with initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems ready!
Issue
This is a duplicate of issue #899 based on #901 refactoring of pre-typing.
The feature requested is to add a new syntax that authorize variable initialization when they are declared.
The syntax is as follow :
Implementation
With the proposed implementation, we can also initialize multiple variables at the same time:
The current implementation doesn't support array initializing with this feature because array can currently be explicitly created only at top level. Allowing it would require reworking a large part of the grammar and pre-typing analysis.
In terms of semantic, the proposed implementation works as follow :
is abstractly equivalent to :