fix(parser::ast_builder: parse_number gcd bug
This commit is contained in:
parent
3500de6cb0
commit
06a998666f
|
@ -32,6 +32,10 @@ impl Rational {
|
||||||
Rational::new(self.denominator, self.numerator)
|
Rational::new(self.denominator, self.numerator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn left_shift(&self) -> Self {
|
||||||
|
Rational::new(self.numerator, self.denominator * 10)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
||||||
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
||||||
res.reduce()
|
res.reduce()
|
||||||
|
|
|
@ -79,20 +79,22 @@ fn get_parentheses_map(tokens: &Vec<Token>) -> Vec<usize> {
|
||||||
fn parse_number(string: &String, sign: i128) -> GaussianRational {
|
fn parse_number(string: &String, sign: i128) -> GaussianRational {
|
||||||
let mut number = rational_zero();
|
let mut number = rational_zero();
|
||||||
let mut is_floating = false;
|
let mut is_floating = false;
|
||||||
|
let mut count = 0;
|
||||||
for c in string.chars() {
|
for c in string.chars() {
|
||||||
match c {
|
match c {
|
||||||
'.' => is_floating = true,
|
'.' => is_floating = true,
|
||||||
'0'..='9' => {
|
'0'..='9' => {
|
||||||
number = 10 * number + String::from(c).parse::<i128>().unwrap();
|
number = (10 * number) + String::from(c).parse::<i128>().unwrap();
|
||||||
if is_floating == true {
|
if is_floating == true {
|
||||||
number = number * Rational::new(1, 10);
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _ in 0..count {
|
||||||
|
number = number.left_shift();
|
||||||
|
}
|
||||||
number = number * sign;
|
number = number * sign;
|
||||||
|
|
||||||
GaussianRational::new(number, rational_zero())
|
GaussianRational::new(number, rational_zero())
|
||||||
|
@ -117,23 +119,15 @@ fn check_parentheses(tokens: &Vec<Token>) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_ast(mut tokens: Vec<Token>) -> Node {
|
pub fn build_ast(mut tokens: Vec<Token>) -> Node {
|
||||||
//let mut tokens_copy = tokens.clone();
|
|
||||||
//let mut not_done = true;
|
|
||||||
|
|
||||||
// Lowest prio : equal
|
|
||||||
let split = tokens.iter().position(|x| x == &Token::Equal());
|
let split = tokens.iter().position(|x| x == &Token::Equal());
|
||||||
if let Some(position) = split {
|
if let Some(position) = split {
|
||||||
return insert_operator_node(tokens, position);
|
return insert_operator_node(tokens, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
while tokens.starts_with(&[Token::OpenParenthesis()]) && tokens.ends_with(&[Token::CloseParenthesis()]) && check_parentheses(&tokens) == true {
|
while tokens.starts_with(&[Token::OpenParenthesis()]) && tokens.ends_with(&[Token::CloseParenthesis()]) && check_parentheses(&tokens) == true {
|
||||||
tokens = tokens.split_off(1);
|
tokens = tokens.split_off(1);
|
||||||
tokens.pop();
|
tokens.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
let parentheses_map = get_parentheses_map(&tokens);
|
let parentheses_map = get_parentheses_map(&tokens);
|
||||||
//let iter = zip(tokens, parentheses_map);
|
|
||||||
// Lowest prio : rightest minus or plus, also manages minus unary operator
|
|
||||||
let split = tokens.iter().enumerate().rposition(|(i, x)| parentheses_map[i] == 0 && (x == &Token::Addition() || x == &Token::Substraction()));
|
let split = tokens.iter().enumerate().rposition(|(i, x)| parentheses_map[i] == 0 && (x == &Token::Addition() || x == &Token::Substraction()));
|
||||||
if let Some(position) = split {
|
if let Some(position) = split {
|
||||||
let tokens_right = tokens.split_off(position + 1);
|
let tokens_right = tokens.split_off(position + 1);
|
||||||
|
@ -152,13 +146,10 @@ pub fn build_ast(mut tokens: Vec<Token>) -> Node {
|
||||||
rhs: Box::new(build_ast(tokens_right)),
|
rhs: Box::new(build_ast(tokens_right)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lowest prio : righest times or divide or modulo
|
|
||||||
let split = tokens.iter().enumerate().rposition(|(i, x)| parentheses_map[i] == 0 && (x == &Token::Multiplication() || x == &Token::Division() || x == &Token::Modulo()));
|
let split = tokens.iter().enumerate().rposition(|(i, x)| parentheses_map[i] == 0 && (x == &Token::Multiplication() || x == &Token::Division() || x == &Token::Modulo()));
|
||||||
if let Some(position) = split {
|
if let Some(position) = split {
|
||||||
return insert_operator_node(tokens, position);
|
return insert_operator_node(tokens, position);
|
||||||
}
|
}
|
||||||
// Lowest prio : exponentiation
|
|
||||||
let split = tokens.iter().enumerate().rposition(|(i, x)| parentheses_map[i] == 0 && x == &Token::Exponentiation());
|
let split = tokens.iter().enumerate().rposition(|(i, x)| parentheses_map[i] == 0 && x == &Token::Exponentiation());
|
||||||
if let Some(position) = split {
|
if let Some(position) = split {
|
||||||
return insert_operator_node(tokens, position);
|
return insert_operator_node(tokens, position);
|
||||||
|
|
Loading…
Reference in New Issue