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

feature: add constant declarations #423

Merged
merged 7 commits into from
Nov 20, 2024
Merged

feature: add constant declarations #423

merged 7 commits into from
Nov 20, 2024

Conversation

b1ek
Copy link
Member

@b1ek b1ek commented Aug 25, 2024

i've added support for the const X = Y expression itself. it will resolve to declare -r X=Y, for both local and global variables, since declare -r is the same for locals as well

i also set args in main(args) to be constant. function parameters are never constant.

i don't know which is the minimal bash version for declare -r

b1ek added 4 commits August 25, 2024 14:18
this is implemented as `is_const` field in `VariableDecl`, so constants are technically variables that cannot be reassigned.
@b1ek b1ek requested review from Mte90 and Ph0enixKM August 25, 2024 07:09
@b1ek
Copy link
Member Author

b1ek commented Aug 25, 2024

i just checked, declare -r works for bash 4.0:

screenshot that demonstrates how it works in bash 4.0

@b1ek b1ek marked this pull request as ready for review August 26, 2024 02:09
@Mte90 Mte90 linked an issue Aug 30, 2024 that may be closed by this pull request
@Ph0enixKM
Copy link
Member

The reason why I never introduces const is because sh does not support it. So for instance on ash it fails:

image

@Ph0enixKM
Copy link
Member

When we start to support sh - we should compile it to the regular variable instead (but still never let reassign the variable in the script)

@b1ek
Copy link
Member Author

b1ek commented Sep 5, 2024

When we start to support sh - we should compile it to the regular variable instead (but still never let reassign the variable in the script)

i'd rather have it defined as a function that always returns the same literal

@Mte90
Copy link
Member

Mte90 commented Sep 5, 2024

The idea of a function that returns always the same value I think that is the best and assure that can't change.

@mks-h
Copy link
Member

mks-h commented Sep 8, 2024

I'd say go with the @Ph0enixKM idea, since functions have an additional performance penalty. It doesn't matter that a constant is technically a regular variable, so long as it is translated from an Amber constant, thus is proven to be impossible to modify.

@Mte90
Copy link
Member

Mte90 commented Sep 9, 2024

I think that we can move on with a function that return always the same value so we can assure the best compatibility.
In the future we can think for something more sophisticated.

@Ph0enixKM
Copy link
Member

In order to resolve this discussion I've created the following poll: #493

@b1ek
Copy link
Member Author

b1ek commented Sep 30, 2024

@Ph0enixKM im confused, though. afaik we don't support sh right now. why is it an issue?

@Ph0enixKM
Copy link
Member

I think that we eventually will support sh. I just wanted to settle down how are we going to solve this problem holistically (including sh even if we do not implement it's support just yet)

Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good @b1ek. It would be nice to improve that error message. I'll create issues to add constants to our editor plugins

src/modules/variable/set.rs Show resolved Hide resolved
src/modules/statement/stmt.rs Outdated Show resolved Hide resolved
src/utils/metadata/parser.rs Outdated Show resolved Hide resolved
src/modules/variable/constinit.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@hdwalters hdwalters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved subject to suggestions from both myself and @Ph0enixKM.

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Nov 20, 2024

I think that we'll have to write a guideline on writing error messages. But that's not a hurry. Let's merge the feature

@Mte90
Copy link
Member

Mte90 commented Nov 20, 2024

@b1ek can you fix the conflicts so we can merge it?

@Mte90 Mte90 merged commit 3ea8242 into amber-lang:master Nov 20, 2024
1 check passed

main {
const x = 42
unsafe $ x=123 $
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x is a variable in the amber side, but $ x $ is a variable in the bash. Should this be $ {nameof x}=123 $ to some forward compatibility?

@b1ek b1ek deleted the add-const branch November 22, 2024 01:01
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.

[Suggestion] constant declaration
6 participants