@hatchingpoint/point 0.0.6 → 0.0.7

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.
package/src/core/check.ts CHANGED
@@ -1,69 +1,69 @@
1
- import type {
2
- PointCoreDeclaration,
1
+ import type {
2
+ PointCoreDeclaration,
3
3
  PointCoreExpression,
4
4
  PointCoreExternalDeclaration,
5
5
  PointCoreFunctionDeclaration,
6
- PointCoreProgram,
7
- PointCoreStatement,
8
- PointCoreTypeDeclaration,
9
- PointCoreTypeExpression,
10
- PointCoreValueDeclaration,
11
- PointSourceSpan,
12
- } from "./ast.ts";
13
-
14
- export interface PointCoreDiagnostic {
15
- code: string;
16
- message: string;
17
- path: string;
18
- ref: string;
19
- severity: "error";
20
- span: PointSourceSpan | null;
21
- expected?: string | string[];
22
- actual?: string;
23
- repair?: string;
24
- relatedRefs?: string[];
25
- }
26
-
27
- type DiagnosticMetadata = Partial<Pick<PointCoreDiagnostic, "expected" | "actual" | "repair" | "relatedRefs">>;
28
- type ScopeEntry = { type: PointCoreTypeExpression; mutable: boolean };
29
- type Scope = Map<string, ScopeEntry>;
30
-
6
+ PointCoreProgram,
7
+ PointCoreStatement,
8
+ PointCoreTypeDeclaration,
9
+ PointCoreTypeExpression,
10
+ PointCoreValueDeclaration,
11
+ PointSourceSpan,
12
+ } from "./ast.ts";
13
+
14
+ export interface PointCoreDiagnostic {
15
+ code: string;
16
+ message: string;
17
+ path: string;
18
+ ref: string;
19
+ severity: "error";
20
+ span: PointSourceSpan | null;
21
+ expected?: string | string[];
22
+ actual?: string;
23
+ repair?: string;
24
+ relatedRefs?: string[];
25
+ }
26
+
27
+ type DiagnosticMetadata = Partial<Pick<PointCoreDiagnostic, "expected" | "actual" | "repair" | "relatedRefs">>;
28
+ type ScopeEntry = { type: PointCoreTypeExpression; mutable: boolean };
29
+ type Scope = Map<string, ScopeEntry>;
30
+
31
31
  const PRIMITIVE_TYPES = new Set(["Text", "Int", "Float", "Bool", "Void", "List", "Maybe", "Error", "Or"]);
32
-
33
- export function checkPointCore(program: PointCoreProgram): PointCoreDiagnostic[] {
34
- const checker = new CoreChecker(program);
35
- return checker.check();
36
- }
37
-
38
- class CoreChecker {
39
- private readonly diagnostics: PointCoreDiagnostic[] = [];
40
- private readonly types = new Set(PRIMITIVE_TYPES);
41
- private readonly typeDeclarations = new Map<string, PointCoreTypeDeclaration>();
42
- private readonly globals: Scope = new Map();
32
+
33
+ export function checkPointCore(program: PointCoreProgram): PointCoreDiagnostic[] {
34
+ const checker = new CoreChecker(program);
35
+ return checker.check();
36
+ }
37
+
38
+ class CoreChecker {
39
+ private readonly diagnostics: PointCoreDiagnostic[] = [];
40
+ private readonly types = new Set(PRIMITIVE_TYPES);
41
+ private readonly typeDeclarations = new Map<string, PointCoreTypeDeclaration>();
42
+ private readonly globals: Scope = new Map();
43
43
  private readonly functions = new Map<string, PointCoreFunctionDeclaration | PointCoreExternalDeclaration>();
44
-
45
- constructor(private readonly program: PointCoreProgram) {}
46
-
47
- check(): PointCoreDiagnostic[] {
48
- this.collectDeclarations();
49
- for (const declaration of this.program.declarations) this.checkDeclaration(declaration);
50
- return this.diagnostics;
51
- }
52
-
53
- private collectDeclarations() {
54
- for (const declaration of this.program.declarations) {
55
- if (declaration.kind === "type") {
56
- if (this.types.has(declaration.name)) {
57
- this.push("duplicate-type", `Duplicate type ${declaration.name}`, `type.${declaration.name}`, declaration.span);
58
- }
59
- this.types.add(declaration.name);
60
- this.typeDeclarations.set(declaration.name, declaration);
61
- }
62
- if (declaration.kind === "value") this.addGlobal(declaration);
44
+
45
+ constructor(private readonly program: PointCoreProgram) {}
46
+
47
+ check(): PointCoreDiagnostic[] {
48
+ this.collectDeclarations();
49
+ for (const declaration of this.program.declarations) this.checkDeclaration(declaration);
50
+ return this.diagnostics;
51
+ }
52
+
53
+ private collectDeclarations() {
54
+ for (const declaration of this.program.declarations) {
55
+ if (declaration.kind === "type") {
56
+ if (this.types.has(declaration.name)) {
57
+ this.push("duplicate-type", `Duplicate type ${declaration.name}`, `type.${declaration.name}`, declaration.span);
58
+ }
59
+ this.types.add(declaration.name);
60
+ this.typeDeclarations.set(declaration.name, declaration);
61
+ }
62
+ if (declaration.kind === "value") this.addGlobal(declaration);
63
63
  if (declaration.kind === "function") {
64
- if (this.functions.has(declaration.name)) {
65
- this.push("duplicate-function", `Duplicate function ${declaration.name}`, `fn.${declaration.name}`, declaration.span);
66
- }
64
+ if (this.functions.has(declaration.name)) {
65
+ this.push("duplicate-function", `Duplicate function ${declaration.name}`, `fn.${declaration.name}`, declaration.span);
66
+ }
67
67
  this.functions.set(declaration.name, declaration);
68
68
  }
69
69
  if (declaration.kind === "external") {
@@ -72,72 +72,72 @@ class CoreChecker {
72
72
  }
73
73
  this.functions.set(declaration.name, declaration);
74
74
  }
75
- }
76
- }
77
-
78
- private addGlobal(declaration: PointCoreValueDeclaration) {
79
- if (this.globals.has(declaration.name)) {
80
- this.push("duplicate-value", `Duplicate value ${declaration.name}`, `value.${declaration.name}`, declaration.span);
81
- }
82
- this.globals.set(declaration.name, { type: declaration.type, mutable: declaration.mutable });
83
- }
84
-
85
- private checkDeclaration(declaration: PointCoreDeclaration) {
75
+ }
76
+ }
77
+
78
+ private addGlobal(declaration: PointCoreValueDeclaration) {
79
+ if (this.globals.has(declaration.name)) {
80
+ this.push("duplicate-value", `Duplicate value ${declaration.name}`, `value.${declaration.name}`, declaration.span);
81
+ }
82
+ this.globals.set(declaration.name, { type: declaration.type, mutable: declaration.mutable });
83
+ }
84
+
85
+ private checkDeclaration(declaration: PointCoreDeclaration) {
86
86
  if (declaration.kind === "import") return;
87
87
  if (declaration.kind === "external") {
88
88
  for (const param of declaration.params) this.checkType(param.type, `external.${declaration.name}.${param.name}.type`);
89
89
  this.checkType(declaration.returnType, `external.${declaration.name}.return`);
90
90
  return;
91
91
  }
92
- if (declaration.kind === "type") {
93
- for (const field of declaration.fields) this.checkType(field.type, `type.${declaration.name}.${field.name}`);
94
- return;
95
- }
96
- if (declaration.kind === "value") {
97
- this.checkType(declaration.type, `value.${declaration.name}.type`);
98
- this.checkExpressionAssignable(declaration.value, declaration.type, `value.${declaration.name}.value`, this.globals);
99
- return;
100
- }
101
- this.checkFunction(declaration);
102
- }
103
-
104
- private checkFunction(declaration: PointCoreFunctionDeclaration) {
105
- this.checkType(declaration.returnType, `fn.${declaration.name}.return`);
106
- const locals = new Map(this.globals);
107
- for (const param of declaration.params) {
108
- this.checkType(param.type, `fn.${declaration.name}.${param.name}.type`);
109
- locals.set(param.name, { type: param.type, mutable: false });
110
- }
111
- for (const statement of declaration.body) {
112
- this.checkStatement(statement, declaration, locals);
113
- }
114
- }
115
-
116
- private checkStatement(
117
- statement: PointCoreStatement,
118
- fn: PointCoreFunctionDeclaration,
119
- locals: Scope,
120
- ) {
121
- if (statement.kind === "return") {
122
- if (!statement.value) {
123
- if (fn.returnType.name !== "Void") {
124
- this.push("return-type-mismatch", `Function ${fn.name} must return ${fn.returnType.name}`, `fn.${fn.name}.return`, statement.span);
125
- }
126
- return;
127
- }
128
- this.checkExpressionAssignable(statement.value, fn.returnType, `fn.${fn.name}.return`, locals);
129
- return;
130
- }
131
- if (statement.kind === "value") {
132
- this.checkType(statement.type, `fn.${fn.name}.${statement.name}.type`);
133
- this.checkExpressionAssignable(statement.value, statement.type, `fn.${fn.name}.${statement.name}.value`, locals);
134
- locals.set(statement.name, { type: statement.type, mutable: statement.mutable });
135
- return;
136
- }
137
- if (statement.kind === "assignment") {
138
- this.checkAssignment(statement, fn, locals);
139
- return;
140
- }
92
+ if (declaration.kind === "type") {
93
+ for (const field of declaration.fields) this.checkType(field.type, `type.${declaration.name}.${field.name}`);
94
+ return;
95
+ }
96
+ if (declaration.kind === "value") {
97
+ this.checkType(declaration.type, `value.${declaration.name}.type`);
98
+ this.checkExpressionAssignable(declaration.value, declaration.type, `value.${declaration.name}.value`, this.globals);
99
+ return;
100
+ }
101
+ this.checkFunction(declaration);
102
+ }
103
+
104
+ private checkFunction(declaration: PointCoreFunctionDeclaration) {
105
+ this.checkType(declaration.returnType, `fn.${declaration.name}.return`);
106
+ const locals = new Map(this.globals);
107
+ for (const param of declaration.params) {
108
+ this.checkType(param.type, `fn.${declaration.name}.${param.name}.type`);
109
+ locals.set(param.name, { type: param.type, mutable: false });
110
+ }
111
+ for (const statement of declaration.body) {
112
+ this.checkStatement(statement, declaration, locals);
113
+ }
114
+ }
115
+
116
+ private checkStatement(
117
+ statement: PointCoreStatement,
118
+ fn: PointCoreFunctionDeclaration,
119
+ locals: Scope,
120
+ ) {
121
+ if (statement.kind === "return") {
122
+ if (!statement.value) {
123
+ if (fn.returnType.name !== "Void") {
124
+ this.push("return-type-mismatch", `Function ${fn.name} must return ${fn.returnType.name}`, `fn.${fn.name}.return`, statement.span);
125
+ }
126
+ return;
127
+ }
128
+ this.checkExpressionAssignable(statement.value, fn.returnType, `fn.${fn.name}.return`, locals);
129
+ return;
130
+ }
131
+ if (statement.kind === "value") {
132
+ this.checkType(statement.type, `fn.${fn.name}.${statement.name}.type`);
133
+ this.checkExpressionAssignable(statement.value, statement.type, `fn.${fn.name}.${statement.name}.value`, locals);
134
+ locals.set(statement.name, { type: statement.type, mutable: statement.mutable });
135
+ return;
136
+ }
137
+ if (statement.kind === "assignment") {
138
+ this.checkAssignment(statement, fn, locals);
139
+ return;
140
+ }
141
141
  if (statement.kind === "if") {
142
142
  this.checkExpressionAssignable(statement.condition, typeRef("Bool"), `fn.${fn.name}.if.condition`, locals);
143
143
  const thenLocals = new Map(locals);
@@ -173,28 +173,28 @@ class CoreChecker {
173
173
  loopLocals.set(statement.itemName, { type: iterableType.args[0]!, mutable: false });
174
174
  for (const child of statement.body) this.checkStatement(child, fn, loopLocals);
175
175
  }
176
-
177
- private checkAssignment(
178
- statement: Extract<PointCoreStatement, { kind: "assignment" }>,
179
- fn: PointCoreFunctionDeclaration,
180
- locals: Scope,
181
- ) {
182
- const target = locals.get(statement.name);
183
- const path = `fn.${fn.name}.${statement.name}.assignment`;
184
- if (!target) {
185
- this.push("unknown-identifier", `Unknown identifier ${statement.name}`, path, statement.span, {
186
- actual: statement.name,
187
- repair: `Declare var ${statement.name}: <Type> before assigning to it.`,
188
- });
189
- this.typeOfExpression(statement.value, locals, `${path}.value`);
190
- return;
191
- }
192
- if (!target.mutable) {
193
- this.push("immutable-assignment", `Cannot assign to immutable value ${statement.name}`, path, statement.span, {
194
- actual: statement.name,
195
- repair: `Declare ${statement.name} with var if it needs to change.`,
196
- });
197
- }
176
+
177
+ private checkAssignment(
178
+ statement: Extract<PointCoreStatement, { kind: "assignment" }>,
179
+ fn: PointCoreFunctionDeclaration,
180
+ locals: Scope,
181
+ ) {
182
+ const target = locals.get(statement.name);
183
+ const path = `fn.${fn.name}.${statement.name}.assignment`;
184
+ if (!target) {
185
+ this.push("unknown-identifier", `Unknown identifier ${statement.name}`, path, statement.span, {
186
+ actual: statement.name,
187
+ repair: `Declare var ${statement.name}: <Type> before assigning to it.`,
188
+ });
189
+ this.typeOfExpression(statement.value, locals, `${path}.value`);
190
+ return;
191
+ }
192
+ if (!target.mutable) {
193
+ this.push("immutable-assignment", `Cannot assign to immutable value ${statement.name}`, path, statement.span, {
194
+ actual: statement.name,
195
+ repair: `Declare ${statement.name} with var if it needs to change.`,
196
+ });
197
+ }
198
198
  if ((statement.operator === "+=" || statement.operator === "-=") && !isNumeric(String(target.type.name))) {
199
199
  this.push("operator-type-mismatch", `${statement.operator} requires a numeric target`, path, statement.span, {
200
200
  expected: "Int or Float target",
@@ -202,9 +202,9 @@ class CoreChecker {
202
202
  repair: `Use ${statement.operator} only with Int or Float values.`,
203
203
  });
204
204
  }
205
- this.checkExpressionAssignable(statement.value, target.type, `${path}.value`, locals);
206
- }
207
-
205
+ this.checkExpressionAssignable(statement.value, target.type, `${path}.value`, locals);
206
+ }
207
+
208
208
  private checkExpressionAssignable(
209
209
  expression: PointCoreExpression,
210
210
  expected: PointCoreTypeExpression,
@@ -237,80 +237,80 @@ class CoreChecker {
237
237
  if (expression.kind === "list") {
238
238
  this.checkListAssignable(expression, expected, path, scope);
239
239
  return;
240
- }
241
- if (expression.kind === "record") {
242
- this.checkRecordAssignable(expression, expected, path, scope);
243
- return;
244
- }
245
- const actual = this.typeOfExpression(expression, scope, path);
246
- if (actual && !sameType(actual, expected)) {
247
- this.push("type-mismatch", `Expected ${formatType(expected)}, got ${formatType(actual)}`, path, expression.span, {
248
- expected: formatType(expected),
249
- actual: formatType(actual),
250
- repair: `Return or assign a ${formatType(expected)} value here.`,
251
- });
252
- }
253
- }
254
-
255
- private checkListAssignable(
256
- expression: Extract<PointCoreExpression, { kind: "list" }>,
257
- expected: PointCoreTypeExpression,
258
- path: string,
259
- scope: Scope,
260
- ) {
261
- if (expected.name !== "List" || expected.args.length !== 1) {
262
- this.push("type-mismatch", `Expected ${formatType(expected)}, got List`, path, expression.span, {
263
- expected: formatType(expected),
264
- actual: "List",
265
- repair: `Annotate this value as List<T> or replace the list with a ${formatType(expected)} value.`,
266
- });
267
- return;
268
- }
269
- for (const [index, item] of expression.items.entries()) {
270
- this.checkExpressionAssignable(item, expected.args[0]!, `${path}.${index}`, scope);
271
- }
272
- }
273
-
274
- private checkRecordAssignable(
275
- expression: Extract<PointCoreExpression, { kind: "record" }>,
276
- expected: PointCoreTypeExpression,
277
- path: string,
278
- scope: Scope,
279
- ) {
280
- const declaration = this.typeDeclarations.get(String(expected.name));
281
- if (!declaration) {
282
- this.push("type-mismatch", `Expected ${formatType(expected)}, got record`, path, expression.span, {
283
- expected: formatType(expected),
284
- actual: "record",
285
- repair: "Assign record literals to a named type with declared fields.",
286
- });
287
- return;
288
- }
289
- const provided = new Map(expression.fields.map((field) => [field.name, field]));
290
- for (const field of declaration.fields) {
291
- const value = provided.get(field.name);
292
- if (!value) {
293
- this.push("missing-field", `Missing field ${field.name}`, `${path}.${field.name}`, expression.span, {
294
- expected: declaration.fields.map((candidate) => candidate.name),
295
- actual: [...provided.keys()].join(", "),
296
- repair: `Add field ${field.name}: ${formatType(field.type)} to this record literal.`,
297
- relatedRefs: this.fieldRefsFor(declaration),
298
- });
299
- continue;
300
- }
301
- this.checkExpressionAssignable(value.value, field.type, `${path}.${field.name}`, scope);
302
- provided.delete(field.name);
303
- }
304
- for (const extra of provided.values()) {
305
- this.push("unknown-field", `Unknown field ${extra.name}`, `${path}.${extra.name}`, extra.span, {
306
- expected: declaration.fields.map((field) => field.name),
307
- actual: extra.name,
308
- repair: `Use one of: ${declaration.fields.map((field) => field.name).join(", ")}.`,
309
- relatedRefs: this.fieldRefsFor(declaration),
310
- });
311
- }
312
- }
313
-
240
+ }
241
+ if (expression.kind === "record") {
242
+ this.checkRecordAssignable(expression, expected, path, scope);
243
+ return;
244
+ }
245
+ const actual = this.typeOfExpression(expression, scope, path);
246
+ if (actual && !sameType(actual, expected)) {
247
+ this.push("type-mismatch", `Expected ${formatType(expected)}, got ${formatType(actual)}`, path, expression.span, {
248
+ expected: formatType(expected),
249
+ actual: formatType(actual),
250
+ repair: `Return or assign a ${formatType(expected)} value here.`,
251
+ });
252
+ }
253
+ }
254
+
255
+ private checkListAssignable(
256
+ expression: Extract<PointCoreExpression, { kind: "list" }>,
257
+ expected: PointCoreTypeExpression,
258
+ path: string,
259
+ scope: Scope,
260
+ ) {
261
+ if (expected.name !== "List" || expected.args.length !== 1) {
262
+ this.push("type-mismatch", `Expected ${formatType(expected)}, got List`, path, expression.span, {
263
+ expected: formatType(expected),
264
+ actual: "List",
265
+ repair: `Annotate this value as List<T> or replace the list with a ${formatType(expected)} value.`,
266
+ });
267
+ return;
268
+ }
269
+ for (const [index, item] of expression.items.entries()) {
270
+ this.checkExpressionAssignable(item, expected.args[0]!, `${path}.${index}`, scope);
271
+ }
272
+ }
273
+
274
+ private checkRecordAssignable(
275
+ expression: Extract<PointCoreExpression, { kind: "record" }>,
276
+ expected: PointCoreTypeExpression,
277
+ path: string,
278
+ scope: Scope,
279
+ ) {
280
+ const declaration = this.typeDeclarations.get(String(expected.name));
281
+ if (!declaration) {
282
+ this.push("type-mismatch", `Expected ${formatType(expected)}, got record`, path, expression.span, {
283
+ expected: formatType(expected),
284
+ actual: "record",
285
+ repair: "Assign record literals to a named type with declared fields.",
286
+ });
287
+ return;
288
+ }
289
+ const provided = new Map(expression.fields.map((field) => [field.name, field]));
290
+ for (const field of declaration.fields) {
291
+ const value = provided.get(field.name);
292
+ if (!value) {
293
+ this.push("missing-field", `Missing field ${field.name}`, `${path}.${field.name}`, expression.span, {
294
+ expected: declaration.fields.map((candidate) => candidate.name),
295
+ actual: [...provided.keys()].join(", "),
296
+ repair: `Add field ${field.name}: ${formatType(field.type)} to this record literal.`,
297
+ relatedRefs: this.fieldRefsFor(declaration),
298
+ });
299
+ continue;
300
+ }
301
+ this.checkExpressionAssignable(value.value, field.type, `${path}.${field.name}`, scope);
302
+ provided.delete(field.name);
303
+ }
304
+ for (const extra of provided.values()) {
305
+ this.push("unknown-field", `Unknown field ${extra.name}`, `${path}.${extra.name}`, extra.span, {
306
+ expected: declaration.fields.map((field) => field.name),
307
+ actual: extra.name,
308
+ repair: `Use one of: ${declaration.fields.map((field) => field.name).join(", ")}.`,
309
+ relatedRefs: this.fieldRefsFor(declaration),
310
+ });
311
+ }
312
+ }
313
+
314
314
  private typeOfExpression(
315
315
  expression: PointCoreExpression,
316
316
  scope: Scope,
@@ -322,32 +322,32 @@ class CoreChecker {
322
322
  const valueType =
323
323
  typeof expression.value === "string"
324
324
  ? "Text"
325
- : typeof expression.value === "boolean"
326
- ? "Bool"
327
- : Number.isInteger(expression.value)
328
- ? "Int"
329
- : "Float";
330
- return { kind: "typeRef", name: valueType, args: [], span: expression.span };
331
- }
332
- if (expression.kind === "list") return this.typeOfListExpression(expression, scope, path);
333
- if (expression.kind === "record") {
334
- this.push("record-type-required", "Record literals require an expected named type", path, expression.span);
335
- return null;
336
- }
337
- if (expression.kind === "identifier") {
338
- const entry = scope.get(expression.name);
339
- if (!entry) {
340
- this.push("unknown-identifier", `Unknown identifier ${expression.name}`, path, expression.span, {
341
- actual: expression.name,
342
- repair: `Declare ${expression.name}, pass it as a parameter, or replace it with an in-scope symbol.`,
343
- });
344
- return null;
345
- }
346
- return entry.type;
347
- }
348
- if (expression.kind === "binary") {
349
- return this.typeOfBinaryExpression(expression, scope, path);
350
- }
325
+ : typeof expression.value === "boolean"
326
+ ? "Bool"
327
+ : Number.isInteger(expression.value)
328
+ ? "Int"
329
+ : "Float";
330
+ return { kind: "typeRef", name: valueType, args: [], span: expression.span };
331
+ }
332
+ if (expression.kind === "list") return this.typeOfListExpression(expression, scope, path);
333
+ if (expression.kind === "record") {
334
+ this.push("record-type-required", "Record literals require an expected named type", path, expression.span);
335
+ return null;
336
+ }
337
+ if (expression.kind === "identifier") {
338
+ const entry = scope.get(expression.name);
339
+ if (!entry) {
340
+ this.push("unknown-identifier", `Unknown identifier ${expression.name}`, path, expression.span, {
341
+ actual: expression.name,
342
+ repair: `Declare ${expression.name}, pass it as a parameter, or replace it with an in-scope symbol.`,
343
+ });
344
+ return null;
345
+ }
346
+ return entry.type;
347
+ }
348
+ if (expression.kind === "binary") {
349
+ return this.typeOfBinaryExpression(expression, scope, path);
350
+ }
351
351
  if (expression.kind === "property") {
352
352
  return this.typeOfPropertyExpression(expression, scope, path);
353
353
  }
@@ -387,45 +387,45 @@ class CoreChecker {
387
387
  });
388
388
  }
389
389
  if (target.params.length !== expression.args.length) {
390
- this.push("arity-mismatch", `Function ${expression.callee} expects ${target.params.length} args`, path, expression.span, {
391
- expected: `${target.params.length} args`,
392
- actual: `${expression.args.length} args`,
393
- repair: `Call ${expression.callee} with ${target.params.length} argument(s).`,
394
- relatedRefs: [this.refFor(`fn.${target.name}`)],
395
- });
396
- }
397
- for (const [index, arg] of expression.args.entries()) {
398
- const param = target.params[index];
399
- if (param) this.checkExpressionAssignable(arg, param.type, `${path}.arg${index}`, scope);
400
- }
401
- return target.returnType;
402
- }
403
-
404
- private typeOfListExpression(
405
- expression: Extract<PointCoreExpression, { kind: "list" }>,
406
- scope: Scope,
407
- path: string,
408
- ): PointCoreTypeExpression | null {
409
- if (expression.items.length === 0) {
410
- this.push("list-type-required", "Empty lists require an expected List type", path, expression.span);
411
- return null;
412
- }
413
- const first = this.typeOfExpression(expression.items[0]!, scope, `${path}.0`);
414
- if (!first) return null;
415
- for (const [index, item] of expression.items.slice(1).entries()) {
416
- const actual = this.typeOfExpression(item, scope, `${path}.${index + 1}`);
417
- if (actual && !sameType(actual, first)) {
418
- this.push("type-mismatch", `Expected ${formatType(first)}, got ${formatType(actual)}`, `${path}.${index + 1}`, item.span);
419
- }
420
- }
421
- return { kind: "typeRef", name: "List", args: [first], span: expression.span };
422
- }
423
-
424
- private typeOfPropertyExpression(
425
- expression: Extract<PointCoreExpression, { kind: "property" }>,
426
- scope: Scope,
427
- path: string,
428
- ): PointCoreTypeExpression | null {
390
+ this.push("arity-mismatch", `Function ${expression.callee} expects ${target.params.length} args`, path, expression.span, {
391
+ expected: `${target.params.length} args`,
392
+ actual: `${expression.args.length} args`,
393
+ repair: `Call ${expression.callee} with ${target.params.length} argument(s).`,
394
+ relatedRefs: [this.refFor(`fn.${target.name}`)],
395
+ });
396
+ }
397
+ for (const [index, arg] of expression.args.entries()) {
398
+ const param = target.params[index];
399
+ if (param) this.checkExpressionAssignable(arg, param.type, `${path}.arg${index}`, scope);
400
+ }
401
+ return target.returnType;
402
+ }
403
+
404
+ private typeOfListExpression(
405
+ expression: Extract<PointCoreExpression, { kind: "list" }>,
406
+ scope: Scope,
407
+ path: string,
408
+ ): PointCoreTypeExpression | null {
409
+ if (expression.items.length === 0) {
410
+ this.push("list-type-required", "Empty lists require an expected List type", path, expression.span);
411
+ return null;
412
+ }
413
+ const first = this.typeOfExpression(expression.items[0]!, scope, `${path}.0`);
414
+ if (!first) return null;
415
+ for (const [index, item] of expression.items.slice(1).entries()) {
416
+ const actual = this.typeOfExpression(item, scope, `${path}.${index + 1}`);
417
+ if (actual && !sameType(actual, first)) {
418
+ this.push("type-mismatch", `Expected ${formatType(first)}, got ${formatType(actual)}`, `${path}.${index + 1}`, item.span);
419
+ }
420
+ }
421
+ return { kind: "typeRef", name: "List", args: [first], span: expression.span };
422
+ }
423
+
424
+ private typeOfPropertyExpression(
425
+ expression: Extract<PointCoreExpression, { kind: "property" }>,
426
+ scope: Scope,
427
+ path: string,
428
+ ): PointCoreTypeExpression | null {
429
429
  const targetType = this.typeOfExpression(expression.target, scope, `${path}.target`);
430
430
  if (!targetType) return null;
431
431
  if (targetType.name === "Maybe" && targetType.args.length === 1) {
@@ -437,85 +437,85 @@ class CoreChecker {
437
437
  return null;
438
438
  }
439
439
  const declaration = this.typeDeclarations.get(String(targetType.name));
440
- if (!declaration) {
441
- this.push("not-a-record", `${formatType(targetType)} has no fields`, path, expression.span, {
442
- actual: formatType(targetType),
443
- repair: "Only access fields on named record types.",
444
- });
445
- return null;
446
- }
447
- const field = declaration.fields.find((candidate) => candidate.name === expression.name);
448
- if (!field) {
449
- this.push("unknown-field", `Unknown field ${expression.name} on ${targetType.name}`, path, expression.span, {
450
- expected: declaration.fields.map((candidate) => candidate.name),
451
- actual: expression.name,
452
- repair: `Use one of: ${declaration.fields.map((candidate) => candidate.name).join(", ")}.`,
453
- relatedRefs: this.fieldRefsFor(declaration),
454
- });
455
- return null;
456
- }
457
- return field.type;
458
- }
459
-
460
- private typeOfBinaryExpression(
461
- expression: Extract<PointCoreExpression, { kind: "binary" }>,
462
- scope: Scope,
463
- path: string,
464
- ): PointCoreTypeExpression | null {
465
- const left = this.typeOfExpression(expression.left, scope, `${path}.left`);
466
- const right = this.typeOfExpression(expression.right, scope, `${path}.right`);
467
- if (!left || !right) return null;
468
- if (expression.operator === "and" || expression.operator === "or") {
469
- if (left.name !== "Bool" || right.name !== "Bool") {
470
- this.push("operator-type-mismatch", `${expression.operator} requires Bool operands`, path, expression.span, {
471
- expected: "Bool operands",
472
- actual: `${formatType(left)} and ${formatType(right)}`,
473
- repair: `Use Bool expressions on both sides of ${expression.operator}.`,
474
- });
475
- }
476
- return { kind: "typeRef", name: "Bool", args: [], span: expression.span };
477
- }
478
- if (expression.operator === "==" || expression.operator === "!=") {
479
- if (left.name !== right.name) {
480
- this.push("operator-type-mismatch", `${expression.operator} requires matching operand types`, path, expression.span, {
481
- expected: formatType(left),
482
- actual: formatType(right),
483
- repair: "Compare values with the same Point type.",
484
- });
485
- }
486
- return { kind: "typeRef", name: "Bool", args: [], span: expression.span };
487
- }
488
- if (expression.operator === "+" && left.name === "Text" && right.name === "Text") {
489
- return { kind: "typeRef", name: "Text", args: [], span: expression.span };
490
- }
491
- if (!isNumeric(left.name) || !isNumeric(right.name)) {
492
- this.push("operator-type-mismatch", `${expression.operator} requires numeric operands`, path, expression.span, {
493
- expected: "Int or Float operands",
494
- actual: `${formatType(left)} and ${formatType(right)}`,
495
- repair: `Use numeric expressions on both sides of ${expression.operator}.`,
496
- });
497
- return null;
498
- }
499
- if (["<", "<=", ">", ">="].includes(expression.operator)) {
500
- return { kind: "typeRef", name: "Bool", args: [], span: expression.span };
501
- }
502
- return { kind: "typeRef", name: left.name === "Float" || right.name === "Float" ? "Float" : "Int", args: [], span: expression.span };
503
- }
504
-
505
- private checkType(type: PointCoreTypeExpression, path: string) {
506
- if (!this.types.has(type.name)) {
507
- this.push("unknown-type", `Unknown type ${type.name}`, path, type.span, {
508
- expected: [...this.types].sort(),
509
- actual: String(type.name),
510
- repair: `Declare type ${type.name} or use an existing type.`,
511
- });
512
- }
440
+ if (!declaration) {
441
+ this.push("not-a-record", `${formatType(targetType)} has no fields`, path, expression.span, {
442
+ actual: formatType(targetType),
443
+ repair: "Only access fields on named record types.",
444
+ });
445
+ return null;
446
+ }
447
+ const field = declaration.fields.find((candidate) => candidate.name === expression.name);
448
+ if (!field) {
449
+ this.push("unknown-field", `Unknown field ${expression.name} on ${targetType.name}`, path, expression.span, {
450
+ expected: declaration.fields.map((candidate) => candidate.name),
451
+ actual: expression.name,
452
+ repair: `Use one of: ${declaration.fields.map((candidate) => candidate.name).join(", ")}.`,
453
+ relatedRefs: this.fieldRefsFor(declaration),
454
+ });
455
+ return null;
456
+ }
457
+ return field.type;
458
+ }
459
+
460
+ private typeOfBinaryExpression(
461
+ expression: Extract<PointCoreExpression, { kind: "binary" }>,
462
+ scope: Scope,
463
+ path: string,
464
+ ): PointCoreTypeExpression | null {
465
+ const left = this.typeOfExpression(expression.left, scope, `${path}.left`);
466
+ const right = this.typeOfExpression(expression.right, scope, `${path}.right`);
467
+ if (!left || !right) return null;
468
+ if (expression.operator === "and" || expression.operator === "or") {
469
+ if (left.name !== "Bool" || right.name !== "Bool") {
470
+ this.push("operator-type-mismatch", `${expression.operator} requires Bool operands`, path, expression.span, {
471
+ expected: "Bool operands",
472
+ actual: `${formatType(left)} and ${formatType(right)}`,
473
+ repair: `Use Bool expressions on both sides of ${expression.operator}.`,
474
+ });
475
+ }
476
+ return { kind: "typeRef", name: "Bool", args: [], span: expression.span };
477
+ }
478
+ if (expression.operator === "==" || expression.operator === "!=") {
479
+ if (left.name !== right.name) {
480
+ this.push("operator-type-mismatch", `${expression.operator} requires matching operand types`, path, expression.span, {
481
+ expected: formatType(left),
482
+ actual: formatType(right),
483
+ repair: "Compare values with the same Point type.",
484
+ });
485
+ }
486
+ return { kind: "typeRef", name: "Bool", args: [], span: expression.span };
487
+ }
488
+ if (expression.operator === "+" && left.name === "Text" && right.name === "Text") {
489
+ return { kind: "typeRef", name: "Text", args: [], span: expression.span };
490
+ }
491
+ if (!isNumeric(left.name) || !isNumeric(right.name)) {
492
+ this.push("operator-type-mismatch", `${expression.operator} requires numeric operands`, path, expression.span, {
493
+ expected: "Int or Float operands",
494
+ actual: `${formatType(left)} and ${formatType(right)}`,
495
+ repair: `Use numeric expressions on both sides of ${expression.operator}.`,
496
+ });
497
+ return null;
498
+ }
499
+ if (["<", "<=", ">", ">="].includes(expression.operator)) {
500
+ return { kind: "typeRef", name: "Bool", args: [], span: expression.span };
501
+ }
502
+ return { kind: "typeRef", name: left.name === "Float" || right.name === "Float" ? "Float" : "Int", args: [], span: expression.span };
503
+ }
504
+
505
+ private checkType(type: PointCoreTypeExpression, path: string) {
506
+ if (!this.types.has(type.name)) {
507
+ this.push("unknown-type", `Unknown type ${type.name}`, path, type.span, {
508
+ expected: [...this.types].sort(),
509
+ actual: String(type.name),
510
+ repair: `Declare type ${type.name} or use an existing type.`,
511
+ });
512
+ }
513
513
  if (type.name === "List" && type.args.length !== 1) {
514
- this.push("invalid-type-arity", "List requires one type argument", path, type.span, {
515
- expected: "List<T>",
516
- actual: formatType(type),
517
- repair: "Use List<Text>, List<Int>, or another concrete item type.",
518
- });
514
+ this.push("invalid-type-arity", "List requires one type argument", path, type.span, {
515
+ expected: "List<T>",
516
+ actual: formatType(type),
517
+ repair: "Use List<Text>, List<Int>, or another concrete item type.",
518
+ });
519
519
  }
520
520
  if (type.name === "Maybe" && type.args.length !== 1) {
521
521
  this.push("invalid-type-arity", "Maybe requires one type argument", path, type.span, {
@@ -532,59 +532,59 @@ class CoreChecker {
532
532
  });
533
533
  }
534
534
  if (type.name !== "List" && type.name !== "Maybe" && type.name !== "Or" && type.args.length > 0 && !this.typeDeclarations.has(String(type.name))) {
535
- this.push("invalid-type-arity", `${type.name} does not accept type arguments`, path, type.span, {
536
- expected: String(type.name),
537
- actual: formatType(type),
538
- repair: `Remove type arguments from ${type.name}.`,
539
- });
540
- }
541
- for (const arg of type.args) this.checkType(arg, `${path}.arg`);
542
- }
543
-
544
- private push(
545
- code: string,
546
- message: string,
547
- path: string,
548
- span: PointSourceSpan | undefined,
549
- metadata: DiagnosticMetadata = {},
550
- ) {
551
- this.diagnostics.push({
552
- code,
553
- message,
554
- path,
555
- ref: this.refFor(path),
556
- severity: "error",
557
- span: span ?? null,
558
- ...metadata,
559
- });
560
- }
561
-
562
- private refFor(path: string): string {
563
- return `point://core/${this.program.module ?? "anonymous"}/${path}`;
564
- }
565
-
566
- private fieldRefsFor(declaration: PointCoreTypeDeclaration): string[] {
567
- return declaration.fields.map((field) => this.refFor(`type.${declaration.name}.${field.name}`));
568
- }
569
- }
570
-
571
- function isNumeric(type: string): boolean {
572
- return type === "Int" || type === "Float";
573
- }
574
-
575
- function sameType(left: PointCoreTypeExpression, right: PointCoreTypeExpression): boolean {
576
- const leftArgs = left.args ?? [];
577
- const rightArgs = right.args ?? [];
578
- return left.name === right.name && leftArgs.length === rightArgs.length && leftArgs.every((arg, index) => sameType(arg, rightArgs[index]!));
579
- }
580
-
535
+ this.push("invalid-type-arity", `${type.name} does not accept type arguments`, path, type.span, {
536
+ expected: String(type.name),
537
+ actual: formatType(type),
538
+ repair: `Remove type arguments from ${type.name}.`,
539
+ });
540
+ }
541
+ for (const arg of type.args) this.checkType(arg, `${path}.arg`);
542
+ }
543
+
544
+ private push(
545
+ code: string,
546
+ message: string,
547
+ path: string,
548
+ span: PointSourceSpan | undefined,
549
+ metadata: DiagnosticMetadata = {},
550
+ ) {
551
+ this.diagnostics.push({
552
+ code,
553
+ message,
554
+ path,
555
+ ref: this.refFor(path),
556
+ severity: "error",
557
+ span: span ?? null,
558
+ ...metadata,
559
+ });
560
+ }
561
+
562
+ private refFor(path: string): string {
563
+ return `point://core/${this.program.module ?? "anonymous"}/${path}`;
564
+ }
565
+
566
+ private fieldRefsFor(declaration: PointCoreTypeDeclaration): string[] {
567
+ return declaration.fields.map((field) => this.refFor(`type.${declaration.name}.${field.name}`));
568
+ }
569
+ }
570
+
571
+ function isNumeric(type: string): boolean {
572
+ return type === "Int" || type === "Float";
573
+ }
574
+
575
+ function sameType(left: PointCoreTypeExpression, right: PointCoreTypeExpression): boolean {
576
+ const leftArgs = left.args ?? [];
577
+ const rightArgs = right.args ?? [];
578
+ return left.name === right.name && leftArgs.length === rightArgs.length && leftArgs.every((arg, index) => sameType(arg, rightArgs[index]!));
579
+ }
580
+
581
581
  function formatType(type: PointCoreTypeExpression): string {
582
582
  const args = type.args ?? [];
583
583
  if (args.length === 0) return String(type.name);
584
584
  if (type.name === "Or") return args.map(formatType).join(" or ");
585
585
  return `${type.name}<${args.map(formatType).join(", ")}>`;
586
586
  }
587
-
588
- function typeRef(name: string, args: PointCoreTypeExpression[] = [], span?: PointSourceSpan): PointCoreTypeExpression {
589
- return { kind: "typeRef", name, args, span };
590
- }
587
+
588
+ function typeRef(name: string, args: PointCoreTypeExpression[] = [], span?: PointSourceSpan): PointCoreTypeExpression {
589
+ return { kind: "typeRef", name, args, span };
590
+ }