git.iko.soy/rowtype-yoga/purescript-native
on dc14d2d help sign in
229 lines 5.6 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Values
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for values, statements, binders and do notation
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Values where
20import Language.PureScript.Types
21import Language.PureScript.Names
23import Data.Data
24import Data.Generics (mkQ, everything)
26-- |
27-- A guard is just a boolean-valued expression that appears alongside a set of binders
28--
29type Guard = Value
31-- |
32-- Data type for values
33--
34data Value
35 -- |
36 -- A numeric literal
37 --
38 = NumericLiteral (Either Integer Double)
39 -- |
40 -- A string literal
41 --
42 | StringLiteral String
43 -- |
44 -- A boolean literal
45 --
46 | BooleanLiteral Bool
47 -- |
48 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
49 -- will be removed.
50 --
51 | BinaryNoParens (Qualified Ident) Value Value
52 -- |
53 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
54 -- will be removed.
55 --
56 | Parens Value
57 -- |
58 -- An array literal
59 --
60 | ArrayLiteral [Value]
61 -- |
62 -- An object literal
63 --
64 | ObjectLiteral [(String, Value)]
65 -- |
66 -- An record property accessor expression
67 --
68 | Accessor String Value
69 -- |
70 -- Partial record update
71 --
72 | ObjectUpdate Value [(String, Value)]
73 -- |
74 -- Function introduction
75 --
76 | Abs (Either Ident Binder) Value
77 -- |
78 -- Function application
79 --
80 | App Value Value
81 -- |
82 -- Variable
83 --
84 | Var (Qualified Ident)
85 -- |
86 -- Conditional (if-then-else expression)
87 --
88 | IfThenElse Value Value Value
89 -- |
90 -- A data constructor
91 --
92 | Constructor (Qualified ProperName)
93 -- |
94 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
95 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
96 --
97 | Case [Value] [([Binder], Maybe Guard, Value)]
98 -- |
99 -- A value with a type annotation
100 --
101 | TypedValue Bool Value Type
102 -- |
103 -- A let binding
104 --
105 | Let Binder Value Value
106 -- |
107 -- A do-notation block
108 --
109 | Do [DoNotationElement]
110 -- |
111 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
112 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
113 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
114 -- instance type, and the type class dictionaries in scope.
115 --
116 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope] deriving (Show, Data, Typeable)
118-- |
119-- Data representing a type class dictionary which is in scope
120--
121data TypeClassDictionaryInScope
122 = TypeClassDictionaryInScope {
123 -- |
124 -- The identifier with which the dictionary can be accessed at runtime
125 --
126 tcdName :: Qualified Ident
127 -- |
128 -- The name of the type class to which this type class instance applies
129 --
130 , tcdClassName :: Qualified ProperName
131 -- |
132 -- The types to which this type class instance applies
133 --
134 , tcdInstanceTypes :: [Type]
135 -- |
136 -- Type class dependencies which must be satisfied to construct this dictionary
137 --
138 , tcdDependencies :: Maybe [(Qualified ProperName, [Type])]
139 -- |
140 -- The type of this dictionary
141 --
142 , tcdType :: TypeClassDictionaryType
143 } deriving (Show, Data, Typeable)
145-- |
146-- The type of a type class dictionary
147--
148data TypeClassDictionaryType
149 -- |
150 -- A regular type class dictionary
151 --
152 = TCDRegular
153 -- |
154 -- A type class dictionary which is an alias for an imported dictionary from another module
155 --
156 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
158-- |
159-- A statement in a do-notation block
160--
161data DoNotationElement
162 -- |
163 -- A monadic value without a binder
164 --
165 = DoNotationValue Value
166 -- |
167 -- A monadic value with a binder
168 --
169 | DoNotationBind Binder Value
170 -- |
171 -- A let statement, i.e. a pure value with a binder
172 --
173 | DoNotationLet Binder Value deriving (Show, Data, Typeable)
175-- |
176-- Data type for binders
177--
178data Binder
179 -- |
180 -- Wildcard binder
181 --
182 = NullBinder
183 -- |
184 -- A binder which matches a boolean literal
185 --
186 | BooleanBinder Bool
187 -- |
188 -- A binder which matches a string literal
189 --
190 | StringBinder String
191 -- |
192 -- A binder which matches a numeric literal
193 --
194 | NumberBinder (Either Integer Double)
195 -- |
196 -- A binder which binds an identifier
197 --
198 | VarBinder Ident
199 -- |
200 -- A binder which matches a data constructor
201 --
202 | ConstructorBinder (Qualified ProperName) [Binder]
203 -- |
204 -- A binder which matches a record and binds its properties
205 --
206 | ObjectBinder [(String, Binder)]
207 -- |
208 -- A binder which matches an array and binds its elements
209 --
210 | ArrayBinder [Binder]
211 -- |
212 -- A binder which matches an array and binds its head and tail
213 --
214 | ConsBinder Binder Binder
215 -- |
216 -- A binder which binds its input to an identifier
217 --
218 | NamedBinder Ident Binder deriving (Show, Data, Typeable)
221-- |
222-- Collect all names introduced in binders in an expression
223--
224binderNames :: (Data d) => d -> [Ident]
225binderNames = everything (++) (mkQ [] go)
226 where
227 go (VarBinder ident) = [ident]
228 go (NamedBinder ident _) = [ident]
229 go _ = []