git.iko.soy/rowtype-yoga/purescript-native
on 856ca36 help sign in
390 lines 8.1 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
25-- |
26-- A guard is just a boolean-valued expression that appears alongside a set of binders
27--
28type Guard = Value
30-- |
31-- Built-in unary operators
32--
33data UnaryOperator
34 -- |
35 -- Numeric negation
36 --
37 = Negate
38 -- |
39 -- Boolean negation
40 --
41 | Not
42 -- |
43 -- Bitwise negation
44 --
45 | BitwiseNot
46 -- |
47 -- Numeric unary \'plus\'
48 --
49 | Positive deriving (Show, Eq, Data, Typeable)
51-- |
52-- Built-in binary operators
53--
54data BinaryOperator
55 -- |
56 -- Numeric addition
57 --
58 = Add
59 -- |
60 -- Numeric subtraction
61 --
62 | Subtract
63 -- |
64 -- Numeric multiplication
65 --
66 | Multiply
67 -- |
68 -- Numeric division
69 --
70 | Divide
71 -- |
72 -- Remainder
73 --
74 | Modulus
75 -- |
76 -- Generic equality test
77 --
78 | EqualTo
79 -- |
80 -- Generic inequality test
81 --
82 | NotEqualTo
83 -- |
84 -- Numeric less-than
85 --
86 | LessThan
87 -- |
88 -- Numeric less-than-or-equal
89 --
90 | LessThanOrEqualTo
91 -- |
92 -- Numeric greater-than
93 --
94 | GreaterThan
95 -- |
96 -- Numeric greater-than-or-equal
97 --
98 | GreaterThanOrEqualTo
99 -- |
100 -- Boolean and
101 --
102 | And
103 -- |
104 -- Boolean or
105 --
106 | Or
107 -- |
108 -- Bitwise and
109 --
110 | BitwiseAnd
111 -- |
112 -- Bitwise or
113 --
114 | BitwiseOr
115 -- |
116 -- Bitwise xor
117 --
118 | BitwiseXor
119 -- |
120 -- Bitwise left shift
121 --
122 | ShiftLeft
123 -- |
124 -- Bitwise right shift
125 --
126 | ShiftRight
127 -- |
128 -- Bitwise right shift with zero-fill
129 --
130 | ZeroFillShiftRight
131 -- |
132 -- String concatenation
133 --
134 | Concat deriving (Show, Eq, Data, Typeable)
136-- |
137-- Data type for values
138--
139data Value
140 -- |
141 -- A numeric literal
142 --
143 = NumericLiteral (Either Integer Double)
144 -- |
145 -- A string literal
146 --
147 | StringLiteral String
148 -- |
149 -- A boolean literal
150 --
151 | BooleanLiteral Bool
152 -- |
153 -- Unary operator application
154 --
155 | Unary UnaryOperator Value
156 -- |
157 -- Binary operator application
158 --
159 | Binary BinaryOperator Value Value
160 -- |
161 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
162 -- will be removed.
163 --
164 | BinaryNoParens (Qualified Ident) Value Value
165 -- |
166 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
167 -- will be removed.
168 --
169 | Parens Value
170 -- |
171 -- An array literal
172 --
173 | ArrayLiteral [Value]
174 -- |
175 -- An array indexing expression
176 --
177 | Indexer Value Value
178 -- |
179 -- An object literal
180 --
181 | ObjectLiteral [(String, Value)]
182 -- |
183 -- An record property accessor expression
184 --
185 | Accessor String Value
186 -- |
187 -- Partial record update
188 --
189 | ObjectUpdate Value [(String, Value)]
190 -- |
191 -- Function introduction
192 --
193 | Abs [Ident] Value
194 -- |
195 -- Function application
196 --
197 | App Value [Value]
198 -- |
199 -- Variable
200 --
201 | Var (Qualified Ident)
202 -- |
203 -- Conditional (if-then-else expression)
204 --
205 | IfThenElse Value Value Value
206 -- |
207 -- A \"Block\" i.e. a collection of statements which evaluate to a value
208 --
209 | Block [Statement]
210 -- |
211 -- A data constructor
212 --
213 | Constructor (Qualified ProperName)
214 -- |
215 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
216 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
217 --
218 | Case [Value] [([Binder], Maybe Guard, Value)]
219 -- |
220 -- A value with a type annotation
221 --
222 | TypedValue Bool Value Type
223 -- |
224 -- A do-notation block
225 --
226 | Do [DoNotationElement]
227 -- |
228 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
229 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
230 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
231 -- instance type, and the type class dictionaries in scope.
232 --
233 | TypeClassDictionary (Qualified ProperName, Type) [TypeClassDictionaryInScope] deriving (Show, Data, Typeable)
235-- |
236-- The type of a type class dictionary
237--
238data TypeClassDictionaryType
239 -- |
240 -- A regular type class dictionary
241 --
242 = TCDRegular
243 -- |
244 -- A type class dictionary which is an alias for an imported dictionary from another module
245 --
246 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
248-- |
249-- Data representing a type class dictionary which is in scope
250--
251data TypeClassDictionaryInScope
252 = TypeClassDictionaryInScope {
253 -- |
254 -- The identifier with which the dictionary can be accessed at runtime
255 --
256 tcdName :: Qualified Ident
257 -- |
258 -- The name of the type class to which this type class instance applies
259 --
260 , tcdClassName :: Qualified ProperName
261 -- |
262 -- The type to which this type class instance applies
263 --
264 , tcdInstanceType :: Type
265 -- |
266 -- Type class dependencies which must be satisfied to construct this dictionary
267 --
268 , tcdDependencies :: Maybe [(Qualified ProperName, Type)]
269 -- |
270 -- The type of this dictionary
271 --
272 , tcdType :: TypeClassDictionaryType
273 } deriving (Show, Data, Typeable)
275-- |
276-- A statement in a do-notation block
277--
278data DoNotationElement
279 -- |
280 -- A monadic value without a binder
281 --
282 = DoNotationValue Value
283 -- |
284 -- A monadic value with a binder
285 --
286 | DoNotationBind Binder Value
287 -- |
288 -- A let statement, i.e. a pure value with a binder
289 --
290 | DoNotationLet Binder Value deriving (Show, Data, Typeable)
292-- |
293-- Data type for statements which can appear inside a @Block@ expression
294--
295data Statement
296 -- |
297 -- A variable introduction and initial assignment
298 --
299 = VariableIntroduction Ident Value
300 -- |
301 -- A variable reassignment
302 --
303 | Assignment Ident Value
304 -- |
305 -- A while loop
306 --
307 | While Value [Statement]
308 -- |
309 -- A for loop
310 --
311 | For Ident Value Value [Statement]
312 -- |
313 -- An if-then-else statement
314 --
315 | If IfStatement
316 -- |
317 -- A return statement
318 --
319 | Return Value deriving (Show, Data, Typeable)
321-- |
322-- Data type for if-statements
323--
324data IfStatement
325 -- |
326 -- An if statement. Arguments are (in order): boolean condition, true branch, optional else branch.
327 --
328 = IfStatement Value [Statement] (Maybe ElseStatement) deriving (Show, Data, Typeable)
330-- |
331-- Data type for the else branch in an if-statement
332--
333data ElseStatement
334 -- |
335 -- An else branch
336 --
337 = Else [Statement]
338 -- |
339 -- An else-if branch
340 --
341 | ElseIf IfStatement deriving (Show, Data, Typeable)
343-- |
344-- Data type for binders
345--
346data Binder
347 -- |
348 -- Wildcard binder
349 --
350 = NullBinder
351 -- |
352 -- A binder which matches a boolean literal
353 --
354 | BooleanBinder Bool
355 -- |
356 -- A binder which matches a string literal
357 --
358 | StringBinder String
359 -- |
360 -- A binder which matches a numeric literal
361 --
362 | NumberBinder (Either Integer Double)
363 -- |
364 -- A binder which binds an identifier
365 --
366 | VarBinder Ident
367 -- |
368 -- A binder which matches a data constructor with no argument
369 --
370 | NullaryBinder (Qualified ProperName)
371 -- |
372 -- A binder which matches a data constructor with one argument
373 --
374 | UnaryBinder (Qualified ProperName) Binder
375 -- |
376 -- A binder which matches a record and binds its properties
377 --
378 | ObjectBinder [(String, Binder)]
379 -- |
380 -- A binder which matches an array and binds its elements
381 --
382 | ArrayBinder [Binder]
383 -- |
384 -- A binder which matches an array and binds its head and tail
385 --
386 | ConsBinder Binder Binder
387 -- |
388 -- A binder which binds its input to an identifier
389 --
390 | NamedBinder Ident Binder deriving (Show, Data, Typeable)