-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathcompile-structs.arr
More file actions
3026 lines (2971 loc) · 113 KB
/
compile-structs.arr
File metadata and controls
3026 lines (2971 loc) · 113 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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang pyret
provide *
provide-types *
import ast as A
import srcloc as SL
import error-display as ED
import string-dict as SD
import file("concat-lists.arr") as CL
import file("type-structs.arr") as T
import file("js-ast.arr") as J
clist = CL.clist
t-nothing = T.t-nothing(A.dummy-loc)
t-str = T.t-string(A.dummy-loc)
t-boolean = T.t-boolean(A.dummy-loc)
t-number = T.t-number(A.dummy-loc)
t-arrow = T.t-arrow(_, _, A.dummy-loc, false)
t-top = T.t-top(A.dummy-loc, false)
t-bot = T.t-bot(A.dummy-loc, false)
t-record = T.t-record(_, A.dummy-loc, false)
t-forall = T.t-forall(_, _, A.dummy-loc, false)
t-var = T.t-var(_, A.dummy-loc, false)
t-array = T.t-array(_, A.dummy-loc)
t-string = T.t-string(A.dummy-loc)
t-option = T.t-option(_, A.dummy-loc)
t-data = T.t-data(_, _, _, _, A.dummy-loc)
t-variant = T.t-variant(_, _, _, A.dummy-loc)
t-singleton-variant = T.t-singleton-variant(_, _, A.dummy-loc)
t-app = T.t-app(_, _, A.dummy-loc, false)
t-name = T.t-name(_, _, A.dummy-loc, false)
is-t-app = T.is-t-app
type URI = String
type StringDict = SD.StringDict
string-dict = SD.string-dict
mutable-string-dict = SD.mutable-string-dict
type MutableStringDict = SD.MutableStringDict
is-s-block = A.is-s-block
type Loc = SL.Srcloc
data Dependency:
| dependency(protocol :: String, arguments :: List<String>)
with:
method key(self): self.protocol + "(" + self.arguments.join-str(", ") + ")" end
| builtin(modname :: String)
with:
method key(self): "builtin(" + self.modname + ")" end
end
data NativeModule:
| requirejs(path :: String)
end
data BindOrigin:
| bo-local(loc :: Loc)
| bo-module(uri :: URI)
end
data ValueBinder:
| vb-letrec
| vb-let
| vb-var
| vb-module(uri :: URI) # The A in import ast as A (with URI determined from compile env)
end
data ValueBind:
| value-bind(
origin :: BindOrigin,
binder :: ValueBinder,
atom :: A.Name,
ann :: A.Ann,
expr :: Option<A.Expr>)
end
data TypeBinder:
| tb-type-let
| tb-type-var
| tb-module(uri :: URI)
end
data TypeBind:
| type-bind(
origin :: BindOrigin,
binder :: TypeBinder,
atom :: A.Name,
ann :: Option<A.Ann>)
end
data ScopeResolution:
| resolved-scope(ast :: A.Program, errors :: List<CompileError>)
end
data NameResolution:
| resolved-names(
ast :: A.Program,
errors :: List<CompileError>,
bindings :: SD.MutableStringDict<ValueBind>,
type-bindings :: SD.MutableStringDict<TypeBind>,
datatypes :: SD.MutableStringDict<A.Expr>)
end
# Used to describe when additional module imports should be added to a
# program. See wrap-extra-imports
data ExtraImports:
| extra-imports(imports :: List<ExtraImport>)
end
# Import this module, and bind the given value and type bindings from it
data ExtraImport:
| extra-import(dependency :: Dependency, as-name :: String, values :: List<String>, types :: List<String>)
end
data Loadable:
| module-as-string(provides :: Provides, compile-env :: CompileEnvironment, result-printer :: CompileResult<Any>)
# NOTE(joe): there's a circular dependency between this module and js-of-pyret.arr; hence the Any above
end
data CompileEnvironment:
| compile-env(
globals :: Globals,
all-modules :: MutableStringDict<Loadable>,
my-modules :: StringDict<URI>
)
sharing:
# TODO(joe/ben/jose): Write a recursive lookup here
# that fully resolves names (once ValueExport and
# friends have that information)
method value-by-uri(self, uri :: String, name :: String):
self.all-modules
.get-value-now(uri)
.provides
.values.get(name)
end,
method value-by-uri-value(self, uri :: String, name :: String):
cases(Option) self.value-by-uri(uri, name):
| none => raise("Could not find value " + name + " on module " + uri)
| some(v) => v
end
end,
method type-by-uri(self, uri, name):
self.all-modules
.get-value-now(uri)
.provides
.types.get(name)
end,
method type-by-uri-value(self, uri, name):
cases(Option) self.type-by-uri(uri, name):
| none => raise("Could not find type " + name + " on module " + uri)
| some(v) => v
end
end,
method global-value(self, name :: String):
self.globals.values.get(name)
.and-then(self.value-by-dep-key(_, name))
.and-then(_.value)
end,
method global-type(self, name :: String):
self.globals.types.get(name)
.and-then(self.type-by-dep-key(_, name))
.and-then(_.value)
end,
method uri-by-dep-key(self, dep-key):
self.my-modules.get-value(dep-key)
end,
method provides-by-uri(self, uri):
self.all-modules.get-now(uri)
.and-then(_.provides)
end,
method provides-by-dep-key(self, dep-key):
self.my-modules.get(dep-key)
.and-then(self.all-modules.get-value-now(_))
.and-then(_.provides)
end,
method provides-by-dep-key-value(self, dep-key):
cases(Option) self.provides-by-dep-key(dep-key):
| none => raise("Could not find dep key: " + dep-key)
| some(shadow provides) => provides
end
end,
method provides-by-value-name(self, name):
self.globals.values.get(name)
.and-then(self.provides-by-dep-key(_))
.and-then(_.value)
end,
method provides-by-value-name-value(self, name):
cases(Option) self.provides-by-value-name(name):
| none => raise("Could not find value " + name)
| some(shadow provides) => provides
end
end,
method provides-by-type-name(self, name):
self.globals.types.get(name)
.and-then(self.provides-by-dep-key(_))
.and-then(_.value)
end,
method provides-by-type-name-value(self, name):
cases(Option) self.provides-by-type-name(name):
| none => raise("Could not find type " + name)
| some(shadow provides) => provides
end
end,
method value-by-dep-key(self, dep-key, name):
uri = self.my-modules.get-value(dep-key)
self.value-by-uri(uri, name)
end,
method value-by-dep-key-value(self, dep-key, name):
cases(Option) self.value-by-dep-key(dep-key, name):
| none => raise("Could not find " + name + " on " + dep-key)
| some(v) => v
end
end,
method type-by-dep-key(self, dep-key, name):
uri = self.my-modules.get-value(dep-key)
self.type-by-uri(uri, name)
end,
method uri-by-value-name(self, name):
self.globals.values.get(name)
.and-then(self.uri-by-dep-key(_))
end,
method uri-by-type-name(self, name):
self.globals.types.get(name)
.and-then(self.uri-by-dep-key(_))
end
end
# The strings in globals should be the appropriate dependency (e.g. in my-modules)
data Globals:
| globals(values :: StringDict<String>, types :: StringDict<String>)
end
data ValueExport:
| v-just-type(t :: T.Type)
| v-var(t :: T.Type)
| v-fun(t :: T.Type, name :: String, flatness :: Option<Number>)
end
data Provides:
| provides(
from-uri :: URI,
values :: StringDict<ValueExport>,
aliases :: StringDict<T.Type>,
data-definitions :: StringDict<T.DataType>
)
end
fun make-dep(raw-dep) -> Dependency:
if raw-dep.import-type == "builtin":
builtin(raw-dep.name)
else:
dependency(raw-dep.protocol, raw-array-to-list(raw-dep.args))
end
end
rag = raw-array-get
fun value-export-from-raw(uri, val-export, tyvar-env :: SD.StringDict<T.Type>) -> ValueExport block:
t = val-export.tag
typ = type-from-raw(uri, val-export.typ, tyvar-env)
if t == "v-fun":
v-fun(typ, t, none)
else:
v-just-type(typ)
end
end
fun type-from-raw(uri, typ, tyvar-env :: SD.StringDict<T.Type>) block:
tfr = type-from-raw(uri, _, tyvar-env)
# TODO(joe): Make this do something intelligent when location information
# is available
l = SL.builtin(uri)
t = typ.tag
#print("\n\ntyp: " + tostring(typ))
ask:
| t == "any" then: T.t-top(l, false)
| t == "bot" then: T.t-bot(l, false)
| t == "record" then:
T.t-record(typ.fields.foldl(lam(f, fields): fields.set(f.name, tfr(f.value)) end, [string-dict: ]), l, false)
| t == "tuple" then:
T.t-tuple(for map(e from typ.elts): tfr(e) end, l, false)
| t == "name" then:
if typ.origin.import-type == "$ELF":
T.t-name(T.local, A.s-type-global(typ.name), l, false)
else if typ.origin.import-type == "uri":
T.t-name(T.module-uri(typ.origin.uri), A.s-type-global(typ.name), l, false)
else:
T.t-name(T.dependency(make-dep(typ.origin)), A.s-type-global(typ.name), l, false)
end
| t == "tyvar" then:
cases(Option<T.Type>) tyvar-env.get(typ.name):
| none => raise("Unbound type variable " + typ.name + " in provided type.")
| some(tv) => T.t-var(tv, l, false)
end
| t == "forall" then:
new-env = for fold(new-env from tyvar-env, a from typ.args):
tvn = A.global-names.make-atom(a)
new-env.set(a, tvn)
end
params = for SD.map-keys(k from new-env):
T.t-var(new-env.get-value(k), l, false)
end
T.t-forall(params, type-from-raw(uri, typ.onto, new-env), l, false)
| t == "tyapp" then:
T.t-app(tfr(typ.onto), map(tfr, typ.args), l, false)
| t == "arrow" then:
T.t-arrow(map(tfr, typ.args), tfr(typ.ret), l, false)
| otherwise: raise("Unknown raw tag for type: " + t)
end
end
fun tvariant-from-raw(uri, tvariant, env):
l = SL.builtin(uri)
t = tvariant.tag
ask:
| t == "variant" then:
members = tvariant.vmembers.foldr(lam(tm, members):
link({tm.name; type-from-raw(uri, tm.typ, env)}, members)
end, empty)
t-variant(tvariant.name, members, [string-dict: ])
| t == "singleton-variant" then:
t-singleton-variant(tvariant.name, [string-dict: ])
| otherwise: raise("Unkonwn raw tag for variant: " + t)
end
end
fun datatype-from-raw(uri, datatyp):
l = SL.builtin(uri)
if datatyp.tag == "any":
# TODO(joe): this will be replaced when datatypes have a settled format
t-top
else:
pdict = for fold(pdict from SD.make-string-dict(), a from datatyp.params):
tvn = A.global-names.make-atom(a)
pdict.set(a, tvn)
end
params = for SD.map-keys(k from pdict):
T.t-var(pdict.get-value(k), l, false)
end
variants = map(tvariant-from-raw(uri, _, pdict), datatyp.variants)
members = datatyp.methods.foldl(lam(tm, members):
members.set(tm.name, type-from-raw(uri, tm.value, pdict))
end, [string-dict: ])
t-data(datatyp.name, params, variants, members)
end
end
fun provides-from-raw-provides(uri, raw):
values = raw.values
vdict = for fold(vdict from SD.make-string-dict(), v from raw.values):
if is-string(v) block:
vdict.set(v, v-just-type(t-top))
else:
if v.value.bind == "var":
vdict.set(v.name, v-var(type-from-raw(uri, v.value.typ, SD.make-string-dict())))
else if v.value.bind == "fun":
flatness = if is-number(v.value.flatness):
some(v.value.flatness)
else:
none
end
vdict.set(v.name, v-fun(type-from-raw(uri, v.value.typ, SD.make-string-dict()), v.value.name, flatness))
else:
vdict.set(v.name, v-just-type(type-from-raw(uri, v.value.typ, SD.make-string-dict())))
end
end
end
aliases = raw.aliases
adict = for fold(adict from SD.make-string-dict(), a from raw.aliases):
if is-string(a):
adict.set(a, t-top)
else:
adict.set(a.name, type-from-raw(uri, a.typ, SD.make-string-dict()))
end
end
datas = raw.datatypes
ddict = for fold(ddict from SD.make-string-dict(), d from raw.datatypes):
ddict.set(d.name, datatype-from-raw(uri, d.typ))
end
provides(uri, vdict, adict, ddict)
end
fun provides-to-raw-provides-ast(provs, env):
cases(Provides) provs:
| provides(uri, values, aliases, data-defs) =>
#|
value-fields = for CL.map_list(v from values.keys().to-list()):
J.j-field(v, type-to-raw-ast(values.get-value(v), compile-env))
end
data-fields = for CL.map_list(d from data-defs.keys().to-list()):
J.j-field(d, type-to-raw-ast(data-defs.get-value(d), compile-env))
end
alias-fields = for CL.map_list(a from aliases.keys().to-list()):
J.j-field(a, type-to-raw-ast(aliases.get-value(a), compile-env))
end
|#
J.j-obj([clist:
#|J.j-field("values", J.j-obj(value-fields)),
J.j-field("datatypes", J.j-obj(data-fields)),
J.j-field("aliases", J.j-obj(alias-fields))|#
])
end
end
data CompileResult<C>:
| ok(code :: C)
| err(problems :: List<CompileError>)
end
fun draw-and-highlight(l):
ED.loc-display(l, "error-highlight", ED.loc(l))
end
data CompileError:
| wf-err(msg :: List<ED.ErrorDisplay>, loc :: A.Loc) with:
method render-fancy-reason(self):
self.render-reason()
end,
method render-reason(self):
[ED.error:
ED.paragraph([list: ED.highlight(ED.text("Well-formedness:"), [list: self.loc], 0), ED.text(" ")]
+ self.msg),
ED.cmcode(self.loc)]
end
| wf-empty-block(loc :: A.Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight(ED.text("block"),[list: self.loc], 0),
ED.text(" is empty:")],
ED.cmcode(self.loc)]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Pyret rejected your program because there is an empty block at")],
[ED.para: draw-and-highlight(self.loc)]]
end
| wf-err-split(msg :: String, loc :: List<A.Loc>) with:
method render-fancy-reason(self):
self.render-reason()
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Well-formedness:"),
ED.text(self.msg),
ED.text("at")],
ED.v-sequence(self.loc.map(lam(l): [ED.para: draw-and-highlight(l)] end))]
end
| reserved-name(loc :: Loc, id :: String) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Reading a "),
ED.highlight(ED.text("name"), [list: self.loc], 0),
ED.text(" errored:")],
ED.cmcode(self.loc),
[ED.para:
ED.text("This name is reserved is reserved by Pyret, and cannot be used as an identifier.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("The name "),
ED.code(ED.text(self.id)),
ED.text(" at "),
ED.loc(self.loc),
ED.text(" is reserved by Pyret, and cannot be used as an identifier.")]]
end
| contract-on-import(loc :: Loc, name :: String, import-type :: A.ImportType) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Contracts for functions can only be defined once, and the contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0),
ED.text(" is already defined in the "),
ED.highlight(ED.code(ED.text(self.import-type.tosource().pretty(1000).join-str(""))),
[list: self.import-type.l], 1),
ED.text(" library.")],
ED.cmcode(self.loc)]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Contracts for functions can only be defined once, and the contract for "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" is already defined in the "),
ED.code(ED.text(self.import-type.tosource().pretty(1000).join-str(""))),
ED.text(" library.")]]
end
| contract-redefined(loc :: Loc, name :: String, defn-loc :: Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Contracts for functions can only be defined once, and the contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0),
ED.text(" is "),
ED.highlight(ED.text("already defined"), [list: self.defn-loc], -1),
ED.text(": ")],
ED.cmcode(self.defn-loc)]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Contracts for functions can only be defined once, and the contract for "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" is already defined at "), ED.loc(self.defn-loc)]]
end
| contract-non-function(loc :: Loc, name :: String, defn-loc :: Loc, defn-is-function :: Boolean) with:
method render-fancy-reason(self):
if self.defn-is-function:
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0),
ED.text(" is not a valid function contract, but "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.defn-loc], -1),
ED.text(" is defined as a function.")],
ED.cmcode(self.loc),
[ED.para:
ED.text("The contract and the "),
ED.highlight(ED.text("definition"), [list: self.defn-loc], -1),
ED.text(" must be consistent.")],
ED.cmcode(self.defn-loc)]
else:
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0),
ED.text(" is a function contract, but "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.defn-loc], -1),
ED.text(" is not defined as a function.")],
ED.cmcode(self.loc),
[ED.para:
ED.text("The contract and the "),
ED.highlight(ED.text("definition"), [list: self.defn-loc], -1),
ED.text(" must be consistent.")],
ED.cmcode(self.defn-loc)]
end
end,
method render-reason(self):
if self.defn-is-function:
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" is not a valid function contract, but "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.defn-loc),
ED.text(" is defined as a function.")],
[ED.para: ED.text("The contract and the definition must be consistent.")]]
else:
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" is a function contract, but "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.defn-loc),
ED.text(" is not defined as a function.")],
[ED.para: ED.text("The contract and the definition must be consistent.")]]
end
end
| contract-inconsistent-names(loc :: Loc, name :: String, defn-loc :: Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0)],
ED.cmcode(self.loc),
[ED.para:
ED.text("specifies arguments that are inconsistent with the "),
ED.highlight(ED.text("associated definition"), [list: self.defn-loc], -1), ED.text(":")],
ED.cmcode(self.defn-loc)]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" specifies arguments that are inconsistent with the definition at "), ED.loc(self.defn-loc)]]
end
| contract-inconsistent-params(loc :: Loc, name :: String, defn-loc :: Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0)],
ED.cmcode(self.loc),
[ED.para:
ED.text("specifies type parameters that are inconsistent with the "),
ED.highlight(ED.text("associated definition"), [list: self.defn-loc], -1), ED.text(":")],
ED.cmcode(self.defn-loc)]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" specifies type parameters that are inconsistent with the definition at "), ED.loc(self.defn-loc)]]
end
| contract-unused(loc :: Loc, name :: String) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("The contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0)],
ED.cmcode(self.loc),
[ED.para:
ED.text(" does not match the name of any function definition.")],
[ED.para:
ED.text("Contracts must appear just before their function's definition (or just before the function's examples block). Check the spelling of this contract's name, or move it closer to its function if necessary.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("The contract for "), ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" does not match the name of any function definition.")],
[ED.para:
ED.text("Contracts must appear just before their function's definition (or just before the function's examples block). Check the spelling of this contract's name, or move it closer to its function if necessary.")]]
end
| contract-bad-loc(loc :: Loc, name :: String, defn-loc :: Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Contracts must appear just before their associated definition (or just before the function's examples block). The contract for "),
ED.highlight(ED.code(ED.text(self.name)), [list: self.loc], 0)],
ED.cmcode(self.loc),
[ED.para: ED.text(" comes after its "),
ED.highlight(ED.text("associated definition"), [list: self.defn-loc], -1), ED.text(".")],
ED.cmcode(self.defn-loc),
[ED.para: ED.text("Move the contract just before its function.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Contracts must appear just before their associated definition (or just before the function's examples block). The contract for "), ED.code(ED.text(self.name)), ED.text(" at "), ED.loc(self.loc),
ED.text(" comes after its associated definition at "), ED.loc(self.defn-loc), ED.text(". Move the contract just before its function.")]]
end
| zero-fraction(loc, numerator) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Reading a "),
ED.highlight(ED.text("fraction literal expression"), [ED.locs: self.loc], 0),
ED.text(" errored:")],
ED.cmcode(self.loc),
[ED.para:
ED.text("Its denominator is zero.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Pyret disallows the fraction literal expression")],
[ED.para:
ED.code([ED.sequence:
ED.embed(self.numerator),
ED.text(" / 0")])],
[ED.para:
ED.text("at "),
ED.loc(self.loc),
ED.text(" because its denominator is zero.")]]
end
| invalid-unit-power(loc, power) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Reading a "),
ED.highlight(ED.text("unit annotation"), [ED.locs: self.loc], 0),
ED.text(" errored:")],
ED.cmcode(self.loc),
[ED.para:
ED.text("The exponent "),
ED.embed(self.power),
ED.text(" is not allowed in unit expressions. "),
ED.text("Make sure to use a non-zero integer value.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Pyret disallows units with the exponent")],
[ED.para:
ED.embed(self.power)],
[ED.para:
ED.text("at "),
ED.loc(self.loc),
ED.text(". Make sure to use a non-zero integer value.")]]
end
| one-as-power-base(loc, power) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Reading a "),
ED.highlight(ED.text("unit annotation"), [ED.locs: self.loc], 0),
ED.text(" errored:")],
ED.cmcode(self.loc),
[ED.para:
ED.code(ED.text("1")),
ED.text(" is raised to the power "),
ED.embed(self.power),
ED.text(". One cannot be raised to a power")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.code(ED.text("1")),
ED.text(" is raised to the power ")],
[ED.para:
ED.embed(self.power)],
[ED.para:
ED.text("at "),
ED.loc(self.loc),
ED.text(". One cannot be raised to a power")]]
end
| mixed-binops(exp-loc, op-a-name, op-a-loc, op-b-name, op-b-loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Reading this "),
ED.highlight(ED.text("expression"), [ED.locs: self.exp-loc], -1),
ED.text(" errored:")],
ED.cmcode(self.exp-loc),
[ED.para:
ED.text("The "),
ED.code(ED.highlight(ED.text(self.op-a-name),[list: self.op-a-loc], 0)),
ED.text(" operation is at the same level as the "),
ED.code(ED.highlight(ED.text(self.op-b-name),[list: self.op-b-loc], 1)),
ED.text(" operation.")],
[ED.para:
ED.text("Use parentheses to group the operations and to make the order of operations clear.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Operators of different kinds cannot be mixed at the same level, but "),
ED.code(ED.text(self.op-a-name)),
ED.text(" is at "),
ED.loc(self.op-a-loc),
ED.text(" at the same level as "),
ED.code(ED.text(self.op-b-name)),
ED.text(" at "),
ED.loc(self.op-b-loc),
ED.text(". Use parentheses to group the operations and to make the order of operations clear.")]]
end
| mixed-unit-ops(exp-loc, op-a-name, op-a-loc, op-b-name, op-b-loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("Reading this "),
ED.highlight(ED.text("unit"), [ED.locs: self.exp-loc], -1),
ED.text(" errored:")],
ED.cmcode(self.exp-loc),
[ED.para:
ED.text("The "),
ED.code(ED.highlight(ED.text(self.op-a-name),[list: self.op-a-loc], 0)),
ED.text(" operation is at the same level as the "),
ED.code(ED.highlight(ED.text(self.op-b-name),[list: self.op-b-loc], 1)),
ED.text(" operation.")],
[ED.para:
ED.text("Use parentheses to group the operations and to make the order of operations clear.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Unit operators of different kinds cannot be mixed at the same level, but "),
ED.code(ED.text(self.op-a-name)),
ED.text(" is at "),
ED.loc(self.op-a-loc),
ED.text(" at the same level as "),
ED.code(ED.text(self.op-b-name)),
ED.text(" at "),
ED.loc(self.op-b-loc),
ED.text(". Use parentheses to group the operations and to make the order of operations clear.")]]
end
| block-ending(l :: Loc, block-loc :: Loc, kind) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight(ED.text("block"), [list: self.block-loc], -1),
ED.text(" ends with a "),
ED.highlight(ED.text(self.kind), [list: self.l], 0),
ED.text(":")],
ED.cmcode(self.l),
[ED.para:
ED.text("Blocks should end with an expression")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("The block at "),
ED.loc(self.block-loc),
ED.text(" ends with a " + self.kind + " at "),
ED.loc(self.l),
ED.text(". Blocks should end with an expression.")]]
end
| single-branch-if(expr :: A.Expr) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("An "),
ED.highlight(ED.text("if-expression"), [list: self.expr.l], -1),
ED.text(" has only one "),
ED.highlight(ED.text("branch"), [list: self.expr.branches.first.l], 0),
ED.text(":")],
ED.cmcode(self.expr.l)]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("If-expressions may not only have one branch, but the if-expression at "),
ED.loc(self.expr.l),
ED.text(" does not have any other branches.")]]
end
| unwelcome-where(kind, loc, block-loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("A "),
ED.highlight(ED.code(ED.text("where")), [list: self.block-loc], 0),
ED.text(" can't be added to a "),
ED.highlight(ED.text(self.kind), [list: self.loc], -1),
ED.text(":")],
ED.cmcode(self.block-loc),
[ED.para:
ED.text("A "),
ED.code(ED.text("where")),
ED.text(" block may only be added to named function declarations"),
ED.text(".")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.code(ED.text("`where`")),
ED.text(" blocks are only allowed on named function and declarations; a where block may not be added to a "),
ED.loc(self.kind),
ED.text(" at "),
ED.loc(self.loc),
ED.text(".")]]
end
| non-example(expr :: A.Expr) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.highlight(ED.text("This"),[list: self.expr.l], 0),
ED.text(" is not a testing statement:")],
ED.cmcode(self.expr.l),
[ED.para:
ED.code(ED.text("example")),
ED.text(" blocks must only contain testing statements.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.code(ED.text("example")),
ED.text(" blocks must only contain testing statements, but the statement at "),
ED.loc(self.expr.l),
ED.text(" isn't a testing statement.")]]
end
| tuple-get-bad-index(l, tup, index, index-loc) with:
method render-fancy-reason(self):
if not(num-is-integer(self.index)):
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight(ED.text("tuple indexing"), [list: self.l], -1),
ED.text(" expression cannot extract a "),
ED.highlight(ED.text("non-integer position"),[list: self.index-loc],0),
ED.text(".")],
ED.cmcode(self.l)]
else if self.index < 0:
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight(ED.text("tuple indexing"), [list: self.l], -1),
ED.text(" expression cannot extract a "),
ED.highlight(ED.text("negative position"),[list: self.index-loc],0),
ED.text(".")],
ED.cmcode(self.l)]
else:
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight(ED.text("tuple indexing"), [list: self.l], -1),
ED.text(" expression cannot extract an "),
ED.highlight(ED.text("index"),[list: self.index-loc],0),
ED.text(" that large. There are no tuples that big.")],
ED.cmcode(self.l)]
end
end,
method render-reason(self):
if not(num-is-integer(self.index)):
[ED.error:
[ED.para:
ED.text("The tuple indexing expression at "),
ED.loc(self.l),
ED.text(" was given an invalid, non-integer index.")]]
else if self.index < 0:
[ED.error:
[ED.para:
ED.text("The tuple indexing expression at "),
ED.loc(self.l),
ED.text(" was given an invalid, negative index.")]]
else:
[ED.error:
[ED.para:
ED.text("The tuple indexing expression at "),
ED.loc(self.l),
ED.text(" was given an index bigger than any tuple.")]]
end
end
| import-arity-mismatch(l, kind, args, expected-arity, expected-args) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight([ED.sequence: ED.code(ED.text(self.kind)), ED.text(" import statement")],
[list: self.l], -1),
ED.text(":")],
ED.cmcode(self.l),
[ED.para:
ED.text("expects "),
ED.ed-args(self.expected-arity),
ED.text(":")],
ED.bulleted-sequence(self.expected-args.map(ED.text))]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("This "),
ED.code(ED.text(self.kind)),
ED.text(" import statement at "),
ED.loc(self.l),
ED.text(" expects "),
ED.ed-args(self.expected-arity),
ED.text(":")],
ED.bulleted-sequence(self.expected-args.map(ED.text))]
end
| no-arguments(expr) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("This "),
ED.highlight(ED.text("method declaration"), [list: self.expr.l], 0),
ED.text(" should accept at least one argument:")],
ED.cmcode(self.expr.l),
[ED.para:
ED.text("When a method is applied, the first argument is a reference to the object it belongs to.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("Method declarations should accept at least one argument, but the method declaration at "),
ED.loc(self.expr.l),
ED.text(" has no arguments. When a method is applied, the first argument is a reference to the object it belongs to.")]]
end
| non-toplevel(kind, l :: Loc, parent-loc :: Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("This "),
ED.code(ED.highlight(ED.text(self.kind), [ED.locs: self.l], 0)),
ED.text(" is inside "),
ED.highlight(ED.text("another block"), [list: self.parent-loc], -1),
ED.text(":")],
ED.cmcode(self.l),
[ED.para:
ED.text(self.kind),
ED.text(" may only occur at the top-level of the program.")]]
end,
method render-reason(self):
[ED.error:
[ED.para:
ED.text("You may only define the "),
ED.code(ED.text(self.kind)),
ED.text(" at "),
ED.loc(self.l),
ED.text(" at the top-level.")]]
end
| unwelcome-test(loc :: Loc) with:
method render-fancy-reason(self):
[ED.error:
[ED.para:
ED.text("The "),
ED.highlight(ED.text("testing statement"),[list: self.loc], 0)],
ED.cmcode(self.loc),
[ED.para:
ED.text("is not inside a "),
ED.code(ED.text("check")),
ED.text(", "), ED.code(ED.text("where")),
ED.text(" or "),