xbintsc 0.1.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.
Files changed (124) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +191 -0
  3. package/bin/xbintsc.js +26 -0
  4. package/dist/src/ast/factory.d.ts +11 -0
  5. package/dist/src/ast/factory.js +36 -0
  6. package/dist/src/ast/factory.js.map +1 -0
  7. package/dist/src/ast/nodes.d.ts +848 -0
  8. package/dist/src/ast/nodes.js +247 -0
  9. package/dist/src/ast/nodes.js.map +1 -0
  10. package/dist/src/ast/visitor.d.ts +18 -0
  11. package/dist/src/ast/visitor.js +52 -0
  12. package/dist/src/ast/visitor.js.map +1 -0
  13. package/dist/src/binder/binder.d.ts +82 -0
  14. package/dist/src/binder/binder.js +443 -0
  15. package/dist/src/binder/binder.js.map +1 -0
  16. package/dist/src/cli/main.d.ts +16 -0
  17. package/dist/src/cli/main.js +191 -0
  18. package/dist/src/cli/main.js.map +1 -0
  19. package/dist/src/codegen/llvm.d.ts +31 -0
  20. package/dist/src/codegen/llvm.js +1178 -0
  21. package/dist/src/codegen/llvm.js.map +1 -0
  22. package/dist/src/codegen/values.d.ts +23 -0
  23. package/dist/src/codegen/values.js +32 -0
  24. package/dist/src/codegen/values.js.map +1 -0
  25. package/dist/src/diagnostics/diagnostic.d.ts +69 -0
  26. package/dist/src/diagnostics/diagnostic.js +104 -0
  27. package/dist/src/diagnostics/diagnostic.js.map +1 -0
  28. package/dist/src/diagnostics/source.d.ts +43 -0
  29. package/dist/src/diagnostics/source.js +73 -0
  30. package/dist/src/diagnostics/source.js.map +1 -0
  31. package/dist/src/driver/cache.d.ts +37 -0
  32. package/dist/src/driver/cache.js +82 -0
  33. package/dist/src/driver/cache.js.map +1 -0
  34. package/dist/src/driver/compiler.d.ts +41 -0
  35. package/dist/src/driver/compiler.js +128 -0
  36. package/dist/src/driver/compiler.js.map +1 -0
  37. package/dist/src/driver/index.d.ts +4 -0
  38. package/dist/src/driver/index.js +5 -0
  39. package/dist/src/driver/index.js.map +1 -0
  40. package/dist/src/driver/paths.d.ts +4 -0
  41. package/dist/src/driver/paths.js +22 -0
  42. package/dist/src/driver/paths.js.map +1 -0
  43. package/dist/src/driver/toolchain.d.ts +43 -0
  44. package/dist/src/driver/toolchain.js +76 -0
  45. package/dist/src/driver/toolchain.js.map +1 -0
  46. package/dist/src/extensions/node.d.ts +9 -0
  47. package/dist/src/extensions/node.js +20 -0
  48. package/dist/src/extensions/node.js.map +1 -0
  49. package/dist/src/extensions/registry.d.ts +45 -0
  50. package/dist/src/extensions/registry.js +76 -0
  51. package/dist/src/extensions/registry.js.map +1 -0
  52. package/dist/src/index.d.ts +18 -0
  53. package/dist/src/index.js +19 -0
  54. package/dist/src/index.js.map +1 -0
  55. package/dist/src/lexer/scanner.d.ts +48 -0
  56. package/dist/src/lexer/scanner.js +650 -0
  57. package/dist/src/lexer/scanner.js.map +1 -0
  58. package/dist/src/lexer/token.d.ts +170 -0
  59. package/dist/src/lexer/token.js +250 -0
  60. package/dist/src/lexer/token.js.map +1 -0
  61. package/dist/src/parser/parser.d.ts +127 -0
  62. package/dist/src/parser/parser.js +2334 -0
  63. package/dist/src/parser/parser.js.map +1 -0
  64. package/dist/tests/binder/binder.test.d.ts +1 -0
  65. package/dist/tests/binder/binder.test.js +89 -0
  66. package/dist/tests/binder/binder.test.js.map +1 -0
  67. package/dist/tests/cli/main.test.d.ts +1 -0
  68. package/dist/tests/cli/main.test.js +68 -0
  69. package/dist/tests/cli/main.test.js.map +1 -0
  70. package/dist/tests/cli/process.test.d.ts +9 -0
  71. package/dist/tests/cli/process.test.js +57 -0
  72. package/dist/tests/cli/process.test.js.map +1 -0
  73. package/dist/tests/codegen/llvm.test.d.ts +1 -0
  74. package/dist/tests/codegen/llvm.test.js +93 -0
  75. package/dist/tests/codegen/llvm.test.js.map +1 -0
  76. package/dist/tests/driver/cache.test.d.ts +1 -0
  77. package/dist/tests/driver/cache.test.js +66 -0
  78. package/dist/tests/driver/cache.test.js.map +1 -0
  79. package/dist/tests/driver/compiler.test.d.ts +1 -0
  80. package/dist/tests/driver/compiler.test.js +80 -0
  81. package/dist/tests/driver/compiler.test.js.map +1 -0
  82. package/dist/tests/driver/toolchain.test.d.ts +1 -0
  83. package/dist/tests/driver/toolchain.test.js +60 -0
  84. package/dist/tests/driver/toolchain.test.js.map +1 -0
  85. package/dist/tests/e2e/compile.test.d.ts +5 -0
  86. package/dist/tests/e2e/compile.test.js +93 -0
  87. package/dist/tests/e2e/compile.test.js.map +1 -0
  88. package/dist/tests/extensions/registry.test.d.ts +1 -0
  89. package/dist/tests/extensions/registry.test.js +47 -0
  90. package/dist/tests/extensions/registry.test.js.map +1 -0
  91. package/dist/tests/helpers.d.ts +23 -0
  92. package/dist/tests/helpers.js +41 -0
  93. package/dist/tests/helpers.js.map +1 -0
  94. package/dist/tests/lexer/scanner.test.d.ts +1 -0
  95. package/dist/tests/lexer/scanner.test.js +95 -0
  96. package/dist/tests/lexer/scanner.test.js.map +1 -0
  97. package/dist/tests/parser/parser.test.d.ts +1 -0
  98. package/dist/tests/parser/parser.test.js +137 -0
  99. package/dist/tests/parser/parser.test.js.map +1 -0
  100. package/package.json +49 -0
  101. package/runtime/ext_node.c +42 -0
  102. package/runtime/rt.h +177 -0
  103. package/runtime/xt_runtime.c +682 -0
  104. package/scripts/build-runtime.mjs +34 -0
  105. package/src/ast/factory.ts +37 -0
  106. package/src/ast/nodes.ts +1111 -0
  107. package/src/ast/visitor.ts +60 -0
  108. package/src/binder/binder.ts +549 -0
  109. package/src/cli/main.ts +210 -0
  110. package/src/codegen/llvm.ts +1324 -0
  111. package/src/codegen/values.ts +36 -0
  112. package/src/diagnostics/diagnostic.ts +132 -0
  113. package/src/diagnostics/source.ts +97 -0
  114. package/src/driver/cache.ts +96 -0
  115. package/src/driver/compiler.ts +197 -0
  116. package/src/driver/index.ts +4 -0
  117. package/src/driver/paths.ts +24 -0
  118. package/src/driver/toolchain.ts +103 -0
  119. package/src/extensions/node.ts +23 -0
  120. package/src/extensions/registry.ts +101 -0
  121. package/src/index.ts +33 -0
  122. package/src/lexer/scanner.ts +650 -0
  123. package/src/lexer/token.ts +266 -0
  124. package/src/parser/parser.ts +2396 -0
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Node.js compatibility extension.
3
+ *
4
+ * Registering it links `runtime/ext_node.c` and exposes the builtins it
5
+ * implements, so `readFileSync("...")` in TypeScript resolves to the C
6
+ * implementation without the core compiler knowing anything about Node.
7
+ */
8
+
9
+ import { fileURLToPath } from "node:url";
10
+ import { dirname, resolve } from "node:path";
11
+ import type { Extension } from "./registry.js";
12
+
13
+ const here = dirname(fileURLToPath(import.meta.url));
14
+
15
+ export const nodeExtension: Extension = {
16
+ name: "node",
17
+ description: "Node.js host APIs (readFileSync, ...)",
18
+ runtimeSources: () => [resolve(here, "../../runtime/ext_node.c")],
19
+ builtins: () => ({
20
+ readFileSync: { symbol: "xt_node_read_text_file" },
21
+ readTextFile: { symbol: "xt_node_read_text_file" },
22
+ }),
23
+ };
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Pluggable compile modules ("extensions").
3
+ *
4
+ * An extension teaches xbintsc about a host platform: additional C runtime
5
+ * sources to link, libraries to pass to the linker, and global functions that
6
+ * resolve to runtime symbols. Node's `fs`, Bun's `Bun.file`, ... all fit this
7
+ * shape, so the core compiler never has to grow platform specific branches.
8
+ *
9
+ * The default registry ships an empty baseline; callers register the
10
+ * extensions they need before invoking the driver.
11
+ */
12
+
13
+ export interface BuiltinFunction {
14
+ /** Exported C symbol, called as `xt_value symbol(int32_t argc, xt_value *argv)`. */
15
+ readonly symbol: string;
16
+ readonly returnVoid?: boolean;
17
+ }
18
+
19
+ export interface Extension {
20
+ readonly name: string;
21
+ /** Description shown by `xbintsc ext list`. */
22
+ readonly description?: string;
23
+ /** C/asm sources compiled and linked alongside the generated module. */
24
+ runtimeSources?(): readonly string[];
25
+ /** Extra linker flags (e.g. `["-lm"]`, `["-framework", "CoreFoundation"]`). */
26
+ linkerFlags?(): readonly string[];
27
+ /** Global identifiers that resolve to runtime symbols when called. */
28
+ builtins?(): Readonly<Record<string, BuiltinFunction>>;
29
+ }
30
+
31
+ export class ExtensionRegistry {
32
+ private readonly extensions = new Map<string, Extension>();
33
+
34
+ register(extension: Extension): this {
35
+ if (this.extensions.has(extension.name)) {
36
+ throw new Error(`Extension '${extension.name}' is already registered`);
37
+ }
38
+ this.extensions.set(extension.name, extension);
39
+ return this;
40
+ }
41
+
42
+ unregister(name: string): boolean {
43
+ return this.extensions.delete(name);
44
+ }
45
+
46
+ get(name: string): Extension | undefined {
47
+ return this.extensions.get(name);
48
+ }
49
+
50
+ has(name: string): boolean {
51
+ return this.extensions.has(name);
52
+ }
53
+
54
+ all(): readonly Extension[] {
55
+ return [...this.extensions.values()];
56
+ }
57
+
58
+ /** Flatten every registered builtin into one lookup table. */
59
+ builtins(): Readonly<Record<string, BuiltinFunction>> {
60
+ const merged: Record<string, BuiltinFunction> = {};
61
+ for (const extension of this.extensions.values()) {
62
+ const builtins = extension.builtins?.();
63
+ if (builtins) Object.assign(merged, builtins);
64
+ }
65
+ return merged;
66
+ }
67
+
68
+ runtimeSources(): readonly string[] {
69
+ const sources: string[] = [];
70
+ for (const extension of this.extensions.values()) {
71
+ const extra = extension.runtimeSources?.();
72
+ if (extra) sources.push(...extra);
73
+ }
74
+ return sources;
75
+ }
76
+
77
+ linkerFlags(): readonly string[] {
78
+ const flags: string[] = [];
79
+ for (const extension of this.extensions.values()) {
80
+ const extra = extension.linkerFlags?.();
81
+ if (extra) flags.push(...extra);
82
+ }
83
+ return flags;
84
+ }
85
+ }
86
+
87
+ /**
88
+ * A built-in extension that exposes the runtime's own console/IO helpers. It is
89
+ * always present so `console.log` works even without a platform extension.
90
+ */
91
+ export const coreExtension: Extension = {
92
+ name: "core",
93
+ description: "Core runtime helpers (console, print)",
94
+ builtins: () => ({
95
+ print: { symbol: "xt_println" },
96
+ }),
97
+ };
98
+
99
+ export function createDefaultRegistry(): ExtensionRegistry {
100
+ return new ExtensionRegistry().register(coreExtension);
101
+ }
package/src/index.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * xbintsc public API surface.
3
+ *
4
+ * Re-exports the compiler pipeline so the CLI, tests and embedding tools can
5
+ * compose stages (lex, parse, bind, generate) without depending on internals.
6
+ */
7
+
8
+ export { SourceFile, hashText, combineHashes } from "./diagnostics/source.js";
9
+ export {
10
+ DiagnosticBag,
11
+ DiagnosticCode,
12
+ DiagnosticError,
13
+ formatDiagnostic,
14
+ formatDiagnostics,
15
+ type Diagnostic,
16
+ } from "./diagnostics/diagnostic.js";
17
+ export { Scanner } from "./lexer/scanner.js";
18
+ export { TokenKind, KEYWORDS, type Token } from "./lexer/token.js";
19
+ export * from "./ast/nodes.js";
20
+ export { Parser } from "./parser/parser.js";
21
+ export { bind, SymbolKind, ScopeKind, type BindResult, type FunctionInfo, type SymbolInfo } from "./binder/binder.js";
22
+ export { generate, type CodegenResult, type CodegenOptions } from "./codegen/llvm.js";
23
+ export {
24
+ XT_UNDEFINED,
25
+ XT_NULL,
26
+ XT_TRUE,
27
+ XT_FALSE,
28
+ numberLiteral,
29
+ booleanLiteral,
30
+ } from "./codegen/values.js";
31
+ export { ExtensionRegistry, createDefaultRegistry, coreExtension, type Extension } from "./extensions/registry.js";
32
+ export { nodeExtension } from "./extensions/node.js";
33
+ export * from "./driver/index.js";