summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml6
-rw-r--r--src/main.rs124
4 files changed, 139 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e2a3069
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+*~
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..f04a31e
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "rusteqn"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..e96d941
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "rusteqn"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..0bb1740
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,124 @@
+#[derive(Debug, PartialEq)]
+enum Token {
+ Number(f64),
+ Operator(char),
+ Function(String),
+ Variable(String),
+ RightParen,
+ LeftParen
+}
+
+#[derive(Debug, PartialEq)]
+enum TokenizerError {
+ TooManyDots(String),
+ ConstantNotFound(String),
+ BadCharacter(char)
+}
+
+fn str_to_const(s: &str) -> Option<f64> {
+ match s {
+ "PI" => Some(std::f64::consts::PI),
+ "E" => Some(std::f64::consts::E),
+ _ => None
+ }
+}
+
+fn tokenize(input: &str) -> Result<Vec<Token>, TokenizerError> {
+ let mut tokens = Vec::new();
+ let mut chars = input.chars().peekable();
+
+ while let Some(&ch) = chars.peek() {
+ match ch {
+ '0'..='9' => {
+ let mut num = String::new();
+ let mut has_dot = false;
+ while let Some(&c) = chars.peek() {
+ if c.is_ascii_digit() || (c == '.' && !has_dot) {
+ num.push(c);
+ chars.next();
+ has_dot |= c == '.';
+ } else if c == '.' && has_dot {
+ return Err(TokenizerError::TooManyDots(num));
+ } else {
+ break;
+ }
+ }
+ tokens.push(Token::Number(num.parse().unwrap()));
+ }
+
+ '+' | '-' | '*' | '/' | '%' | '^' => {
+ chars.next();
+ tokens.push(Token::Operator(ch));
+ }
+
+ 'a'..='z' | '_' => {
+ let mut pt: Option<Token> = None;
+ let mut s = String::new();
+ while let Some(&c) = chars.peek() {
+ match c {
+ 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {
+ s.push(c);
+ chars.next();
+ }
+
+ '(' => {
+ pt = Some(Token::Function(s.clone()));
+ break;
+ }
+
+ _ => {
+ break;
+ }
+ }
+ }
+ tokens.push(match pt {
+ Some(token) => token,
+ None => Token::Variable(s.clone())
+ });
+ }
+
+ 'A'..='Z' => {
+ let mut s = String::new();
+ while let Some(&c @ ('A'..='Z' | '_')) = chars.peek() {
+ s.push(c);
+ chars.next();
+ }
+ let n = str_to_const(&s).ok_or(TokenizerError::ConstantNotFound(s))?;
+ tokens.push(Token::Number(n));
+ }
+
+ c if c.is_whitespace() || c == ',' => {
+ while let Some(&c) = chars.peek() {
+ if c.is_whitespace() {
+ chars.next();
+ } else {
+ break;
+ }
+ }
+ }
+
+ '(' => {
+ chars.next();
+ tokens.push(Token::RightParen);
+ }
+
+ ')' => {
+ chars.next();
+ tokens.push(Token::LeftParen);
+ }
+
+ _ => {
+ return Err(TokenizerError::BadCharacter(ch));
+ }
+ }
+ }
+
+ Ok(tokens)
+}
+
+fn main() {
+ let input = "x + 4 * (3 - sin((y+PI)) / 2.123)^2";
+ println!("{}", input);
+ let tokens = tokenize(input).unwrap();
+ println!("{:#?}", tokens);
+}