Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 30, 2024
1 parent 303908a commit b944d8f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/elements/expr/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl ArithmeticExpr {
None => return None,
}
},
_ => ans += &elem::to_string(e),
_ => ans += &e.to_string(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/elements/expr/arithmetic/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::ShellCore;
use crate::utils::{error, exit};
use super::elem::ArithElem;
use super::{elem, float, int, rev_polish, trenary, word, array_elem};
use super::{float, int, rev_polish, trenary, word, array_elem};

pub fn pop_operand(stack: &mut Vec<ArithElem>, core: &mut ShellCore) -> Result<ArithElem, String> {
match stack.pop() {
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn calculate(elements: &Vec<ArithElem>, core: &mut ShellCore) -> Result<Arit

let rev_pol = match rev_polish::rearrange(elements) {
Ok(ans) => ans,
Err(e) => return Err( error::syntax(&elem::to_string(&e)) ),
Err(e) => return Err( error::syntax(&e.to_string()) ),
};

let mut stack = vec![];
Expand Down
80 changes: 52 additions & 28 deletions src/elements/expr/arithmetic/elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,60 @@ pub enum ArithElem {
Delimiter(String), //delimiter dividing left and right of &&, ||, and ','
}

pub fn op_order(op: &ArithElem) -> u8 {
match op {
ArithElem::Increment(_) => 20,
ArithElem::UnaryOp(s) => {
match s.as_str() {
"-" | "+" => 19,
_ => 18,
}
},
ArithElem::BinaryOp(s) => {
match s.as_str() {
"**" => 17,
"*" | "/" | "%" => 16,
"+" | "-" => 15,
"<<" | ">>" => 14,
"<=" | ">=" | ">" | "<" => 13,
"==" | "!=" => 12,
"&" => 11,
"^" => 10,
"|" => 9,
"&&" => 8,
"||" => 7,
"," => 0,
_ => 2, //substitution
}
},
ArithElem::Ternary(_, _) => 3,
_ => 1,
impl ArithElem {
pub fn order(&self) -> u8 {
match self {
ArithElem::Increment(_) => 20,
ArithElem::UnaryOp(s) => {
match s.as_str() {
"-" | "+" => 19,
_ => 18,
}
},
ArithElem::BinaryOp(s) => {
match s.as_str() {
"**" => 17,
"*" | "/" | "%" => 16,
"+" | "-" => 15,
"<<" | ">>" => 14,
"<=" | ">=" | ">" | "<" => 13,
"==" | "!=" => 12,
"&" => 11,
"^" => 10,
"|" => 9,
"&&" => 8,
"||" => 7,
"," => 0,
_ => 2, //substitution
}
},
ArithElem::Ternary(_, _) => 3,
_ => 1,
}
}

pub fn to_string(&self) -> String {
match self {
ArithElem::InParen(a) => a.text.to_string(),
ArithElem::Integer(n) => n.to_string(),
ArithElem::Float(f) => f.to_string(),
ArithElem::Word(w, inc) => {
match inc {
1 => w.text.clone() + "++",
-1 => w.text.clone() + "--",
_ => w.text.clone(),
}
},
ArithElem::UnaryOp(s) => s.clone(),
ArithElem::BinaryOp(s) => s.clone(),
ArithElem::Increment(1) => "++".to_string(),
ArithElem::Increment(-1) => "--".to_string(),
_ => "".to_string(),
}
}
}

/*
pub fn to_string(op: &ArithElem) -> String {
match op {
ArithElem::InParen(a) => a.text.to_string(),
Expand All @@ -69,3 +92,4 @@ pub fn to_string(op: &ArithElem) -> String {
_ => "".to_string(),
}
}
*/
6 changes: 3 additions & 3 deletions src/elements/expr/arithmetic/rev_polish.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com
//SPDX-License-Identifier: BSD-3-Clause

use super::elem;
use super::elem::ArithElem;

pub fn rearrange(elements: &[ArithElem]) -> Result<Vec<ArithElem>, ArithElem> {
Expand All @@ -14,7 +13,7 @@ pub fn rearrange(elements: &[ArithElem]) -> Result<Vec<ArithElem>, ArithElem> {
"&&" | "||" => {
while stack.len() > 0 {
let pre_op = stack.pop().unwrap().clone();
match elem::op_order(&pre_op) > elem::op_order(e) {
match pre_op.order() > e.order() {
true => ans.push(pre_op),
false => {stack.push(pre_op); break},
}
Expand Down Expand Up @@ -54,7 +53,8 @@ fn rev_polish_op(elem: &ArithElem,
},
Some(_) => {
let last = stack.last().unwrap();
if elem::op_order(last) <= elem::op_order(elem) {
//if elem::op_order(last) <= elem::op_order(elem) {
if last.order() <= elem.order() {
stack.push(elem.clone());
break;
}
Expand Down
2 changes: 1 addition & 1 deletion test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
./test_builtins.bash
./test_others.bash
./test_calculation.bash
./test_compound.bash
./test_parameters.bash
./test_compound.bash
./test_job.bash

0 comments on commit b944d8f

Please sign in to comment.