reading expressions
This commit is contained in:
parent
96277e3c04
commit
5c950a1f40
|
@ -2,12 +2,27 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
|
@ -124,6 +139,88 @@ version = "0.1.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.17.0"
|
||||
|
@ -178,12 +275,31 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
||||
|
||||
[[package]]
|
||||
name = "rust_command_line_calculator"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"num",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -18,3 +18,5 @@ path = "src/main.rs"
|
|||
[dependencies]
|
||||
clap = { version = "4.0", features = ["derive"] }
|
||||
anyhow = "1.0"
|
||||
num = "0.4.0"
|
||||
regex = "1.7.1"
|
||||
|
|
|
@ -1,2 +1,100 @@
|
|||
use std::fmt;
|
||||
use regex::Regex;
|
||||
|
||||
// In an expression like `sqrt(25)` the Task would correspond to `sqrt`. This is the enum to
|
||||
// configure possible Tasks.
|
||||
// None means, the Expression doesn't send it's Value to a Task Handler
|
||||
#[derive(Debug)] // automatically generate Debug Formatter
|
||||
enum Task {
|
||||
None,
|
||||
Sqrt,
|
||||
Power,
|
||||
Log,
|
||||
}
|
||||
|
||||
// How to clone a Task, i was supprised I had to do it myself.
|
||||
impl Clone for Task {
|
||||
fn clone(&self) -> Self {
|
||||
// This can probably be done cleaner than with a verbose match. FIXME
|
||||
match self {
|
||||
Task::None => Task::None,
|
||||
Task::Sqrt => Task::Sqrt,
|
||||
Task::Power => Task::Power,
|
||||
Task::Log => Task::Log,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An Expression is something that can be calculated. 20+5 is an expression. Expressions can
|
||||
// contain other
|
||||
// Expressions and have tasks: 20+sqrt(20+5)
|
||||
// Expressions are marked down with braces and a task before those braces:
|
||||
// task(Expression)
|
||||
// once the Value of the Expression got calculated, the calculated value should be sent to the
|
||||
// TaskHandler, if the Task of the Expression is not Task::None
|
||||
pub struct Expression {
|
||||
text: String,
|
||||
task: Task,
|
||||
complex: bool,
|
||||
inner_value: f64,
|
||||
outer_value: f64,
|
||||
children: Vec<Expression>,
|
||||
}
|
||||
|
||||
// Debug Formatter for Expression
|
||||
impl fmt::Debug for Expression {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Expression")
|
||||
.field("text", &self.text)
|
||||
.field("task", &self.task)
|
||||
.field("is complex?", &self.complex)
|
||||
.field("inner value", &self.inner_value)
|
||||
.field("outer value", &self.outer_value)
|
||||
.field("children", &self.children)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// implement clone by ourselves, as it's not automatically done for us.
|
||||
impl Clone for Expression{
|
||||
fn clone(&self) -> Self {
|
||||
Expression {
|
||||
text: self.text.clone(),
|
||||
task: self.task.clone(),
|
||||
complex: self.complex.clone(), // TODO add support for complex numbers
|
||||
inner_value: self.inner_value.clone(),
|
||||
outer_value: self.outer_value.clone(),
|
||||
children: self.children.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Main logic for the Expression struct
|
||||
*/
|
||||
impl Expression {
|
||||
/*
|
||||
* Main function for making text into Expression
|
||||
*/
|
||||
pub fn new(expression_text: String) -> Expression {
|
||||
|
||||
// find children
|
||||
let re_sub_expression = Regex::new(r"\w+\(.+\)").unwrap();
|
||||
if re_sub_expression.is_match(&expression_text) {
|
||||
for sub_expression_text in re_sub_expression.captures_iter(&expression_text) {
|
||||
println!("{}", &sub_expression_text[0]);
|
||||
}
|
||||
}
|
||||
|
||||
let expression = Expression {
|
||||
text: expression_text,
|
||||
// TODO generate these from the text!
|
||||
task: Task::None,
|
||||
complex: false,
|
||||
inner_value: 0.0,
|
||||
outer_value: 0.0,
|
||||
children: Vec::new(),
|
||||
};
|
||||
expression
|
||||
}
|
||||
}
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -1,7 +1,10 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
|
||||
mod expression_parser;
|
||||
mod linear_algebra;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use expression_parser::Expression;
|
||||
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
|
@ -18,19 +21,30 @@ struct Arg {
|
|||
command: Option<Commands>,
|
||||
|
||||
/// An expression that should be used to calculate something
|
||||
expressionVector: Vec<String>,
|
||||
expression_texts: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Assert if two expressions are equal to each other
|
||||
Equal {
|
||||
expressionVector: Vec<String>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Arg::parse();
|
||||
dbg!(args.expressionVector);
|
||||
}
|
||||
let mut expression_vec: Vec<Expression> = Vec::new();
|
||||
|
||||
// join expression_texts to a big expression text, split at '%'. Remove unnessecary whitespace
|
||||
// at the start ot end.
|
||||
// TODO implement splitting of expressions, currently they are made only into a single big
|
||||
// expression text
|
||||
let mut expression_texts_concat: Vec<String> = Vec::new();
|
||||
expression_texts_concat.push(args.expression_texts.join(" ").trim().to_string());
|
||||
|
||||
for expression_text in expression_texts_concat {
|
||||
expression_vec.push(Expression::new(expression_text));
|
||||
}
|
||||
dbg!(expression_vec);
|
||||
|
||||
}
|
||||
|
|
Reference in New Issue