mirror of
https://github.com/PaddiM8/kalker.git
synced 2024-11-08 00:44:40 +01:00
Allow spaces in input number literals
This commit is contained in:
parent
6d8e182578
commit
a1af433ebd
@ -339,7 +339,7 @@ fn parse_primary(context: &mut Context) -> Result<Expr, CalcError> {
|
||||
TokenKind::OpenParenthesis => parse_group(context)?,
|
||||
TokenKind::Pipe | TokenKind::OpenCeil | TokenKind::OpenFloor => parse_group_fn(context)?,
|
||||
TokenKind::Identifier => parse_identifier(context)?,
|
||||
TokenKind::Literal => Expr::Literal(advance(context).value.parse::<f64>().unwrap()),
|
||||
TokenKind::Literal => Expr::Literal(string_to_num(&advance(context).value)),
|
||||
_ => return Err(CalcError::UnableToParseExpression),
|
||||
};
|
||||
|
||||
@ -375,7 +375,7 @@ fn parse_identifier(context: &mut Context) -> Result<Expr, CalcError> {
|
||||
if match_token(context, TokenKind::Literal) {
|
||||
// If there is a function with this name, parse it as a function, with the next token as the argument.
|
||||
if context.symbol_table.contains_fn(&identifier.value) {
|
||||
let parameter = Expr::Literal(advance(context).value.parse::<f64>().unwrap());
|
||||
let parameter = Expr::Literal(string_to_num(&advance(context).value));
|
||||
return Ok(Expr::FnCall(identifier.value, vec![parameter]));
|
||||
}
|
||||
}
|
||||
@ -458,6 +458,10 @@ fn is_at_end(context: &Context) -> bool {
|
||||
context.pos >= context.tokens.len() || peek(context).kind == TokenKind::EOF
|
||||
}
|
||||
|
||||
fn string_to_num(value: &str) -> f64 {
|
||||
value.replace(" ", "").parse::<f64>().unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user