made a graphing calculator

idk why
This commit is contained in:
FantasyPvP
2023-10-03 00:43:20 +01:00
parent 410278b6e3
commit e92e01c5b8
13 changed files with 367 additions and 29 deletions
+21 -1
View File
@@ -403,15 +403,18 @@ impl Application for Calculator {
return Err(ShellError::CommandFailed(String::from("failed")))
},
};
Ok(())
}
}
}
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();
@@ -439,6 +442,23 @@ fn calculate_inner(mut equation: String) -> Result<f64, Error> {
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)
}
+74
View File
@@ -0,0 +1,74 @@
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use alloc::boxed::Box;
use async_trait::async_trait;
use crate::println;
use crate::shell::command_handler;
use crate::std::application::{Application, Error};
use crate::std::frame::Element;
use crate::std::io::{Screen, Stdin};
pub struct Grapher {
points: Vec<Point>,
}
struct Point {
x: i16,
y: i16,
}
#[async_trait]
impl Application for Grapher {
fn new() -> Self {
Self {
points: Vec::new(),
}
}
async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
let mut equation: String = args.into_iter().collect();
use super::calc;
for x in -40..40 {
let new_eq = equation.chars().map(|c| {
if c == 'x' { x.to_string() } else { c.to_string() }
}).collect::<String>();
let y = calc::calc_outer(new_eq).map_err(|_| Error::ApplicationError(String::from("failed to calculate")))?;
self.points.push(Point {
x,
y: y as i16,
})
};
Screen::application_mode();
self.render();
loop {
match Stdin::keystroke().await {
'x' => break,
_ => continue,
}
}
Screen::terminal_mode();
Ok(())
}
}
impl Grapher {
fn render(&self) {
let mut frame: Vec<Vec<char>> = vec![vec![' '; 80]; 25];
self.points.iter().filter(|i| i.x >= -40 && i.x < 40 && i.y >= -12 && i.y <= 12).for_each(|i| {
let offset_x = i.x + 40;
let offset_y = i.y + 12;
//println!("{} {}", i.x, i.y);
frame[24-offset_y as usize][offset_x as usize] = '*';
});
let mut elem = Element::generate(frame, (80, 25));
elem.render((0, 0));
}
}
+1
View File
@@ -8,3 +8,4 @@ pub mod tasks;
mod gigachad_detector;
//mod shellrewrite;
mod snake;
mod grapher;
+15
View File
@@ -8,6 +8,7 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
use vga::writers::{GraphicsWriter, PrimitiveDrawing};
use crate::{
print, println,
@@ -16,6 +17,7 @@ use crate::{
};
use crate::std::io::{Color, write, Screen};
use crate::user::bin::gigachad_detector::GigachadDetector;
use crate::user::bin::grapher::Grapher;
lazy_static! {
pub static ref CMD: Mutex<CommandHandler> = Mutex::new(CommandHandler::new());
@@ -97,6 +99,19 @@ async fn exec() -> Result<(), Error> {
let mut gameloop = crystal_rpg::init::GameLoop::new();
gameloop.run(args).await?;
}
"VGA" => {
use vga::colors::Color16;
use vga::writers::{GraphicsWriter, Graphics640x480x16};
let mode = Graphics640x480x16::new();
mode.set_mode();
mode.clear_screen(Color16::Black);
mode.draw_line((80, 60), (80, 420), Color16::Cyan);
}
"graph" => {
let mut grapher = Grapher::new();
grapher.run(args).await?;
}
// direct OS functions (not applications)
"echo" => {