forked from monte-language/spotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotter.ml
1218 lines (1033 loc) · 37.4 KB
/
spotter.ml
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
open CamomileLibraryDefault.Camomile
type monte =
< call: string -> monte list -> (monte * monte) list -> monte option
; stringOf: string
; unwrap: monteprim option >
and monteprim =
| MNull
| MBool of bool
| MChar of int
| MDouble of float
| MInt of Z.t
| MStr of string
| MList of monte list
let prim_name p =
match p with
| MNull -> "Null"
| MBool _ -> "Bool"
| MChar _ -> "Char"
| MDouble _ -> "Double"
| MInt _ -> "Int"
| MStr _ -> "Str"
| MList _ -> "List"
(* Narrowing: Cast away extra non-private methods of Monte objects. *)
let to_monte
(m :
< call: string -> monte list -> (monte * monte) list -> monte option
; stringOf: string
; unwrap: monteprim option
; .. >) : monte =
(m :> monte)
module UTF8D = struct
(** `decode bs` gives either Ok (code, rest) or Error (consumed_bytes, rest) *)
let decode1 (bs : int Seq.t) : (int * int Seq.t, int list * int Seq.t) result
=
let open Int in
let mask2 = 0b11000000
and mask3 = 0b11100000
and mask4 = 0b11110000
and mask5 = 0b11111000
and mask b m = logand b (lognot m) in
let is_0xxxxxxx b = shift_right b 7 = 0b0
and is_110xxxxx b = shift_right b 5 = 0b110
and is_1110xxxx b = shift_right b 4 = 0b1110
and is_11110xxx b = shift_right b 3 = 0b11110
and cat hi n lo = logor (shift_left hi n) lo in
match bs () with
| Nil -> Error ([], bs)
| Cons (b0, b1n) when is_0xxxxxxx b0 -> Ok (b0, b1n)
| Cons (b0, b1n) -> (
match b1n () with
| Nil -> Error ([b0], b1n)
| Cons (b1, b2n) when is_110xxxxx b0 ->
let code = cat (mask b0 mask3) 6 (mask b1 mask2) in
Ok (code, b2n)
| Cons (b1, b2n) -> (
match b2n () with
| Nil -> Error ([b0; b1], b2n)
| Cons (b2, b3n) when is_1110xxxx b0 ->
let code =
cat (cat (mask b0 mask4) 6 (mask b1 mask2)) 6 (mask b2 mask2)
in
Ok (code, b3n)
| Cons (b2, b3n) -> (
match b3n () with
| Nil -> Error ([b0; b1; b2], b3n)
| Cons (b3, b4n) when is_11110xxx b0 ->
let code =
cat
(cat
(cat (mask b0 mask5) 6 (mask b1 mask2))
6 (mask b2 mask2))
6 (mask b3 mask2) in
Ok (code, b4n)
| Cons (b3, b4n) -> Error ([b0; b1; b2; b3], b4n) ) ) )
end
let rec seq_of_chan inch : int Seq.t =
fun () ->
try
let b0 = input_byte inch in
Seq.Cons (b0, seq_of_chan inch)
with End_of_file -> Nil
module type MAST = sig
type span
val oneToOne : Z.t * Z.t * Z.t * Z.t -> span
val blob : Z.t * Z.t * Z.t * Z.t -> span
type t
type patt
type narg
type nparam
type meth
type matcher
val charExpr : int -> span -> t
val doubleExpr : float -> span -> t
val intExpr : Z.t -> span -> t
val strExpr : string -> span -> t
val nounExpr : string -> span -> t
val bindingExpr : string -> span -> t
val seqExpr : t list -> span -> t
val callExpr : t -> string -> t list -> narg list -> span -> t
val defExpr : patt -> t option -> t -> span -> t
val escapeExpr : patt -> t -> span -> t
val escapeCatchExpr : patt -> t -> patt -> t -> span -> t
val objectExpr :
string
-> patt
-> t option
-> t list
-> meth list
-> matcher list
-> span
-> t
val assignExpr : string -> t -> span -> t
val tryExpr : t -> patt -> t -> span -> t
val finallyExpr : t -> t -> span -> t
val hideExpr : t -> span -> t
val ifExpr : t -> t -> t option -> span -> t
val metaStateExpr : span -> t
val metaContextExpr : span -> t
val metho :
string
-> string
-> patt list
-> nparam list
-> t option
-> t
-> span
-> meth
val matche : patt -> t -> span -> matcher
val namedArg : t -> t -> span -> narg
val namedParam : t -> patt -> t option -> span -> nparam
val ignorePatt : t option -> span -> patt
val finalPatt : string -> t option -> span -> patt
val varPatt : string -> t option -> span -> patt
val listPatt : patt list -> span -> patt
val viaPatt : t -> patt -> span -> patt
val bindingPatt : string -> span -> patt
end
module Dict = Map.Make (String)
module AtomDict = Map.Make (struct
type t = string * int
let compare = compare
end)
let nullObj : monte =
object
method call verb args namedArgs = None
method stringOf = "<null>"
method unwrap = Some MNull
end
let boolObj b : monte =
object
method call verb args namedArgs =
match (verb, args) with
| "pick", [x; y] -> Some (if b then x else y)
| _ -> None
method stringOf = if b then "true" else "false"
method unwrap = Some (MBool b)
end
let char_lit c =
match c with
| _ when c > 0xffff -> Printf.sprintf "\\U%08x" c
| _ when c > 0x7f -> Printf.sprintf "\\u%04x" c
| _ when c < 0x20 -> Printf.sprintf "\\u%04x" c
| _ when c = Char.code '\\' -> "\"\\\""
| _ when c = Char.code '\"' -> "\"\"\""
| _ -> Char.escaped (char_of_int c)
let charObj (c : int) : monte =
object
method call verb args namedArgs = match (verb, args) with _ -> None
(* XXX quotes? *)
method stringOf = "'" ^ char_lit c ^ "'"
method unwrap = Some (MChar c)
end
let doubleObj d : monte =
object
method call verb args namedArgs = match (verb, args) with _ -> None
method stringOf = string_of_float d
method unwrap = Some (MDouble d)
end
let rec intObj i : monte =
object
method call verb args namedArgs =
match (verb, args) with
| "next", [] -> Some (intObj (Z.succ i))
| "previous", [] -> Some (intObj (Z.pred i))
| "add", [jj] -> (
match jj#unwrap with
| Some (MInt j) -> Some (intObj (Z.add i j))
| _ -> None )
| "multiply", [jj] -> (
match jj#unwrap with
| Some (MInt j) -> Some (intObj (Z.mul i j))
| _ -> None )
| _ -> None
method stringOf = Z.to_string i
method unwrap = Some (MInt i)
end
let rec strObj s : monte =
object
method call verb (args : monte list) namedArgs : monte option =
match (verb, args) with
| "add", [otherObj] -> (
match otherObj#unwrap with
| Some (MStr other) -> Some (strObj (s ^ other))
| Some (MChar other) ->
let utf8 code = String.make 0 (Char.chr code) (* XXX *) in
Some (strObj (s ^ utf8 other))
| _ -> None (* WrongType? fwd ref *) )
| "size", [] -> Some (intObj (Z.of_int (UTF8.length s)))
| _ -> None
method stringOf =
let parts =
List.map
(fun ch -> char_lit (Char.code ch))
(List.of_seq (String.to_seq s)) in
"\"" ^ String.concat "" parts ^ "\""
method unwrap = Some (MStr s)
end
let bindingObj slot : monte =
object
method call verb args namedArgs =
match (verb, args) with "get", [] -> Some slot | _ -> None
method stringOf = "<binding>"
method unwrap = None
end
let finalSlotObj value : monte =
object
method call verb args namedArgs =
match (verb, args) with "get", [] -> Some value | _ -> None
method stringOf = "<final slot>"
method unwrap = None
end
let varSlotObj value : monte =
object
val mutable cell = value
method call verb args namedArgs =
match (verb, args) with
| "get", [] -> Some cell
| "put", [v] ->
cell <- v ;
Some nullObj
| _ -> None
method stringOf = "<var slot>"
method unwrap = None
end
type mspan =
| OneToOne of (Z.t * Z.t * Z.t * Z.t)
| Blob of (Z.t * Z.t * Z.t * Z.t)
let string_of_span span =
let sos (x1, y1, x2, y2) =
String.concat ":" (List.map Z.to_string [x1; y1; x2; y2]) in
match span with OneToOne t -> "str:" ^ sos t | Blob t -> "blob:" ^ sos t
type mexn =
| Refused of (monte * string * monte list * monte list)
| Ejecting of (monte * monte)
| DoubleThrown
| WrongType of (monteprim * monte)
| NameError of (string * mspan)
| MissingNamedArg of (monte * mspan)
| UserException of monte
let string_of_mexn m =
match m with
| Refused (target, verb, args, namedArgs) ->
"Message refused: " ^ target#stringOf ^ "." ^ verb ^ "/"
^ string_of_int (List.length args)
| Ejecting (payload, ej) ->
"Ejector: " ^ ej#stringOf ^ "(" ^ payload#stringOf ^ ")"
| DoubleThrown ->
"An ejector has come forward with a complaint of being thrown...twice!"
| WrongType (expected, actual) ->
"Wrong type while unwrapping " ^ prim_name expected ^ ": "
^ actual#stringOf
| NameError (name, span) ->
"name error at " ^ string_of_span span ^ ": " ^ name
| MissingNamedArg (k, span) ->
"Named arg " ^ k#stringOf ^ " missing in call at span "
^ string_of_span span
| UserException value -> "User-created exception : " ^ value#stringOf
exception MonteException of mexn
let loaderObj =
object
method call verb args namedArgs =
match (verb, args) with
| "import", [_] ->
raise (MonteException (UserException (strObj "XXX loader not impl")))
| _ -> None
method stringOf = "<import>"
method unwrap = None
end
(* The main calling interface. Handles Miranda methods. Propagates exceptions
* on failure. *)
let call_exn target verb args namedArgs : monte =
match target#call verb args namedArgs with
| Some rv -> rv
| None -> (
match (verb, args) with
(* Miranda behaviors. *)
| "_sealedDispatch", [_] -> nullObj
| "_uncall", [] -> nullObj
| _ ->
raise
(MonteException
(Refused (target, verb, args, List.map fst namedArgs))) )
let throwObj : monte =
object
method call verb args nargs =
match (verb, args) with
| "run", [value] -> raise (MonteException (UserException value))
| "eject", [ej; value] ->
let _ = call_exn ej "run" [value] [] in
raise (MonteException (UserException value))
| _ -> None
method stringOf = "throw"
method unwrap = None
end
let dataGuardObj example : monte =
let name = prim_name example in
object
method call verb args nargs =
match (verb, args) with
| "coerce", [specimen; exit] -> (
match specimen#unwrap with
| Some sp when prim_name sp = name -> Some specimen
| _ ->
Some
(call_exn exit "run"
[strObj (specimen#stringOf ^ "does not conform to " ^ name)]
[]) )
| _, _ -> None
method stringOf = name
method unwrap = None
end
let voidGuardObj : monte =
object
method call verb args nargs =
match (verb, args) with
| "coerce", [specimen; exit] ->
Some
(call_exn exit "run"
[strObj (specimen#stringOf ^ "does not conform to Void")]
[])
| _, _ -> None
method stringOf = "Void"
method unwrap = None
end
let todoGuardObj name : monte =
object
method call verb args nargs =
match (verb, args) with
| "coerce", [specimen; exit] ->
Printf.printf "\nXXX %s.coerce(...) not implemented\n" name ;
Some specimen
| _ -> None
method stringOf = "DeepFrozenStamp"
method unwrap = None
end
let flexMapObj (init : (monte * monte) list) : monte =
object
val mutable pairs = init
method call verb args nargs = None
(* XXX *)
method stringOf = "<FlexMap>"
method unwrap = None
end
let rec listObj l : monte =
object
method call verb args namedArgs =
match (verb, args) with
| "asMap", [] when l = [] -> Some (mapObj [])
| "diverge", [] -> Some (flexListObj l)
| "size", [] -> Some (intObj (Z.of_int (List.length l)))
| _ -> None
method stringOf =
"[" ^ String.concat " " (List.map (fun o -> o#stringOf) l) ^ "]"
method unwrap = Some (MList l)
end
and flexListObj (init : monte list) : monte =
object
val mutable items = init
method call verb args nargs =
match (verb, args) with
| "push", [item] ->
items <- item :: items ;
Some nullObj
| "snapshot", [] -> Some (listObj items)
| _ -> None
(* XXX *)
method stringOf = "<FlexList>"
method unwrap = None
end
and mapObj (pairs : (monte * monte) list) : monte =
(* XXX make sure ej doesn't return. *)
let throwStr ej msg = call_exn ej "run" [strObj msg] [] in
(* An iterator on a map, producing its keys and values. *)
let _makeIterator () =
object
val mutable _index = 0
method call verb args nargs =
match (verb, args) with
| "next", [ej] ->
if _index < List.length pairs then (
let k, v = List.nth pairs _index in
let rv = listObj [k; v] in
_index <- _index + 1 ;
Some rv )
else Some (throwStr ej "next/1: Iterator exhausted")
| _ -> None
method stringOf = "<mapIterator>"
method unwrap = None
end in
object
method call (verb : string) (args : monte list) namedArgs : monte option =
match (verb, args) with
| "diverge", [] -> Some (flexMapObj pairs)
| "getKeys", [] -> Some (listObj (List.map fst pairs))
| "getValues", [] -> Some (listObj (List.map snd pairs))
| "_makeIterator", [] -> Some (_makeIterator ())
| _ ->
Printf.printf "\nXXX Map verb todo? %s\n" verb ;
None
method stringOf =
let item (k, v) = String.concat " => " [k#stringOf; v#stringOf] in
let items = String.concat ", " (List.map item pairs) in
"[" ^ items ^ "]"
method unwrap = None
(* XXX? Map unwrap? *)
end
let _makeList : monte =
object
method call verb args namedArgs =
match verb with "run" -> Some (listObj args) | _ -> None
method stringOf = "_makeList"
method unwrap = None
end
let unwrapList specimen =
match specimen#unwrap with
| Some (MList l) -> l
| _ -> raise (MonteException (WrongType (MList [], specimen)))
let unwrapBool specimen =
match specimen#unwrap with
| Some (MBool b) -> b
| _ -> raise (MonteException (WrongType (MBool true, specimen)))
let unwrapStr specimen : string =
match specimen#unwrap with
| Some (MStr s) -> s
| _ -> raise (MonteException (WrongType (MStr "", specimen)))
let _makeMap : monte =
object
method call verb (args : monte list) namedArgs : monte option =
match (verb, args) with
| "fromPairs", [pairsObj] ->
let unwrapPair (itemList : monte list) =
match itemList with
| [k; v] -> (k, v)
| _ ->
raise
(MonteException
(WrongType
(MList [strObj "key"; strObj "val"], listObj itemList)))
in
let pairs =
List.map unwrapPair (List.map unwrapList (unwrapList pairsObj))
in
Some (mapObj pairs)
| _ -> None
method stringOf = "_makeMap"
method unwrap = None
end
let todoObj name : monte =
object
method call verb args nargs = None
method stringOf = name
method unwrap = None
end
let traceObj suffix : monte =
object
method call verb args nargs =
match verb with
| "run" ->
Printf.printf " ~ " ;
List.iter (fun obj -> Printf.printf "%s, " obj#stringOf) args ;
Printf.printf "%s" suffix ;
Some nullObj
| _ -> None
method stringOf = "trace"
method unwrap = None
end
let calling verb args namedArgs target = call_exn target verb args namedArgs
let prettyPrint formatter obj = Format.pp_print_string formatter obj#stringOf
let input_varint ic =
let rec go shift acc =
let b = Z.of_int (input_byte ic) in
let n = Z.logor acc (Z.shift_left (Z.logand b (Z.of_int 0x7f)) shift) in
if not (Z.testbit b 7) then n else go (shift + 7) n in
go 0 Z.zero
exception InvalidMAST of (string * int)
let throw_invalid_mast ic message = raise (InvalidMAST (message, pos_in ic))
let input_span ic =
match input_char ic with
| 'S' ->
OneToOne
(input_varint ic, input_varint ic, input_varint ic, input_varint ic)
| 'B' ->
Blob (input_varint ic, input_varint ic, input_varint ic, input_varint ic)
| _ -> throw_invalid_mast ic "input_span"
let ejectTo span =
let ej =
object (self)
val mutable thrown = false
method disable =
if thrown then raise (MonteException DoubleThrown) ;
thrown <- true
method private throw v =
self#disable ;
raise (MonteException (Ejecting (v, to_monte self)))
method call verb args namedArgs =
match (verb, args) with
| "run", [v] -> self#throw v
| "run", [] -> self#throw nullObj
| _ -> None
method stringOf = "<ejector at " ^ string_of_span span ^ ">"
method unwrap = None
end in
(to_monte ej, fun () -> ej#disable)
let _loop : monte =
object
method call verb (args : monte list) nargs =
let run iterable consumer : monte =
let iterator = call_exn iterable "_makeIterator" [] [] in
let no_span : mspan = Blob (Z.zero, Z.zero, Z.zero, Z.zero) in
let ej, _ = ejectTo no_span in
let rec next () =
try
let values = call_exn iterator "next" [ej] [] in
ignore (call_exn consumer "run" (unwrapList values) []) ;
next ()
with MonteException (Ejecting (e, _)) as ex ->
if e == ej then nullObj else raise ex in
next () in
match (verb, args) with
| "run", [iterable; consumer] -> Some (run iterable consumer)
| _ -> None
method stringOf = "_loop"
method unwrap = None
end
let makeScope (pairs : (string * monte) list) : monte Dict.t =
Dict.of_seq
(List.to_seq
(List.map (fun (k, v) -> (k, bindingObj (finalSlotObj v))) pairs))
let unwrapScope obj =
let no_amp s =
String.(if sub s 0 2 = "&&" then sub s 2 (length s - 2) else s) in
let keys =
List.map
(fun o -> no_amp (unwrapStr o))
(unwrapList (call_exn obj "getKeys" [] [])) in
let values = unwrapList (call_exn obj "getValues" [] []) in
let items = List.combine keys values in
Dict.of_seq (List.to_seq items)
let safeScope =
makeScope
[ ("Bool", dataGuardObj (MBool true)); ("Bytes", todoGuardObj "Bytes")
; ("Char", dataGuardObj (MChar 32))
; ("DeepFrozen", todoGuardObj "DeepFrozen")
; ("DeepFrozenStamp", todoGuardObj "DeepFrozenStamp")
; ("Double", dataGuardObj (MDouble 1.0)); ("Infinity", doubleObj infinity)
; ("NaN", doubleObj nan); ("Int", dataGuardObj (MInt Z.zero))
; ("Near", todoGuardObj "Near")
; ("KernelAstStamp", todoObj "KernelAstStamp")
; ("Same", todoGuardObj "Same"); ("Ref", todoObj "Ref")
; ("astEval", todoObj "astEval"); ("Selfless", todoGuardObj "Selfless")
; ("Str", todoGuardObj "Str")
; ("SemitransparentStamp", todoObj "SemitransparentStamp")
; ("SubrangeGuard", todoGuardObj "SubrangeGuard")
; ("TransparentStamp", todoObj "TransparentStamp"); ("Void", voidGuardObj)
; ("_auditedBy", todoObj "_auditedBy"); ("_equalizer", todoObj "_equalizer")
; ("_loop", _loop); ("_makeBytes", todoObj "_makeBytes")
; ("_makeDouble", todoObj "_makeDouble")
; ("_makeFinalSlot", todoObj "_makeFinalSlot")
; ("_makeInt", todoObj "_makeInt"); ("_makeList", _makeList)
; ("_makeInt", todoObj "_makeInt"); ("_makeMap", _makeMap)
; ("false", boolObj false); ("null", nullObj)
; ("_makeSourceSpan", todoObj "_makeSourceSpan")
; ("_makeStr", todoObj "_makeStr"); ("_makeVarSlot", todoObj "_makeVarSlot")
; ("M", todoObj "M"); ("_slotToBinding", todoObj "_slotToBinding")
; ("loadMAST", todoObj "loadMAST"); ("makeLazySlot", todoObj "makeLazySlot")
; ("promiseAllFulfilled", todoObj "promiseAllFulfilled")
; ("throw", throwObj); ("Any", todoGuardObj "Any")
; ("traceln", traceObj "\n"); ("M", todoObj "M"); ("true", boolObj true)
; ("trace", traceObj "")
; ("typhonAstBuilder", todoObj "typhonAstBuilder" (* XXX typhon objects? *))
; ("typhonAstEval", todoObj "typhonAstEval" (* XXX typhon objects? *)) ]
let const k _ = k
let rec sequence actions =
match actions with
| f :: fs ->
State.bind f (fun x ->
State.bind (sequence fs) (fun xs -> State.return (x :: xs)))
| [] -> State.return []
let lazyState f s = f () s
module Compiler = struct
type span = mspan
let oneToOne t = OneToOne t
let blob t = Blob t
type menv = monte Dict.t
type t = (monte, menv) State.t
type patt = monte -> monte -> (unit, menv) State.t
type narg = (monte * monte, menv) State.t
type nparam = (monte * monte) list -> (unit, menv) State.t
type meth = string * patt list * nparam list * t
type matcher = patt * t
let charExpr c _ = State.return (charObj c)
let doubleExpr d _ = State.return (doubleObj d)
let intExpr i _ = State.return (intObj i)
let strExpr s _ = State.return (strObj s)
let nounExpr n span =
let get = calling "get" [] [] in
State.bind State.get (fun env ->
match Dict.find_opt n env with
| Some b -> State.return (get (get b))
| None -> raise (MonteException (NameError (n, span))))
let nullExpr span = nounExpr "null" span
let bindingExpr n span =
State.bind State.get (fun env ->
match Dict.find_opt n env with
| Some b -> State.return b
| None -> raise (MonteException (NameError ("&&" ^ n, span))))
let seqExpr exprs _ =
List.fold_left
(fun ma expr -> State.bind ma (fun _ -> expr))
(State.return nullObj) exprs
let callExpr target verb args namedArgs span =
State.bind target (fun t ->
State.bind (sequence args) (fun a ->
State.bind (sequence namedArgs) (fun na ->
State.return (call_exn t verb a na))))
let defExpr patt exitOpt expr span =
let withOptionalExpr exprOpt d f =
match exprOpt with Some expr -> State.bind expr f | None -> f d in
withOptionalExpr exitOpt throwObj (fun exit ->
State.bind expr (fun e ->
State.and_then (patt e exit) (State.return e)))
let escapeExpr patt body span =
lazyState (fun () ->
let ej, disable = ejectTo span in
State.bind
(State.and_then (patt ej nullObj) State.get)
(fun s ->
try
let x, _ = body s in
disable () ; State.return x
with MonteException (Ejecting (o, thrower)) when thrower == ej ->
State.return o))
let escapeCatchExpr patt body cpatt cbody span =
lazyState (fun () ->
let ej, disable = ejectTo span in
State.bind
(State.and_then (patt ej nullObj) State.get)
(fun s ->
try
let x, _ = body s in
disable () ; State.return x
with MonteException (Ejecting (o, thrower)) when thrower == ej ->
State.and_then (cpatt o nullObj) cbody))
let objectExpr doc (namePatt : patt) (asOpt : t option) (auditors : t list)
(meths : meth list) (matchs : matcher list) (span : mspan) : t =
let methdict =
List.fold_left
(fun d (v, ps, nps, body) ->
AtomDict.add (v, List.length ps) (ps, nps, body) d)
AtomDict.empty meths in
State.bind
(Option.value asOpt ~default:(State.return nullObj))
(fun ase ->
State.bind (sequence auditors) (fun auds (* XXX rebind into env *) s ->
let userObj =
object (self)
(* XXX method dispatch, matcher dispatch *)
method call verb args namedArgs : monte option =
Printf.printf "(call: %s/%d)" verb (List.length args) ;
match
AtomDict.find_opt (verb, List.length args) methdict
with
| None ->
Printf.printf "no such method" ;
None (* refused. XXX matchers *)
| Some (params, nParams, body) ->
let exit = throwObj in
(* XXX duplicate code with listPatt, refactor! *)
let env' =
List.fold_left2
(fun ma p s -> State.and_then ma (p s exit))
(State.return ()) params args in
let env'' =
State.and_then (namePatt self throwObj) env' in
Printf.printf "\n(executing %s(" verb ;
List.iter (fun a -> Printf.printf "%s, " a#stringOf) args ;
Printf.printf ") at %s)" (string_of_span span) ;
let o, _ = State.and_then env'' body s in
Some o
(* XXX miranda methods *)
(* XXX call printOn *)
method stringOf = "<user>"
method unwrap = None
end in
let _, s' = (namePatt userObj throwObj) s in
(userObj, s')))
let assignExpr name rhs span =
State.bind rhs (fun rv ->
State.and_then (State.modify (Dict.add name rv)) (State.return rv))
let tryExpr body patt catcher span s =
try body s
with MonteException ex -> (
match ex with
(* Ejectors unwind at try-exprs, but do not run their catchers. *)
| Ejecting _ -> raise (MonteException ex)
(* XXX sealed *)
| _ -> State.and_then (patt nullObj nullObj) catcher s )
let finallyExpr body unwinder span env =
try body env
with MonteException m -> unwinder env ; raise (MonteException m)
let hideExpr expr _ = expr
let ifExpr test cons alt span =
let alt' = Option.value alt ~default:(nullExpr span) in
State.bind test (fun t -> if unwrapBool t then cons else alt')
let metaStateExpr span =
State.return
(object
method call verb args namedArgs = None
method stringOf = "<meta.getState()>"
method unwrap = None
end)
let metaContextExpr span =
State.return
(object
method call verb args namedArgs = None
method stringOf = "<meta.context()>"
method unwrap = None
end)
let metho doc verb patts nparams rguard body span =
(* XXX rguard? signature synthesis? *)
(verb, patts, nparams, body)
let matche patt body span = (patt, body)
let namedArg key value span =
State.bind key (fun k -> State.bind value (fun v -> State.return (k, v)))
let namedParam key patt defaultOpt span map =
State.bind key (fun k ->
(* XXX uses OCaml equality!! *)
match (List.assoc_opt k map, defaultOpt) with
| Some value, _ -> patt value throwObj
| None, Some default -> State.bind default (const (State.return ()))
| None, None -> raise (MonteException (MissingNamedArg (k, span))))
let coerceOpt guardOpt specimen exit =
match guardOpt with
| None -> State.return specimen
| Some guard ->
State.bind guard (fun g ->
let s = call_exn g "coerce" [specimen; exit] [] in
State.return s)
let ignorePatt guardOpt span specimen exit =
State.map (fun _ -> ()) (coerceOpt guardOpt specimen exit)
let finalPatt noun guard span specimen exit =
State.bind (coerceOpt guard specimen exit) (fun s ->
Printf.printf "(finalPatt: %s := %s)" noun s#stringOf ;
(* XXX guards *)
State.modify (Dict.add noun (bindingObj (finalSlotObj s))))
let varPatt noun guard span specimen exit =
State.bind (coerceOpt guard specimen exit) (fun s ->
(* XXX guards *)
State.modify (Dict.add noun (bindingObj (varSlotObj s))))
let listPatt patts span specimen exit =
let specimens = unwrapList specimen in
List.fold_left2
(fun ma p s -> State.and_then ma (p s exit))
(State.return ()) patts specimens
let viaPatt transformer patt span specimen exit =
State.bind transformer (fun trans ->
patt (call_exn trans "run" [specimen; exit] []) exit)
let bindingPatt noun span specimen exit =
State.modify (Dict.add noun specimen)
end
let input_str ic = really_input_string ic (Z.to_int (input_varint ic))
let input_many f ic =
let l = Z.to_int (input_varint ic) in
List.init l (fun _ -> f ic)
(* A growing mutable list that is indexed backwards. Simulates a portion of
* the Python list API. *)
let backlist () =
object
val mutable l = []
val mutable len = 0
method push x =
l <- x :: l ;
len <- len + 1
method get i = List.nth l (len - 1 - i)
method tl = List.hd l
end
exception InvalidMagic
let mast_magic = "Mont\xe0MAST\x01"
let open_in_mast path =
let ic = open_in_bin path in
(* Check the magic number. *)
for i = 0 to String.length mast_magic - 1 do
if input_char ic <> mast_magic.[i] then (close_in ic ; raise InvalidMagic)
done ;
ic
module MASTContext (Monte : MAST) = struct
type masthack =
| HNone
| HExpr of Monte.t
| HMeth of Monte.meth
| HMatch of Monte.matcher
let logged label ch =
(* XXX Printf.printf "%s%c..." label ch; *)
ch
let make () =
object (self)
(* Compared to the classic MAST context, we store the exprs and patts
* backwards, so that we can build them quickly. *)
val exprs = backlist ()
val patts = backlist ()
method private eat_span ic =
let v4 ic =
let i1 = input_varint ic in
let i2 = input_varint ic in
let i3 = input_varint ic in
let i4 = input_varint ic in
(i1, i2, i3, i4) in
match input_char ic with
| 'S' -> Monte.oneToOne (v4 ic)