refactor: lexNumbers

This commit is contained in:
Laborratte 5 2024-07-15 23:46:50 +02:00
parent 5c2dc50872
commit 9123e0b882
Signed by: Laborratte5
GPG key ID: 3A30072E35202C02

View file

@ -26,7 +26,11 @@ lex' (x:xs) tokens orgLength = case x of
lexNumbers :: String -> [Token] -> Int -> [Token]
lexNumbers (x:xs) tokens orgLength
| isDigit x = lex' (dropWhile isDigit (x:xs)) ((Integer(read (takeWhile isDigit (x:xs))::Int)):tokens) orgLength
| otherwise = error $ "Syntax error: " ++ [x] ++ " (" ++ show (orgLength - (length (x:xs))) ++ ")"
| isDigit x = lex' rest ((Integer(read digits::Int)):tokens) orgLength
| otherwise = syntaxError x (orgLength - (length (x:xs)) + 1)
where
digits = takeWhile isDigit (x:xs)
rest = dropWhile isDigit (x:xs)
isDigit c = c `elem` "0123456789"
isDigit x = x `elem` "0123456789"
syntaxError char pos = error $ "Syntax error: " ++ [char] ++ " (" ++ show pos ++ ")"