Created
July 6, 2016 13:47
-
-
Save dbalan/902c25f95c33a0a07bada9ab277b83ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Expression where | |
import Text.ParserCombinators.Parsec | |
import Text.Parsec.Token | |
import Control.Monad.Error | |
data Expr = LispInt Integer | | |
LispSym String | | |
LispLi [Expr] | |
instance Show Expr where | |
show (LispInt x) = show x | |
show (LispSym x) = x | |
-- show (LispFun _) = "<fun>" | |
show (LispLi x) = "(" ++ unwords (map show x) ++ ")" | |
parseInteger = do | |
num <- many1 digit | |
return (LispInt (read num)) | |
parseSymbol = do | |
pre <- oneOf "*-+" <|> letter | |
suf <- many (letter <|> digit) | |
return $ LispSym ( [pre] ++ suf) | |
parseList = do | |
char '(' | |
skipMany space | |
x <- parseExpAux `sepEndBy` (many1 space) | |
skipMany space | |
char ')' | |
return $ LispLi x | |
parseExpAux = (try parseInteger) <|> (try parseList) <|> (try parseSymbol) | |
parseExp :: Parser Expr | |
parseExp = do skipMany space | |
x <- parseExpAux | |
skipMany space; eof | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment