module AST where import Data.List data Program = Program [Value] type Name = String data Value = VList [Value] | VNum Int | VString String | VName Name | VQuoted Value | VDeclare Name | VDefine Name Value | VLambda [Name] Value | VLambdaRec Name [Name] Value | VLet [(Name, Value)] Value | VBuiltin String | VEllipsis deriving (Eq, Ord) instance Show Program where show (Program l) = intercalate "\n" $ map show l instance Show Value where show (VList es) = '(' : intercalate " " (map show es) ++ ")" show (VNum i) = show i show (VString s) = show s show (VName n) = n show (VQuoted e) = '\'' : show e show (VDeclare n) = "(declare " ++ n ++ ")" show (VDefine n v) = "(define " ++ n ++ " " ++ show v ++ ")" show (VLambda as v) = "(lambda (" ++ intercalate " " as ++ ") " ++ show v ++ ")" show (VLambdaRec rn as v) = "(lambdarec " ++ rn ++ " (" ++ intercalate " " as ++ ") " ++ show v ++ ")" show (VLet ps v) = "(let (" ++ intercalate " " ["(" ++ n ++ " " ++ show w ++ ")" | (n, w) <- ps] ++ ") " ++ show v ++ ")" show (VBuiltin str) = "[[builtin " ++ str ++ "]]" show VEllipsis = "..." fromVName :: Value -> Maybe Name fromVName (VName s) = Just s fromVName _ = Nothing fromVNum :: Value -> Maybe Int fromVNum (VNum i) = Just i fromVNum _ = Nothing fromVString :: Value -> Maybe String fromVString (VString s) = Just s fromVString _ = Nothing