Skip to content

Commit

Permalink
Merge branch 'dev-test-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Sep 15, 2024
2 parents 493eacb + 82aa17e commit 8ea0e45
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 66 deletions.
14 changes: 12 additions & 2 deletions src/elements/arithmetic_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use super::word::Word;
pub struct ArithmeticExpr {
pub text: String,
elements: Vec<Elem>,
paren_stack: Vec<char>,
output_base: String,
hide_base: bool,
}
Expand Down Expand Up @@ -47,6 +46,18 @@ impl ArithmeticExpr {
ans
}

pub fn eval_elems(&mut self, core: &mut ShellCore) -> Result<Elem, String> {
if self.elements.len() == 0 {
return Err("operand expexted (error token: \")\")".to_string());
}
let es = match self.decompose_increments() {
Ok(data) => data,
Err(err_msg) => return Err(err_msg),
};

calculate(&es, core)
}

fn ans_to_string(&self, n: i64) -> Option<String> {
let base_str = self.output_base.clone();

Expand Down Expand Up @@ -168,7 +179,6 @@ impl ArithmeticExpr {
ArithmeticExpr {
text: String::new(),
elements: vec![],
paren_stack: vec![],
output_base: "10".to_string(),
hide_base: false,
}
Expand Down
27 changes: 8 additions & 19 deletions src/elements/arithmetic_expr/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ use super::elem::Elem;
use super::{elem, float, int, rev_polish, trenary, word};

pub fn pop_operand(stack: &mut Vec<Elem>, core: &mut ShellCore) -> Result<Elem, String> {
let n = match stack.pop() {
Some(Elem::Word(w, inc)) => {
match word::to_operand(&w, 0, inc, core) {
Ok(op) => op,
Err(e) => return Err(e),
}
},
Some(elem) => elem,
None => return Err("no operand".to_string()),
};
Ok(n)
match stack.pop() {
Some(Elem::Word(w, inc)) => word::to_operand(&w, 0, inc, core),
Some(Elem::InParen(mut a)) => a.eval_elems(core),
Some(elem) => Ok(elem),
None => Err("no operand".to_string()),
}
}

fn bin_operation(op: &str, stack: &mut Vec<Elem>, core: &mut ShellCore) -> Result<(), String> {
Expand Down Expand Up @@ -79,15 +74,14 @@ pub fn calculate(elements: &Vec<Elem>, core: &mut ShellCore) -> Result<Elem, Str

for e in rev_pol {
let result = match e {
Elem::Integer(_) | Elem::Float(_) | Elem::Word(_, _) => {
Elem::Integer(_) | Elem::Float(_) | Elem::Word(_, _) | Elem::InParen(_) => {
stack.push(e.clone());
Ok(())
},
Elem::BinaryOp(ref op) => bin_operation(&op, &mut stack, core),
Elem::UnaryOp(ref op) => unary_operation(&op, &mut stack, core),
Elem::Increment(n) => inc(n, &mut stack, core),
Elem::Ternary(left, right) => trenary::operation(&left, &right, &mut stack, core),
_ => Err( error_message::syntax(&elem::to_string(&e)) ),
};

if let Err(err_msg) = result {
Expand All @@ -98,12 +92,7 @@ pub fn calculate(elements: &Vec<Elem>, core: &mut ShellCore) -> Result<Elem, Str
if stack.len() != 1 {
return Err( format!("unknown syntax error_message (stack inconsistency)",) );
}

match stack.pop() {
Some(Elem::Word(w, inc)) => word::to_operand(&w, 0, inc, core),
Some(elem) => Ok(elem),
None => Err( format!("unknown syntax error_message",) ),
}
pop_operand(&mut stack, core)
}

fn inc(inc: i64, stack: &mut Vec<Elem>, core: &mut ShellCore) -> Result<(), String> {
Expand Down
6 changes: 2 additions & 4 deletions src/elements/arithmetic_expr/elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pub enum Elem {
Float(f64),
Ternary(Box<Option<ArithmeticExpr>>, Box<Option<ArithmeticExpr>>),
Word(Word, i64), // Word + post increment or decrement
LeftParen,
RightParen,
InParen(ArithmeticExpr),
Increment(i64), //pre increment
}

Expand Down Expand Up @@ -50,6 +49,7 @@ pub fn op_order(op: &Elem) -> u8 {

pub fn to_string(op: &Elem) -> String {
match op {
Elem::InParen(a) => a.text.to_string(),
Elem::Integer(n) => n.to_string(),
Elem::Float(f) => f.to_string(),
Elem::Word(w, inc) => {
Expand All @@ -61,8 +61,6 @@ pub fn to_string(op: &Elem) -> String {
},
Elem::UnaryOp(s) => s.clone(),
Elem::BinaryOp(s) => s.clone(),
Elem::LeftParen => "(".to_string(),
Elem::RightParen => ")".to_string(),
Elem::Increment(1) => "++".to_string(),
Elem::Increment(-1) => "--".to_string(),
_ => "".to_string(),
Expand Down
31 changes: 15 additions & 16 deletions src/elements/arithmetic_expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ArithmeticExpr {
Some(Elem::Integer(_))
| Some(Elem::Float(_))
| Some(Elem::Word(_, _))
| Some(Elem::RightParen) => return false,
| Some(Elem::InParen(_)) => return false,
_ => {},
}

Expand All @@ -124,24 +124,23 @@ impl ArithmeticExpr {
true
}

fn eat_paren(feeder: &mut Feeder, ans: &mut Self) -> bool {
if feeder.starts_with("(") {
ans.paren_stack.push( '(' );
ans.elements.push( Elem::LeftParen );
ans.text += &feeder.consume(1);
return true;
fn eat_paren(feeder: &mut Feeder, core: &mut ShellCore, ans: &mut Self) -> bool {
if ! feeder.starts_with("(") {
return false;
}

if feeder.starts_with(")") {
if let Some('(') = ans.paren_stack.last() {
ans.paren_stack.pop();
ans.elements.push( Elem::RightParen );
ans.text += &feeder.consume(1);
return true;
}
ans.text += &feeder.consume(1);

let arith = Self::parse(feeder, core);
if arith.is_none() || ! feeder.starts_with(")") {
return false;
}

false
ans.text += &arith.as_ref().unwrap().text;
ans.elements.push( Elem::InParen(arith.unwrap()) );

ans.text += &feeder.consume(1);
return true;
}

fn eat_binary_operator(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
Expand Down Expand Up @@ -170,7 +169,7 @@ impl ArithmeticExpr {
|| Self::eat_conditional_op(feeder, &mut ans, core)
|| Self::eat_incdec(feeder, &mut ans)
|| Self::eat_unary_operator(feeder, &mut ans, core)
|| Self::eat_paren(feeder, &mut ans)
|| Self::eat_paren(feeder, core, &mut ans)
|| Self::eat_binary_operator(feeder, &mut ans, core)
|| Self::eat_word(feeder, &mut ans, core) {
continue;
Expand Down
27 changes: 2 additions & 25 deletions src/elements/arithmetic_expr/rev_polish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@ use super::elem::Elem;
pub fn rearrange(elements: &[Elem]) -> Result<Vec<Elem>, Elem> {
let mut ans = vec![];
let mut stack = vec![];
let mut last = None;

for e in elements {
let ok = match e {
Elem::Float(_) | Elem::Integer(_) | Elem::Word(_, _)
Elem::Float(_) | Elem::Integer(_) | Elem::Word(_, _) | Elem::InParen(_)
=> {ans.push(e.clone()); true},
Elem::LeftParen => {stack.push(e.clone()); true},
Elem::RightParen => rev_polish_paren(&mut stack, &mut ans),
op => rev_polish_op(&op, &mut stack, &mut ans),
};

if !ok {
return Err(e.clone());
}

match (last, e) {
( Some(Elem::LeftParen), Elem::RightParen ) => return Err(e.clone()),
_ => {},
}

last = Some(e.clone());
}

while stack.len() > 0 {
Expand All @@ -37,24 +27,11 @@ pub fn rearrange(elements: &[Elem]) -> Result<Vec<Elem>, Elem> {
Ok(ans)
}

fn rev_polish_paren(stack: &mut Vec<Elem>, ans: &mut Vec<Elem>) -> bool {
loop {
match stack.last() {
None => return false,
Some(Elem::LeftParen) => {
stack.pop();
return true;
},
Some(_) => ans.push(stack.pop().unwrap()),
}
}
}

fn rev_polish_op(elem: &Elem,
stack: &mut Vec<Elem>, ans: &mut Vec<Elem>) -> bool {
loop {
match stack.last() {
None | Some(Elem::LeftParen) => {
None => {
stack.push(elem.clone());
break;
},
Expand Down

0 comments on commit 8ea0e45

Please sign in to comment.