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

Combine array and value identifiers #615

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modules/builtin/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl SyntaxModule<ParserMetadata> for LinesInvocation {

impl TranslateModule for LinesInvocation {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let name = format!("__AMBER_ARRAY_{}", meta.gen_array_id());
let name = format!("__AMBER_ARRAY_{}", meta.gen_value_id());
let temp = format!("__AMBER_LINE_{}", meta.gen_value_id());
let path = (*self.path).as_ref()
.map(|p| p.translate_eval(meta, false))
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl TranslateModule for Add {
match self.kind {
Type::Array(_) => {
let quote = meta.gen_quote();
let id = meta.gen_array_id();
let id = meta.gen_value_id();
let name = format!("__AMBER_ARRAY_ADD_{id}");
meta.stmt_queue.push_back(format!("{name}=({left} {right})"));
format!("{quote}${{{name}[@]}}{quote}")
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/literal/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl SyntaxModule<ParserMetadata> for Array {

impl TranslateModule for Array {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let name = format!("__AMBER_ARRAY_{}", meta.gen_array_id());
let name = format!("__AMBER_ARRAY_{}", meta.gen_value_id());
let args = self.exprs.iter().map(|expr| expr.translate_eval(meta, false)).collect::<Vec<String>>().join(" ");
let quote = meta.gen_quote();
let dollar = meta.gen_dollar();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/variable/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl TranslateModule for VariableGet {
},
(true, Type::Array(_)) => match *self.index {
Some(ref expr) => {
let id = meta.gen_array_id();
let id = meta.gen_value_id();
let expr = expr.translate_eval(meta, true);
meta.stmt_queue.push_back(format!("eval \"local __AMBER_ARRAY_GET_{id}_{name}=\\\"\\${{${name}[{expr}]}}\\\"\""));
format!("$__AMBER_ARRAY_GET_{id}_{name}") // echo $__ARRAY_GET
Expand Down
11 changes: 1 addition & 10 deletions src/utils/metadata/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub struct TranslateMetadata {
pub stmt_queue: VecDeque<String>,
/// The metadata of the function that is currently being translated.
pub fun_meta: Option<FunctionMetadata>,
/// Array id is used to determine the array that is being evaluated.
pub array_id: usize,
/// Value ID - used to store values in variables.
/// Used to determine the value or array being evaluated.
pub value_id: usize,
/// Determines whether the current context is a context in bash's `eval`.
pub eval_ctx: bool,
Expand All @@ -39,7 +37,6 @@ impl TranslateMetadata {
fun_cache: meta.fun_cache,
fun_meta: None,
stmt_queue: VecDeque::new(),
array_id: 0,
value_id: 0,
eval_ctx: false,
silenced: false,
Expand All @@ -64,12 +61,6 @@ impl TranslateMetadata {
self.indent -= 1;
}

pub fn gen_array_id(&mut self) -> usize {
let id = self.array_id;
self.array_id += 1;
id
}

pub fn gen_value_id(&mut self) -> usize {
let id = self.value_id;
self.value_id += 1;
Expand Down