-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSharedTypes.grace
More file actions
404 lines (320 loc) · 13.1 KB
/
Copy pathSharedTypes.grace
File metadata and controls
404 lines (320 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
dialect "none"
import "standardGrace" as sg
import "ast" as ast
import "xmodule" as xmodule
import "io" as io
inherit sg.methods
// Error resulting from type checking
def StaticTypingError: ExceptionKind is public = Exception.refine "StaticTypingError"
// Returned when searching for a method that is not there.
def noSuchMethod: outer.Pattern = object {
use BasicPattern
method matches(obj : Object) -> Boolean {
if(self.isMe(obj)) then {
true
} else {
false
}
}
}
// Replace newline characters with spaces. This is a
// workaround for issue #116 on the gracelang/minigrace
// git repo. The result of certain astNodes.toGrace(0)
// is a string containing newlines, and error messages
// containing these strings get cut off at the first
// newline character, resulting in an unhelpful error
// message.
method stripNewLines(str) → String {
str.replace("\n")with(" ")
}
//This type is used for checking subtyping
type TypePair = {
first → ObjectType
second → ObjectType
== (other:Object)→ Boolean
asString → String
}
//Used in publicType, publicTypeElse, and gatherTypesForMethods Methods
//All of which are helper methods for processBody in staticTyping
type SetMethodTypePair = {
first -> Set⟦MethodType⟧
second → Set⟦MethodType⟧
}
//Used in the publicType method in staticTyping
type PublicTypeReturnBundle = {
allMethods -> Set⟦MethodType⟧
publicMethods -> Set⟦MethodType⟧
internalType -> ObjectType
}
//Used in the visitMethod method in staticTyping
type VisitMethodHelperBundle = {
mType -> MethodType
returnType -> ObjectType
typeParams -> List⟦String⟧
}
//This type is used for checking subtyping
type Answer = {
ans → Boolean
asString → String
}
//Stores needed information used when type-checking a type defined by an opNode
type TypeOp = {
op → String
left → ObjectType
right → ObjectType
}
// type of a parameter
type Param = {
name → String
typeAnnotation → ObjectType
}
// generators for parameters
type ParamFactory = {
withName (name' : String) ofType (type' : ObjectType) → Param
ofType (type' : ObjectType) → Param
}
// MixPart is a "segment" of a method:
// Ex. for (param1) do (param2): for (param1, and do (param2)
// are separate "MixParts."
type MixPart = {
name → String
parameters → List⟦Param⟧
}
// Method signature information.
// isSpecialisation and restriction are used for type-checking
type MethodType = {
// name of the method
name → String
// name of the method with number of parameters for each part
nameString → String
// parameters and their types for each part
signature → List⟦MixPart⟧
// return type
retType → ObjectType
// optional type parameters
typeParams → List⟦String⟧
// checks if method has type parameters
hasTypeParams → Boolean
// check equivalence of part names, param types, and return type;
// does not check if param names are the same
== (other: MethodType) → Boolean
// create restriction of method type using other ?? Needs better descr.
restriction (other : MethodType) → MethodType
// Does it extend other
isSpecialisationOf (other : MethodType) → Answer
// pre-condition: This MethodType has type parameters
apply(replacementTypes : List⟦ObjectType⟧) → MethodType
// Takes a mapping of generic-to-ObjectType and returns a copy of self
// with all of the generics replaced with their corresponding ObjectType
updateTypeWith(replacements:Dictionary⟦String, ObjectType⟧) → MethodType
hash -> Number
}
// Type of generator creating method types by various means
type MethodTypeFactory = {
// Create method type from info on parameters and return type
signature (signature' : List⟦MixPart⟧)
returnType (rType : ObjectType)→ MethodType
signature (signature' : List⟦MixPart⟧)
with (typeParams': List[[String]])
returnType (retType' : ObjectType) → MethodType
// Create method type for parameterless method
member (name : String) ofType (rType : ObjectType) → MethodType
// DO WE NEED THIS??
// Create method type for imported method
// fromGctLine (line : String, importName: String) → MethodType
// Create method type from AST node representing method
fromNode (node: AstNode) → MethodType
}
// Representation of type with type parameters
type GenericType = {
// name of the type
name → String
// List of type parameters
typeParams → List⟦String⟧
// base type (removing type variables)
oType → ObjectType
// instantiate type by replacing type variables
apply (replacementTypes : List⟦ObjectType⟧) → ObjectType
}
// Type of generator creating generic types
type GenericTypeFactory = {
// Create generic type from name, parameters, and base type
fromName (name' : String) parameters (typeParams' : List⟦String⟧)
objectType (oType' : ObjectType) → GenericType
// Create generic type from AST node representing type definition
fromTypeDec (node : AstNode) → GenericType
}
// Generic type used in type checking, supertype of all others
type ObjectType = {
// Returns self or an object type from scope when an object type
// is built from identifier
resolve -> ObjectType
// Is this type built using & or |?
isOp -> Boolean
// Does this type have a name
isId -> Boolean
// Does this represent a type variable?
isTypeVble -> Boolean
// Is this object type built from a collection of methods?
isMeths -> Boolean
// Return the list of sets of methods of the type
// Used to build a normal-form representation of
// methods in variant types
normalFormMeths -> List[[Set[[MethodType]]]]
// Create new object type from self and other using op
withOp(op: String, other: ObjectType) -> ObjectType
// getMethod (name : String) → MethodType | noSuchMethod
// resolve → ObjectType
// isResolved → Boolean
// Does this type represent the dynamic or unknown type
isDynamic → Boolean
// Is this just a placeholder for real type?
isPlaceholder -> Boolean
== (other:ObjectType) → Boolean
// Determines if self is a subtype of other
isSubtypeOf (other : ObjectType) → Boolean
// isSubtypeHelper (trials : List⟦TypePair⟧, other : ObjectType) → Answer
// restriction (other : ObjectType) → ObjectType
// isConsistentSubtypeOf (other : ObjectType) → Boolean
// getVariantTypes → List⟦ObjectType⟧
// setVariantTypes(newVariantTypes:List⟦ObjectType⟧) → Done
// getOpNode → TypeOp
// setOpNode (op : TypeOp) → Done
// Construct new type as self | other
| (other : ObjectType) → ObjectType
// Construct new type as self & other
& (other : ObjectType) → ObjectType
// replace type variables with object types using replacements
updateTypeWith(replacements:Dictionary[[String,ObjectType]]) -> ObjectType
}
type ObjectTypeFromMeths = ObjectType & interface {
// return the set of methods of the type
methods → Set⟦MethodType⟧
}
// type of object built from & or |
type ObjectTypeFromOp = ObjectType & interface {
// operator used to build object type: & or |
op -> String
left -> ObjectType
right -> ObjectType
}
// methods to create an object type from various inputs
type ObjectTypeFactory = {
// Create an ObjectType from a collection of method signatures
fromMethods (methods' : Set⟦MethodType⟧) → ObjectTypeFromMeths
// Create an ObjectType from a collection of method signatures and a name
fromMethods(methods' : Set⟦MethodType⟧) withName(name : String) → ObjectTypeFromMeths
// Create an ObjectType using | or &
makeWithOp(op:String, left: ObjectType, right: ObjectType) -> ObjectType
//takes an AstNode representing a type and returns its corresponding ObjectType
fromDType (dtype) with (lst) → ObjectType
//Create an ObjectType from a GenericNode; the type used in the GenericNode
//must have already been declared and put into the generics scope. The types
//scope will also contain the returned ObjectType after this method is done.
fromGeneric (node : Generic) → ObjectType
//Find ObjectType corresponding to the identifier in the scope. If not
//already there, adds it to the scope.
fromIdentifier(ident : Identifier) with (lst) → ObjectType
// ObjectType corresponding to a type variable (e.g. from generic type)
typeVble(name': String) -> ObjectType
// Create a type corresponding to Dynamic or unknown
dynamic → ObjectType
// Type corresponding to bottom element, i.e., has all methods, so subtype of all
bottom → ObjectType
// type of a block with params and return type rType
blockTaking(params:List⟦Parameter⟧) returning(rType:ObjectType) → ObjectType
// type of block without prarameters returning value of type rType
blockReturning (rType : ObjectType) → ObjectType
// Used for type-checking imports; please update when additional types are
// added
preludeTypes → Set⟦String⟧
// represents type "Object"
base → ObjectType
// type Done
doneType → ObjectType
// other built-in types from prelude
pattern → ObjectType
iterator → ObjectType
boolean → ObjectType
number → ObjectType
string → ObjectType
listTp → ObjectType
set → ObjectType
sequence → ObjectType
dictionary → ObjectType
point → ObjectType
binding → ObjectType
}
// AST node type, kind tells what kind of node it is,
// e.g., identifier, generic, literal, etc
type AstNode = { kind → String }
// Create a pattern for matching kind for match
class aPatternMatchingNode (kind : String) → Pattern {
use BasicPattern
method matches (obj : Object) → Boolean {
match (obj)
case { node : AstNode →
if (kind == node.kind) then {
true
} else {
false
}
} else { false }
}
}
// Same as Pattern??
//type Matcher = {
// match(obj: AstNode) → MatchResult | false
//}
// A pattern that matches if parameter satisfies predicate
// Use in matches
class booleanPattern (predicate: Function1⟦AstNode⟧) → Pattern {
use BasicPattern
method matches (obj: AstNode) → Boolean {
if (predicate.apply (obj)) then {
true
} else {
false
}
}
}
// patterns for built-in AST Nodes
def If: Pattern is public = aPatternMatchingNode "if"
def BlockLiteral: Pattern is public = aPatternMatchingNode "block"
def MatchCase: Pattern is public = aPatternMatchingNode "matchcase"
def TryCatch: Pattern is public = aPatternMatchingNode "trycatch"
def Outer: Pattern is public = aPatternMatchingNode "outer"
def MethodSignature: Pattern is public = aPatternMatchingNode "methodtype"
def TypeLiteral: Pattern is public = aPatternMatchingNode "typeliteral"
def TypeDeclaration: Pattern is public = aPatternMatchingNode "typedec"
def TypeAnnotation: Pattern is public = aPatternMatchingNode "dtype"
def Member: Pattern is public = aPatternMatchingNode "member"
def Method: Pattern is public = aPatternMatchingNode "method"
def Parameter: Pattern is public = aPatternMatchingNode "parameter"
// matches anything that is a call
def Request: Pattern is public = booleanPattern { x → x.isCall }
def Class: Pattern is public = aPatternMatchingNode "class"
def ObjectLiteral: Pattern is public = aPatternMatchingNode "object"
def ArrayLiteral: Pattern is public = aPatternMatchingNode "array"
def Generic: Pattern is public = aPatternMatchingNode "generic"
def Identifier: Pattern is public = aPatternMatchingNode "identifier"
def OctetsLiteral: Pattern is public = aPatternMatchingNode "octets"
def StringLiteral: Pattern is public = aPatternMatchingNode "string"
def NumberLiteral: Pattern is public = aPatternMatchingNode "num"
def Operator: Pattern is public = aPatternMatchingNode "op"
def Bind: Pattern is public = aPatternMatchingNode "bind"
def Def: Pattern is public = aPatternMatchingNode "defdec"
def Var: Pattern is public = aPatternMatchingNode "vardec"
def Import: Pattern is public = aPatternMatchingNode "import"
def Dialect: Pattern is public = aPatternMatchingNode "dialect"
def Return: Pattern is public = aPatternMatchingNode "return"
def Inherit: Pattern is public = aPatternMatchingNode "inherit"
def Module: Pattern is public = aPatternMatchingNode "module"
// Set of prelude types; used by the importVisitor in this file as well as
// inside StaticTyping.grace and ObjectTypeModule.grace.
//
// Please update this set when new prelude types are added.
def preludeTypes: Set⟦String⟧ is public = emptySet⟦String⟧
preludeTypes.addAll( ["Done", "Pattern", "Iterator", "Boolean", "Number",
"String", "List", "Set", "Sequence", "Dictionary","Point",
"Binding", "Collection", "Enumerable", "Range", "Object"])