lemmascript 0.1.0 → 0.3.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.
@@ -4,35 +4,81 @@
4
4
  * Single source of truth for type-related decisions.
5
5
  * The transform phase imports this.
6
6
  */
7
- export function tyToLean(ty) {
8
- switch (ty.kind) {
9
- case "nat": return "Nat";
10
- case "int": return "Int";
11
- case "bool": return "Bool";
12
- case "string": return "String";
13
- case "void": return "Unit";
14
- case "array": {
15
- const elem = tyToLean(ty.elem);
16
- return elem.includes(" ") ? `Array (${elem})` : `Array ${elem}`;
7
+ /** Split generic type arguments respecting nested angle brackets. */
8
+ function splitTypeArgs(s) {
9
+ const args = [];
10
+ let depth = 0, start = 0;
11
+ for (let i = 0; i < s.length; i++) {
12
+ if (s[i] === '<')
13
+ depth++;
14
+ else if (s[i] === '>')
15
+ depth--;
16
+ else if (s[i] === ',' && depth === 0) {
17
+ args.push(s.slice(start, i));
18
+ start = i + 1;
17
19
  }
18
- case "user": return ty.name;
19
- case "unknown": return "_";
20
20
  }
21
+ args.push(s.slice(start));
22
+ return args.map(a => a.trim());
21
23
  }
22
24
  export function parseTsType(tsType) {
23
25
  const t = tsType.trim();
26
+ // Union: T | undefined → optional<T>
27
+ if (t.includes(" | ")) {
28
+ let arms = t.split(" | ").map(a => a.trim());
29
+ // Normalize expanded boolean literals: true | false → boolean
30
+ const boolLits = new Set(["true", "false"]);
31
+ const hasBoth = arms.includes("true") && arms.includes("false");
32
+ if (hasBoth) {
33
+ arms = arms.filter(a => !boolLits.has(a));
34
+ arms.unshift("boolean");
35
+ }
36
+ const nonUndef = arms.filter(a => a !== "undefined");
37
+ if (nonUndef.length === 1 && arms.length === 2) {
38
+ return { kind: "optional", inner: parseTsType(nonUndef[0]) };
39
+ }
40
+ }
24
41
  if (t === "number")
25
42
  return { kind: "int" };
43
+ if (t === "bigint")
44
+ return { kind: "int" };
26
45
  if (t === "nat")
27
46
  return { kind: "nat" };
28
- if (t === "boolean")
47
+ if (t === "boolean" || t === "true" || t === "false")
29
48
  return { kind: "bool" };
30
49
  if (t === "string")
31
50
  return { kind: "string" };
32
51
  if (t === "void" || t === "undefined")
33
52
  return { kind: "void" };
53
+ if (t === "unknown")
54
+ return { kind: "unknown" };
55
+ // Record<K, V> → map
56
+ const recordMatch = t.match(/^Record<(.+)>$/);
57
+ if (recordMatch) {
58
+ const args = splitTypeArgs(recordMatch[1]);
59
+ if (args.length === 2)
60
+ return { kind: "map", key: parseTsType(args[0]), value: parseTsType(args[1]) };
61
+ }
62
+ // Array<T> or T[]
34
63
  const m = t.match(/^(?:Array<(.+)>|(.+)\[\])$/);
35
64
  if (m)
36
65
  return { kind: "array", elem: parseTsType(m[1] || m[2]) };
66
+ // Map<K, V>
67
+ const mapMatch = t.match(/^Map<(.+)>$/);
68
+ if (mapMatch) {
69
+ const args = splitTypeArgs(mapMatch[1]);
70
+ if (args.length === 2)
71
+ return { kind: "map", key: parseTsType(args[0]), value: parseTsType(args[1]) };
72
+ }
73
+ // Set<T>
74
+ const setMatch = t.match(/^Set<(.+)>$/);
75
+ if (setMatch)
76
+ return { kind: "set", elem: parseTsType(setMatch[1]) };
77
+ // Tuple [T1, T2, ...] → array of the common element type
78
+ const tupleMatch = t.match(/^\[(.+)\]$/);
79
+ if (tupleMatch) {
80
+ const elems = splitTypeArgs(tupleMatch[1]);
81
+ return { kind: "array", elem: parseTsType(elems[0]) };
82
+ }
37
83
  return { kind: "user", name: t };
38
84
  }