Added "ans" variable

This commit is contained in:
2023-06-14 14:04:32 -07:00
parent d00679c44a
commit 2dec478b74
7 changed files with 62 additions and 29 deletions

View File

@ -83,6 +83,9 @@ impl PreToken {
let c = Unit::from_string(&s);
if c.is_some() { return Ok(Token::Quantity(c.unwrap())); }
if s == "ans" { return Ok(Token::Variable(String::from("ans"))); }
return Err((l, ParserError::Undefined(s)));
}

View File

@ -50,9 +50,9 @@ fn push_token(g: &mut VecDeque<PreToken>, t: Option<PreToken>, stop_i: usize) {
}
}
// Some operators are written as words.
if let PreToken::PreWord(l, s) = &t {
let o = Operator::from_string(s);
if o.is_some() {
if Operator::from_string(s).is_some() {
t = PreToken::PreOperator(*l, s.clone());
}
}

View File

@ -8,6 +8,7 @@ use super::Constant;
#[derive(Debug)]
#[derive(Clone)]
pub enum Token {
Variable(String),
Quantity(Quantity),
Constant(Constant),
Operator(Operator, VecDeque<Token>),
@ -18,6 +19,7 @@ impl ToString for Token {
match self {
Token::Quantity(v) => v.to_string(),
Token::Constant(c) => c.to_string(),
Token::Variable(s) => s.clone(),
Token::Operator(o,a) => o.print(a)
}
}
@ -30,6 +32,7 @@ impl Token {
match self {
Token::Quantity(v) => v.to_string_outer(),
Token::Constant(c) => c.to_string(),
Token::Variable(s) => s.clone(),
Token::Operator(o,a) => o.print(a)
}
}