Skip to content

Instantly share code, notes, and snippets.

@apskii
Created August 31, 2012 04:27
Show Gist options
  • Save apskii/3549067 to your computer and use it in GitHub Desktop.
Save apskii/3549067 to your computer and use it in GitHub Desktop.
data T = SymT Id
| StrT String
| IntT Integer
| ChrT Char
| LisT [T]
| CloT Env Id T
-------------------------------------------------------------------------
(~|~) = (() <$) .: (<|>)
strLit = StrT <$> (char '"' *> many (noneOf "\"") <* char '"')
chrLit = ChrT <$> (string "#\\" *> anyChar)
intLit = IntT . read .: (:) <$> option ' ' (char '-') <*> many1 digit
<* lookAhead (space ~|~ oneOf "()" ~|~ eof)
symbol = SymT <$> many1 (noneOf "() \n\t")
atom = strLit <|> chrLit <|> try intLit <|> symbol
expr = LisT <$> (char '(' *> (form `sepEndBy` spaces) <* char ')')
quoted = LisT . (SymT "quote" :) . (:[]) <$> (char '\'' *> spaces *> form)
form = quoted <|> expr <|> atom
parse :: Parser T -> String -> [T]
parse p = either (error . show) id . runP top () ""
where top = many (spaces *> p <* spaces)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment