gdritter repos s-cargot / 6f8a584
Finished Haskell float parser Getty Ritter 9 years ago
1 changed file(s) with 27 addition(s) and 6 deletion(s). Collapse all Expand all
106106 | otherwise = error ("Unknown letter in number: " ++ show c)
107107
108108 pFloat :: Parser Double
109 pFloat = fail "???"
109 pFloat = do
110 n <- decimal
111 withDot n <|> noDot n
112 where withDot n = do
113 char '.'
114 m <- decimal
115 e <- option 1.0 exponent
116 return ((fromIntegral n + asDec m 0) * e)
117 noDot n = do
118 e <- exponent
119 return (fromIntegral n * e)
120 exponent = do
121 oneOf "eE"
122 s <- power
123 x <- decimal
124 return (10 ** s (fromIntegral x))
125 asDec 0 k = k
126 asDec n k =
127 asDec (n `div` 10) ((fromIntegral (n `rem` 10) + k) * 0.1)
128
129 power :: Num a => Parser (a -> a)
130 power = negate <$ char '-' <|> id <$ char '+' <|> return id
110131
111132 pInt :: Parser Integer
112133 pInt = do
113 s <- negate <$ char '-' <|> id <$ char '+' <|> return id
134 s <- power
114135 n <- pZeroNum <|> decimal
115 return (s n)
136 return (fromIntegral (s n))
116137
117138 pZeroNum :: Parser Integer
118139 pZeroNum = char '0' >>
123144 )
124145
125146 pHaskLikeAtom :: Parser HaskLikeAtom
126 pHaskLikeAtom =
127 HSInt <$> (try pInt <?> "integer")
128 <|> HSFloat <$> (try pFloat <?> "float")
147 pHaskLikeAtom
148 = HSFloat <$> (try pFloat <?> "float")
149 <|> HSInt <$> (try pInt <?> "integer")
129150 <|> HSString <$> (pString <?> "string literal")
130151 <|> HSIdent <$> (pToken <?> "token")
131152