-
Notifications
You must be signed in to change notification settings - Fork 8
/
ast.sml
603 lines (536 loc) · 17.8 KB
/
ast.sml
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
(* -*- mode: sml; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- *)
(*
* The following licensing terms and conditions apply and must be
* accepted in order to use the Reference Implementation:
*
* 1. This Reference Implementation is made available to all
* interested persons on the same terms as Ecma makes available its
* standards and technical reports, as set forth at
* http://www.ecma-international.org/publications/.
*
* 2. All liability and responsibility for any use of this Reference
* Implementation rests with the user, and not with any of the parties
* who contribute to, or who own or hold any copyright in, this Reference
* Implementation.
*
* 3. THIS REFERENCE IMPLEMENTATION IS PROVIDED BY THE COPYRIGHT
* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* End of Terms and Conditions
*
* Copyright (c) 2007 Adobe Systems Inc., The Mozilla Foundation, Opera
* Software ASA, and others.
*)
(* A sketch of the ES4 AST in SML *)
structure Ast = struct
type SOURCE_POS = { line: int, col: int }
type LOC = { file: string, span: SOURCE_POS * SOURCE_POS, post_newline: bool }
type IDENT = Ustring.STRING
type AST_NONCE = int
type TYPEVAR_NONCE = int
datatype NAMESPACE =
Intrinsic
| Private of IDENT
| Protected of IDENT
| Public of IDENT
| Internal of IDENT
| UserNamespace of Ustring.STRING
| AnonUserNamespace of int
| LimitedNamespace of (IDENT * NAMESPACE)
type NAME = { ns: NAMESPACE, id: IDENT }
type MULTINAME = { nss: NAMESPACE list list, id: IDENT }
datatype BINTYPEOP =
Cast
| Is
| Wrap
| To
datatype BINOP =
Plus
| Minus
| Times
| Divide
| Remainder
| LeftShift
| RightShift
| RightShiftUnsigned
| BitwiseAnd
| BitwiseOr
| BitwiseXor
| LogicalAnd
| LogicalOr
| InstanceOf
| In
| Equals
| NotEquals
| StrictEquals
| StrictNotEquals
| Less
| LessOrEqual
| Greater
| GreaterOrEqual
| Comma
datatype ASSIGNOP =
Assign
| AssignPlus
| AssignMinus
| AssignTimes
| AssignDivide
| AssignRemainder
| AssignLeftShift
| AssignRightShift
| AssignRightShiftUnsigned
| AssignBitwiseAnd
| AssignBitwiseOr
| AssignBitwiseXor
| AssignLogicalAnd
| AssignLogicalOr
datatype UNOP =
Delete
| Void
| Typeof
| PreIncrement
| PreDecrement
| PostIncrement
| PostDecrement
| UnaryPlus
| UnaryMinus
| BitwiseNot
| LogicalNot
| Type
| Splat
datatype VAR_DEFN_TAG =
Const
| Var
| LetVar
| LetConst
datatype SPECIAL_TY =
Any
| Null
| Undefined
| VoidType
datatype PRAGMA =
UseNamespace of EXPR
| UseDefaultNamespace of EXPR
| UseDecimalContext of EXPR
| UseStrict
| UseStandard
| Import of
{ package: IDENT list,
name: IDENT }
and FUNC_NAME_KIND =
Ordinary
| Operator
| Get
| Set
| Call
| Has
and CLS =
Cls of
{ name: NAME,
typeParams: IDENT list,
nonnullable: bool,
dynamic: bool,
extends: TYPE_EXPR option,
implements: TYPE_EXPR list,
classRib: RIB,
instanceRib: RIB,
instanceInits: HEAD,
constructor: CTOR option,
classType: TYPE_EXPR, (* ObjectType *)
instanceType: TYPE_EXPR }
and IFACE =
Iface of
{ name: NAME,
typeParams: IDENT list,
nonnullable: bool,
extends: TYPE_EXPR list,
instanceRib: RIB,
instanceType: TYPE_EXPR }
and CTOR =
Ctor of
{ settings: HEAD, (* FIXME should be a EXPR list of LetExpr of InitExpr *)
superArgs: EXPR list,
func: FUNC }
and FUNC =
Func of
{ name: FUNC_NAME,
fsig: FUNC_SIG, (* redundant, not used in verify *)
native: bool,
block: BLOCK option, (* NONE => abstract *)
param: HEAD, (* CF: not sure what this is ... *)
defaults: EXPR list,
ty: TYPE_EXPR,
loc: LOC option }
and DEFN =
ClassDefn of CLASS_DEFN
| VariableDefn of VAR_DEFN
| FunctionDefn of FUNC_DEFN
| ConstructorDefn of CTOR_DEFN
| InterfaceDefn of INTERFACE_DEFN
| NamespaceDefn of NAMESPACE_DEFN
| TypeDefn of TYPE_DEFN
and FUNC_SIG = (* redundant, not used in verify *)
FunctionSignature of
{ typeParams: IDENT list,
params: BINDINGS,
paramTypes: TYPE_EXPR list,
defaults: EXPR list,
ctorInits: (BINDINGS * EXPR list) option, (* settings + super args *)
returnType: TYPE_EXPR,
thisType: TYPE_EXPR option,
hasRest: bool }
and BINDING =
Binding of
{ ident: BINDING_IDENT, (* FIXME: use tuple *)
ty: TYPE_EXPR }
and BINDING_IDENT =
TempIdent of int
| ParamIdent of int
| PropIdent of IDENT
and INIT_STEP = (* used to encode init of bindings *)
InitStep of (BINDING_IDENT * EXPR)
| AssignStep of (EXPR * EXPR)
and TYPE_EXPR =
SpecialType of SPECIAL_TY
| UnionType of TYPE_EXPR list
| ArrayType of TYPE_EXPR list
| TypeName of (IDENT_EXPR * AST_NONCE option)
| ElementTypeRef of (TYPE_EXPR * int)
| FieldTypeRef of (TYPE_EXPR * IDENT)
| FunctionType of FUNC_TYPE
| ObjectType of FIELD_TYPE list
| LikeType of TYPE_EXPR
| WrapType of TYPE_EXPR
| AppType of
{ base: TYPE_EXPR,
args: TYPE_EXPR list }
| LamType of
{ params: IDENT list,
body: TYPE_EXPR }
| NullableType of
{ expr:TYPE_EXPR,
nullable:bool }
| InstanceType of INSTANCE_TYPE
| TypeVarFixtureRef of TYPEVAR_NONCE
and STMT =
EmptyStmt
| ExprStmt of EXPR
| InitStmt of {
kind: VAR_DEFN_TAG,
ns: EXPR option,
prototype: bool,
static: bool,
temps: BINDINGS,
inits: INIT_STEP list }
| ClassBlock of CLASS_BLOCK
| ForInStmt of FOR_ENUM_STMT
| ThrowStmt of EXPR
| ReturnStmt of EXPR
| BreakStmt of IDENT option
| ContinueStmt of IDENT option
| BlockStmt of BLOCK
| LabeledStmt of (IDENT * STMT)
| LetStmt of BLOCK
| WhileStmt of WHILE_STMT
| DoWhileStmt of WHILE_STMT
| ForStmt of FOR_STMT
| IfStmt of {
cnd: EXPR,
thn: STMT,
els: STMT }
| WithStmt of {
obj: EXPR,
ty: TYPE_EXPR,
body: STMT }
| TryStmt of {
block: BLOCK,
catches: CATCH_CLAUSE list,
finally: BLOCK option }
| SwitchStmt of { (* FIXME: needs HEAD, DEFNS for defns hoisted from body *)
cond: EXPR,
labels: IDENT list,
cases: CASE list }
| SwitchTypeStmt of {
cond: EXPR,
ty: TYPE_EXPR,
cases: CATCH_CLAUSE list }
| DXNStmt of {
expr: EXPR }
and EXPR =
TernaryExpr of (EXPR * EXPR * EXPR)
| BinaryExpr of (BINOP * EXPR * EXPR)
| BinaryTypeExpr of (BINTYPEOP * EXPR * TYPE_EXPR)
| UnaryExpr of (UNOP * EXPR)
| TypeExpr of TYPE_EXPR
| ThisExpr of THIS_KIND option
| YieldExpr of EXPR option
| SuperExpr of EXPR option
| LiteralExpr of LITERAL
| CallExpr of {
func: EXPR,
actuals: EXPR list }
| ApplyTypeExpr of {
expr: EXPR, (* apply expr to type list *)
actuals: TYPE_EXPR list }
(* defs are rewritten into head by defn phase, and so defs are ignored in verifier and in eval *)
| LetExpr of {
defs: BINDINGS,
body: EXPR,
head: HEAD option }
| NewExpr of {
obj: EXPR,
actuals: EXPR list }
| ObjectRef of {
base: EXPR,
ident: IDENT_EXPR,
loc: LOC option }
| LexicalRef of {
ident: IDENT_EXPR,
loc: LOC option }
| SetExpr of (ASSIGNOP * EXPR * EXPR)
| ListExpr of EXPR list
| InitExpr of (INIT_TARGET * HEAD * INITS) (* HEAD is for temporaries *)
| GetTemp of int
| GetParam of int
| Comprehension of (EXPR * FOR_ENUM_HEAD list * EXPR option)
and INIT_TARGET = Hoisted
| Local
| Prototype
and THIS_KIND = FunctionThis
| GeneratorThis
and FIXTURE_NAME = TempName of int
| PropName of NAME
and IDENT_EXPR =
Identifier of
{ ident : IDENT,
openNamespaces : NAMESPACE list list }
(* CF: the above should be unified with
type MULTINAME = { nss: NAMESPACE list list, id: IDENT }
Perhaps Identifier should be Multiname
*)
| QualifiedExpression of (* type * *)
{ qual : EXPR,
expr : EXPR }
| AttributeIdentifier of IDENT_EXPR
(* for bracket exprs: o[x] and @[x] *)
| ExpressionIdentifier of
{ expr: EXPR,
openNamespaces : NAMESPACE list list }
| QualifiedIdentifier of
{ qual : EXPR,
ident : Ustring.STRING }
| UnresolvedPath of (IDENT list * IDENT_EXPR) (* QualifiedIdentifier or ObjectRef *)
| WildcardIdentifier (* CF: not really an identifier, should be part of T *)
and LITERAL =
LiteralNull
| LiteralUndefined
| LiteralDouble of Real64.real
| LiteralDecimal of Decimal.DEC
| LiteralInt of Int32.int
| LiteralUInt of Word32.word
| LiteralBoolean of bool
| LiteralString of Ustring.STRING
| LiteralArray of
{ exprs: EXPR, (* FIXME: more specific type here *)
ty:TYPE_EXPR option }
| LiteralXML of EXPR list
| LiteralNamespace of NAMESPACE
| LiteralObject of
{ expr : FIELD list,
ty: TYPE_EXPR option }
| LiteralFunction of FUNC
| LiteralRegExp of
{ str: Ustring.STRING }
and BLOCK = Block of DIRECTIVES
(* RIBs are built by the definition phase, not the parser; but they
* are patched back into the AST in class-definition and block
* nodes, so we must define them here. *)
(* ClassFixture only at package level,
* VirtualValFixture only in classes,
*)
and FIXTURE =
NamespaceFixture of NAMESPACE
| ClassFixture of CLS
| InterfaceFixture of IFACE
| TypeVarFixture of TYPEVAR_NONCE
| TypeFixture of TYPE_EXPR
| MethodFixture of
{ func: FUNC,
ty: TYPE_EXPR,
readOnly: bool, (* ES3 funcs are r/w methods with ty=Ast.Special Ast.Any *)
override: bool,
final: bool }
| ValFixture of
{ ty: TYPE_EXPR,
readOnly: bool }
| VirtualValFixture of
{ ty: TYPE_EXPR,
getter: FUNC option,
setter: FUNC option } (* VIRTUAL_VAL_FIXTURE *)
| InheritedFixture of
{ baseName: NAME,
baseTypeArgs: TYPE_EXPR list }
and HEAD =
Head of RIB * INITS
withtype
BINDINGS = (BINDING list * INIT_STEP list)
and RIB = (FIXTURE_NAME * FIXTURE) list
and RIBS = RIB list
and INITS = (FIXTURE_NAME * EXPR) list
(* cf: a class ref of the form C.<int> is represented as
AppType
{ base = LamType
{ params = ["X"],
body = InstanceType
{ name = { ns = Public "", id = "C"},
typeParams = ["X"],
typeArgs = [],
... }},
args = ... }
In the above AST, typeArgs is implicitly ["X"]
*)
and INSTANCE_TYPE =
{ name: NAME,
typeParams: IDENT list,
typeArgs: TYPE_EXPR list,
nonnullable: bool, (* redundant, ignored in verify.sml *)
superTypes: TYPE_EXPR list, (* redundant, ignored in verify.sml *)
ty: TYPE_EXPR, (* redundant, ignored in verify.sml *)
dynamic: bool } (* redundant, ignored in verify.sml *)
and FIELD =
{ kind: VAR_DEFN_TAG,
name: IDENT_EXPR,
init: EXPR }
and FIELD_TYPE =
{ name: IDENT,
ty: TYPE_EXPR }
and FUNC_TYPE =
{ params: TYPE_EXPR list,
result: TYPE_EXPR,
thisType: TYPE_EXPR option,
hasRest: bool, (* if true, the last elem in params is array type *)
minArgs: int } (* necessary because some of params can have defaults *)
and FUNC_DEFN =
{ kind : VAR_DEFN_TAG,
ns: EXPR option,
final: bool,
override: bool,
prototype: bool,
static: bool,
func : FUNC }
and CTOR_DEFN = CTOR
and VAR_DEFN =
{ kind : VAR_DEFN_TAG,
ns : EXPR option,
static : bool,
prototype : bool,
bindings : (BINDING list * INIT_STEP list) (* BINDINGS *)
}
and NAMESPACE_DEFN =
{ ident: IDENT,
ns: EXPR option,
init: EXPR option }
and CLASS_DEFN =
{ ns: EXPR option,
ident: IDENT,
nonnullable: bool,
dynamic: bool,
final: bool,
params: IDENT list,
extends: TYPE_EXPR option,
implements: TYPE_EXPR list,
classDefns: DEFN list,
instanceDefns: DEFN list,
instanceStmts: STMT list,
ctorDefn: CTOR option }
and INTERFACE_DEFN =
{ ident: IDENT,
ns: EXPR option,
nonnullable: bool,
params: IDENT list,
extends: TYPE_EXPR list,
instanceDefns: DEFN list }
and TYPE_DEFN =
{ ident: IDENT,
ns: EXPR option,
init: TYPE_EXPR }
and CLASS_BLOCK =
{ ns: EXPR option,
ident: IDENT,
name: NAME option,
block: BLOCK }
and FOR_ENUM_HEAD = (* FIXME: use this in FOR_ENUM_STMT *)
{ isEach: bool,
bindings: (BINDING list * INIT_STEP list),
expr: EXPR }
and FOR_ENUM_STMT =
{ isEach: bool,
(* VAR_DEFN option *)
defn: { kind : VAR_DEFN_TAG,
ns : EXPR option,
static : bool,
prototype : bool,
bindings : (BINDING list * INIT_STEP list) (* BINDINGS *)
} option,
obj: EXPR,
rib: ((FIXTURE_NAME * FIXTURE) list) option, (* RIB option *)
next: STMT,
labels: IDENT list,
body: STMT }
and FOR_STMT =
{ rib: ((FIXTURE_NAME * FIXTURE) list) option, (* RIB option *) (* CF: list option seems redundant *)
(* VAR_DEFN option *)
defn: { kind : VAR_DEFN_TAG,
ns : EXPR option,
static : bool,
prototype : bool,
bindings : (BINDING list * INIT_STEP list) (* BINDINGS *)
} option,
init: STMT list,
cond: EXPR,
update: EXPR,
labels: IDENT list,
body: STMT }
and WHILE_STMT =
{ cond: EXPR,
rib: ((FIXTURE_NAME * FIXTURE) list) option, (* RIB option *)
body: STMT,
labels: IDENT list }
and DIRECTIVES =
{ pragmas: PRAGMA list,
defns: DEFN list,
head: HEAD option,
body: STMT list,
loc: LOC option }
and CASE =
{ label: EXPR option,
inits: ((FIXTURE_NAME * EXPR) list) option, (* INITS option, replace by INITS?? *)
body: BLOCK } (* FIXME: should be STMT list *)
and CATCH_CLAUSE =
{ bindings:(BINDING list * INIT_STEP list), (* BINDINGS *)
ty: TYPE_EXPR,
rib: ((FIXTURE_NAME * FIXTURE) list) option, (* RIB option *)
inits: ((FIXTURE_NAME * EXPR) list) option, (* INITS option, TODO: replace by INITS?? *)
block:BLOCK }
and FUNC_NAME =
{ kind : FUNC_NAME_KIND,
ident : IDENT }
type VIRTUAL_VAL_FIXTURE =
{ ty: TYPE_EXPR,
getter: FUNC option,
setter: FUNC option }
datatype FRAGMENT =
Package of { name: IDENT list,
fragments: FRAGMENT list }
| Anon of BLOCK
end