Skip to content

Commit

Permalink
Merge pull request #42 from coco33920/symbolic_relex
Browse files Browse the repository at this point in the history
Symbolic relex
  • Loading branch information
coco33920 authored May 19, 2024
2 parents d7bf3c3 + 307875b commit db59d5a
Show file tree
Hide file tree
Showing 23 changed files with 3,479 additions and 1,326 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Rust

on:
push:
branches: [ "mistress" ]
pull_request:
branches: [ "mistress" ]

Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Version 3.0.0 : Symbolic calculation!
Version 3.0.0, at last! (if you've seen the PR for it it's been sitting for more
than three weeks!)

Symbolic computation was added you can compute expressions with symbols (such as
x,y,whatever) and more !

# Version 2.13.2 : Bug fix
Fix #43

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mini-calc"
version = "2.13.2"
version = "3.0.0"
license = "GPL-3.0-or-later"
description = "A fully-featured minimalistic configurable rust calculator"
homepage = "https://calc.nwa2coco.fr"
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ release:
cargo build --release
test:
cargo test
publish:
cargo publish
2 changes: 1 addition & 1 deletion src/configuration/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn load_color(string: String) -> Color {

pub fn replace_variable(str: String) -> String {
str.replace("%author%", "Charlotte Thomas")
.replace("%version%", "v2.13.2")
.replace("%version%", "v3.0.0")
.to_string()
}

Expand Down
1 change: 1 addition & 0 deletions src/exact_math/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod rationals;
pub mod symbolic;
15 changes: 13 additions & 2 deletions src/exact_math/rationals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt::Display, ops};

use crate::utils::integer_utils::gcd;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct Rationals {
pub under: i64,
pub over: i64,
Expand Down Expand Up @@ -32,6 +32,17 @@ impl Rationals {
return self.over == 0;
}

pub fn opposite(self) -> Self {
Rationals::new(self.under, -1 * self.over)
}

pub fn invert(self) -> Result<Rationals, Rationals> {
match self.over {
0 => Err(Rationals::new(0, 1)),
_ => Ok(Rationals::new(self.over, self.under).reduce()),
}
}

pub fn reduce(self) -> Self {
let minus;
let i1;
Expand Down Expand Up @@ -84,7 +95,7 @@ impl Rationals {

impl Display for Rationals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fs = self.clone().reduce();
let fs = self.reduce();
if fs.under == 1 {
write!(f, "{}", fs.over)
} else {
Expand Down
121 changes: 121 additions & 0 deletions src/exact_math/symbolic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use crate::parsing::ast::Parameters;
use crate::parsing::ast::Parameters::*;

pub fn size(p: &Parameters) -> i32 {
match p {
Null
| Int(_)
| Str(_)
| Float(_)
| Identifier(_)
| PlusOperation
| MinusOperation
| MultiplicationOperation
| DivideOperation
| Assign
| ExpoOperation
| GreaterOperation
| GreaterOrEqualOperation
| LesserOperation
| LesserOrEqualOperation
| Equal
| Bool(_)
| Rational(_)
| OrOperation
| AndOperation
| Not
| Vector(_)
| InterpreterVector(_) => 0,
Plus(x, y) => 1 + size(x) + size(y),
Var(x, _, _) => 1 + size(x),
Mul(x, y) => 1 + size(x) + size(y),
Div(x, y) => 1 + size(x) + size(y),
}
}

#[cfg(test)]
mod test {

use crate::{
exact_math::rationals::Rationals,
parsing::ast::{
Ast,
Parameters::{self, *},
},
};

use super::size;
#[test]
fn test_leaf() {
let v = vec![
Null,
Int(1),
Str("".to_string()),
Float(1.0),
Identifier("".to_string()),
PlusOperation,
MinusOperation,
MultiplicationOperation,
DivideOperation,
Assign,
ExpoOperation,
GreaterOperation,
GreaterOrEqualOperation,
LesserOperation,
LesserOrEqualOperation,
Equal,
Bool(false),
Rational(Rationals::new(10, 10)),
OrOperation,
AndOperation,
Not,
Vector(Box::from(vec![Ast::Nil])),
InterpreterVector(Box::from(vec![Null])),
];

let should = vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
];

let _ = v
.into_iter()
.map(|f| size(&f))
.zip(should)
.for_each(|(x, y)| assert_eq!(x, y));
}

#[test]
fn test_size() {
let should = 2;
let tree = Plus(
Box::from(Plus(
Box::from(Parameters::Int(1)),
Box::from(Parameters::Int(2)),
)),
Box::from(Parameters::Int(3)),
);
let result = size(&tree);
assert_eq!(result, should);
}
#[test]
fn test_size_mul() {
let should = 2;
let tree = Mul(
Box::from(Plus(
Box::from(Parameters::Int(1)),
Box::from(Parameters::Int(2)),
)),
Box::from(Parameters::Int(3)),
);
let result = size(&tree);
assert_eq!(result, should);
}
#[test]
fn test_size_var() {
let should = 1;
let tree = Var(Box::from(Parameters::Int(1)), 1, "s".to_string());
let result = size(&tree);
assert_eq!(result, should);
}
}
Loading

0 comments on commit db59d5a

Please sign in to comment.