-
Notifications
You must be signed in to change notification settings - Fork 0
Skeloton #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Skeloton #16
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| use std::fmt::{Display, Formatter}; | ||
| #![feature(map_first_last)] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can also remove that |
||
|
|
||
| use bigdecimal::BigDecimal; | ||
| use bigdecimal::One; | ||
| use bigdecimal::Zero; | ||
|
|
||
| use std::collections::BTreeMap; | ||
| use std::fmt::{Display, Formatter}; | ||
| use std::ops::*; | ||
| //use bigint::{BigInt,Sign,BigUInt}; | ||
| pub mod parser; | ||
|
|
||
| #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)] | ||
|
|
@@ -34,15 +40,262 @@ pub enum SyntaxTree { | |
| } | ||
|
|
||
| // TODO check whether necessary | ||
| // pub enum Values { | ||
| // Variable(String), | ||
| // Number(BigDecimal), | ||
| // Sum(Vec<Values>), | ||
| // Product(Vec<Values>), | ||
| // Exponent(Vec<Values>), | ||
| // AddInv(Box<Values>), | ||
| // MulInv(Box<Values>), | ||
| // } | ||
|
|
||
| #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] | ||
| pub enum Values { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possible introduce a reference to LocalizedSyntaxNode/Values and indicating how it got introduced |
||
| Number(BigDecimal), | ||
| Variable(String), | ||
| Sum(BigDecimal, BTreeMap<Values, BigDecimal>), | ||
| Product(BigDecimal, BTreeMap<Values, BigDecimal>), | ||
| Exponent(Box<(Values, Values)>), | ||
| } | ||
|
|
||
| impl From<LocalizedSyntaxNode> for Values { | ||
| fn from(node: LocalizedSyntaxNode) -> Values { | ||
| match node.tree { | ||
| SyntaxTree::Variable(value) => Values::Variable(value), | ||
| SyntaxTree::Number(value) => Values::Number(value), | ||
| SyntaxTree::Sum(box left, box right) => Values::from(left).add(right.into()), | ||
| SyntaxTree::Product(box left, box right) => Values::from(left).mul(right.into()), | ||
| SyntaxTree::Exponent(box left, box right) => Values::from(left).exp(right.into()), | ||
| SyntaxTree::Subtraction(box left, box right) => Values::from(left) | ||
| .add(Values::from(right)) | ||
| .mul(Values::Number(BigDecimal::from(-1))), | ||
| SyntaxTree::Division(box left, box right) => Values::from(left) | ||
| .mul(Values::from(right).exp(Values::Number(BigDecimal::from(-1)))), | ||
| SyntaxTree::Negation(box value) => { | ||
| Values::from(value).mul(Values::Number(BigDecimal::from(-1))) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for Values { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs a biit of rework. 2x will displayed as 0 + 2x |
||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| Values::Variable(value) => write!(f, "{}", value), | ||
| Values::Number(value) => write!(f, "{}", value), | ||
| Values::Sum(constant, vmap) => { | ||
| write!(f, "( {constant} ").unwrap(); | ||
| for (k, v) in vmap { | ||
| write!(f, "+ {v} * {k} ").unwrap(); | ||
| } | ||
| write!(f, ")") | ||
| } | ||
| Values::Product(constant, vmap) => { | ||
| write!(f, "( {constant} ").unwrap(); | ||
| for (k, v) in vmap { | ||
| write!(f, "* {k} ^ {v} ").unwrap(); | ||
| } | ||
| write!(f, ")") | ||
| } | ||
| Values::Exponent(box (left, right)) => write!(f, "({} ^ {})", left, right), | ||
| } | ||
| } | ||
| } | ||
| //const one : BigDecimal = BigDecimal::from(1); | ||
|
|
||
| //const two : BigDecimal = BigDecimal::from(2); | ||
|
|
||
| fn exponantiate(mut x: BigDecimal, mut y: BigDecimal) -> BigDecimal { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need figure smthng out for rational powers |
||
| if (&y).is_integer() { | ||
| if (&y).rem(BigDecimal::from(2)) == BigDecimal::zero() { | ||
| let n = y.half(); | ||
|
|
||
| while (&y) > &n { | ||
| x = x.square(); | ||
| y -= BigDecimal::one(); | ||
| } | ||
| x | ||
| } else { | ||
| let n = (y.clone() - BigDecimal::one()).half(); | ||
| let m = x.clone(); | ||
| while (&y) > &n { | ||
| x = x.square(); | ||
| y -= BigDecimal::one(); | ||
| } | ||
| x * m | ||
| } | ||
| } else { | ||
| panic!("not implemented yet") | ||
| } | ||
| } | ||
|
|
||
| impl Values { | ||
| // fn entry_adder( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should come back. i had a brain fart |
||
| // mut map: BTreeMap<Values, BigDecimal>, | ||
| // pair: (&Values, &BigDecimal), | ||
| // ) -> BTreeMap<Values, BigDecimal> { | ||
| // let (key, value) = pair; | ||
| // map.entry(key.to_owned()) | ||
| // .and_modify(|x| *x += value) | ||
| // .or_insert(value.to_owned()); | ||
| // map | ||
| // } | ||
|
|
||
| fn entry_adder<F>( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is unnecessarily general. i had a brain fart. commented out one should work |
||
| mut map: BTreeMap<Values, BigDecimal>, | ||
| pair: (&Values, &BigDecimal), | ||
| fun: F, | ||
| ) -> BTreeMap<Values, BigDecimal> | ||
| where | ||
| F: Fn(&mut BigDecimal, BigDecimal), | ||
| { | ||
| let (key, value) = pair; | ||
| map.entry(key.to_owned()) | ||
| .and_modify(|x| fun(x, value.to_owned())) | ||
| .or_insert(value.to_owned()); | ||
| map | ||
| } | ||
|
|
||
| fn const_folder<F>( | ||
|
||
| mut map: BTreeMap<Values, BigDecimal>, | ||
| value: BigDecimal, | ||
| fun: F, | ||
| ) -> BTreeMap<Values, BigDecimal> | ||
| where | ||
| F: Fn(BigDecimal, BigDecimal) -> BigDecimal, | ||
| { | ||
| match map.pop_first().unwrap() { | ||
| (Values::Number(v1), v2) => map.insert(Values::Number(fun(v1, value)), v2), | ||
| (v1, v2) => map.insert(v1, v2), | ||
| }; | ||
| map | ||
| } | ||
|
|
||
| fn add(self, other: Values) -> Values { | ||
| let zero: BigDecimal = BigDecimal::from(0); | ||
| let one: BigDecimal = BigDecimal::from(1); | ||
| let two: BigDecimal = BigDecimal::from(2); | ||
| match (self, other) { | ||
| (Values::Number(x), other) | (other, Values::Number(x)) if x == zero => other, | ||
| (Values::Number(x), Values::Number(y)) => Values::Number(x + y), | ||
| (Values::Sum(cx, x), Values::Number(y)) | (Values::Number(y), Values::Sum(cx, x)) => { | ||
| Values::Sum(cx + y, x) | ||
| } | ||
|
|
||
| (Values::Sum(cx, x), Values::Sum(cy, y)) => Values::Sum( | ||
| cx + cy, | ||
| x.iter().chain(y.iter()).fold(BTreeMap::new(), |a, b| { | ||
| Self::entry_adder(a, b, BigDecimal::add_assign) | ||
| }), | ||
| ), | ||
| (Values::Sum(cx, x), Values::Product(cy, y)) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this arm needs rework |
||
| | (Values::Product(cy, y), Values::Sum(cx, x)) => Values::Sum( | ||
| cx, | ||
| Self::entry_adder( | ||
| x, | ||
| (&Values::Product(BigDecimal::one(), y), &cy), | ||
| BigDecimal::add_assign, | ||
| ), | ||
| ), | ||
|
|
||
| (Values::Sum(cx, x), other) | (other, Values::Sum(cx, x)) => Values::Sum( | ||
| cx, | ||
| Self::entry_adder(x, (&other, &one), BigDecimal::add_assign), | ||
| ), | ||
|
|
||
| (otherleft, otherright) if otherleft == otherright => { | ||
| Values::Sum(BigDecimal::zero(), BTreeMap::from([(otherleft, two)])) | ||
| } | ||
| (otherleft, otherright) => Values::Sum( | ||
| BigDecimal::zero(), | ||
| BTreeMap::from([(otherleft, one.clone()), (otherright, one)]), | ||
| ), | ||
| } | ||
| } | ||
| fn mul(self, other: Values) -> Values { | ||
| let zero: BigDecimal = BigDecimal::from(0); | ||
| let one: BigDecimal = BigDecimal::from(1); | ||
| let two: BigDecimal = BigDecimal::from(2); | ||
| //todo!(); | ||
| match (self, other) { | ||
| (Values::Number(x), _other) | (_other, Values::Number(x)) if x == zero => { | ||
| Values::Number(zero) | ||
| } | ||
| (Values::Number(x), other) | (other, Values::Number(x)) if x == one => other, | ||
| (Values::Number(x), Values::Product(xz, z)) | ||
| | (Values::Product(xz, z), Values::Number(x)) => Values::Product(xz * x, z), | ||
| (Values::Number(x), Values::Number(y)) => Values::Number(x * y), | ||
| (Values::Number(x), other) | (other, Values::Number(x)) => { | ||
| Values::Product(x, BTreeMap::from([(other, one.clone())])) | ||
| } | ||
|
|
||
| (Values::Exponent(box (a, b)), Values::Exponent(box (c, d))) => { | ||
| if a == c { | ||
| a.exp(b.add(d)) | ||
| } else if b == d { | ||
| a.mul(c).exp(d) | ||
| } else { | ||
| Values::Product( | ||
| BigDecimal::one(), | ||
| BTreeMap::from([(a.exp(b), one.clone()), (c.exp(d), one)]), | ||
| ) | ||
| } | ||
| } | ||
| (z, Values::Exponent(box (x, y))) | (Values::Exponent(box (x, y)), z) => { | ||
| if x == z { | ||
| x.exp(y.add(Values::Number(one))) | ||
| } else { | ||
| Values::Product( | ||
| BigDecimal::one(), | ||
| BTreeMap::from([(z, one.clone()), (x.exp(y), one)]), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| (Values::Sum(cx, x), Values::Sum(cy, y)) => y | ||
| .iter() | ||
| .flat_map(|(y_val, y_key)| { | ||
| x.iter().map(move |(x_val, x_key)| { | ||
| x_val | ||
| .clone() | ||
| .mul(y_val.clone()) | ||
| .mul(Values::Number(x_key * y_key.clone())) | ||
| }) | ||
| }) | ||
| .fold(Values::Number(cx * cy), |x: Values, y: Values| x.add(y)), | ||
| (Values::Sum(cx, x), other) | (other, Values::Sum(cx, x)) => { | ||
| let mut ret = Values::Number(cx); | ||
| for (k, v) in x { | ||
| ret = ret.add(k.mul(other.clone()).mul(Values::Number(v))); | ||
| } | ||
| ret | ||
| } | ||
| (Values::Product(cx, x), Values::Product(cy, y)) => Values::Product( | ||
| cx * cy, | ||
| x.iter().chain(y.iter()).fold(BTreeMap::new(), |a, b| { | ||
| Self::entry_adder(a, b, BigDecimal::mul_assign) | ||
| }), | ||
| ), | ||
| (Values::Product(cx, x), other) | (other, Values::Product(cx, x)) => Values::Product( | ||
| cx, | ||
| Self::entry_adder(x, (&other, &one), BigDecimal::mul_assign), | ||
| ), | ||
|
|
||
| (otherleft, otherright) => { | ||
| if otherleft == otherright { | ||
| Values::Product(BigDecimal::one(), BTreeMap::from([(otherleft, two)])) | ||
| } else { | ||
| Values::Product( | ||
| BigDecimal::one(), | ||
| BTreeMap::from([(otherleft, one.clone()), (otherright, one)]), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| fn exp(self, other: Values) -> Values { | ||
| match (self, other) { | ||
| (Values::Number(x), _) if x == BigDecimal::zero() => Values::Number(BigDecimal::zero()), | ||
| (_, Values::Number(x)) if x == BigDecimal::zero() => Values::Number(BigDecimal::one()), | ||
| (Values::Number(x), _) if x == BigDecimal::one() => Values::Number(BigDecimal::one()), | ||
| (other, Values::Number(x)) if x == BigDecimal::one() => other, | ||
| (Values::Number(x), Values::Number(y)) => Values::Number(exponantiate(x, y)), | ||
| (otherleft, otherright) => Values::Exponent(Box::new((otherleft, otherright))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub struct LocalizedSyntaxNode { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,7 +109,7 @@ mod atoms { | |
| use crate::ast::parser::parse; | ||
| use crate::ast::parser::tests::number; | ||
| use crate::ast::Localization; | ||
|
|
||
| #[test] | ||
| fn integer() { | ||
| let result = parse("123;".to_string()); | ||
|
|
@@ -169,7 +169,8 @@ mod add { | |
| use crate::ast::parser::parse; | ||
| use crate::ast::parser::tests::number; | ||
| use crate::ast::Localization; | ||
|
|
||
| use crate::ast::Values; | ||
| use std::fmt::Write; | ||
| #[test] | ||
| fn add_two_integers() { | ||
| let result = parse("123 + 456".to_string()); | ||
|
|
@@ -179,6 +180,16 @@ mod add { | |
| expected.assert_matches(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_two_variables_and_convert_to_values() { | ||
|
||
| let result : Values = parse("x + x".to_string()).unwrap().pop().unwrap().into(); | ||
|
|
||
| let expected = "( 0 + 2 * x )"; | ||
| let mut result_text = String::new(); | ||
| write!(result_text, "{result}").unwrap(); | ||
| assert_eq!(expected,result_text) | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_three_integers() { | ||
| let result = parse("123 + 456 + 789".to_string()); | ||
|
|
@@ -187,6 +198,15 @@ mod add { | |
|
|
||
| expected.assert_matches(result); | ||
| } | ||
| #[test] | ||
| fn add_two_variables_and_convert_to_values2() { | ||
| let result : Values = parse("(x+y)*(x-y) ".to_string()).unwrap().pop().unwrap().into(); | ||
|
|
||
| let expected = "( 0 + x + x ^ 2 )"; | ||
| let mut result_text = String::new(); | ||
| write!(result_text, "{result}").unwrap(); | ||
| assert_eq!(expected,result_text) | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_with_missing_first_summand() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove that