Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Sep 25, 2024
1 parent ef98102 commit 59127ee
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/elements/expr/arithmetic/error
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
../../../test/test_others.bash
../../../test/test_others.bash
../../../../test/test_others.bash
../../../../test/test_others.bash
6 changes: 5 additions & 1 deletion src/elements/expr/arithmetic/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub fn substitute(op: &str, name: &String, cur: i64, right: i64, core: &mut Shel
}

fn parse_with_base(base: i64, s: &mut String) -> Option<i64> {
if s.len() == 0 {
return None;
}

let mut ans = 0;
for ch in s.chars() {
ans *= base;
Expand Down Expand Up @@ -124,7 +128,7 @@ fn get_base(s: &mut String) -> Option<i64> {
return Some(16);
}

if s.starts_with("0") {
if s.starts_with("0") && s.len() > 1 {
s.remove(0);
return Some(8);
}
Expand Down
60 changes: 42 additions & 18 deletions src/elements/expr/arithmetic/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{ShellCore, Feeder};
use crate::elements::word::Word;
use super::{ArithmeticExpr, ArithElem, int};
use super::{ArithmeticExpr, ArithElem, int, float};

impl ArithmeticExpr {
fn eat_blank(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) {
Expand Down Expand Up @@ -63,11 +63,48 @@ impl ArithmeticExpr {
true
}

fn convert_dobule_quoted_word(word: &mut Word, unquoted: &String,
ans: &mut Self, core: &mut ShellCore) -> bool {
if ! word.text.starts_with("\"") || ! word.text.ends_with("\"") {
return false;
}

let mut f = Feeder::new(unquoted);
let mut arith = match ArithmeticExpr::parse(&mut f, core, false) {
Some(e) => e,
None => return false,
};
if f.len() != 0 {
return false;
}

if arith.elements.len() == 1 {
match &arith.elements[0] {
ArithElem::Word(w, s) => {
*word = w.clone();
return false;
},
_ => {},
}
}

ans.elements.append( &mut arith.elements );
true
}

fn eat_word(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
let mut word = match Word::parse(feeder, core, true) {
Some(w) => w,
_ => return false,
};

if word.text.starts_with("\"")
&& word.text.ends_with("\"")
&& word.text.len() >= 2 {
feeder.replace(0, &word.text[1..word.text.len()-1]);
return true;
}

ans.text += &word.text.clone();

if let Some(w) = word.make_unquoted_word() {
Expand All @@ -76,31 +113,18 @@ impl ArithmeticExpr {
ans.elements.push( ArithElem::Integer(n) );
return true;
}
if let Ok(f) = w.parse::<f64>() {
if let Some(f) = float::parse(&w) {
ans.elements.push( ArithElem::Float(f) );
return true;
}
}
if word.text.starts_with("\"") && word.text.ends_with("\"") {
let mut f = Feeder::new(&w);
if let Some(mut e) = ArithmeticExpr::parse(&mut f, core, false) {
if f.len() == 0 {
if e.elements.len() != 1 {
ans.elements.append( &mut e.elements );
return true;
}

match &e.elements[0] {
ArithElem::Word(w, _) => word = w.clone(),
_ => {},
}
}
}

if Self::convert_dobule_quoted_word(&mut word, &w, ans, core) {
return true;
}
}

Self::eat_blank(feeder, ans, core);

let suffix = Self::eat_suffix(feeder, ans);
ans.elements.push( ArithElem::Word(word, suffix) );
true
Expand Down
3 changes: 3 additions & 0 deletions src/elements/expr/error
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
5 changes: 0 additions & 5 deletions test/error
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
../../../test/test_others.bash
3 changes: 1 addition & 2 deletions test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
./test_compound.bash
./test_others.bash
./test_job.bash
../../../test/test_others.bash
../../../test/test_others.bash
../../../../test/test_others.bash
6 changes: 6 additions & 0 deletions test/test_others.bash
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,12 @@ res=$($com <<< 'A=1; echo $(( "1 + A" * 3 ))')
res=$($com <<< 'echo $(( "1 << 1" ))')
[ "$res" == "2" ] || err $LINENO

res=$($com <<< 'echo $(( "1 +" 1 ))')
[ "$res" == "2" ] || err $LINENO

res=$($com <<< 'echo $(( 1 "+" 1 ))')
[ "$res" == "2" ] || err $LINENO

# escaping

res=$($com <<< "echo a\ \ \ a")
Expand Down

0 comments on commit 59127ee

Please sign in to comment.