Skip to content

Commit

Permalink
Merge pull request #14 from coco33920/add_functions
Browse files Browse the repository at this point in the history
Add functions
  • Loading branch information
vanilla-extracts authored Nov 14, 2023
2 parents 6428da9 + 52ce053 commit 879196f
Show file tree
Hide file tree
Showing 14 changed files with 613 additions and 15 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ cargo install mini-calc
- [X] Config
- [X] Config colours
- [X] Config prompt
- [ ] Add more operations
- [X] Add more operations
- [X] exponent
- [ ] Add support for functions
- [ ] exp
- [ ] ln
- [ ] log base a
- [ ] cos/sin/tan
- [ ] cosh/sinh/tanh
- [X] Add support for functions
- [X] exp
- [X] ln
- [X] log base a
- [X] cos/sin/tan
- [X] cosh/sinh/tanh
- [X] atan/acos/asin
- [ ] For later
- [ ] Defining your own functions
- [ ] Add RPN mode
Expand Down Expand Up @@ -131,4 +132,31 @@ Configuration:

It looks like:

![img.png](docs/assets/config_looks.png)
![img.png](docs/assets/config_looks.png)

## Functions

The following functions are available

- sin
- cos
- tan
- sinh
- cosh
- tanh
- asin
- acos
- atan
- exp
- ln
- log (alias of ln)

### Trigonometry

For trigonometry, the input are assumed to be in radian, if not, you have to put "false" or "true" as second argument, example shown bellow
![img.png](docs/assets/trigo.png)

### Exp/ln

If you use the exp function you can pass a second argument for the base you are using, if no second arguments are passed this is assumed to be in natural base
![img.png](docs/assets/expln.png)
Binary file added docs/assets/expln.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/trigo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/configuration/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn load_color(string: String) -> Color {

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

Expand Down
30 changes: 30 additions & 0 deletions src/interpreting/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap<String, Parameter
(Parameters::Identifier(s), Parameters::Int(i)) => {
apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add)
}
(Parameters::Null, Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add)
}
(Parameters::Identifier(s), Parameters::Null) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add)
}
(Parameters::Int(i), Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add)
}
Expand Down Expand Up @@ -107,6 +113,12 @@ pub fn minus(
(Parameters::Identifier(s), Parameters::Int(i)) => {
apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus)
}
(Parameters::Null, Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus)
}
(Parameters::Identifier(s), Parameters::Null) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus)
}
(Parameters::Int(i), Parameters::Identifier(s)) => {
let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus);
match v {
Expand Down Expand Up @@ -154,6 +166,12 @@ pub fn mult(
(Parameters::Int(i), Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult)
}
(Parameters::Null, Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult)
}
(Parameters::Identifier(s), Parameters::Null) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult)
}
(Parameters::Identifier(s), Parameters::Float(i)) => {
apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult)
}
Expand Down Expand Up @@ -194,6 +212,12 @@ pub fn divide(
_ => Parameters::Null,
}
}
(Parameters::Null, Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide)
}
(Parameters::Identifier(s), Parameters::Null) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide)
}
(Parameters::Identifier(s), Parameters::Float(i)) => {
apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide)
}
Expand Down Expand Up @@ -237,6 +261,12 @@ pub fn expo(
(Parameters::Identifier(s), Parameters::Float(i)) => {
apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, expo)
}
(Parameters::Identifier(s), Parameters::Null) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo)
}
(Parameters::Null, Parameters::Identifier(s)) => {
apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo)
}
(Parameters::Float(i), Parameters::Identifier(s)) => {
apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, expo)
}
Expand Down
6 changes: 6 additions & 0 deletions src/interpreting/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use crate::interpreting::function::{add, assign, divide, expo, minus, mult};
use crate::interpreting::stdlib::exec;
use crate::parsing::ast::{Ast, Parameters};

pub fn interpret(ast: Ast, mut ram: &mut HashMap<String, Parameters>) -> Parameters {
Expand Down Expand Up @@ -29,10 +30,15 @@ pub fn interpret(ast: Ast, mut ram: &mut HashMap<String, Parameters>) -> Paramet
Parameters::Float(f) => Parameters::Float(f),
Parameters::Int(i) => Parameters::Int(i),
Parameters::Identifier(s) => Parameters::Identifier(s),
Parameters::Call(s) => Parameters::Identifier(s),
Parameters::Null => Parameters::Null,
};
last.clone()
}
Ast::Call { name: n, lst: list } => {
let v: Vec<Parameters> = list.iter().map(|x| interpret(x.clone(), ram)).collect();
exec(n, v, Some(&ram))
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/interpreting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod function;
pub(crate) mod interpreter;
mod stdlib;
Loading

0 comments on commit 879196f

Please sign in to comment.