.
This commit is contained in:
FantasyPvP
2023-09-29 19:25:34 +01:00
parent b3e31727b0
commit ef192302b8
6 changed files with 92 additions and 58 deletions
+14 -32
View File
@@ -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<f64, Error> {
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<Vec<Token>, Error> {
tokens.push(Token::Func(current_string.clone()));
}
is_var = false;
current_string = "".to_string();
}
}
+24 -2
View File
@@ -3,11 +3,13 @@ use alloc::string::String;
use crate::println;
pub fn run_func(func: String, x: f64) -> Result<f64, String> {
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<f64, String> {
return Err(String::from("Cannot take the natural log of a negative number"));
}
Ok(libm::log(x))
}
fn factorial(x: f64) -> Result<f64, String> {
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<f64, String> {
Ok(libm::sin(x))
}
fn cos(x: f64) -> Result<f64, String> {
Ok(libm::cos(x))
}
fn tan(x: f64) -> Result<f64, String> {
Ok(libm::tan(x))
}
+41 -19
View File
@@ -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(())
}
+5 -5
View File
@@ -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 {