improve parsing (very janky)
This commit is contained in:
55
src/misc.rs
55
src/misc.rs
@@ -1,7 +1,62 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
use meval::Expr;
|
||||
|
||||
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
pub fn test_func_basic(function_string: String) -> String {
|
||||
let expr_result = function_string.parse();
|
||||
let expr_error = match &expr_result {
|
||||
Ok(_) => "".to_string(),
|
||||
Err(error) => format!("{}", error),
|
||||
};
|
||||
if !expr_error.is_empty() {
|
||||
return expr_error;
|
||||
}
|
||||
|
||||
let expr: Expr = expr_result.unwrap();
|
||||
let func_result = expr.bind("x");
|
||||
let func_error = match &func_result {
|
||||
Ok(_) => "".to_string(),
|
||||
Err(error) => format!("{}", error),
|
||||
};
|
||||
if !func_error.is_empty() {
|
||||
return func_error;
|
||||
}
|
||||
|
||||
"".to_string()
|
||||
}
|
||||
|
||||
|
||||
// EXTREMELY Janky function that tries to put asterisks in the proper places to be parsed
|
||||
pub fn add_asterisks(function: String) -> String {
|
||||
let letters: Vec<char>= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect();
|
||||
let numbers: Vec<char> = "0123456789".chars().collect();
|
||||
let function_chars = function.chars();
|
||||
let mut output_string: String = String::new();
|
||||
let mut prev_char: char = ' ';
|
||||
for c in function_chars {
|
||||
if prev_char == ')' {
|
||||
if c == '(' {
|
||||
output_string += "*";
|
||||
} else if (c == 'x') | numbers.contains(&c) {
|
||||
output_string += "*"
|
||||
}
|
||||
} else if letters.contains(&c) {
|
||||
if numbers.contains(&prev_char) {
|
||||
output_string += "*";
|
||||
}
|
||||
} else if numbers.contains(&c) {
|
||||
if letters.contains(&prev_char) {
|
||||
output_string += "*";
|
||||
}
|
||||
}
|
||||
prev_char = c;
|
||||
output_string += &c.to_string();
|
||||
}
|
||||
|
||||
return output_string;
|
||||
}
|
||||
|
||||
/// Result of screen to chart coordinates conversion.
|
||||
#[wasm_bindgen]
|
||||
pub struct Point {
|
||||
|
||||
Reference in New Issue
Block a user