-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ml
More file actions
1747 lines (1631 loc) · 82.1 KB
/
Copy pathmain.ml
File metadata and controls
1747 lines (1631 loc) · 82.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
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
(* SPDX-License-Identifier: PMPL-1.0-or-later *)
(* SPDX-FileCopyrightText: 2024-2026 Jonathan D.A. Jewell (hyperpolymath) *)
(** AffineScript compiler CLI
Phase A of the LSP integration adds [--json] to [check], [lint],
[compile], and [eval] commands. When enabled, all diagnostics are
emitted as a single JSON object on stderr (see {!Json_output} for
the schema), and human-readable output is suppressed. This is the
contract the LSP (and any other tool) relies on for phases B-D. *)
let () = Fmt_tty.setup_std_outputs ()
(** Version string *)
let version = "0.1.0"
(** Read file contents *)
let read_file path =
let ic = open_in_bin path in
let n = in_channel_length ic in
let s = really_input_string ic n in
close_in ic;
s
(** {1 JSON-aware error helpers}
Each helper collects diagnostics into a list ref when [json] is true,
or prints the human-readable message and returns a Cmdliner error
when [json] is false. *)
(** Emit a JSON report and return the appropriate Cmdliner result. *)
let json_finish diags =
let success = List.for_all (fun (d : Affinescript.Json_output.diagnostic) ->
match d.severity with
| Affinescript.Json_output.Error -> false
| _ -> true
) diags in
Affinescript.Json_output.emit_report ~success diags;
if success then `Ok () else `Error (false, "")
(** {1 Command implementations} *)
(** Lex a file and print tokens (no --json support — token streams are
not diagnostics). *)
let lex_file path =
let source = read_file path in
let lexer = Affinescript.Lexer.from_string ~file:path source in
let rec loop () =
let (tok, span) = lexer () in
Format.printf "%a: %s@."
Affinescript.Span.pp_short span
(Affinescript.Token.to_string tok);
if tok <> Affinescript.Token.EOF then loop ()
in
try
loop ();
`Ok ()
with Affinescript.Lexer.Lexer_error (msg, pos) ->
Format.eprintf "@[<v>%s:%d:%d: error: %s@]@." path pos.line pos.col msg;
`Error (false, "Lexer error")
(** Resolve the effective face. Priority order, highest first:
1. An explicit non-Canonical [--face] flag (always wins).
2. A [face:] pragma in the leading comment lines of the source file
(see {!Affinescript.Face_pragma}). This is the recommended mechanism
once every source file shares the canonical [.affine] extension.
3. A deprecated face-implying file extension ([.rattle], [.pyaff],
[.jsaff], [.pseudoaff]). Triggers a one-line stderr warning.
4. Canonical default.
[quiet] suppresses the deprecation notice for JSON and LSP commands
whose stderr is consumed by tools (LSP clients, CI, editors). *)
let resolve_face ?(quiet = false) face path =
match face with
| Affinescript.Face.Canonical ->
(match Affinescript.Face_pragma.detect_in_file path with
| Some f -> f
| None ->
let ext =
try
let dot = String.rindex path '.' in
String.sub path (dot + 1) (String.length path - dot - 1)
with Not_found -> ""
in
let warn_deprecated alias =
if not quiet then
Format.eprintf
"@[<v>warning: file extension .%s is deprecated; \
rename to .affine and add a pragma '# face: %s' on the first \
line (use --face %s to silence this warning)@]@."
ext alias alias
in
(match ext with
| "rattle" -> warn_deprecated "rattle"; Affinescript.Face.Python
| "pyaff" -> warn_deprecated "python"; Affinescript.Face.Python
| "jsaff" -> warn_deprecated "js"; Affinescript.Face.Js
| "pseudoaff" -> warn_deprecated "pseudocode"; Affinescript.Face.Pseudocode
| _ -> Affinescript.Face.Canonical))
| other -> other (* explicit --face flag always wins *)
(** Parse a file using the requested face. *)
let parse_with_face (face : Affinescript.Face.face) path =
match face with
| Affinescript.Face.Canonical -> Affinescript.Parse_driver.parse_file path
| Affinescript.Face.Python -> Affinescript.Python_face.parse_file_python path
| Affinescript.Face.Js -> Affinescript.Js_face.parse_file_js path
| Affinescript.Face.Pseudocode -> Affinescript.Pseudocode_face.parse_file_pseudocode path
| Affinescript.Face.Lucid -> Affinescript.Lucid_face.parse_file_lucid path
| Affinescript.Face.Cafe -> Affinescript.Cafe_face.parse_file_cafe path
(** Preview the Python-face text transform (debug tool). *)
let preview_python_transform path =
let ch = open_in_bin path in
let s = really_input_string ch (in_channel_length ch) in
close_in ch;
print_string (Affinescript.Python_face.preview_transform s);
`Ok ()
(** Preview the JS-face text transform (debug tool). *)
let preview_js_transform path =
let ch = open_in_bin path in
let s = really_input_string ch (in_channel_length ch) in
close_in ch;
print_string (Affinescript.Js_face.preview_transform s);
`Ok ()
(** Preview the pseudocode-face text transform (debug tool). *)
let preview_pseudocode_transform path =
let ch = open_in_bin path in
let s = really_input_string ch (in_channel_length ch) in
close_in ch;
print_string (Affinescript.Pseudocode_face.preview_transform s);
`Ok ()
(** Preview the Lucid-face (PureScript-style) text transform (debug tool). *)
let preview_lucid_transform path =
let ch = open_in_bin path in
let s = really_input_string ch (in_channel_length ch) in
close_in ch;
print_string (Affinescript.Lucid_face.preview_transform s);
`Ok ()
(** Preview the Cafe-face (CoffeeScript-style) text transform (debug tool). *)
let preview_cafe_transform path =
let ch = open_in_bin path in
let s = really_input_string ch (in_channel_length ch) in
close_in ch;
print_string (Affinescript.Cafe_face.preview_transform s);
`Ok ()
(** Parse a file and print AST (no --json support). *)
let parse_file (face : Affinescript.Face.face) path =
let face = resolve_face face path in
try
let prog = parse_with_face face path in
Format.printf "%s@." (Affinescript.Ast.show_program prog);
`Ok ()
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
Format.eprintf "@[<v>%s:%d:%d: lexer error: %s@]@." path pos.line pos.col msg;
`Error (false, "Lexer error")
| Affinescript.Parse_driver.Parse_error (msg, span) ->
Format.eprintf "@[<v>%a: parse error: %s@]@."
Affinescript.Span.pp_short span msg;
`Error (false, "Parse error")
(** Type-check a file. With [--json], emits a structured diagnostic
report on stderr. *)
let check_file face json path =
let face = resolve_face ~quiet:json face path in
if json then begin
let diags = ref [] in
let add d = diags := d :: !diags in
let symbols_table = ref None in
let resolve_refs = ref [] in
begin try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, span) ->
add (Affinescript.Json_output.of_resolve_error e span)
| Ok (resolve_ctx, _type_ctx) ->
(* Phase B+C: capture symbol table and use-site references. *)
symbols_table := Some resolve_ctx.symbols;
resolve_refs := List.rev resolve_ctx.references;
(match Affinescript.Typecheck.check_program resolve_ctx.symbols prog with
| Error e ->
add (Affinescript.Json_output.of_type_error e)
| Ok _ctx ->
(match Affinescript.Borrow.check_program resolve_ctx.symbols prog with
| Error e ->
add (Affinescript.Json_output.of_borrow_error e)
| Ok () ->
(* Stage 1: QTT quantity enforcement *)
(match Affinescript.Quantity.check_program_quantities prog with
| Error (err, span) ->
add (Affinescript.Json_output.of_quantity_error (err, span))
| Ok () -> ()))))
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
add (Affinescript.Json_output.of_lexer_error msg pos path)
| Affinescript.Parse_driver.Parse_error (msg, span) ->
add (Affinescript.Json_output.of_parse_error msg span)
end;
(* Emit v2 report with symbol table when available, v1 otherwise. *)
let final_diags = List.rev !diags in
(match !symbols_table with
| Some symbols ->
let success = List.for_all (fun (d : Affinescript.Json_output.diagnostic) ->
match d.severity with Affinescript.Json_output.Error -> false | _ -> true
) final_diags in
(* Convert resolve references to json_output references. *)
let json_refs = List.map (fun (r : Affinescript.Resolve.reference) ->
Affinescript.Json_output.{ ref_symbol_id = r.ref_symbol_id; ref_span = r.ref_span }
) !resolve_refs in
Affinescript.Json_output.emit_report_v2 ~success final_diags symbols json_refs;
if success then `Ok () else `Error (false, "")
| None ->
json_finish final_diags)
end else begin
try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, _span) ->
Format.eprintf "@[<v>Resolution error: %s@]@."
(Affinescript.Face.format_resolve_error face e);
`Error (false, "Resolution error")
| Ok (resolve_ctx, _type_ctx) ->
(match Affinescript.Typecheck.check_program resolve_ctx.symbols prog with
| Error e ->
Format.eprintf "@[<v>%s@]@."
(Affinescript.Face.format_type_error face e);
`Error (false, "Type error")
| Ok _ctx ->
(match Affinescript.Borrow.check_program resolve_ctx.symbols prog with
| Error e ->
Format.eprintf "@[<v>Borrow error: %s@]@."
(Affinescript.Face.format_borrow_error face e);
`Error (false, "Borrow error")
| Ok () ->
(* Stage 1: QTT quantity enforcement *)
(match Affinescript.Quantity.check_program_quantities prog with
| Error (err, _span) ->
Format.eprintf "@[<v>Quantity error: %s@]@."
(Affinescript.Face.format_quantity_error face err);
`Error (false, "Quantity error")
| Ok () ->
Format.printf "Type checking passed@.";
`Ok ()))))
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
Format.eprintf "@[<v>%s:%d:%d: lexer error: %s@]@." path pos.line pos.col msg;
`Error (false, "Lexer error")
| Affinescript.Parse_driver.Parse_error (msg, span) ->
Format.eprintf "@[<v>%a: parse error: %s@]@."
Affinescript.Span.pp_short span msg;
`Error (false, "Parse error")
end
(** Evaluate a file with the interpreter. With [--json], emits
diagnostics on stderr instead of human-readable error text. *)
let eval_file face json path =
let face = resolve_face ~quiet:json face path in
if json then begin
let diags = ref [] in
let add d = diags := d :: !diags in
begin try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, span) ->
add (Affinescript.Json_output.of_resolve_error e span)
| Ok (resolve_ctx, type_ctx) ->
let type_ctx = { type_ctx with symbols = resolve_ctx.symbols } in
(* Register builtins (print, int_to_string, tea_run, etc.) before type-checking *)
Affinescript.Typecheck.register_builtins type_ctx;
(match List.fold_left (fun acc decl ->
match acc with
| Error _ as e -> e
| Ok () -> Affinescript.Typecheck.check_decl type_ctx decl
) (Ok ()) prog.prog_decls with
| Error e ->
add (Affinescript.Json_output.of_type_error e)
| Ok () ->
(* Stage 1: QTT quantity enforcement *)
(match Affinescript.Quantity.check_program_quantities prog with
| Error (err, span) ->
add (Affinescript.Json_output.of_quantity_error (err, span))
| Ok () ->
(match Affinescript.Interp.eval_program prog with
| Ok env ->
(* Auto-call main() if defined — entry point for TEA apps and scripts *)
(match Affinescript.Value.lookup_env "main" env with
| Ok main_fn ->
(match Affinescript.Interp.apply_function main_fn [] with
| Ok _ -> ()
| Error e -> add (Affinescript.Json_output.of_eval_error e))
| Error _ -> ()) (* no main — that's fine, just eval declarations *)
| Error e ->
add (Affinescript.Json_output.of_eval_error e)))))
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
add (Affinescript.Json_output.of_lexer_error msg pos path)
| Affinescript.Parse_driver.Parse_error (msg, span) ->
add (Affinescript.Json_output.of_parse_error msg span)
end;
json_finish (List.rev !diags)
end else begin
try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, _span) ->
Format.eprintf "@[<v>Resolution error: %s@]@."
(Affinescript.Face.format_resolve_error face e);
`Error (false, "Resolution error")
| Ok (resolve_ctx, type_ctx) ->
let type_ctx = { type_ctx with symbols = resolve_ctx.symbols } in
(* Register builtins (print, int_to_string, tea_run, etc.) before type-checking *)
Affinescript.Typecheck.register_builtins type_ctx;
(match List.fold_left (fun acc decl ->
match acc with
| Error _ as e -> e
| Ok () -> Affinescript.Typecheck.check_decl type_ctx decl
) (Ok ()) prog.prog_decls with
| Error e ->
Format.eprintf "@[<v>%s@]@."
(Affinescript.Face.format_type_error face e);
`Error (false, "Type error")
| Ok () ->
(* Stage 1: QTT quantity enforcement *)
(match Affinescript.Quantity.check_program_quantities prog with
| Error (err, _span) ->
Format.eprintf "@[<v>Quantity error: %s@]@."
(Affinescript.Face.format_quantity_error face err);
`Error (false, "Quantity error")
| Ok () ->
(match Affinescript.Interp.eval_program prog with
| Ok env ->
(* Auto-call main() if defined — entry point for TEA apps and scripts *)
(match Affinescript.Value.lookup_env "main" env with
| Ok main_fn ->
(match Affinescript.Interp.apply_function main_fn [] with
| Ok _ -> `Ok ()
| Error e ->
Format.eprintf "@[<v>Runtime error: %s@]@."
(Affinescript.Value.show_eval_error e);
`Error (false, "Runtime error"))
| Error _ ->
(* No main function — declarations evaluated, nothing to run *)
Format.printf "Program evaluated successfully@.";
`Ok ())
| Error e ->
Format.eprintf "@[<v>Runtime error: %s@]@."
(Affinescript.Value.show_eval_error e);
`Error (false, "Runtime error")))))
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
Format.eprintf "@[<v>%s:%d:%d: lexer error: %s@]@." path pos.line pos.col msg;
`Error (false, "Lexer error")
| Affinescript.Parse_driver.Parse_error (msg, span) ->
Format.eprintf "@[<v>%a: parse error: %s@]@."
Affinescript.Span.pp_short span msg;
`Error (false, "Parse error")
end
(** Generate the AffineScript TEA bridge Wasm module.
Emits a validated Wasm 1.0 binary that implements the TitleScreen
TEA state machine with clean i32 exports for AffineTEA.js.
No source file is needed — the module is generated directly from the
TEA ABI specification. Write to a .wasm file, copy to IDApTIK's
public/assets/wasm/ directory, and load via AffineTEA.load(). *)
let tea_bridge_cmd_fn output =
let m = Affinescript.Tea_bridge.generate () in
(* Stage 8: auto-verify ownership constraints before writing. *)
(match Affinescript.Tw_verify.verify_from_module m with
| Ok () ->
Format.printf "typed-wasm ownership verification: OK@."
| Error errs ->
Affinescript.Tw_verify.pp_report Format.std_formatter errs);
Affinescript.Wasm_encode.write_module_to_file output m;
Format.printf "TEA bridge written to %s@." output;
Format.printf " affinescript_init() — initialise TitleModel@.";
Format.printf " affinescript_update(msg: i32) — 0=NewGame 1=LoadGame 2=Settings 3=Credits@.";
Format.printf " affinescript_get_selected() -> i32 — 0=none 1=new_game 2=load_game 3=settings 4=credits@.";
Format.printf " affinescript_get_screen_w/h() -> i32 — current screen dimensions@.";
Format.printf " affinescript_set_screen(w: i32, h: i32) — handle resize events@.";
Format.printf " memory — exported linear memory (model at offset 64)@.";
Format.printf "Custom sections: affinescript.ownership, affinescript.tea_layout@.";
`Ok ()
(** Generate the Cadre Router Wasm module.
Produces a WebAssembly 1.0 module encoding IDApTIK's screen back-stack as
an affine resource. Push/pop consume the old stack linearly and produce a
new one — navigation history as a linear type.
No source file is needed — the module is generated directly from the router
ABI specification. Write to a .wasm file, copy to IDApTIK's
public/assets/wasm/ directory, and load via AffineTEARouter.load(). *)
let router_bridge_cmd_fn output =
let m = Affinescript.Tea_router.generate () in
(* Stage 8: auto-verify ownership constraints before writing. *)
(match Affinescript.Tw_verify.verify_from_module m with
| Ok () ->
Format.printf "typed-wasm ownership verification: OK@."
| Error errs ->
Affinescript.Tw_verify.pp_report Format.std_formatter errs);
Affinescript.Wasm_encode.write_module_to_file output m;
Format.printf "Router bridge written to %s@." output;
Format.printf " affinescript_router_init() — initialise RouterModel@.";
Format.printf " affinescript_router_push(screen_tag: i32) — push screen (Linear param)@.";
Format.printf " affinescript_router_pop() — pop current screen@.";
Format.printf " affinescript_router_present_popup(popup_tag: i32) — show popup (Linear param)@.";
Format.printf " affinescript_router_dismiss_popup() — dismiss popup@.";
Format.printf " affinescript_router_resize(w: i32, h: i32) — update screen dims (Linear)@.";
Format.printf " affinescript_router_get_stack_top() -> i32 — current screen (−1 if empty)@.";
Format.printf " affinescript_router_get_popup_tag() -> i32 — active popup (−1 if none)@.";
Format.printf "Screen tags: 0=Title 1=CharacterSelect 2=WorldMap 3=Load 4=Game@.";
Format.printf "Popup tags: 0=Settings 1=Inventory 2=Hacking@.";
Format.printf "Custom sections: affinescript.ownership, affinescript.tea_layout@.";
`Ok ()
(** Generate the CharacterSelect TEA Bridge Wasm module.
Produces a WebAssembly 1.0 module that implements the AffineScript TEA
ABI for CharacterSelectScreen. The model holds the player's background
selection as a single i32 selected_tag (0=none, 1-6=background, 7=confirmed).
The exported API surface is identical to the TitleScreen bridge, so the
same AffineTEA.js / AffineTEA.res bindings work without modification.
Msg tags (input to affinescript_update):
0=SelectAssault 1=SelectRecon 2=SelectEngineer
3=SelectSignals 4=SelectMedic 5=SelectLogistics 6=Confirm
selected_tag (output from affinescript_get_selected):
0=none 1=Assault 2=Recon 3=Engineer 4=Signals 5=Medic 6=Logistics
7=confirmed (navigate to JessicaCustomise)
No source file needed — generated from the CharacterSelect TEA ABI.
Write to a .wasm file, copy to IDApTIK's public/assets/wasm/ directory. *)
let cs_bridge_cmd_fn output =
let m = Affinescript.Tea_cs_bridge.generate () in
(* Auto-verify ownership constraints before writing. *)
(match Affinescript.Tw_verify.verify_from_module m with
| Ok () ->
Format.printf "typed-wasm ownership verification: OK@."
| Error errs ->
Affinescript.Tw_verify.pp_report Format.std_formatter errs);
Affinescript.Wasm_encode.write_module_to_file output m;
Format.printf "CharacterSelect TEA bridge written to %s@." output;
Format.printf " affinescript_init() — initialise CharacterSelectModel@.";
Format.printf " affinescript_update(msg: i32) — 0-5=SelectClass 6=Confirm@.";
Format.printf " affinescript_get_selected() -> i32 — 0=none 1-6=class 7=confirmed@.";
Format.printf " affinescript_get_screen_w/h() -> i32 — current screen dimensions@.";
Format.printf " affinescript_set_screen(w: i32, h: i32) — handle resize events@.";
Format.printf " memory — exported linear memory (model at offset 64)@.";
Format.printf "Custom sections: affinescript.ownership, affinescript.tea_layout@.";
`Ok ()
(** Start the REPL *)
let repl_cmd_fn () =
(* TODO: Re-enable when REPL module is restored *)
(* Affinescript.Repl.start (); *)
Format.eprintf "REPL not yet implemented@.";
`Error (false, "REPL not yet implemented")
(** Compile a file. With [--json], emits diagnostics for any
compilation errors. With [--wasm-gc], targets the WebAssembly GC
proposal instead of WASM 1.0 linear memory. *)
let compile_file face json wasm_gc vscode_ext vscode_adapter vscode_no_lc
path output =
let face = resolve_face ~quiet:json face path in
if json then begin
let diags = ref [] in
let add d = diags := d :: !diags in
begin try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, span) ->
add (Affinescript.Json_output.of_resolve_error e span)
| Ok (resolve_ctx, import_type_ctx) ->
(match Affinescript.Typecheck.check_program
~import_types:import_type_ctx.Affinescript.Typecheck.name_types
resolve_ctx.symbols prog with
| Error e ->
add (Affinescript.Json_output.of_type_error e)
| Ok _type_ctx ->
(match Affinescript.Borrow.check_program resolve_ctx.symbols prog with
| Error e ->
add (Affinescript.Json_output.of_borrow_error e)
| Ok () ->
(* Stage 1: QTT quantity enforcement *)
(match Affinescript.Quantity.check_program_quantities prog with
| Error (err, span) ->
add (Affinescript.Json_output.of_quantity_error (err, span))
| Ok () ->
(* For non-Wasm codegens (Julia, JS, C, ReScript, ...), inline
imports into prog.prog_decls so each backend doesn't need its
own module-system implementation. The Wasm and Wasm-GC paths
use the original [prog] because they handle imports natively
via Codegen.gen_imports / the import section. *)
let flat_prog = Affinescript.Module_loader.flatten_imports loader prog in
let is_julia = Filename.check_suffix output ".jl" in
let is_js = Filename.check_suffix output ".js" in
let is_c = Filename.check_suffix output ".c" in
let is_wgsl = Filename.check_suffix output ".wgsl" in
let is_faust = Filename.check_suffix output ".dsp" in
let is_onnx = Filename.check_suffix output ".onnx" in
let is_ocaml = Filename.check_suffix output ".ml" in
let is_lua = Filename.check_suffix output ".lua" in
let is_bash = Filename.check_suffix output ".sh" in
let is_nickel = Filename.check_suffix output ".ncl" in
let is_rescript = Filename.check_suffix output ".res" in
let is_rust = Filename.check_suffix output ".rs" in
let is_llvm = Filename.check_suffix output ".ll" in
let is_verilog = Filename.check_suffix output ".v" in
let is_gleam = Filename.check_suffix output ".gleam" in
let is_cuda = Filename.check_suffix output ".cu" in
let is_metal = Filename.check_suffix output ".metal" in
let is_opencl = Filename.check_suffix output ".cl" in
let is_mlir = Filename.check_suffix output ".mlir" in
let is_why3 = Filename.check_suffix output ".mlw" in
let is_lean = Filename.check_suffix output ".lean" in
let is_spirv = Filename.check_suffix output ".spv" in
if is_julia then begin
match Affinescript.Julia_codegen.codegen_julia flat_prog resolve_ctx.symbols with
| Error msg ->
add { severity = Error; code = "E0800";
message = Printf.sprintf "Julia codegen error: %s" msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok julia_code ->
let oc = open_out output in
output_string oc julia_code;
close_out oc
end else if is_js then begin
match Affinescript.Js_codegen.codegen_js flat_prog resolve_ctx.symbols with
| Error msg ->
add { severity = Error; code = "E0803";
message = Printf.sprintf "JS codegen error: %s" msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok js_code ->
let oc = open_out output in
output_string oc js_code;
close_out oc
end else if is_c then begin
match Affinescript.C_codegen.codegen_c flat_prog resolve_ctx.symbols with
| Error msg ->
add { severity = Error; code = "E0804";
message = Printf.sprintf "C codegen error: %s" msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok c_code ->
let oc = open_out output in
output_string oc c_code;
close_out oc
end else if is_wgsl then begin
match Affinescript.Wgsl_codegen.codegen_wgsl flat_prog resolve_ctx.symbols with
| Error msg ->
add { severity = Error; code = "E0805";
message = Printf.sprintf "WGSL codegen error: %s" msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok wgsl_code ->
let oc = open_out output in
output_string oc wgsl_code;
close_out oc
end else if is_faust then begin
match Affinescript.Faust_codegen.codegen_faust flat_prog resolve_ctx.symbols with
| Error msg ->
add { severity = Error; code = "E0806";
message = Printf.sprintf "Faust codegen error: %s" msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok faust_code ->
let oc = open_out output in
output_string oc faust_code;
close_out oc
end else if is_onnx then begin
match Affinescript.Onnx_codegen.codegen_onnx flat_prog resolve_ctx.symbols with
| Error msg ->
add { severity = Error; code = "E0807";
message = Printf.sprintf "ONNX codegen error: %s" msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok bytes ->
let oc = open_out_bin output in
output_string oc bytes;
close_out oc
end else if is_ocaml || is_lua || is_bash || is_nickel || is_rescript
|| is_rust || is_llvm || is_verilog || is_gleam
|| is_cuda || is_metal || is_opencl || is_mlir
|| is_why3 || is_lean || is_spirv then begin
let (label, code, result) =
if is_ocaml then ("OCaml", "E0808",
Affinescript.Ocaml_codegen.codegen_ocaml flat_prog resolve_ctx.symbols)
else if is_lua then ("Lua", "E0809",
Affinescript.Lua_codegen.codegen_lua flat_prog resolve_ctx.symbols)
else if is_bash then ("Bash", "E0810",
Affinescript.Bash_codegen.codegen_bash flat_prog resolve_ctx.symbols)
else if is_nickel then ("Nickel", "E0811",
Affinescript.Nickel_codegen.codegen_nickel flat_prog resolve_ctx.symbols)
else if is_rescript then ("ReScript", "E0812",
Affinescript.Rescript_codegen.codegen_rescript flat_prog resolve_ctx.symbols)
else if is_rust then ("Rust", "E0813",
Affinescript.Rust_codegen.codegen_rust flat_prog resolve_ctx.symbols)
else if is_llvm then ("LLVM", "E0814",
Affinescript.Llvm_codegen.codegen_llvm flat_prog resolve_ctx.symbols)
else if is_verilog then ("Verilog", "E0815",
Affinescript.Verilog_codegen.codegen_verilog flat_prog resolve_ctx.symbols)
else if is_gleam then ("Gleam", "E0816",
Affinescript.Gleam_codegen.codegen_gleam flat_prog resolve_ctx.symbols)
else if is_cuda then ("CUDA", "E0817",
Affinescript.Cuda_codegen.codegen_cuda flat_prog resolve_ctx.symbols)
else if is_metal then ("Metal", "E0818",
Affinescript.Metal_codegen.codegen_metal flat_prog resolve_ctx.symbols)
else if is_opencl then ("OpenCL", "E0819",
Affinescript.Opencl_codegen.codegen_opencl flat_prog resolve_ctx.symbols)
else if is_mlir then ("MLIR", "E0820",
Affinescript.Mlir_codegen.codegen_mlir flat_prog resolve_ctx.symbols)
else if is_why3 then ("Why3", "E0821",
Affinescript.Why3_codegen.codegen_why3 flat_prog resolve_ctx.symbols)
else if is_lean then ("Lean", "E0822",
Affinescript.Lean_codegen.codegen_lean flat_prog resolve_ctx.symbols)
else ("SPIR-V", "E0823",
Affinescript.Spirv_codegen.codegen_spirv flat_prog resolve_ctx.symbols)
in
match result with
| Error msg ->
add { severity = Error; code;
message = Printf.sprintf "%s codegen error: %s" label msg;
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok src ->
let oc = if is_spirv then open_out_bin output else open_out output in
output_string oc src;
close_out oc
end else if wasm_gc then begin
match Affinescript.Codegen_gc.generate_gc_module prog with
| Error e ->
add { severity = Error; code = "E0802";
message = Printf.sprintf "WASM GC codegen error: %s"
(Affinescript.Codegen_gc.format_codegen_error e);
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok gc_module ->
Affinescript.Wasm_gc_encode.write_gc_module_to_file output gc_module
end else if Filename.check_suffix output ".cjs" then begin
(* Issue #35 Phase 1: Node-CJS shim around the compiled wasm. *)
let optimized_prog = Affinescript.Opt.fold_constants_program prog in
match Affinescript.Codegen.generate_module ~loader optimized_prog with
| Error e ->
add { severity = Error; code = "E0810";
message = Printf.sprintf "Node-CJS codegen error: %s"
(Affinescript.Codegen.show_codegen_error e);
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok wasm_module ->
let cjs =
Affinescript.Codegen_node.emit_node_cjs
~vscode_extension:vscode_ext
?vscode_extension_adapter:vscode_adapter
~vscode_extension_no_lc:vscode_no_lc
wasm_module
in
let oc = open_out output in
output_string oc cjs;
close_out oc
end else begin
let optimized_prog = Affinescript.Opt.fold_constants_program prog in
match Affinescript.Codegen.generate_module ~loader optimized_prog with
| Error e ->
add { severity = Error; code = "E0801";
message = Printf.sprintf "WASM codegen error: %s"
(Affinescript.Codegen.show_codegen_error e);
span = Affinescript.Span.dummy; help = None; labels = [] }
| Ok wasm_module ->
Affinescript.Wasm_encode.write_module_to_file output wasm_module
end))))
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
add (Affinescript.Json_output.of_lexer_error msg pos path)
| Affinescript.Parse_driver.Parse_error (msg, span) ->
add (Affinescript.Json_output.of_parse_error msg span)
end;
json_finish (List.rev !diags)
end else begin
try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, _span) ->
Format.eprintf "@[<v>Resolution error: %s@]@."
(Affinescript.Face.format_resolve_error face e);
`Error (false, "Resolution error")
| Ok (resolve_ctx, import_type_ctx) ->
(match Affinescript.Typecheck.check_program
~import_types:import_type_ctx.Affinescript.Typecheck.name_types
resolve_ctx.symbols prog with
| Error e ->
Format.eprintf "@[<v>%s@]@."
(Affinescript.Face.format_type_error face e);
`Error (false, "Type error")
| Ok _type_ctx ->
(match Affinescript.Borrow.check_program resolve_ctx.symbols prog with
| Error e ->
Format.eprintf "@[<v>Borrow error: %s@]@."
(Affinescript.Face.format_borrow_error face e);
`Error (false, "Borrow error")
| Ok () ->
(* Stage 1: QTT quantity enforcement *)
(match Affinescript.Quantity.check_program_quantities prog with
| Error (err, _span) ->
Format.eprintf "@[<v>Quantity error: %s@]@."
(Affinescript.Face.format_quantity_error face err);
`Error (false, "Quantity error")
| Ok () ->
begin
(* See JSON-path notes above for the [flat_prog] rationale: inline
cross-module imports for backends that don't have native
module-system support. Wasm/Wasm-GC keep the original [prog]. *)
let flat_prog = Affinescript.Module_loader.flatten_imports loader prog in
let is_julia = Filename.check_suffix output ".jl" in
let is_js = Filename.check_suffix output ".js" in
let is_c = Filename.check_suffix output ".c" in
let is_wgsl = Filename.check_suffix output ".wgsl" in
let is_faust = Filename.check_suffix output ".dsp" in
let is_onnx = Filename.check_suffix output ".onnx" in
let is_ocaml = Filename.check_suffix output ".ml" in
let is_lua = Filename.check_suffix output ".lua" in
let is_bash = Filename.check_suffix output ".sh" in
let is_nickel = Filename.check_suffix output ".ncl" in
let is_rescript = Filename.check_suffix output ".res" in
let is_rust = Filename.check_suffix output ".rs" in
let is_llvm = Filename.check_suffix output ".ll" in
let is_verilog = Filename.check_suffix output ".v" in
let is_gleam = Filename.check_suffix output ".gleam" in
let is_cuda = Filename.check_suffix output ".cu" in
let is_metal = Filename.check_suffix output ".metal" in
let is_opencl = Filename.check_suffix output ".cl" in
let is_mlir = Filename.check_suffix output ".mlir" in
let is_why3 = Filename.check_suffix output ".mlw" in
let is_lean = Filename.check_suffix output ".lean" in
let is_spirv = Filename.check_suffix output ".spv" in
if is_julia then
(match Affinescript.Julia_codegen.codegen_julia flat_prog resolve_ctx.symbols with
| Error e ->
Format.eprintf "@[<v>Julia codegen error: %s@]@." e;
`Error (false, "Julia codegen error")
| Ok julia_code ->
let oc = open_out output in
output_string oc julia_code;
close_out oc;
Format.printf "Compiled %s -> %s (Julia)@." path output;
`Ok ())
else if is_js then
(match Affinescript.Js_codegen.codegen_js flat_prog resolve_ctx.symbols with
| Error e ->
Format.eprintf "@[<v>JS codegen error: %s@]@." e;
`Error (false, "JS codegen error")
| Ok js_code ->
let oc = open_out output in
output_string oc js_code;
close_out oc;
Format.printf "Compiled %s -> %s (JS)@." path output;
`Ok ())
else if is_c then
(match Affinescript.C_codegen.codegen_c flat_prog resolve_ctx.symbols with
| Error e ->
Format.eprintf "@[<v>C codegen error: %s@]@." e;
`Error (false, "C codegen error")
| Ok c_code ->
let oc = open_out output in
output_string oc c_code;
close_out oc;
Format.printf "Compiled %s -> %s (C)@." path output;
`Ok ())
else if is_wgsl then
(match Affinescript.Wgsl_codegen.codegen_wgsl flat_prog resolve_ctx.symbols with
| Error e ->
Format.eprintf "@[<v>WGSL codegen error: %s@]@." e;
`Error (false, "WGSL codegen error")
| Ok wgsl_code ->
let oc = open_out output in
output_string oc wgsl_code;
close_out oc;
Format.printf "Compiled %s -> %s (WGSL)@." path output;
`Ok ())
else if is_faust then
(match Affinescript.Faust_codegen.codegen_faust flat_prog resolve_ctx.symbols with
| Error e ->
Format.eprintf "@[<v>Faust codegen error: %s@]@." e;
`Error (false, "Faust codegen error")
| Ok faust_code ->
let oc = open_out output in
output_string oc faust_code;
close_out oc;
Format.printf "Compiled %s -> %s (Faust)@." path output;
`Ok ())
else if is_onnx then
(match Affinescript.Onnx_codegen.codegen_onnx flat_prog resolve_ctx.symbols with
| Error e ->
Format.eprintf "@[<v>ONNX codegen error: %s@]@." e;
`Error (false, "ONNX codegen error")
| Ok bytes ->
let oc = open_out_bin output in
output_string oc bytes;
close_out oc;
Format.printf "Compiled %s -> %s (ONNX)@." path output;
`Ok ())
else if is_ocaml || is_lua || is_bash || is_nickel || is_rescript
|| is_rust || is_llvm || is_verilog || is_gleam
|| is_cuda || is_metal || is_opencl || is_mlir
|| is_why3 || is_lean || is_spirv then
let (label, result) =
if is_ocaml then ("OCaml",
Affinescript.Ocaml_codegen.codegen_ocaml flat_prog resolve_ctx.symbols)
else if is_lua then ("Lua",
Affinescript.Lua_codegen.codegen_lua flat_prog resolve_ctx.symbols)
else if is_bash then ("Bash",
Affinescript.Bash_codegen.codegen_bash flat_prog resolve_ctx.symbols)
else if is_nickel then ("Nickel",
Affinescript.Nickel_codegen.codegen_nickel flat_prog resolve_ctx.symbols)
else if is_rescript then ("ReScript",
Affinescript.Rescript_codegen.codegen_rescript flat_prog resolve_ctx.symbols)
else if is_rust then ("Rust",
Affinescript.Rust_codegen.codegen_rust flat_prog resolve_ctx.symbols)
else if is_llvm then ("LLVM",
Affinescript.Llvm_codegen.codegen_llvm flat_prog resolve_ctx.symbols)
else if is_verilog then ("Verilog",
Affinescript.Verilog_codegen.codegen_verilog flat_prog resolve_ctx.symbols)
else if is_gleam then ("Gleam",
Affinescript.Gleam_codegen.codegen_gleam flat_prog resolve_ctx.symbols)
else if is_cuda then ("CUDA",
Affinescript.Cuda_codegen.codegen_cuda flat_prog resolve_ctx.symbols)
else if is_metal then ("Metal",
Affinescript.Metal_codegen.codegen_metal flat_prog resolve_ctx.symbols)
else if is_opencl then ("OpenCL",
Affinescript.Opencl_codegen.codegen_opencl flat_prog resolve_ctx.symbols)
else if is_mlir then ("MLIR",
Affinescript.Mlir_codegen.codegen_mlir flat_prog resolve_ctx.symbols)
else if is_why3 then ("Why3",
Affinescript.Why3_codegen.codegen_why3 flat_prog resolve_ctx.symbols)
else if is_lean then ("Lean",
Affinescript.Lean_codegen.codegen_lean flat_prog resolve_ctx.symbols)
else ("SPIR-V",
Affinescript.Spirv_codegen.codegen_spirv flat_prog resolve_ctx.symbols)
in
(match result with
| Error e ->
Format.eprintf "@[<v>%s codegen error: %s@]@." label e;
`Error (false, label ^ " codegen error")
| Ok src ->
let oc = if is_spirv then open_out_bin output else open_out output in
output_string oc src;
close_out oc;
Format.printf "Compiled %s -> %s (%s)@." path output label;
`Ok ())
else if wasm_gc then
(match Affinescript.Codegen_gc.generate_gc_module prog with
| Error e ->
Format.eprintf "@[<v>%s@]@."
(Affinescript.Codegen_gc.format_codegen_error e);
`Error (false, "WASM GC codegen error")
| Ok gc_module ->
Affinescript.Wasm_gc_encode.write_gc_module_to_file output gc_module;
Format.printf "Compiled %s -> %s (WASM GC)@." path output;
`Ok ())
else if Filename.check_suffix output ".cjs" then
(* Issue #35 Phase 1: Node-CJS shim around the compiled wasm. *)
let optimized_prog = Affinescript.Opt.fold_constants_program prog in
(match Affinescript.Codegen.generate_module ~loader optimized_prog with
| Error e ->
Format.eprintf "@[<v>Node-CJS codegen error: %s@]@."
(Affinescript.Codegen.show_codegen_error e);
`Error (false, "Node-CJS codegen error")
| Ok wasm_module ->
let cjs =
Affinescript.Codegen_node.emit_node_cjs
~vscode_extension:vscode_ext
?vscode_extension_adapter:vscode_adapter
~vscode_extension_no_lc:vscode_no_lc
wasm_module
in
let oc = open_out output in
output_string oc cjs;
close_out oc;
Format.printf "Compiled %s -> %s (Node-CJS%s)@." path output
(if vscode_ext then ", --vscode-extension" else "");
`Ok ())
else
let optimized_prog = Affinescript.Opt.fold_constants_program prog in
(match Affinescript.Codegen.generate_module ~loader optimized_prog with
| Error e ->
Format.eprintf "@[<v>Code generation error: %s@]@."
(Affinescript.Codegen.show_codegen_error e);
`Error (false, "Code generation error")
| Ok wasm_module ->
(* Stage 8: auto-verify typed-wasm ownership constraints.
Printed as informational; does not block compilation. *)
(match Affinescript.Tw_verify.verify_from_module wasm_module with
| Ok () -> ()
| Error errs ->
Affinescript.Tw_verify.pp_report Format.err_formatter errs);
Affinescript.Wasm_encode.write_module_to_file output wasm_module;
Format.printf "Compiled %s -> %s (WASM)@." path output;
`Ok ())
end))))
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
Format.eprintf "@[<v>%s:%d:%d: lexer error: %s@]@." path pos.line pos.col msg;
`Error (false, "Lexer error")
| Affinescript.Parse_driver.Parse_error (msg, span) ->
Format.eprintf "@[<v>%a: parse error: %s@]@."
Affinescript.Span.pp_short span msg;
`Error (false, "Parse error")
end
(** Format a file. Only canonical face is supported — non-canonical face
formatting requires a reverse transform that is not yet implemented. *)
let fmt_file face path =
let face = resolve_face face path in
(match face with
| Affinescript.Face.Python ->
Format.eprintf "fmt --face python is not yet supported \
(reverse Python transform is pending).@."; ()
| Affinescript.Face.Js ->
Format.eprintf "fmt --face js is not yet supported \
(reverse JS transform is pending).@."; ()
| Affinescript.Face.Pseudocode ->
Format.eprintf "fmt --face pseudocode is not yet supported \
(reverse pseudocode transform is pending).@."; ()
| Affinescript.Face.Lucid ->
Format.eprintf "fmt --face lucid is not yet supported \
(reverse Lucid/PureScript transform is pending).@."; ()
| Affinescript.Face.Cafe ->
Format.eprintf "fmt --face cafe is not yet supported \
(reverse Cafe/CoffeeScript transform is pending).@."; ()
| Affinescript.Face.Canonical -> ());
try
Affinescript.Formatter.format_file path;
Format.printf "Formatted %s@." path;
`Ok ()
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
Format.eprintf "@[<v>%s:%d:%d: lexer error: %s@]@." path pos.line pos.col msg;
`Error (false, "Lexer error")
| Affinescript.Parse_driver.Parse_error (msg, span) ->
Format.eprintf "@[<v>%a: parse error: %s@]@."
Affinescript.Span.pp_short span msg;
`Error (false, "Parse error")
(** Lint a file. With [--json], emits lint diagnostics as structured
JSON on stderr. *)
let lint_file face json path =
let face = resolve_face ~quiet:json face path in
if json then begin
let diags = ref [] in
let add d = diags := d :: !diags in
begin try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, span) ->
add (Affinescript.Json_output.of_resolve_error e span)
| Ok (resolve_ctx, _type_ctx) ->
let lint_diags = Affinescript.Linter.lint_program resolve_ctx.symbols prog in
List.iter (fun d ->
add (Affinescript.Json_output.of_lint_diagnostic d)
) lint_diags)
with
| Affinescript.Lexer.Lexer_error (msg, pos) ->
add (Affinescript.Json_output.of_lexer_error msg pos path)
| Affinescript.Parse_driver.Parse_error (msg, span) ->
add (Affinescript.Json_output.of_parse_error msg span)
end;
json_finish (List.rev !diags)
end else begin
try
let prog = parse_with_face face path in
let loader_config = Affinescript.Module_loader.default_config () in
let loader = Affinescript.Module_loader.create loader_config in
(match Affinescript.Resolve.resolve_program_with_loader prog loader with
| Error (e, _span) ->
Format.eprintf "@[<v>Resolution error: %s@]@."
(Affinescript.Face.format_resolve_error face e);
`Error (false, "Resolution error")
| Ok (resolve_ctx, _type_ctx) ->
let diagnostics = Affinescript.Linter.lint_program resolve_ctx.symbols prog in
if List.length diagnostics = 0 then
(Format.printf "No issues found@."; `Ok ())
else
(Affinescript.Linter.print_diagnostics diagnostics;
`Error (false, "Lint issues found")))