git.iko.soy/rowtype-yoga/purescript-native
on 83367bd help sign in
268 lines 6.4 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-- Data type for values
32--
33data Value
34 -- |
35 -- A numeric literal
36 --
37 = NumericLiteral (Either Integer Double)
38 -- |
39 -- A string literal
40 --
41 | StringLiteral String
42 -- |
43 -- A boolean literal
44 --
45 | BooleanLiteral Bool
46 -- |
47 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
48 -- will be removed.
49 --
50 | BinaryNoParens (Qualified Ident) Value Value
51 -- |
52 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
53 -- will be removed.
54 --
55 | Parens Value
56 -- |
57 -- An array literal
58 --
59 | ArrayLiteral [Value]
60 -- |
61 -- An object literal
62 --
63 | ObjectLiteral [(String, Value)]
64 -- |
65 -- An record property accessor expression
66 --
67 | Accessor String Value
68 -- |
69 -- Partial record update
70 --
71 | ObjectUpdate Value [(String, Value)]
72 -- |
73 -- Function introduction
74 --
75 | Abs Ident Value
76 -- |
77 -- Function application
78 --
79 | App Value Value
80 -- |
81 -- Variable
82 --
83 | Var (Qualified Ident)
84 -- |
85 -- Conditional (if-then-else expression)
86 --
87 | IfThenElse Value Value Value
88 -- |
89 -- A \"Block\" i.e. a collection of statements which evaluate to a value
90 --
91 | Block [Statement]
92 -- |
93 -- A data constructor
94 --
95 | Constructor (Qualified ProperName)
96 -- |
97 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
98 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
99 --
100 | Case [Value] [([Binder], Maybe Guard, Value)]
101 -- |
102 -- A value with a type annotation
103 --
104 | TypedValue Bool Value Type
105 -- |
106 -- A do-notation block
107 --
108 | Do [DoNotationElement]
109 -- |
110 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
111 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
112 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
113 -- instance type, and the type class dictionaries in scope.
114 --
115 | TypeClassDictionary (Qualified ProperName, Type) [TypeClassDictionaryInScope] deriving (Show, Data, Typeable)
117-- |
118-- The type of a type class dictionary
119--
120data TypeClassDictionaryType
121 -- |
122 -- A regular type class dictionary
123 --
124 = TCDRegular
125 -- |
126 -- A type class dictionary which is an alias for an imported dictionary from another module
127 --
128 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
130-- |
131-- Data representing a type class dictionary which is in scope
132--
133data TypeClassDictionaryInScope
134 = TypeClassDictionaryInScope {
135 -- |
136 -- The identifier with which the dictionary can be accessed at runtime
137 --
138 tcdName :: Qualified Ident
139 -- |
140 -- The name of the type class to which this type class instance applies
141 --
142 , tcdClassName :: Qualified ProperName
143 -- |
144 -- The type to which this type class instance applies
145 --
146 , tcdInstanceType :: Type
147 -- |
148 -- Type class dependencies which must be satisfied to construct this dictionary
149 --
150 , tcdDependencies :: Maybe [(Qualified ProperName, Type)]
151 -- |
152 -- The type of this dictionary
153 --
154 , tcdType :: TypeClassDictionaryType
155 } deriving (Show, Data, Typeable)
157-- |
158-- A statement in a do-notation block
159--
160data DoNotationElement
161 -- |
162 -- A monadic value without a binder
163 --
164 = DoNotationValue Value
165 -- |
166 -- A monadic value with a binder
167 --
168 | DoNotationBind Binder Value
169 -- |
170 -- A let statement, i.e. a pure value with a binder
171 --
172 | DoNotationLet Binder Value deriving (Show, Data, Typeable)
174-- |
175-- Data type for statements which can appear inside a @Block@ expression
176--
177data Statement
178 -- |
179 -- A variable introduction and initial assignment
180 --
181 = VariableIntroduction Ident Value
182 -- |
183 -- A variable reassignment
184 --
185 | Assignment Ident Value
186 -- |
187 -- A while loop
188 --
189 | While Value [Statement]
190 -- |
191 -- A for loop
192 --
193 | For Ident Value Value [Statement]
194 -- |
195 -- An if-then-else statement
196 --
197 | If IfStatement
198 -- |
199 -- A return statement
200 --
201 | Return Value deriving (Show, Data, Typeable)
203-- |
204-- Data type for if-statements
205--
206data IfStatement
207 -- |
208 -- An if statement. Arguments are (in order): boolean condition, true branch, optional else branch.
209 --
210 = IfStatement Value [Statement] (Maybe ElseStatement) deriving (Show, Data, Typeable)
212-- |
213-- Data type for the else branch in an if-statement
214--
215data ElseStatement
216 -- |
217 -- An else branch
218 --
219 = Else [Statement]
220 -- |
221 -- An else-if branch
222 --
223 | ElseIf IfStatement deriving (Show, Data, Typeable)
225-- |
226-- Data type for binders
227--
228data Binder
229 -- |
230 -- Wildcard binder
231 --
232 = NullBinder
233 -- |
234 -- A binder which matches a boolean literal
235 --
236 | BooleanBinder Bool
237 -- |
238 -- A binder which matches a string literal
239 --
240 | StringBinder String
241 -- |
242 -- A binder which matches a numeric literal
243 --
244 | NumberBinder (Either Integer Double)
245 -- |
246 -- A binder which binds an identifier
247 --
248 | VarBinder Ident
249 -- |
250 -- A binder which matches a data constructor
251 --
252 | ConstructorBinder (Qualified ProperName) [Binder]
253 -- |
254 -- A binder which matches a record and binds its properties
255 --
256 | ObjectBinder [(String, Binder)]
257 -- |
258 -- A binder which matches an array and binds its elements
259 --
260 | ArrayBinder [Binder]
261 -- |
262 -- A binder which matches an array and binds its head and tail
263 --
264 | ConsBinder Binder Binder
265 -- |
266 -- A binder which binds its input to an identifier
267 --
268 | NamedBinder Ident Binder deriving (Show, Data, Typeable)