Created
April 27, 2013 10:47
-
-
Save funrep/5472667 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Main where | |
import Text.ParserCombinators.Parsec | |
import Control.Monad.Error | |
import System.Environment | |
main = getArgs >>= putStrLn . readExpr . head | |
-- Parser | |
readExpr :: String -> String | |
readExpr x = | |
case parse parseExpr "lisp" x of | |
Left err -> "MUHAHAH" ++ show err | |
Right val -> "KEBAB!" | |
parseNumber :: Parser Value | |
parseNumber = do | |
sign <- many (oneOf "-") | |
num <- many1 digit | |
case sign of | |
"-" -> return $ Number . negate $ read num | |
_ -> return $ Number $ read num | |
parseBool :: Parser Value | |
parseBool = do | |
bool <- many1 letter | |
case bool of | |
"True" -> return $ Bool True | |
"False" -> return $ Bool False | |
parseString :: Parser Value | |
parseString = do | |
char '"' | |
str <- many (noneOf "\"") | |
char '"' | |
return $ String str | |
parseList :: Parser Value | |
parseList = do | |
char '[' | |
xs <- sepBy parseValue (char ' ') | |
char '[' | |
return $ List [xs] | |
parseValue :: Parser Value | |
parseValue = | |
parseNumber <|> | |
parseBool <|> | |
parseString <|> | |
parseList | |
valueToExpr :: Value -> Expr | |
valueToExpr = return . Value | |
parseCode :: Parser Expr | |
parseCode = fmap Code $ sepBy parseExpr (oneOf " \n\r\t") | |
parseExpr :: Parser Expr | |
parseExpr = | |
(valueToExpr $ parseValue) <|> | |
do char '(' | |
xs <- parseCode | |
char ')' | |
return xs | |
-- Types | |
data RunTimeError | |
= NotInScope Sym | |
| TypeMismatch Expr | |
instance Show RunTimeError where | |
show x = case x of | |
NotInScope y -> "Not in scope " ++ show y | |
TypeMismatch y -> "Type mismatch in " ++ show y | |
instance Error RunTimeError | |
data Expr | |
= Var Sym | |
| Value Value | |
| If Expr Expr Expr | |
| Code [Expr] | |
deriving Show | |
data Value | |
= Number Integer | |
| Bool Bool | |
| String String | |
| List [Value] | |
| Func Env Sym Expr | |
-- | Action IO (Maybe String) | |
instance Show Value where | |
show x = case x of | |
Number y -> show y | |
Bool y -> show y | |
String y -> show y | |
List ys -> show $ unwords $ map show ys | |
Func _ y z -> y ++ " " ++ show z | |
-- Action _ -> "An action" | |
type Sym = String | |
newtype Env = Env [(Sym, Expr)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Main.hs:44:18:
Couldn't match expected type
Value' with actual type
[Value]'In the expression: xs
In the first argument of
List', namely
[xs]'In the second argument of
($)', namely
List [xs]'Main.hs:54:15:
Couldn't match expected type
Expr' with actual type
m0 b0'Expected type: b0 -> Expr
Actual type: b0 -> m0 b0
In the first argument of
(.)', namely
return'In the expression: return . Value
Main.hs:61:18:
Couldn't match expected type
Value' with actual type
Parser Value'In the second argument of
($)', namely
parseValue'In the first argument of
(<|>)', namely
(valueToExpr $ parseValue)'In the expression:
(valueToExpr $ parseValue)
<|>
do { char '(';
xs <- parseCode;
char ')';
return xs }
cabal: Error: some packages failed to install:
lisp-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1