From 8e2a3ff4d76e0c68453fd0844f82ebbc8b19b403 Mon Sep 17 00:00:00 2001 From: Kasper Date: Sun, 8 Dec 2019 21:04:35 +0100 Subject: [PATCH] Number parsing --- Cargo.lock | 83 ++++++++++++++++++++++++----------------------- Cargo.toml | 5 +-- README.md | 4 ++- src/bignumbers.rs | 13 -------- src/main.rs | 57 +++++++++++++++++--------------- 5 files changed, 78 insertions(+), 84 deletions(-) delete mode 100644 src/bignumbers.rs diff --git a/Cargo.lock b/Cargo.lock index c8a6fc9..a6230f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,8 +9,13 @@ dependencies = [ ] [[package]] -name = "autocfg" -version = "0.1.7" +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cc" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -22,9 +27,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "cpc" version = "0.1.0" dependencies = [ + "decimal 2.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "decimal" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "ord_subset 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -39,6 +56,11 @@ dependencies = [ "static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "libc" +version = "0.2.66" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "memchr" version = "2.2.1" @@ -60,42 +82,14 @@ dependencies = [ ] [[package]] -name = "num-bigint" -version = "0.2.3" +name = "ord_subset" +version = "3.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] -name = "num-integer" -version = "0.1.41" +name = "rustc-serialize" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-rational" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-traits" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "rustc_version" @@ -123,6 +117,11 @@ name = "semver-parser" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "static_assertions" version = "0.3.4" @@ -135,19 +134,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" -"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum cc 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "f52a465a666ca3d838ebbf08b241383421412fe7ebb463527bba275526d89f76" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum decimal 2.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e6458723bc760383275fbc02f4c769b2e5f3de782abaf5e7e0b9b7f0368a63ed" "checksum lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14" +"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" "checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" "checksum nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c618b63422da4401283884e6668d39f819a106ef51f5f59b81add00075da35ca" -"checksum num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f9c3f34cdd24f334cb265d9bf8bfa8a241920d026916785747a92f0e55541a1a" -"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" -"checksum num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2885278d5fe2adc2f75ced642d52d879bffaceb5a2e0b1d4309ffdfb239b454" -"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4" +"checksum ord_subset 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ce14664caf5b27f5656ff727defd68ae1eb75ef3c4d95259361df1eb376bef" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)" = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702" "checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" diff --git a/Cargo.toml b/Cargo.toml index d01f044..20e9a36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] # logos = "0.9.7" -num-traits = "0.2" -num-rational = "0.2" +# num-traits = "0.2" +# num-rational = "0.2" nom = "5.0" +decimal = "2.0.4" diff --git a/README.md b/README.md index df25d1a..d520e5f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -wip +calculation + conversion + +Uses Decimal Floating Point numbers instead of Binary Coded Decimals for better accuracy. https://adriann.github.io/rust_parser.html diff --git a/src/bignumbers.rs b/src/bignumbers.rs deleted file mode 100644 index 42dd309..0000000 --- a/src/bignumbers.rs +++ /dev/null @@ -1,13 +0,0 @@ -// reference for later - -use num_rational::BigRational; -use num_traits::ops::checked::CheckedMul; - -pub fn main() { - let s1 = "531137992816767098"; - let s2 = "200137992816767098"; - let one: BigRational = s1.parse().unwrap(); - let two: BigRational = s2.parse().unwrap(); - let result = one.checked_mul(&two); - println!("{:?}", result); -} diff --git a/src/main.rs b/src/main.rs index 4bbb87e..cec801e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::time::{Instant}; -// use num::rational::BigRational; +// use num_rational::BigRational; +use decimal::d128; #[derive(Debug)] enum Operator { @@ -15,30 +16,21 @@ enum Operator { } use Operator::*; -// #[derive(Debug)] -// enum Number { -// FloatNumber(i32), -// BigRationalNumber(BigRational), -// } -// use Number::*; - #[derive(Debug)] enum Token { Operator(Operator), - // Number(Number), + Number(d128), } type TokenVector = Vec; fn lex(input: &str) -> Option { - let chars:Vec = input.chars().collect(); + let mut chars = input.chars().enumerate().peekable(); let mut tokens: TokenVector = vec![]; - let mut index = 0; - while index < chars.len() { - let value = chars[index]; - match value { + while let Some((index, current_char)) = chars.next() { + match current_char { '+' => tokens.push(Token::Operator(Plus)), '-' => tokens.push(Token::Operator(Minus)), '*' => tokens.push(Token::Operator(Multiply)), @@ -52,36 +44,47 @@ fn lex(input: &str) -> Option { value if value.is_whitespace() => continue, value if value.is_alphabetic() => { - // word - }, '.' | '0'..='9' => { - // numbers - if value == '.' { - if !chars[index+1].is_digit(10) { return None }; + let start_index = index; + let mut end_index = index+1; + while let Some((_idk, current_char)) = chars.peek() { + if current_char == &'.' || current_char.is_digit(10) { + chars.next(); + end_index += 1; + } else { + break; + } } + match &input[start_index..end_index].parse::() { + Ok(number) => { + tokens.push(Token::Number(*number)); + println!("parsed as d128: {}", number); + }, + Err(e) => { + println!("{:?}", e); + return None; + } + }; }, _ => { - println!("{}", value); + println!("{}", current_char); return None; }, } - index += 1; }; return Some(tokens) } -mod bignumbers; fn main() { - bignumbers::main(); let now = Instant::now(); - // use std::env; - // let s = &args[1]; - // let s = "3 +pi^ (1+1* 2)-pi^3 "; // = 3 - let s = "*+(/)"; + use std::env; + let args: Vec = env::args().collect(); + let s = if args.len() == 2 { &args[1] } else { "0.1" }; + match lex(s) { Some(vector) => println!("{:?}", vector), None => println!("Error"),