-
Notifications
You must be signed in to change notification settings - Fork 87
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
base: master
Are you sure you want to change the base?
Conversation
this is implemented as `is_const` field in `VariableDecl`, so constants are technically variables that cannot be reassigned.
When we start to support |
i'd rather have it defined as a function that always returns the same literal |
The idea of a function that returns always the same value I think that is the best and assure that can't change. |
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. |
I think that we can move on with a function that return always the same value so we can assure the best compatibility. |
In order to resolve this discussion I've created the following poll: #493 |
@Ph0enixKM im confused, though. afaik we don't support |
I think that we eventually will support |
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 looks pretty good @b1ek. It would be nice to improve that error message. I'll create issues to add constants to our editor plugins
@@ -36,6 +36,10 @@ impl SyntaxModule<ParserMetadata> for VariableSet { | |||
let variable = handle_variable_reference(meta, tok.clone(), &self.name)?; | |||
self.global_id = variable.global_id; | |||
self.is_ref = variable.is_ref; | |||
// Check for constant reassignment | |||
if variable.is_const { | |||
return error!(meta, tok, format!("Cannot reassign constant")) |
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.
Can this error message say a bit more about what variable cannot be reassigned?
@@ -68,7 +69,8 @@ pub enum StatementType { | |||
Mv(Mv), | |||
CommandModifier(CommandModifier), | |||
Comment(Comment), | |||
CommentDoc(CommentDoc) | |||
CommentDoc(CommentDoc), | |||
ConstInit(ConstInit) |
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.
Would be helpful to get in the habit of adding a comma to the last item of multi-line Rust lists (applies to struct fields, function definition parameters, function call arguments, vec![]
initialisations, etc) when you're making changes anyway. This it makes subsequent pull requests (marginally) nicer. If this had been done to CommentDoc(CommentDoc)
when that was added, you would have one changed line here, not two.
let global_id = self.is_global_scope().then(|| self.gen_var_id()); | ||
let scope = self.context.scopes.last_mut().unwrap(); | ||
scope.add_var(VariableDecl { | ||
name: name.to_string(), | ||
kind, | ||
global_id, | ||
is_ref: false | ||
is_ref: false, | ||
is_const |
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.
Two more places to add trailing commas, in add_var()
and add_param()
.
} | ||
|
||
impl SyntaxModule<ParserMetadata> for ConstInit { | ||
syntax_name!("Constant 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.
The equivalent line in init.rs
is:
syntax_name!("Variable Initialize");
Consider changing one or the other to match.
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.
Approved subject to suggestions from both myself and @Ph0enixKM.
i've added support for the
const X = Y
expression itself. it will resolve todeclare -r X=Y
, for both local and global variables, sincedeclare -r
is the same for locals as welli also set
args
inmain(args)
to be constant. function parameters are never constant.i don't know which is the minimal bash version for
declare -r