Skip to content

Instantly share code, notes, and snippets.

@Ceasar Ceasar/Lisp.hs
Created Jul 27, 2013

Embed
What would you like to do?
Lisp in Haskell. WIP.
import Text.ParserCombinators.Parsec
import System.Environment
data LispVal = Atom String
-- | List [LispVal]
-- | DottedList [LispVal] LispVal
| Number Integer
| String String
| Bool Bool deriving Show
readExpr :: String -> String
readExpr input = case parse parseExpr "lisp" input of
Left err -> "No match: " ++ show err
Right val -> "Found value: " ++ show val
parseAtom :: Parser LispVal
parseAtom = do atom <- many1 (letter <|> symbol)
return $ case atom of
"true" -> Bool True
"false" -> Bool False
_ -> Atom atom
where
symbol :: Parser Char
symbol = oneOf "!#$%&|*+-/:<=>?@^_~"
parseString :: Parser LispVal
parseString = do char '"'
x <- many letter
char '"'
return $ String x
parseNumber :: Parser LispVal
parseNumber = do x <- many1 digit
return $ Number (read x)
separator :: Parser ()
separator = skipMany1 space
parseVal :: Parser LispVal
parseVal = parseAtom
<|> parseString
<|> parseNumber
parseExpr :: Parser [LispVal]
parseExpr = do char '('
expr <- sepBy1 (parseVal <|> parseExpr) separator
char ')'
return expr
main :: IO ()
main = do args <- getArgs
putStrLn (readExpr (args !! 1))
type Token = String
tokenize :: String -> [Token]
tokenize = undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.