@hatchingpoint/point 0.0.6 → 0.0.8

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.
@@ -1,120 +1,120 @@
1
- import type {
2
- PointCoreDeclaration,
3
- PointCoreExpression,
4
- PointCoreFunctionDeclaration,
5
- PointCoreParameter,
6
- PointCoreProgram,
7
- PointCoreStatement,
8
- PointCoreTypeDeclaration,
9
- PointCoreValueDeclaration,
10
- } from "../ast.ts";
11
-
12
- /** Debug/test helper: pretty-print core AST as legacy core text. Not used in production. */
13
- export function formatPointCore(program: PointCoreProgram): string {
14
- const lines: string[] = [];
15
- if (program.module) {
16
- lines.push(`module ${program.module}`, "");
17
- }
18
- program.declarations.forEach((declaration, index) => {
19
- if (index > 0) lines.push("");
20
- lines.push(...formatDeclaration(declaration));
21
- });
22
- return `${lines.join("\n")}\n`;
23
- }
24
-
25
- function formatDeclaration(declaration: PointCoreDeclaration): string[] {
26
- if (declaration.kind === "import") {
27
- return [`import { ${declaration.names.join(", ")} } from ${JSON.stringify(declaration.from)}`];
28
- }
29
- if (declaration.kind === "external") {
30
- const imported = declaration.importName ? ` as ${declaration.importName}` : "";
31
- return [
32
- `external fn ${declaration.name}(${declaration.params.map(formatParam).join(", ")}): ${formatTypeExpression(declaration.returnType)} from ${JSON.stringify(declaration.from)}${imported}`,
33
- ];
34
- }
35
- if (declaration.kind === "value") return [formatValue(declaration)];
36
- if (declaration.kind === "type") return formatType(declaration);
37
- return formatFunction(declaration);
38
- }
39
-
40
- function formatType(declaration: PointCoreTypeDeclaration): string[] {
41
- return [
42
- `type ${declaration.name} {`,
43
- ...declaration.fields.map((field) => ` ${formatParam(field)}`),
44
- "}",
45
- ];
46
- }
47
-
48
- function formatFunction(declaration: PointCoreFunctionDeclaration): string[] {
49
- return [
50
- `fn ${declaration.name}(${declaration.params.map(formatParam).join(", ")}): ${formatTypeExpression(declaration.returnType)} {`,
51
- ...indentLines(declaration.body.flatMap((statement) => formatStatementLines(statement))),
52
- "}",
53
- ];
54
- }
55
-
56
- function formatStatementLines(statement: PointCoreStatement): string[] {
57
- if (statement.kind === "return") {
58
- return [statement.value ? `return ${formatExpression(statement.value)}` : "return"];
59
- }
60
- if (statement.kind === "value") return [formatValue(statement)];
61
- if (statement.kind === "assignment") return [`${statement.name} ${statement.operator} ${formatExpression(statement.value)}`];
62
- if (statement.kind === "if") return formatIf(statement);
63
- if (statement.kind === "for") {
64
- return [
65
- `for ${statement.itemName} in ${formatExpression(statement.iterable)} {`,
66
- ...indentLines(statement.body.flatMap((child) => formatStatementLines(child))),
67
- "}",
68
- ];
69
- }
70
- return [formatExpression(statement.value)];
71
- }
72
-
73
- function formatIf(statement: Extract<PointCoreStatement, { kind: "if" }>): string[] {
74
- const lines = [
75
- `if ${formatExpression(statement.condition)} {`,
76
- ...indentLines(statement.thenBody.flatMap((child) => formatStatementLines(child))),
77
- "}",
78
- ];
79
- if (statement.elseBody.length > 0) {
80
- lines.push(
81
- "else {",
82
- ...indentLines(statement.elseBody.flatMap((child) => formatStatementLines(child))),
83
- "}",
84
- );
85
- }
86
- return lines;
87
- }
88
-
89
- function formatValue(declaration: PointCoreValueDeclaration): string {
90
- return `${declaration.mutable ? "var" : "let"} ${declaration.name}: ${formatTypeExpression(declaration.type)} = ${formatExpression(declaration.value)}`;
91
- }
92
-
93
- function formatParam(param: PointCoreParameter): string {
94
- return `${param.name}: ${formatTypeExpression(param.type)}`;
95
- }
96
-
97
- function formatExpression(expression: PointCoreExpression): string {
98
- if (expression.kind === "literal") return JSON.stringify(expression.value);
99
- if (expression.kind === "identifier") return expression.name;
100
- if (expression.kind === "list") return `[${expression.items.map(formatExpression).join(", ")}]`;
101
- if (expression.kind === "record") {
102
- return `{ ${expression.fields.map((field) => `${field.name}: ${formatExpression(field.value)}`).join(", ")} }`;
103
- }
104
- if (expression.kind === "await") return `await ${formatExpression(expression.value)}`;
105
- if (expression.kind === "property") return `${formatExpression(expression.target)}.${expression.name}`;
106
- if (expression.kind === "binary") {
107
- return `${formatExpression(expression.left)} ${expression.operator} ${formatExpression(expression.right)}`;
108
- }
109
- return `${expression.callee}(${expression.args.map(formatExpression).join(", ")})`;
110
- }
111
-
112
- function formatTypeExpression(type: PointCoreParameter["type"]): string {
113
- if (type.args.length === 0) return String(type.name);
114
- if (type.name === "Or") return type.args.map(formatTypeExpression).join(" or ");
115
- return `${type.name}<${type.args.map(formatTypeExpression).join(", ")}>`;
116
- }
117
-
118
- function indentLines(lines: string[]): string[] {
119
- return lines.map((line) => ` ${line}`);
120
- }
1
+ import type {
2
+ PointCoreDeclaration,
3
+ PointCoreExpression,
4
+ PointCoreFunctionDeclaration,
5
+ PointCoreParameter,
6
+ PointCoreProgram,
7
+ PointCoreStatement,
8
+ PointCoreTypeDeclaration,
9
+ PointCoreValueDeclaration,
10
+ } from "../ast.ts";
11
+
12
+ /** Debug/test helper: pretty-print core AST as legacy core text. Not used in production. */
13
+ export function formatPointCore(program: PointCoreProgram): string {
14
+ const lines: string[] = [];
15
+ if (program.module) {
16
+ lines.push(`module ${program.module}`, "");
17
+ }
18
+ program.declarations.forEach((declaration, index) => {
19
+ if (index > 0) lines.push("");
20
+ lines.push(...formatDeclaration(declaration));
21
+ });
22
+ return `${lines.join("\n")}\n`;
23
+ }
24
+
25
+ function formatDeclaration(declaration: PointCoreDeclaration): string[] {
26
+ if (declaration.kind === "import") {
27
+ return [`import { ${declaration.names.join(", ")} } from ${JSON.stringify(declaration.from)}`];
28
+ }
29
+ if (declaration.kind === "external") {
30
+ const imported = declaration.importName ? ` as ${declaration.importName}` : "";
31
+ return [
32
+ `external fn ${declaration.name}(${declaration.params.map(formatParam).join(", ")}): ${formatTypeExpression(declaration.returnType)} from ${JSON.stringify(declaration.from)}${imported}`,
33
+ ];
34
+ }
35
+ if (declaration.kind === "value") return [formatValue(declaration)];
36
+ if (declaration.kind === "type") return formatType(declaration);
37
+ return formatFunction(declaration);
38
+ }
39
+
40
+ function formatType(declaration: PointCoreTypeDeclaration): string[] {
41
+ return [
42
+ `type ${declaration.name} {`,
43
+ ...declaration.fields.map((field) => ` ${formatParam(field)}`),
44
+ "}",
45
+ ];
46
+ }
47
+
48
+ function formatFunction(declaration: PointCoreFunctionDeclaration): string[] {
49
+ return [
50
+ `fn ${declaration.name}(${declaration.params.map(formatParam).join(", ")}): ${formatTypeExpression(declaration.returnType)} {`,
51
+ ...indentLines(declaration.body.flatMap((statement) => formatStatementLines(statement))),
52
+ "}",
53
+ ];
54
+ }
55
+
56
+ function formatStatementLines(statement: PointCoreStatement): string[] {
57
+ if (statement.kind === "return") {
58
+ return [statement.value ? `return ${formatExpression(statement.value)}` : "return"];
59
+ }
60
+ if (statement.kind === "value") return [formatValue(statement)];
61
+ if (statement.kind === "assignment") return [`${statement.name} ${statement.operator} ${formatExpression(statement.value)}`];
62
+ if (statement.kind === "if") return formatIf(statement);
63
+ if (statement.kind === "for") {
64
+ return [
65
+ `for ${statement.itemName} in ${formatExpression(statement.iterable)} {`,
66
+ ...indentLines(statement.body.flatMap((child) => formatStatementLines(child))),
67
+ "}",
68
+ ];
69
+ }
70
+ return [formatExpression(statement.value)];
71
+ }
72
+
73
+ function formatIf(statement: Extract<PointCoreStatement, { kind: "if" }>): string[] {
74
+ const lines = [
75
+ `if ${formatExpression(statement.condition)} {`,
76
+ ...indentLines(statement.thenBody.flatMap((child) => formatStatementLines(child))),
77
+ "}",
78
+ ];
79
+ if (statement.elseBody.length > 0) {
80
+ lines.push(
81
+ "else {",
82
+ ...indentLines(statement.elseBody.flatMap((child) => formatStatementLines(child))),
83
+ "}",
84
+ );
85
+ }
86
+ return lines;
87
+ }
88
+
89
+ function formatValue(declaration: PointCoreValueDeclaration): string {
90
+ return `${declaration.mutable ? "var" : "let"} ${declaration.name}: ${formatTypeExpression(declaration.type)} = ${formatExpression(declaration.value)}`;
91
+ }
92
+
93
+ function formatParam(param: PointCoreParameter): string {
94
+ return `${param.name}: ${formatTypeExpression(param.type)}`;
95
+ }
96
+
97
+ function formatExpression(expression: PointCoreExpression): string {
98
+ if (expression.kind === "literal") return JSON.stringify(expression.value);
99
+ if (expression.kind === "identifier") return expression.name;
100
+ if (expression.kind === "list") return `[${expression.items.map(formatExpression).join(", ")}]`;
101
+ if (expression.kind === "record") {
102
+ return `{ ${expression.fields.map((field) => `${field.name}: ${formatExpression(field.value)}`).join(", ")} }`;
103
+ }
104
+ if (expression.kind === "await") return `await ${formatExpression(expression.value)}`;
105
+ if (expression.kind === "property") return `${formatExpression(expression.target)}.${expression.name}`;
106
+ if (expression.kind === "binary") {
107
+ return `${formatExpression(expression.left)} ${expression.operator} ${formatExpression(expression.right)}`;
108
+ }
109
+ return `${expression.callee}(${expression.args.map(formatExpression).join(", ")})`;
110
+ }
111
+
112
+ function formatTypeExpression(type: PointCoreParameter["type"]): string {
113
+ if (type.args.length === 0) return String(type.name);
114
+ if (type.name === "Or") return type.args.map(formatTypeExpression).join(" or ");
115
+ return `${type.name}<${type.args.map(formatTypeExpression).join(", ")}>`;
116
+ }
117
+
118
+ function indentLines(lines: string[]): string[] {
119
+ return lines.map((line) => ` ${line}`);
120
+ }
@@ -1,3 +1,3 @@
1
- export { parsePointCore, PointCoreParserError, mergeSpans } from "./core-text-parser.ts";
2
- export { parsePointSourceLegacy } from "./legacy-lowering.ts";
3
- export { formatPointCore } from "./format-core.ts";
1
+ export { parsePointCore, PointCoreParserError, mergeSpans } from "./core-text-parser.ts";
2
+ export { parsePointSourceLegacy } from "./legacy-lowering.ts";
3
+ export { formatPointCore } from "./format-core.ts";