git.iko.soy/rowtype-yoga/purescript-native
on 1b46a84 help sign in
221 lines 5.4 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Environment
4-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable #-}
17module Language.PureScript.Environment where
19import Data.Data
21import Language.PureScript.Names
22import Language.PureScript.Types
23import Language.PureScript.Kinds
24import Language.PureScript.TypeClassDictionaries
25import qualified Language.PureScript.Constants as C
27import qualified Data.Map as M
29-- |
30-- The @Environment@ defines all values and types which are currently in scope:
31--
32data Environment = Environment {
33 -- |
34 -- Value names currently in scope
35 --
36 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
37 -- |
38 -- Type names currently in scope
39 --
40 , types :: M.Map (Qualified ProperName) (Kind, TypeKind)
41 -- |
42 -- Data constructors currently in scope, along with their associated data type constructors
43 --
44 , dataConstructors :: M.Map (Qualified ProperName) (DataDeclType, ProperName, Type)
45 -- |
46 -- Type synonyms currently in scope
47 --
48 , typeSynonyms :: M.Map (Qualified ProperName) ([String], Type)
49 -- |
50 -- Available type class dictionaries
51 --
52 , typeClassDictionaries :: M.Map (Qualified Ident, Maybe ModuleName) TypeClassDictionaryInScope
53 -- |
54 -- Type classes
55 --
56 , typeClasses :: M.Map (Qualified ProperName) ([String], [(Ident, Type)], [(Qualified ProperName, [Type])])
57 } deriving (Show)
59-- |
60-- The initial environment with no values and only the default javascript types defined
61--
62initEnvironment :: Environment
63initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty M.empty
65-- |
66-- The type of a foreign import
67--
68data ForeignImportType
69 -- |
70 -- A regular foreign import
71 --
72 = ForeignImport
73 -- |
74 -- A foreign import which contains inline Javascript as a string literal
75 --
76 | InlineJavascript deriving (Show, Eq, Data, Typeable)
78-- |
79-- The visibility of a name in scope
80--
81data NameVisibility
82 -- |
83 -- The name is defined in the current binding group, but is not visible
84 --
85 = Undefined
86 -- |
87 -- The name is defined in the another binding group, or has been made visible by a function binder
88 --
89 | Defined deriving (Show, Eq)
91-- |
92-- The kind of a name
93--
94data NameKind
95 -- |
96 -- A value introduced as a binding in a module
97 --
98 = Value
99 -- |
100 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
101 --
102 | TypeClassAccessorImport
103 -- |
104 -- A foreign import
105 --
106 | Extern ForeignImportType
107 -- |
108 -- A local name introduced using a lambda abstraction, variable introduction or binder
109 --
110 | LocalVariable
111 -- |
112 -- A data constructor
113 --
114 | DataConstructor
115 -- |
116 -- A type class dictionary, generated during desugaring of type class declarations
117 --
118 | TypeInstanceDictionaryValue deriving (Show, Eq, Data, Typeable)
120-- |
121-- The kinds of a type
122--
123data TypeKind
124 -- |
125 -- Data type
126 --
127 = DataType [String] [(ProperName, [Type])]
128 -- |
129 -- Type synonym
130 --
131 | TypeSynonym
132 -- |
133 -- Foreign data
134 --
135 | ExternData
136 -- |
137 -- A local type variable
138 --
139 | LocalTypeVariable deriving (Show, Eq, Data, Typeable)
141-- |
142-- The type ('data' or 'newtype') of a data type declaration
143--
144data DataDeclType
145 -- |
146 -- A standard data constructor
147 --
148 = Data
149 -- |
150 -- A newtype constructor
151 --
152 | Newtype deriving (Eq, Ord, Data, Typeable)
154instance Show DataDeclType where
155 show Data = "data"
156 show Newtype = "newtype"
158-- |
159-- Construct a ProperName in the Prim module
160--
161primName :: String -> Qualified ProperName
162primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
164-- |
165-- Construct a type in the Prim module
166--
167primTy :: String -> Type
168primTy = TypeConstructor . primName
170-- |
171-- Type constructor for functions
172--
173tyFunction :: Type
174tyFunction = primTy "Function"
176-- |
177-- Type constructor for strings
178--
179tyString :: Type
180tyString = primTy "String"
182-- |
183-- Type constructor for numbers
184--
185tyNumber :: Type
186tyNumber = primTy "Number"
188-- |
189-- Type constructor for booleans
190--
191tyBoolean :: Type
192tyBoolean = primTy "Boolean"
194-- |
195-- Type constructor for arrays
196--
197tyArray :: Type
198tyArray = primTy "Array"
200-- |
201-- Type constructor for objects
202--
203tyObject :: Type
204tyObject = primTy "Object"
206-- |
207-- Smart constructor for function types
208--
209function :: Type -> Type -> Type
210function t1 = TypeApp (TypeApp tyFunction t1)
212-- |
213-- The primitive types in the external javascript environment with their associated kinds.
214--
215primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
216primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
217 , (primName "Array" , (FunKind Star Star, ExternData))
218 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
219 , (primName "String" , (Star, ExternData))
220 , (primName "Number" , (Star, ExternData))
221 , (primName "Boolean" , (Star, ExternData)) ]