Skip to content

Commit

Permalink
Merge pull request #37 from coco33920/uninteractive
Browse files Browse the repository at this point in the history
start non interactive use
  • Loading branch information
vanilla-extracts authored Feb 3, 2024
2 parents bad9bee + 6c5362d commit e1216c8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,8 @@ functions added
As of 2.11.3 matrices are pretty printed !

![](docs/assets/matrix_pretty_printed.png)

## Non interactive use
As of 2.12.0 non interactive use was added

![](docs/assets/non_interactive_use.png)
Binary file added docs/assets/non_interactive_use.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 modified manual.pdf
Binary file not shown.
Binary file modified manual/main.pdf
Binary file not shown.
10 changes: 10 additions & 0 deletions manual/main.typ
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,13 @@ As of 2.11.0 rational exact math was added, supports for
image("assets/exact_inverse.png"),
caption: [Example of rational in matrices]
)
#pagebreak()
= Non interactive use

With version 2.12.0 non interactive use was added to the calculator if you have
a quick computation to run and don't want to start the REPL.

#figure(
image("assets/non_interactive_use.png"),
caption: [Example of non interactive use]
)
44 changes: 42 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::env::{self, Args};
use std::f64::consts::{E, PI};
use std::process::exit;
use std::str::SplitWhitespace;
Expand All @@ -14,7 +15,7 @@ use crate::configuration::loader::{
use crate::interpreting::interpreter::interpret;
use crate::lexing::lexer::lex;
use crate::parsing::ast::{Ast, Parameters};
use crate::parsing::parser::CalcParser;
use crate::parsing::parser::{init_calc_parser, CalcParser};

mod configuration;
mod exact_math;
Expand Down Expand Up @@ -256,6 +257,44 @@ fn handle_config(line: &str, config: Config) -> (String, Option<Config>) {
}

fn main() {
let mut args: Args = env::args();
if args.len() > 1 {
args.nth(0);
let mut a = vec![];
args.for_each(|f| a.push(f));
let arg_final = a.join("");


if arg_final == "-h" || arg_final == "--help" {

println!("-----Help Calc-----");
println!("");
println!("mini-calc > launch the mini-calc REPL");
println!("mini-calc [arg] > compute non interactively");
println!("mini-calc -h || --help > open this help");
println!("");
println!("------Help Calc-----");
exit(0);

}

let lexed = lex(arg_final);
let mut parser = init_calc_parser(&lexed);
let parsed = parser.parse();
let mut ram: HashMap<String, Parameters> = HashMap::new();
let mut functions: HashMap<String, (Vec<Ast>, Ast)> = HashMap::new();
ram.insert("pi".to_string(), Parameters::Float(PI));
ram.insert("e".to_string(), Parameters::Float(E));
let result = interpret(&parsed, &mut ram, &mut functions);
if result != Parameters::Null {
println!(
"{}",
result.pretty_print(Some(&mut ram), Some(&mut functions))
)
}
exit(0);
}

let mut config = match load() {
Ok(config) => config,
Err(_) => {
Expand All @@ -271,7 +310,6 @@ fn main() {
};

let mut loaded: Loaded = load_config(config.clone());

let message = &loaded.greeting_message;
println!("{}", message.to_string());

Expand All @@ -289,6 +327,7 @@ fn main() {
suffix = style.suffix()
))
.unwrap();

let mut ram: HashMap<String, Parameters> = HashMap::new();
let mut functions: HashMap<String, (Vec<Ast>, Ast)> = HashMap::new();
ram.insert("pi".to_string(), Parameters::Float(PI));
Expand Down Expand Up @@ -350,6 +389,7 @@ fn main() {
println!("{:#?}", p);
println!()
}

let result = interpret(&p, &mut ram, &mut functions);
if result != Parameters::Null {
println!(
Expand Down

0 comments on commit e1216c8

Please sign in to comment.