diff --git a/Cargo.lock b/Cargo.lock index 8bf9cb2..7dc8e2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,7 @@ dependencies = [ "lazy_static", "libm", "linked_list_allocator", + "log", "pc-keyboard", "pic8259", "rand", @@ -218,6 +219,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + [[package]] name = "maybe-uninit" version = "2.0.0" diff --git a/Cargo.toml b/Cargo.toml index f618f47..d9949de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ rand = { version = "0.8.5", default-features = false, features = ["small_rng"]} hashbrown = "0.13.2" cmos-rtc = "0.1.2" libm = "0.2.7" +log = "0.4.20" [dependencies.lazy_static] version = "1.0" diff --git a/src/user/bin/calc/calc.rs b/src/user/bin/calc/calc.rs index 2899a79..6495e53 100755 --- a/src/user/bin/calc/calc.rs +++ b/src/user/bin/calc/calc.rs @@ -51,7 +51,7 @@ impl Interpreter { let inner = self.visit(self.get_node(node.clone(), "argument")?.expect("returned none").to_owned())?; if let Value::Number(x) = inner { - return Ok(Value::Number(super::functions::run_func(function_name, x).unwrap())); + return Ok(Value::Number(super::functions::run_func(function_name, x).map_err(|x| Error::Other(x.to_string()))?)); } else { return Err(Error::Other(String::from("function argument is not a number"))) } @@ -416,44 +416,25 @@ fn calculate_inner(mut equation: String) -> Result { 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") }; - let return_res = { - if let Value::Number(x) = result { - x - } else { - panic!("did not return a float!"); - } - }; - println!("\n\n - _____ _ _ - / ____| | | | | - | | _ __ _ _ ___| |_ __ _| | - | | | '__| | | / __| __/ _` | | - | |____| | | |_| \\__ \\ || (_| | | - \\_____|_| \\__, |___/\\__\\__,_|_| - _____ __/ | - / ____||___/ | - | | __ _| | ___ - | | / _` | |/ __| - | |___| (_| | | (__ - \\_____\\__,_|_|\\___| + println!(" + + [ EXPRESSION ] + + {} + + [ RESULT ] + + {} - ┌────────────────────────────────────────────┐ - │ │ - │ Expression -> [ {} ] - │ │ - │ Calculated Solution -> [ {} ] - │ │ - └────────────────────────────────────────────┘ ", neweq, return_res); Ok(return_res) @@ -481,6 +462,7 @@ fn tokenise(equation: &str) -> Result, Error> { tokens.push(Token::Func(current_string.clone())); } is_var = false; + current_string = "".to_string(); } } diff --git a/src/user/bin/calc/functions.rs b/src/user/bin/calc/functions.rs index 08f93d0..55f4486 100644 --- a/src/user/bin/calc/functions.rs +++ b/src/user/bin/calc/functions.rs @@ -3,11 +3,13 @@ use alloc::string::String; use crate::println; pub fn run_func(func: String, x: f64) -> Result { - println!("function being run: {}({})", func, x); - match func.as_str() { "sqrt" => sqrt(x), "ln" => ln(x), + "fact" => factorial(x), + "sin" => sin(x), + "cos" => cos(x), + "tan" => tan(x), _ => Err(String::from(format!("unrecognised function name: {}", func))), } } @@ -24,4 +26,24 @@ fn ln(x: f64) -> Result { return Err(String::from("Cannot take the natural log of a negative number")); } Ok(libm::log(x)) +} + +fn factorial(x: f64) -> Result { + if x < 0.0 { + return Err(String::from("Cannot take the factorial of a negative number")); + } + let x = x as u64; + Ok((1..=x).fold(1, |a, b| a * b) as f64) +} + +fn sin(x: f64) -> Result { + Ok(libm::sin(x)) +} + +fn cos(x: f64) -> Result { + Ok(libm::cos(x)) +} + +fn tan(x: f64) -> Result { + Ok(libm::tan(x)) } \ No newline at end of file diff --git a/src/user/bin/crystalfetch.rs b/src/user/bin/crystalfetch.rs index 6ed5cb7..97810bd 100644 --- a/src/user/bin/crystalfetch.rs +++ b/src/user/bin/crystalfetch.rs @@ -1,12 +1,40 @@ use async_trait::async_trait; -use alloc::{boxed::Box, string::String, vec::Vec}; +use alloc::{boxed::Box, format, string::String, vec::Vec}; +use log::info; use crate::{std::os::OS, std::io::{Color, write, clear}, println, std::application::{ Application, Error, }, std}; +const CRYSTAL_LOGO: &str = +" $$$$$$\\ $$\\ $$\\ $$$$$$\\ $$$$$$\\ +$$ __$$\\ $$ | $$ $$ __$$\\$$ __$$\\ +$$ / \\__|$$$$$$\\ $$\\ $$\\ $$$$$$$\\$$$$$$\\ $$$$$$\\ $$ $$ / $$ $$ / \\__| +$$ | $$ __$$\\$$ | $$ $$ _____\\_$$ _| \\____$$\\$$ $$ | $$ \\$$$$$$\\ +$$ | $$ | \\__$$ | $$ \\$$$$$$\\ $$ | $$$$$$$ $$ $$ | $$ |\\____$$\\ +$$ | $$\\$$ | $$ | $$ |\\____$$\\ $$ |$$\\$$ __$$ $$ $$ | $$ $$\\ $$ | +\\$$$$$$ $$ | \\$$$$$$$ $$$$$$$ | \\$$$$ \\$$$$$$$ $$ |$$$$$$ \\$$$$$$ | + \\______/\\__| \\____$$ \\_______/ \\$$$$$$\\_______\\__|\\______/ \\______/ + $$\\ $$ | $$ __$$\\ + \\$$$$$$ | $$\\ $$\\__/ $$ | + \\______/ \\$$\\ $$ $$$$$$ | + \\$$\\$$ $$ ____/ + \\$$$ /$$ | + \\$ / $$$$$$$$\\ + \\_/ \\________| "; +const ZXQ5_LOGO: &str = " + + /$$$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ + |_____ $$ | $$ / $$ /$$__ $$ /$$__ $$ /$$__ $$ /$$$$ + /$$/ | $$/ $$/| $$ \\ $$| $$ \\ $$| $$ \\__/ /$$ /$$|_ $$ + /$$/ \\ $$$$/ | $$ | $$| $$ | $$| $$$$$$ | $$ /$$/ | $$ + /$$/ >$$ $$ | $$ | $$| $$ | $$ \\____ $$ \\ $$/$$/ | $$ + /$$/ /$$/\\ $$| $$/$$ $$| $$ | $$ /$$ \\ $$ \\ $$$/ | $$ + /$$$$$$$$| $$ \\ $$| $$$$$$/| $$$$$$/| $$$$$$/ \\ $/ /$$$$$$ + |________/|__/ |__/ \\____ $$$ \\______/ \\______/ \\_/ |______/ +"; pub struct CrystalFetch {} #[async_trait] @@ -23,29 +51,23 @@ impl Application for CrystalFetch { clear(); - write(format_args!(" - /$$$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ - |_____ $$ | $$ / $$ /$$__ $$ /$$__ $$ /$$__ $$ /$$$$ - /$$/ | $$/ $$/| $$ \\ $$| $$ \\ $$| $$ \\__/ /$$ /$$|_ $$ - /$$/ \\ $$$$/ | $$ | $$| $$ | $$| $$$$$$ | $$ /$$/ | $$ - /$$/ >$$ $$ | $$ | $$| $$ | $$ \\____ $$ \\ $$/$$/ | $$ - /$$/ /$$/\\ $$| $$/$$ $$| $$ | $$ /$$ \\ $$ \\ $$$/ | $$ - /$$$$$$$$| $$ \\ $$| $$$$$$/| $$$$$$/| $$$$$$/ \\ $/ /$$$$$$ - |________/|__/ |__/ \\____ $$$ \\______/ \\______/ \\_/ |______/ -"), (Color::Cyan, Color::Black)); - println!(" - - - [ OS » {} + let logo_string = CRYSTAL_LOGO; + let info_string = format!( +" [ OS » {} [ BUILD » {} [ Shell » CrySH [ Github » https://github.com/FantasyPvP/CrystalOS-Restructured - [ Author » FantasyPvP / ZXQ5 + [ Author » FantasyPvP / ZXQ5", os, version); + + let spacer = "\n".repeat(24 - logo_string.lines().count() - 4 - info_string.lines().count()); + + // write values to console + write(format_args!("{}", logo_string), (Color::Cyan, Color::Black)); + println!("\n\n"); + println!("{}", info_string); + println!("{}", spacer); - - -", os, version); Ok(()) } diff --git a/src/user/bin/shell.rs b/src/user/bin/shell.rs index 19e87d7..a7a7c93 100644 --- a/src/user/bin/shell.rs +++ b/src/user/bin/shell.rs @@ -14,6 +14,7 @@ use crate::{ std::application::{Application, Error}, user::bin::*, }; +use crate::std::io::{Color, write}; use crate::user::bin::gigachad_detector::GigachadDetector; lazy_static! { @@ -54,7 +55,9 @@ pub async fn eventloop() { } } -fn handle_error(e: Error) {} +fn handle_error(e: Error) { + println!("there was an error! exiting program!"); +} async fn exec() -> Result<(), Error> { let mut current = CMD.lock().current.clone(); @@ -189,11 +192,8 @@ impl CommandHandler { // TODO: coloured prompt pub fn prompt(&self) { - print!("\n [ Crystal ] >> "); + write(format_args!("\n Crystal> "), (Color::Cyan, Color::Black)); } - - // this function is run every time the enter key is pressed in the command line mode. - // it detects the command that is being run and then executes it, passing the arguments to it. } struct CmdHistory {