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.Declarations
29import PureScript.TypeChecker.Monad
30import PureScript.CodeGen.Common
32externToPs :: String -> (Type, NameKind) -> String
33externToPs name (ty, _) = "extern " ++ name ++ " :: " ++ typeToPs ty
35typeLiterals :: Pattern Type String
36typeLiterals = Pattern $ A.Kleisli match
38 match Number = Just "Number"
39 match String = Just "String"
40 match Boolean = Just "Boolean"
41 match (Array ty) = Just $ "[" ++ typeToPs ty ++ "]"
42 match (Object row) = Just $ "{ " ++ rowToPs row ++ " }"
43 match (TypeVar var) = Just var
44 match (TypeConstructor ctor) = Just ctor
45 match (TUnknown u) = Just $ show u
48rowToPs :: Row -> String
49rowToPs = (\(tys, tail) -> intercalate "; " (map (uncurry nameAndTypeToPs) tys) ++ tailToPs tail) . toList
51 nameAndTypeToPs :: String -> Type -> String
52 nameAndTypeToPs name ty = name ++ " :: " ++ typeToPs ty
53 tailToPs :: Row -> String
55 tailToPs (RUnknown u) = show u
56 tailToPs (RowVar var) = " | " ++ var
57 toList :: Row -> ([(String, Type)], Row)
58 toList (RCons name ty row) = let (tys, rest) = toList row
59 in ((name, ty):tys, rest)
62typeApp :: Pattern Type (Type, Type)
63typeApp = Pattern $ A.Kleisli match
65 match (TypeApp f x) = Just (f, x)
68function :: Pattern Type ([Type], Type)
69function = Pattern $ A.Kleisli match
71 match (Function args ret) = Just (args, ret)
74forAll :: Pattern Type ([String], Type)
75forAll = Pattern $ A.Kleisli match
77 match (ForAll args t) = Just (args, t)
80typeToPs :: Type -> String
81typeToPs = fromMaybe (error "Incomplete pattern") . pattern matchType
83 matchType :: Pattern Type String
84 matchType = buildPrettyPrinter operators (typeLiterals <|> fmap parens matchType)
85 operators :: OperatorTable Type String
87 OperatorTable $ [ AssocL typeApp $ \f x -> f ++ " " ++ x
88 , Split function $ \args ret -> "(" ++ intercalate ", " (map typeToPs args) ++ ") -> " ++ typeToPs ret
89 , Wrap forAll $ \args t -> "forall " ++ intercalate " " args ++ ". " ++ t
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