From ad7113c9119458f076fea90f55b8a9ceecb45891 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 24 May 2026 13:42:41 +0300 Subject: refactor(rpn): simplify isValidIdentifier(), removing dead multi-char loop (task 5j) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The loop checking remaining characters was dead code — the final check meant any token longer than 1 char was rejected regardless of what the loop found. Replace the entire function with a simple single-char check. --- internal/rpn/rpn_parse.go | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index 8bc2b96..c3785b2 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -439,34 +439,17 @@ func (r *RPN) dispatchOperator(stack *Stack, token string) (string, error) { } // isValidIdentifier checks if a token looks like a valid variable identifier. -// Valid identifiers contain only alphanumeric characters and underscores, -// and start with a letter or underscore (not a digit or special character). +// Valid identifiers are single alphanumeric characters or underscores. // // IMPORTANT: To prevent natural language words (like "what", "is", "of") from // being incorrectly treated as RPN symbols in mixed-mode expressions, -// this function currently restricts valid identifiers to a length of 1. +// this function restricts valid identifiers to a length of 1. func isValidIdentifier(token string) bool { - if len(token) == 0 { + if len(token) != 1 { return false } - - // Check first character - must be letter or underscore - first := token[0] - if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_') { - return false - } - - // Check remaining characters - must be alphanumeric or underscore - for i := 1; i < len(token); i++ { - c := token[i] - if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') { - return false - } - } - - // Only allow single-character identifiers for symbol support - // This prevents words like "what", "is", "of" from becoming symbols - return len(token) == 1 + c := token[0] + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' } // extractVariableName extracts a variable name from a token, stripping the leading colon if present. -- cgit v1.2.3