lemmascript 0.1.0 → 0.2.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,68 @@
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
+ const arms = t.split(" | ").map(a => a.trim());
29
+ const nonUndef = arms.filter(a => a !== "undefined");
30
+ if (nonUndef.length === 1 && arms.length === 2) {
31
+ return { kind: "optional", inner: parseTsType(nonUndef[0]) };
32
+ }
33
+ }
24
34
  if (t === "number")
25
35
  return { kind: "int" };
36
+ if (t === "bigint")
37
+ return { kind: "int" };
26
38
  if (t === "nat")
27
39
  return { kind: "nat" };
28
- if (t === "boolean")
40
+ if (t === "boolean" || t === "true" || t === "false")
29
41
  return { kind: "bool" };
30
42
  if (t === "string")
31
43
  return { kind: "string" };
32
44
  if (t === "void" || t === "undefined")
33
45
  return { kind: "void" };
46
+ if (t === "unknown")
47
+ return { kind: "unknown" };
48
+ // Record<K, V> → map
49
+ const recordMatch = t.match(/^Record<(.+)>$/);
50
+ if (recordMatch) {
51
+ const args = splitTypeArgs(recordMatch[1]);
52
+ if (args.length === 2)
53
+ return { kind: "map", key: parseTsType(args[0]), value: parseTsType(args[1]) };
54
+ }
55
+ // Array<T> or T[]
34
56
  const m = t.match(/^(?:Array<(.+)>|(.+)\[\])$/);
35
57
  if (m)
36
58
  return { kind: "array", elem: parseTsType(m[1] || m[2]) };
59
+ // Map<K, V>
60
+ const mapMatch = t.match(/^Map<(.+)>$/);
61
+ if (mapMatch) {
62
+ const args = splitTypeArgs(mapMatch[1]);
63
+ if (args.length === 2)
64
+ return { kind: "map", key: parseTsType(args[0]), value: parseTsType(args[1]) };
65
+ }
66
+ // Set<T>
67
+ const setMatch = t.match(/^Set<(.+)>$/);
68
+ if (setMatch)
69
+ return { kind: "set", elem: parseTsType(setMatch[1]) };
37
70
  return { kind: "user", name: t };
38
71
  }