Skip to content

Commit

Permalink
Fix variable layer bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 22, 2024
1 parent 97eefbd commit 31a2bd8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/core/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ impl DataBase {
"".to_string()
}

pub fn get_layer_pos(&mut self, name: &str) -> Option<usize> {
let num = self.params.len();
for layer in (0..num).rev() {
if self.params[layer].get(name).is_some() {
return Some(layer);
}
}
None
}

fn call_speial(&mut self, name: &str) -> Option<String> {
let num = self.params.len();
for layer in (0..num).rev() {
Expand Down
2 changes: 2 additions & 0 deletions src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ impl SimpleCommand {
fn exec_set_param(&mut self, core: &mut ShellCore) -> Option<Pid> {
core.db.set_param("_", "");
self.option_x_output(core);

self.substitutions.iter_mut()
.for_each(|s| {s.eval(core, 0, false);});

None
}

Expand Down
8 changes: 8 additions & 0 deletions src/elements/substitution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ impl Substitution {
}
};

let layer = match layer {
0 => match core.db.get_layer_pos(&self.name) {
Some(n) => n,
None => 0,
},
n => n,
};

match env {
false => self.set_to_shell(core, layer),
true => self.set_to_env(),
Expand Down
3 changes: 2 additions & 1 deletion test/ok
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
./test_script.bash
./test_options.bash
./test_glob.bash
./test_builtins.bash
./test_brace.bash
./test_builtins.bash
./test_parameters.bash
./test_compound.bash
./test_others.bash
./test_job.bash
../test/test_compound.bash
4 changes: 4 additions & 0 deletions test/test_compound.bash
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ b" ] || err $LINENO
res=$($com <<< 'function f () { local A=BBB ; echo $A; } ; f ; echo $A')
[ "$res" = BBB ] || err $LINENO

res=$($com <<< 'A=3 ; function f () { local A ; A=BBB ; echo $A; } ; f ; echo $A')
[ "$res" = "BBB
3" ] || err $LINENO

res=$($com <<< 'function f () { local A=BBB ; echo $A ; } ; echo $A')
[ "$res" = "" ] || err $LINENO

Expand Down

0 comments on commit 31a2bd8

Please sign in to comment.