1-----------------------------------------------------------------------------
3-- Module : PureScript.CodeGen.Externs
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15{-# LANGUAGE GADTs, GeneralizedNewtypeDeriving #-}
17module PureScript.CodeGen.Externs (
21import Data.Maybe (fromMaybe)
22import Data.List (intersperse, intercalate)
23import qualified Control.Arrow as A
24import Control.Applicative
26import PureScript.Values
27import PureScript.Types
28import PureScript.Names
29import PureScript.Declarations
30import PureScript.TypeChecker.Monad
31import PureScript.CodeGen.Common
33externToPs :: Ident -> (PolyType, NameKind) -> String
34externToPs name (ty, _) = "extern " ++ show name ++ " :: " ++ polyTypeToPs ty
36typeLiterals :: Pattern Type String
37typeLiterals = Pattern $ A.Kleisli match
39 match Number = Just "Number"
40 match String = Just "String"
41 match Boolean = Just "Boolean"
42 match (Array ty) = Just $ "[" ++ typeToPs ty ++ "]"
43 match (Object row) = Just $ "{ " ++ rowToPs row ++ " }"
44 match (TypeVar var) = Just var
45 match (TypeConstructor ctor) = Just ctor
46 match (TUnknown u) = Just $ show u
49rowToPs :: Row -> String
50rowToPs = (\(tys, tail) -> intercalate "; " (map (uncurry nameAndTypeToPs) tys) ++ tailToPs tail) . toList
52 nameAndTypeToPs :: String -> Type -> String
53 nameAndTypeToPs name ty = name ++ " :: " ++ typeToPs ty
54 tailToPs :: Row -> String
56 tailToPs (RUnknown u) = show u
57 tailToPs (RowVar var) = " | " ++ var
58 toList :: Row -> ([(String, Type)], Row)
59 toList (RCons name ty row) = let (tys, rest) = toList row
60 in ((name, ty):tys, rest)
63typeApp :: Pattern Type (Type, Type)
64typeApp = Pattern $ A.Kleisli match
66 match (TypeApp f x) = Just (f, x)
69singleArgumentFunction :: Pattern Type (Type, Type)
70singleArgumentFunction = Pattern $ A.Kleisli match
72 match (Function [arg] ret) = Just (arg, ret)
75function :: Pattern Type ([Type], Type)
76function = Pattern $ A.Kleisli match
78 match (Function args ret) = Just (args, ret)
81typeToPs :: Type -> String
82typeToPs = fromMaybe (error "Incomplete pattern") . pattern matchType
84 matchType :: Pattern Type String
85 matchType = buildPrettyPrinter operators (typeLiterals <|> fmap parens matchType)
86 operators :: OperatorTable Type String
88 OperatorTable $ [ AssocL typeApp $ \f x -> f ++ " " ++ x
89 , Split function $ \args ret -> "(" ++ intercalate ", " (map typeToPs args) ++ ") -> " ++ typeToPs ret
90 , AssocL singleArgumentFunction $ \arg ret -> arg ++ " -> " ++ ret
93polyTypeToPs :: PolyType -> String
94polyTypeToPs (PolyType [] ty) = typeToPs ty
95polyTypeToPs (PolyType idents ty) = "forall " ++ intercalate " " idents ++ ". " ++ typeToPs ty
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list