@magnusopera/fscript 0.83.2 → 0.84.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,136 @@
1
+
2
+ import { Record } from "../fable_modules/fable-library-js.5.13.0/Types.js";
3
+ import { record_type, list_type, string_type } from "../fable_modules/fable-library-js.5.13.0/Reflection.js";
4
+ import { Type_$reflection } from "./Types.js";
5
+ import { choose, singleton, collect, length, isEmpty, empty, reverse, cons } from "../fable_modules/fable-library-js.5.13.0/List.js";
6
+ import { comparePrimitives, Exception } from "../fable_modules/fable-library-js.5.13.0/Util.js";
7
+ import { ofList } from "../fable_modules/fable-library-js.5.13.0/Map.js";
8
+
9
+ export class ExportedFunctionSignature extends Record {
10
+ constructor(Name, ParameterNames, ParameterTypes, ReturnType) {
11
+ super();
12
+ this.Name = Name;
13
+ this.ParameterNames = ParameterNames;
14
+ this.ParameterTypes = ParameterTypes;
15
+ this.ReturnType = ReturnType;
16
+ }
17
+ }
18
+
19
+ export function ExportedFunctionSignature_$reflection() {
20
+ return record_type("FScript.Language.ExportSignatures.ExportedFunctionSignature", [], ExportedFunctionSignature, () => [["Name", string_type], ["ParameterNames", list_type(string_type)], ["ParameterTypes", list_type(Type_$reflection())], ["ReturnType", Type_$reflection()]]);
21
+ }
22
+
23
+ function flattenFunctionType(t) {
24
+ const loop = (acc_mut, current_mut) => {
25
+ loop:
26
+ while (true) {
27
+ const acc = acc_mut, current = current_mut;
28
+ if (current.tag === 11) {
29
+ acc_mut = cons(current.fields[0], acc);
30
+ current_mut = current.fields[1];
31
+ continue loop;
32
+ }
33
+ else {
34
+ return [reverse(acc), current];
35
+ }
36
+ break;
37
+ }
38
+ };
39
+ return loop(empty(), t);
40
+ }
41
+
42
+ function flattenParameterNames(expression) {
43
+ const loop = (acc_mut, current_mut) => {
44
+ loop:
45
+ while (true) {
46
+ const acc = acc_mut, current = current_mut;
47
+ if (current.tag === 4) {
48
+ acc_mut = cons(current.fields[0].Name, acc);
49
+ current_mut = current.fields[1];
50
+ continue loop;
51
+ }
52
+ else {
53
+ return reverse(acc);
54
+ }
55
+ break;
56
+ }
57
+ };
58
+ return loop(empty(), expression);
59
+ }
60
+
61
+ function fromLet(name, expression, expressionType) {
62
+ const parameterNames = flattenParameterNames(expression);
63
+ const patternInput = flattenFunctionType(expressionType);
64
+ const parameterTypes = patternInput[0];
65
+ if (isEmpty(parameterNames) ? true : isEmpty(parameterTypes)) {
66
+ return undefined;
67
+ }
68
+ else if (length(parameterNames) !== length(parameterTypes)) {
69
+ throw new Exception(`Signature mismatch for function '${name}'`);
70
+ }
71
+ else {
72
+ return new ExportedFunctionSignature(name, parameterNames, parameterTypes, patternInput[1]);
73
+ }
74
+ }
75
+
76
+ export function fromTypedProgram(program) {
77
+ return ofList(collect((_arg) => {
78
+ let matchResult, expression_1, expressionType_1, isExported_2, name_1, bindings_1, isExported_3;
79
+ switch (_arg.tag) {
80
+ case 1: {
81
+ if (_arg.fields[4]) {
82
+ matchResult = 0;
83
+ expression_1 = _arg.fields[1];
84
+ expressionType_1 = _arg.fields[2];
85
+ isExported_2 = _arg.fields[4];
86
+ name_1 = _arg.fields[0];
87
+ }
88
+ else {
89
+ matchResult = 2;
90
+ }
91
+ break;
92
+ }
93
+ case 3: {
94
+ if (_arg.fields[1]) {
95
+ matchResult = 1;
96
+ bindings_1 = _arg.fields[0];
97
+ isExported_3 = _arg.fields[1];
98
+ }
99
+ else {
100
+ matchResult = 2;
101
+ }
102
+ break;
103
+ }
104
+ default:
105
+ matchResult = 2;
106
+ }
107
+ switch (matchResult) {
108
+ case 0: {
109
+ const matchValue = fromLet(name_1, expression_1, expressionType_1);
110
+ if (matchValue == null) {
111
+ return empty();
112
+ }
113
+ else {
114
+ const signature = matchValue;
115
+ return singleton([signature.Name, signature]);
116
+ }
117
+ }
118
+ case 1:
119
+ return choose((tupledArg) => {
120
+ let signature_1;
121
+ const option_1 = fromLet(tupledArg[0], tupledArg[1], tupledArg[2]);
122
+ if (option_1 != null) {
123
+ return (signature_1 = option_1, [signature_1.Name, signature_1]);
124
+ }
125
+ else {
126
+ return undefined;
127
+ }
128
+ }, bindings_1);
129
+ default:
130
+ return empty();
131
+ }
132
+ }, program), {
133
+ Compare: (x, y) => (comparePrimitives(x, y) | 0),
134
+ });
135
+ }
136
+
@@ -8,7 +8,7 @@ import { map as map_1, toList as toList_1, fold as fold_2, forAll } from "../fab
8
8
  import { union, toList, ofList, FSharpSet__Contains, empty as empty_1, add } from "../fable_modules/fable-library-js.5.13.0/Set.js";
9
9
  import { curry4, defaultOf, disposeSafe, getEnumerator, Exception, curry2, comparePrimitives } from "../fable_modules/fable-library-js.5.13.0/Util.js";
10
10
  import { ofSeq, empty as empty_2, add as add_1, FSharpMap__ContainsKey, FSharpMap__get_Item } from "../fable_modules/fable-library-js.5.13.0/Map.js";
11
- import { TypeDef, Stmt, InterpolatedPart, MapEntry, Param, Expr, Pattern, TypeRef } from "./Ast.js";
11
+ import { Stmt, TypeDef, InterpolatedPart, MapEntry, Param, Expr, Pattern, TypeRef } from "./Ast.js";
12
12
  import { parseProgramWithSourceName } from "./Parser.js";
13
13
  import { FSharpRef } from "../fable_modules/fable-library-js.5.13.0/Types.js";
14
14
  import { Span, Position } from "./Span.js";
@@ -427,23 +427,39 @@ function tryFlattenExprPath(expr) {
427
427
  }
428
428
 
429
429
  function rewriteModuleScopedStatements(selfPrefix, aliases, statements) {
430
- const topLevelValueNames = ofList(collect((_arg) => {
431
- switch (_arg.tag) {
430
+ const unwrap = (_arg_mut) => {
431
+ unwrap:
432
+ while (true) {
433
+ const _arg = _arg_mut;
434
+ if (_arg.tag === 6) {
435
+ _arg_mut = _arg.fields[1];
436
+ continue unwrap;
437
+ }
438
+ else {
439
+ return _arg;
440
+ }
441
+ break;
442
+ }
443
+ };
444
+ const topLevelValueNames = ofList(collect((arg) => {
445
+ const _arg_1 = unwrap(arg);
446
+ switch (_arg_1.tag) {
432
447
  case 2:
433
- return singleton(_arg.fields[0]);
448
+ return singleton(_arg_1.fields[0]);
434
449
  case 3:
435
- return toList(collectPatternBindings(_arg.fields[0]));
450
+ return toList(collectPatternBindings(_arg_1.fields[0]));
436
451
  case 4:
437
- return map((tupledArg) => tupledArg[0], _arg.fields[0]);
452
+ return map((tupledArg) => tupledArg[0], _arg_1.fields[0]);
438
453
  default:
439
454
  return empty();
440
455
  }
441
456
  }, statements), {
442
457
  Compare: (x, y) => (comparePrimitives(x, y) | 0),
443
458
  });
444
- const topLevelTypeNames = ofList(choose((_arg_5) => {
445
- if (_arg_5.tag === 0) {
446
- return _arg_5.fields[0].Name;
459
+ const topLevelTypeNames = ofList(choose((arg_1) => {
460
+ const _arg_6 = unwrap(arg_1);
461
+ if (_arg_6.tag === 0) {
462
+ return _arg_6.fields[0].Name;
447
463
  }
448
464
  else {
449
465
  return undefined;
@@ -540,12 +556,12 @@ function rewriteModuleScopedStatements(selfPrefix, aliases, statements) {
540
556
  case 17:
541
557
  return new Expr(/* EStructuralRecord */ 17, [map((tupledArg_5) => [tupledArg_5[0], rewriteExpr(boundNames_1, tupledArg_5[1])], expr.fields[0]), expr.fields[1]]);
542
558
  case 18:
543
- return new Expr(/* EMap */ 18, [map((_arg_10) => {
544
- if (_arg_10.tag === 1) {
545
- return new MapEntry(/* MESpread */ 1, [rewriteExpr(boundNames_1, _arg_10.fields[0])]);
559
+ return new Expr(/* EMap */ 18, [map((_arg_11) => {
560
+ if (_arg_11.tag === 1) {
561
+ return new MapEntry(/* MESpread */ 1, [rewriteExpr(boundNames_1, _arg_11.fields[0])]);
546
562
  }
547
563
  else {
548
- return new MapEntry(/* MEKeyValue */ 0, [rewriteExpr(boundNames_1, _arg_10.fields[0]), rewriteExpr(boundNames_1, _arg_10.fields[1])]);
564
+ return new MapEntry(/* MEKeyValue */ 0, [rewriteExpr(boundNames_1, _arg_11.fields[0]), rewriteExpr(boundNames_1, _arg_11.fields[1])]);
549
565
  }
550
566
  }, expr.fields[0]), expr.fields[1]]);
551
567
  case 19:
@@ -594,12 +610,12 @@ function rewriteModuleScopedStatements(selfPrefix, aliases, statements) {
594
610
  case 26:
595
611
  return new Expr(/* ESome */ 26, [rewriteExpr(boundNames_1, expr.fields[0]), expr.fields[1]]);
596
612
  case 30:
597
- return new Expr(/* EInterpolatedString */ 30, [map((_arg_11) => {
598
- if (_arg_11.tag === 1) {
599
- return new InterpolatedPart(/* IPExpr */ 1, [rewriteExpr(boundNames_1, _arg_11.fields[0])]);
613
+ return new Expr(/* EInterpolatedString */ 30, [map((_arg_12) => {
614
+ if (_arg_12.tag === 1) {
615
+ return new InterpolatedPart(/* IPExpr */ 1, [rewriteExpr(boundNames_1, _arg_12.fields[0])]);
600
616
  }
601
617
  else {
602
- return new InterpolatedPart(/* IPText */ 0, [_arg_11.fields[0]]);
618
+ return new InterpolatedPart(/* IPText */ 0, [_arg_12.fields[0]]);
603
619
  }
604
620
  }, expr.fields[0]), expr.fields[1]]);
605
621
  default:
@@ -618,27 +634,32 @@ function rewriteModuleScopedStatements(selfPrefix, aliases, statements) {
618
634
  let option_11;
619
635
  return new Param(param_1.Name, (option_11 = param_1.Annotation, (option_11 != null) ? rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, option_11) : undefined), param_1.Span);
620
636
  };
621
- return map((_arg_12) => {
637
+ const rewriteStatement = (_arg_13) => {
622
638
  let def_1;
623
- switch (_arg_12.tag) {
639
+ switch (_arg_13.tag) {
640
+ case 0:
641
+ return new Stmt(/* SType */ 0, [(def_1 = _arg_13.fields[0], new TypeDef(qualifySelfName(def_1.Name), def_1.IsRecursive, map((tupledArg_8) => [tupledArg_8[0], rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, tupledArg_8[1])], def_1.Fields), map((tupledArg_9) => {
642
+ let option_13;
643
+ return [tupledArg_9[0], (option_13 = tupledArg_9[1], (option_13 != null) ? rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, option_13) : undefined)];
644
+ }, def_1.Cases), def_1.Import, def_1.Span))]);
624
645
  case 2: {
625
- const isRec_1 = _arg_12.fields[4];
626
- const rewrittenName = qualifySelfName(_arg_12.fields[0]);
627
- const rewrittenArgs_1 = map(rewriteParam, _arg_12.fields[1]);
646
+ const isRec_1 = _arg_13.fields[4];
647
+ const rewrittenName = qualifySelfName(_arg_13.fields[0]);
648
+ const rewrittenArgs_1 = map(rewriteParam, _arg_13.fields[1]);
628
649
  let rewrittenReturnAnnotation_2;
629
- const option_15 = _arg_12.fields[2];
650
+ const option_15 = _arg_13.fields[2];
630
651
  rewrittenReturnAnnotation_2 = ((option_15 != null) ? rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, option_15) : undefined);
631
652
  const bound = fold_1((s_1, p_2) => add(p_2.Name, s_1), empty_1({
632
653
  Compare: (x_3, y_3) => (comparePrimitives(x_3, y_3) | 0),
633
654
  }), rewrittenArgs_1);
634
- return new Stmt(/* SLet */ 2, [rewrittenName, rewrittenArgs_1, rewrittenReturnAnnotation_2, rewriteExpr(isRec_1 ? add(rewrittenName, bound) : bound, _arg_12.fields[3]), isRec_1, _arg_12.fields[5], _arg_12.fields[6]]);
655
+ return new Stmt(/* SLet */ 2, [rewrittenName, rewrittenArgs_1, rewrittenReturnAnnotation_2, rewriteExpr(isRec_1 ? add(rewrittenName, bound) : bound, _arg_13.fields[3]), isRec_1, _arg_13.fields[5], _arg_13.fields[6]]);
635
656
  }
636
657
  case 3:
637
- return new Stmt(/* SLetPattern */ 3, [rewritePattern(aliases, selfPrefix, topLevelTypeNames, _arg_12.fields[0]), rewriteExpr(empty_1({
658
+ return new Stmt(/* SLetPattern */ 3, [rewritePattern(aliases, selfPrefix, topLevelTypeNames, _arg_13.fields[0]), rewriteExpr(empty_1({
638
659
  Compare: (x_4, y_4) => (comparePrimitives(x_4, y_4) | 0),
639
- }), _arg_12.fields[1]), _arg_12.fields[2], _arg_12.fields[3]]);
660
+ }), _arg_13.fields[1]), _arg_13.fields[2], _arg_13.fields[3]]);
640
661
  case 4: {
641
- const bindings_2 = _arg_12.fields[0];
662
+ const bindings_2 = _arg_13.fields[0];
642
663
  const names_1 = ofList(map((tupledArg_10) => qualifySelfName(tupledArg_10[0]), bindings_2), {
643
664
  Compare: (x_5, y_5) => (comparePrimitives(x_5, y_5) | 0),
644
665
  });
@@ -647,21 +668,21 @@ function rewriteModuleScopedStatements(selfPrefix, aliases, statements) {
647
668
  const rewrittenName_1 = qualifySelfName(tupledArg_11[0]);
648
669
  const rewrittenArgs_2 = map(rewriteParam, tupledArg_11[1]);
649
670
  return [rewrittenName_1, rewrittenArgs_2, (option_17 = tupledArg_11[2], (option_17 != null) ? rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, option_17) : undefined), rewriteExpr(fold_1((s_2, p_3) => add(p_3.Name, s_2), names_1, rewrittenArgs_2), tupledArg_11[3]), tupledArg_11[4]];
650
- }, bindings_2), _arg_12.fields[1], _arg_12.fields[2]]);
671
+ }, bindings_2), _arg_13.fields[1], _arg_13.fields[2]]);
651
672
  }
652
673
  case 5:
653
- return new Stmt(/* SExpr */ 5, [rewriteExpr(empty_1({
674
+ return new Stmt(/* SBackendImport */ 5, [qualifySelfName(_arg_13.fields[0]), rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, _arg_13.fields[1]), _arg_13.fields[2], _arg_13.fields[3]]);
675
+ case 7:
676
+ return new Stmt(/* SExpr */ 7, [rewriteExpr(empty_1({
654
677
  Compare: (x_6, y_6) => (comparePrimitives(x_6, y_6) | 0),
655
- }), _arg_12.fields[0])]);
678
+ }), _arg_13.fields[0])]);
656
679
  case 1:
657
680
  throw new Exception("Unexpected unresolved import directive");
658
681
  default:
659
- return new Stmt(/* SType */ 0, [(def_1 = _arg_12.fields[0], new TypeDef(qualifySelfName(def_1.Name), def_1.IsRecursive, map((tupledArg_8) => [tupledArg_8[0], rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, tupledArg_8[1])], def_1.Fields), map((tupledArg_9) => {
660
- let option_13;
661
- return [tupledArg_9[0], (option_13 = tupledArg_9[1], (option_13 != null) ? rewriteTypeRef(aliases, selfPrefix, topLevelTypeNames, option_13) : undefined)];
662
- }, def_1.Cases), def_1.Span))]);
682
+ return new Stmt(/* SAttributed */ 6, [_arg_13.fields[0], rewriteStatement(_arg_13.fields[1]), _arg_13.fields[2]]);
663
683
  }
664
- }, statements);
684
+ };
685
+ return map(rewriteStatement, statements);
665
686
  }
666
687
 
667
688
  function expandProgram(rootDirectoryWithSeparator, fileSpan, getOrCreatePrefix, loadFileRef, stack, isMainFile, currentFile, currentPrefix, program) {
@@ -44,7 +44,7 @@ export function Submission_$reflection() {
44
44
 
45
45
  function retainedStatements(program) {
46
46
  return filter((_arg) => {
47
- if (_arg.tag === 5) {
47
+ if (_arg.tag === 7) {
48
48
  return false;
49
49
  }
50
50
  else {
@@ -55,7 +55,7 @@ function retainedStatements(program) {
55
55
 
56
56
  function appendRetained(reversed, program) {
57
57
  return fold((acc, statement) => {
58
- if (statement.tag === 5) {
58
+ if (statement.tag === 7) {
59
59
  return acc;
60
60
  }
61
61
  else {
@@ -89,8 +89,12 @@ function updateInferenceBindings(bindings, typedProgram) {
89
89
  const name_2 = tupledArg[0];
90
90
  return add(name_2, inferenceBinding(name_2, tupledArg[2]), state_1);
91
91
  }, current, statement.fields[0]);
92
+ case 4: {
93
+ const name_3 = statement.fields[0];
94
+ return add(name_3, inferenceBinding(name_3, statement.fields[1]), current);
95
+ }
92
96
  case 0:
93
- case 4:
97
+ case 5:
94
98
  return current;
95
99
  default: {
96
100
  const name = statement.fields[0];
@@ -267,13 +267,13 @@ function readInterpolatedRaw(sourceName, src, i, line, col) {
267
267
  return [(contentLen <= 0) ? "" : substring(src, start, contentLen), idx, cLine, cCol];
268
268
  }
269
269
 
270
- export function tokenizeWithSourceName(sourceName, src) {
270
+ export function tokenizeWithSourceNameAndOrigin(sourceName, startLine, startColumn, src) {
271
271
  const tokens = [];
272
272
  const indentStack = [];
273
273
  void (indentStack.push(0));
274
274
  let i = 0;
275
- let line = 1;
276
- let col = 1;
275
+ let line = startLine;
276
+ let col = startColumn;
277
277
  let atLineStart = true;
278
278
  let inLetBindingHead = false;
279
279
  let parenDepth = 0;
@@ -1061,6 +1061,10 @@ export function tokenizeWithSourceName(sourceName, src) {
1061
1061
  return toList(tokens);
1062
1062
  }
1063
1063
 
1064
+ export function tokenizeWithSourceName(sourceName, src) {
1065
+ return tokenizeWithSourceNameAndOrigin(sourceName, 1, 1, src);
1066
+ }
1067
+
1064
1068
  export function tokenize(src) {
1065
1069
  return tokenizeWithSourceName(undefined, src);
1066
1070
  }
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { parseProgramWithSourceName, parseProgram } from "./Parser.js";
3
3
  import { parseProgramFromSourceWithIncludesResolver, parseProgramFromSourceWithIncludes, parseProgramFromFile } from "./IncludeResolver.js";
4
- import { inferProgramWithRegistration, inferProgramWithExterns, inferProgram } from "./TypeInfer.js";
4
+ import { inferProgramWithRegistration, inferResolvedProgramWithExterns, inferResolvedProgram, inferProgramWithExterns, inferProgram } from "./TypeInfer.js";
5
5
  import { evalProgramWithExterns, evalProgram, evalProgramWithRegistration } from "./Eval.js";
6
6
 
7
7
  export const parse = parseProgram;
@@ -18,6 +18,10 @@ export const infer = inferProgram;
18
18
 
19
19
  export const inferWithExterns = (externs) => ((program) => inferProgramWithExterns(externs, program));
20
20
 
21
+ export const inferResolved = inferResolvedProgram;
22
+
23
+ export const inferResolvedWithExterns = (externs) => ((program) => inferResolvedProgramWithExterns(externs, program));
24
+
21
25
  export const inferWithRegistration = (registration) => ((program) => inferProgramWithRegistration(registration, program));
22
26
 
23
27
  export const evalWithRegistration = (registration) => ((program) => evalProgramWithRegistration(registration, program));