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

Builtin lines function part I - Refactoring #565

Merged

Conversation

hdwalters
Copy link
Contributor

Refactor the += shorthand add operator, by consolidating the various self.is_ref tests for text, array and numeric addition. This is necessary because in a subsequent PR, we will append functionality for array addition only, and this is not possible with the current implementation. Make corresponding changes to other shorthand operators, initialisation and setter statements.

let id = meta.gen_array_id();
meta.stmt_queue.push_back(format!("__AMBER_ARRAY_ADD_{id}=({left} {right})"));
format!("{quote}${{__AMBER_ARRAY_ADD_{id}[@]}}{quote}")
let name = format!("__AMBER_ARRAY_ADD_{id}");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'll need to pass name into a new TranslateModule function in the next PR, so it makes sense to store it in a local variable here.

}
}
Type::Array(_) => {
if self.is_ref {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I unrolled some of the code which was previously shared across expression types in this match statement; this is necessary so we can append additional code to the Array case in the next PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should also make it slightly easier to implement @Ph0enixKM's proposed new translation layer.

let name = match self.global_id {
Some(id) => format!("__{id}_{}", self.var),
None => if self.is_ref { format!("${{{}}}", self.var) } else { self.var.clone() }
let name = if let Some(id) = self.global_id {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to match the shorthand add operator.

match self.global_id {
Some(id) => format!("__{id}_{name}={expr}"),
None => format!("{local}{name}={expr}")
if let Some(id) = self.global_id {
Copy link
Contributor Author

@hdwalters hdwalters Nov 3, 2024

Choose a reason for hiding this comment

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

We previously tested self.global_id after self.is_fun_ctx, but I switched the order to match the shorthand operators. I think this is safe, because global_id and is_fun_ctx cannot both be enabled at the same time.

.map(|index| format!("[{}]", self.is_ref
.then(|| index.translate_eval(meta, true))
.unwrap_or_else(|| index.translate(meta))))
.map(|index| self.translate_eval_if_ref(index, meta))
Copy link
Contributor Author

@hdwalters hdwalters Nov 3, 2024

Choose a reason for hiding this comment

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

Added helper function translate_eval_if_ref() to remove duplicated code here. I would have moved this new function to the definition of TranslateModule, but it only seems to be required here.

}
if self.is_ref {
if let Some(id) = self.global_id {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We previously tested self.global_id after self.is_ref, but I switched the order to match the shorthand operators. I think this is safe, because global_id and is_ref cannot both be enabled at the same time.

@Ph0enixKM
Copy link
Member

@hdwalters should I wait for this PR until I implement the TranslationModules?

@hdwalters
Copy link
Contributor Author

Please wait for this PR and the next one, which I have not submitted yet.

@hdwalters hdwalters self-assigned this Nov 6, 2024
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.

Looks good to me

@hdwalters hdwalters changed the title Implement builtin lines function part I - Refactoring Builtin lines function part I - Refactoring Nov 7, 2024
@hdwalters hdwalters merged commit 188abb4 into amber-lang:master Nov 7, 2024
1 check passed
@hdwalters hdwalters deleted the implement-builtin-lines-function-i branch November 7, 2024 17:34
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.

3 participants