changed some calculator stuff idk

idk
This commit is contained in:
FantasyPvP
2023-10-04 00:40:18 +01:00
parent f5cc41c132
commit 4a7cf2a634
8 changed files with 117 additions and 83 deletions
+28 -47
View File
@@ -385,7 +385,7 @@ impl Application for Calculator {
if inp == String::from("exit\n") {
return Ok(());
}
match calculate_inner(inp) {
match self.calculate_and_format(inp) {
Ok(_) => (),
Err(e) => {
println!("your input must be a valid mathematical expression contaning only numbers (including floats) and the operators: [ +, -, *, **, /, //, % ]");
@@ -395,7 +395,7 @@ impl Application for Calculator {
};
}
} else {
match calculate_inner(args.into_iter().collect()) {
match self.calculate_and_format(args.into_iter().collect()) {
Ok(x) => x,
Err(e) => {
println!("your input must be a valid mathematical expression contaning only numbers (including floats) and the operators: [ +, -, *, **, /, //, % ]");
@@ -410,57 +410,38 @@ impl Application for Calculator {
}
}
pub fn calc_outer(mut equation: String) -> Result<f64, String> {
calculate_inner2(equation).map_err(|_| String::from("failed to calculate"))
}
fn calculate_inner(mut equation: String) -> Result<f64, Error> {
equation.push('\n');
let mut neweq = equation.clone();
neweq.pop();
let tokens = tokenise(&equation)?;
let mut parser = Parser::new(tokens)?;
let ast = parser.parse()?;
let mut interpreter = Interpreter::new()?;
let result = interpreter.visit(ast)?;
let return_res = if let Value::Number(x) = result {
x
} else { panic!("the value returned was not a float! THIS IS A BUG") };
println!("
[ EXPRESSION ]
impl Calculator {
pub fn calculate(&self, equation: String) -> Result<f64, String> {
self.calculate_inner(equation).map_err(|_| String::from("failed to calculate"))
}
pub fn calculate_and_format(&self, equation: String) -> Result<f64, String> {
let res = self.calculate_inner(equation.clone()).map_err(|_| String::from("failed to calculate"))?;
println!("
Calculating...
{}
Result:
{}", equation, res);
Ok(res)
}
[ RESULT ]
fn calculate_inner(&self, mut equation: String) -> Result<f64, Error> {
equation.push('\n');
let mut neweq = equation.clone();
neweq.pop();
{}
let tokens = tokenise(&equation)?;
let mut parser = Parser::new(tokens)?;
let ast = parser.parse()?;
let mut interpreter = Interpreter::new()?;
let result = interpreter.visit(ast)?;
let return_res = if let Value::Number(x) = result {
x
} else { panic!("the value returned was not a float! THIS IS A BUG") };
", neweq, return_res);
Ok(return_res)
Ok(return_res)
}
}
fn calculate_inner2(mut equation: String) -> Result<f64, Error> {
equation.push('\n');
let mut neweq = equation.clone();
neweq.pop();
let tokens = tokenise(&equation)?;
let mut parser = Parser::new(tokens)?;
let ast = parser.parse()?;
let mut interpreter = Interpreter::new()?;
let result = interpreter.visit(ast)?;
let return_res = if let Value::Number(x) = result {
x
} else { panic!("the value returned was not a float! THIS IS A BUG") };
Ok(return_res)
}
fn tokenise(equation: &str) -> Result<Vec<Token>, Error> {
let mut tokens = Vec::new();
+3 -2
View File
@@ -39,6 +39,8 @@ impl Application for Grapher {
let mut equation: String = args.into_iter().collect();
use super::calc;
let cal = calc::Calculator::new();
for x in -4000..4000 {
let x = x as f64 / 100.0;
@@ -46,7 +48,7 @@ impl Application for Grapher {
if c == 'x' { format!("({})", x) } else { c.to_string() }
}).collect::<String>();
let fx = calc::calc_outer(new_eq).map_err(|_| Error::ApplicationError(String::from("failed to calculate")));
let fx = cal.calculate(new_eq).map_err(|_| Error::ApplicationError(String::from("failed to calculate")));
if let Ok(y) = fx {
self.render_point(PointF64 {
@@ -54,7 +56,6 @@ impl Application for Grapher {
y,
})
}
};
Screen::application_mode();
+10 -14
View File
@@ -3,11 +3,7 @@ use lazy_static::lazy_static;
use spin::Mutex;
use x86_64::instructions::interrupts;
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use alloc::{boxed::Box, string::{String, ToString}, vec, vec::Vec};
use vga::writers::{GraphicsWriter, PrimitiveDrawing};
use crate::{
@@ -16,6 +12,7 @@ use crate::{
user::bin::*,
};
use crate::std::io::{Color, write, Screen};
use crate::std::random::Random;
use crate::user::bin::gigachad_detector::GigachadDetector;
use crate::user::bin::grapher::Grapher;
@@ -117,6 +114,14 @@ async fn exec() -> Result<(), Error> {
let mut grapher = Grapher::new();
grapher.run(args).await?;
}
"snake" => {
let mut game = snake::Game::new();
game.run(Vec::new()).await;
}
"gigachad?" => {
let mut gigachad_detector = GigachadDetector::new();
gigachad_detector.run(args).await?;
}
// direct OS functions (not applications)
"echo" => {
@@ -130,7 +135,6 @@ async fn exec() -> Result<(), Error> {
.collect::<String>()
)
}
"clear" => {
Screen::clear();
// not sure why this code was here but leaving it in case weird bugs happen so i remember to add it back if so
@@ -145,14 +149,6 @@ async fn exec() -> Result<(), Error> {
"switch" => {
Screen::switch();
}
"snake" => {
let mut game = snake::Game::new();
game.run(Vec::new()).await;
}
"gigachad?" => {
let mut gigachad_detector = GigachadDetector::new();
gigachad_detector.run(args).await?;
}
"time" => {
use crate::std::time::timer;
timer();