Ignoring operators at the end (with option to disable)

This commit is contained in:
Kasper 2020-01-14 02:50:10 +01:00
parent 0b592ac5f1
commit 22a5faa6be
2 changed files with 21 additions and 5 deletions

View File

@ -9,9 +9,19 @@ use crate::LexerKeyword::{In, PercentChar, Per, Mercury, Hg, PoundForce, PoundWo
use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan}; use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan};
use crate::units::Unit::*; use crate::units::Unit::*;
pub fn lex(input: &str) -> Result<TokenVector, String> { pub fn lex(input: &str, allow_trailing_operators: bool) -> Result<TokenVector, String> {
let mut input = input.replace(",", ""); // ignore commas
if allow_trailing_operators {
match &input.chars().last().unwrap_or('x') {
'+' | '-' | '*' | '/' | '%' | '^' | '(' => {
input.pop();
},
_ => {},
}
}
let input = input.replace(",", "");
let mut chars = input.chars().peekable(); let mut chars = input.chars().peekable();
let mut tokens: TokenVector = vec![]; let mut tokens: TokenVector = vec![];
let max_word_length = 30; let max_word_length = 30;

View File

@ -88,14 +88,20 @@ mod evaluator;
mod lookup; mod lookup;
fn main() { fn main() {
use std::env; use std::env;
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let s = if args.len() >= 2 { &args[1] } else { "0.1" }; if args.len() >= 2 {
eval(&args[1], true);
} else {
println!("No argument supplied");
}
}
pub fn eval(input: &str, allow_trailing_operators: bool) {
let lex_start = Instant::now(); let lex_start = Instant::now();
match lexer::lex(s) { match lexer::lex(input, allow_trailing_operators) {
Ok(tokens) => { Ok(tokens) => {
let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32; let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32;