Lex special symbols as one token

This commit is contained in:
bakk 2021-05-18 17:49:31 +02:00
parent 4db84aa441
commit 2636f51077
2 changed files with 14 additions and 3 deletions

View File

@ -116,6 +116,13 @@ impl<'a> Lexer<'a> {
';' => build(TokenKind::Semicolon, "", span),
'%' => build(TokenKind::Percent, "", span),
'\'' => build(TokenKind::Tick, "", span),
// Some of the special symbols will be lexed here,
// so that they don't merge with other symbols.
'π' => build(TokenKind::Identifier, "π", span),
'√' => build(TokenKind::Identifier, "", span),
'τ' => build(TokenKind::Identifier, "τ", span),
'ϕ' => build(TokenKind::Identifier, "ϕ", span),
'Γ' => build(TokenKind::Identifier, "Γ", span),
_ => build(TokenKind::Unknown, "", span),
};
@ -221,7 +228,7 @@ fn is_valid_identifier(c: Option<&char>) -> bool {
if let Some(c) = c {
match c {
'+' | '-' | '/' | '*' | '%' | '^' | '!' | '(' | ')' | '=' | '.' | ',' | ';' | '|'
| '⌊' | '⌋' | '⌈' | '⌉' | ']' => false,
| '⌊' | '⌋' | '⌈' | '⌉' | ']' | 'π' | '√' | 'τ' | 'ϕ' | 'Γ' => false,
_ => !c.is_digit(10),
}
} else {

View File

@ -481,10 +481,14 @@ fn parse_identifier(context: &mut Context) -> Result<Expr, CalcError> {
let identifier = Identifier::from_full_name(&advance(context).value);
// Eg. sqrt64
if match_token(context, TokenKind::Literal) {
if match_token(context, TokenKind::Literal)
|| peek(context).value == "π"
|| peek(context).value == "τ"
|| peek(context).value == "ϕ"
{
// 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.pure_name) {
let parameter = Expr::Literal(string_to_num(&advance(context).value));
let parameter = parse_primary(context)?;
return Ok(Expr::FnCall(identifier, vec![parameter]));
}
}