updated code
parent
8894e08c90
commit
1f3a7177d8
@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "ocalc"
|
||||
version = "1.0.0"
|
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "ocalc"
|
||||
version = "1.0.0"
|
||||
authors = ["peshwar9"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,48 @@
|
||||
/// This is the main command-line application for arithmetic expression evaluator
|
||||
|
||||
// Standard library
|
||||
use std::f64;
|
||||
use std::io;
|
||||
|
||||
// code for arithmetic expression evaluation is in parsemath module
|
||||
mod parsemath;
|
||||
use parsemath::parser::ParseError;
|
||||
|
||||
|
||||
// Function to invoke Parser and evaluate expression
|
||||
fn evaluate(expr: String) -> Result<f64, ParseError> {
|
||||
let len = expr.len();
|
||||
let mut expr = expr;
|
||||
expr.truncate(len - 1); // remove newline character
|
||||
let mut math_parser = parsemath::parser::Parser::new(&expr)?;
|
||||
let ast = math_parser.parse()?;
|
||||
println!("The generated AST is {:?}", ast);
|
||||
|
||||
Ok(parsemath::ast::eval(ast)?)
|
||||
}
|
||||
|
||||
// Main function reads aritnmetic expression from command-line and displays result and error.
|
||||
// It calls the evaluate function to perform compuation.
|
||||
|
||||
fn main() {
|
||||
println!("Hello! Welcome to Arithmetic expression evaluator.");
|
||||
println!("You can calculate value for expression such as 2*3+(4-5)+2^3/4. ");
|
||||
println!("Allowed numbers: positive, negative and decimals.");
|
||||
println!("Supported operations: Add, Subtract, Multiply, Divide, PowerOf(^). ");
|
||||
println!("Enter your arithmetic expression below:");
|
||||
loop {
|
||||
let mut input = String::new();
|
||||
match io::stdin().read_line(&mut input) {
|
||||
Ok(_) => {
|
||||
match evaluate(input) {
|
||||
Ok(val) => println!("The computed number is {}\n", val),
|
||||
Err(_) => {
|
||||
println!("Error in evaluating expression. Please enter valid expression\n");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Err(error) => println!("error: {}", error),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
{"rustc_fingerprint":4503571881771466578,"outputs":{"1164083562126845933":["rustc 1.43.0 (4fb7144ed 2020-04-20)\nbinary: rustc\ncommit-hash: 4fb7144ed159f94491249e86d5bbd033b5d60550\ncommit-date: 2020-04-20\nhost: x86_64-apple-darwin\nrelease: 1.43.0\nLLVM version: 9.0\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/prabhueshwarla/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
bc1679c1c2047cad
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":7786015957388683530,"profile":1647870076477133176,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/ocalc-628cd1d3b6bbee1e/dep-test-bin-ocalc-628cd1d3b6bbee1e"}}],"rustflags":[],"metadata":13779719443416291531}
|
@ -0,0 +1 @@
|
||||
084a8cc2b1018ce0
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":7786015957388683530,"profile":14891217944882224483,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/ocalc-9bb38f3499aac79e/dep-bin-ocalc-9bb38f3499aac79e"}}],"rustflags":[],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1,10 @@
|
||||
/Users/prabhueshwarla/rust/learn/commandline/ocalc/target/rls/debug/deps/ocalc-628cd1d3b6bbee1e.rmeta: src/main.rs src/parsemath/mod.rs src/parsemath/ast.rs src/parsemath/parser.rs src/parsemath/token.rs src/parsemath/tokenizer.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/learn/commandline/ocalc/target/rls/debug/deps/ocalc-628cd1d3b6bbee1e.d: src/main.rs src/parsemath/mod.rs src/parsemath/ast.rs src/parsemath/parser.rs src/parsemath/token.rs src/parsemath/tokenizer.rs
|
||||
|
||||
src/main.rs:
|
||||
src/parsemath/mod.rs:
|
||||
src/parsemath/ast.rs:
|
||||
src/parsemath/parser.rs:
|
||||
src/parsemath/token.rs:
|
||||
src/parsemath/tokenizer.rs:
|
@ -0,0 +1,10 @@
|
||||
/Users/prabhueshwarla/rust/learn/commandline/ocalc/target/rls/debug/deps/ocalc-9bb38f3499aac79e.rmeta: src/main.rs src/parsemath/mod.rs src/parsemath/ast.rs src/parsemath/parser.rs src/parsemath/token.rs src/parsemath/tokenizer.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/learn/commandline/ocalc/target/rls/debug/deps/ocalc-9bb38f3499aac79e.d: src/main.rs src/parsemath/mod.rs src/parsemath/ast.rs src/parsemath/parser.rs src/parsemath/token.rs src/parsemath/tokenizer.rs
|
||||
|
||||
src/main.rs:
|
||||
src/parsemath/mod.rs:
|
||||
src/parsemath/ast.rs:
|
||||
src/parsemath/parser.rs:
|
||||
src/parsemath/token.rs:
|
||||
src/parsemath/tokenizer.rs:
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue