xbintsc 0.1.6 → 0.1.9

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 (152) hide show
  1. package/README.md +7 -4
  2. package/dist/src/ast/common.d.ts +33 -0
  3. package/dist/src/ast/common.js +7 -0
  4. package/dist/src/ast/common.js.map +1 -0
  5. package/dist/src/ast/declarations.d.ts +110 -0
  6. package/dist/src/ast/declarations.js +5 -0
  7. package/dist/src/ast/declarations.js.map +1 -0
  8. package/dist/src/ast/expressions.d.ts +219 -0
  9. package/dist/src/ast/expressions.js +6 -0
  10. package/dist/src/ast/expressions.js.map +1 -0
  11. package/dist/src/ast/kinds.d.ts +160 -0
  12. package/dist/src/ast/kinds.js +171 -0
  13. package/dist/src/ast/kinds.js.map +1 -0
  14. package/dist/src/ast/modules.d.ts +61 -0
  15. package/dist/src/ast/modules.js +5 -0
  16. package/dist/src/ast/modules.js.map +1 -0
  17. package/dist/src/ast/nodes.d.ts +11 -839
  18. package/dist/src/ast/nodes.js +11 -237
  19. package/dist/src/ast/nodes.js.map +1 -1
  20. package/dist/src/ast/operators.d.ts +71 -0
  21. package/dist/src/ast/operators.js +77 -0
  22. package/dist/src/ast/operators.js.map +1 -0
  23. package/dist/src/ast/statements.d.ts +120 -0
  24. package/dist/src/ast/statements.js +5 -0
  25. package/dist/src/ast/statements.js.map +1 -0
  26. package/dist/src/ast/types.d.ts +123 -0
  27. package/dist/src/ast/types.js +6 -0
  28. package/dist/src/ast/types.js.map +1 -0
  29. package/dist/src/binder/binder.d.ts +35 -1
  30. package/dist/src/binder/binder.js +138 -0
  31. package/dist/src/binder/binder.js.map +1 -1
  32. package/dist/src/codegen/generator/calls.d.ts +26 -0
  33. package/dist/src/codegen/generator/calls.js +373 -0
  34. package/dist/src/codegen/generator/calls.js.map +1 -0
  35. package/dist/src/codegen/generator/context.d.ts +47 -0
  36. package/dist/src/codegen/generator/context.js +164 -0
  37. package/dist/src/codegen/generator/context.js.map +1 -0
  38. package/dist/src/codegen/generator/expressions.d.ts +25 -0
  39. package/dist/src/codegen/generator/expressions.js +454 -0
  40. package/dist/src/codegen/generator/expressions.js.map +1 -0
  41. package/dist/src/codegen/generator/generator.d.ts +22 -0
  42. package/dist/src/codegen/generator/generator.js +21 -0
  43. package/dist/src/codegen/generator/generator.js.map +1 -0
  44. package/dist/src/codegen/generator/module.d.ts +19 -0
  45. package/dist/src/codegen/generator/module.js +214 -0
  46. package/dist/src/codegen/generator/module.js.map +1 -0
  47. package/dist/src/codegen/generator/state.d.ts +42 -0
  48. package/dist/src/codegen/generator/state.js +125 -0
  49. package/dist/src/codegen/generator/state.js.map +1 -0
  50. package/dist/src/codegen/generator/statements.d.ts +22 -0
  51. package/dist/src/codegen/generator/statements.js +433 -0
  52. package/dist/src/codegen/generator/statements.js.map +1 -0
  53. package/dist/src/codegen/generator/tables.d.ts +23 -0
  54. package/dist/src/codegen/generator/tables.js +186 -0
  55. package/dist/src/codegen/generator/tables.js.map +1 -0
  56. package/dist/src/codegen/llvm.d.ts +4 -13
  57. package/dist/src/codegen/llvm.js +4 -1537
  58. package/dist/src/codegen/llvm.js.map +1 -1
  59. package/dist/src/driver/compiler.js +36 -11
  60. package/dist/src/driver/compiler.js.map +1 -1
  61. package/dist/src/driver/modules.d.ts +24 -0
  62. package/dist/src/driver/modules.js +347 -0
  63. package/dist/src/driver/modules.js.map +1 -0
  64. package/dist/src/lexer/char.d.ts +78 -0
  65. package/dist/src/lexer/char.js +106 -0
  66. package/dist/src/lexer/char.js.map +1 -0
  67. package/dist/src/lexer/scanner.js +1 -98
  68. package/dist/src/lexer/scanner.js.map +1 -1
  69. package/dist/src/parser/context.d.ts +58 -0
  70. package/dist/src/parser/context.js +174 -0
  71. package/dist/src/parser/context.js.map +1 -0
  72. package/dist/src/parser/declarations.d.ts +26 -0
  73. package/dist/src/parser/declarations.js +461 -0
  74. package/dist/src/parser/declarations.js.map +1 -0
  75. package/dist/src/parser/expressions.d.ts +27 -0
  76. package/dist/src/parser/expressions.js +440 -0
  77. package/dist/src/parser/expressions.js.map +1 -0
  78. package/dist/src/parser/helpers.d.ts +7 -0
  79. package/dist/src/parser/helpers.js +36 -0
  80. package/dist/src/parser/helpers.js.map +1 -0
  81. package/dist/src/parser/literals.d.ts +16 -0
  82. package/dist/src/parser/literals.js +193 -0
  83. package/dist/src/parser/literals.js.map +1 -0
  84. package/dist/src/parser/modules.d.ts +12 -0
  85. package/dist/src/parser/modules.js +175 -0
  86. package/dist/src/parser/modules.js.map +1 -0
  87. package/dist/src/parser/operators.d.ts +12 -0
  88. package/dist/src/parser/operators.js +172 -0
  89. package/dist/src/parser/operators.js.map +1 -0
  90. package/dist/src/parser/parser.d.ts +17 -122
  91. package/dist/src/parser/parser.js +18 -2311
  92. package/dist/src/parser/parser.js.map +1 -1
  93. package/dist/src/parser/speculation.d.ts +6 -0
  94. package/dist/src/parser/speculation.js +10 -0
  95. package/dist/src/parser/speculation.js.map +1 -0
  96. package/dist/src/parser/statements.d.ts +28 -0
  97. package/dist/src/parser/statements.js +402 -0
  98. package/dist/src/parser/statements.js.map +1 -0
  99. package/dist/src/parser/types.d.ts +21 -0
  100. package/dist/src/parser/types.js +338 -0
  101. package/dist/src/parser/types.js.map +1 -0
  102. package/dist/tests/codegen/llvm.test.js +1 -1
  103. package/dist/tests/codegen/llvm.test.js.map +1 -1
  104. package/dist/tests/e2e/compile.test.js +86 -0
  105. package/dist/tests/e2e/compile.test.js.map +1 -1
  106. package/package.json +1 -1
  107. package/runtime/rt.h +55 -4
  108. package/runtime/rt_internal.h +169 -0
  109. package/runtime/xt_alloc.c +38 -0
  110. package/runtime/xt_builtins.c +238 -0
  111. package/runtime/xt_containers.c +312 -0
  112. package/runtime/xt_io.c +246 -0
  113. package/runtime/xt_promise.c +292 -0
  114. package/runtime/xt_stdlib.c +556 -0
  115. package/runtime/xt_stdlib2.c +1043 -0
  116. package/runtime/xt_values.c +291 -0
  117. package/scripts/build-runtime.mjs +8 -1
  118. package/src/ast/common.ts +210 -0
  119. package/src/ast/declarations.ts +130 -0
  120. package/src/ast/expressions.ts +275 -0
  121. package/src/ast/kinds.ts +176 -0
  122. package/src/ast/modules.ts +73 -0
  123. package/src/ast/nodes.ts +11 -1101
  124. package/src/ast/operators.ts +76 -0
  125. package/src/ast/statements.ts +148 -0
  126. package/src/ast/types.ts +173 -0
  127. package/src/binder/binder.ts +196 -0
  128. package/src/codegen/generator/calls.ts +433 -0
  129. package/src/codegen/generator/context.ts +190 -0
  130. package/src/codegen/generator/expressions.ts +508 -0
  131. package/src/codegen/generator/generator.ts +35 -0
  132. package/src/codegen/generator/module.ts +244 -0
  133. package/src/codegen/generator/state.ts +169 -0
  134. package/src/codegen/generator/statements.ts +480 -0
  135. package/src/codegen/generator/tables.ts +206 -0
  136. package/src/codegen/llvm.ts +4 -1720
  137. package/src/driver/compiler.ts +45 -12
  138. package/src/driver/modules.ts +377 -0
  139. package/src/lexer/char.ts +116 -0
  140. package/src/lexer/scanner.ts +9 -109
  141. package/src/parser/context.ts +196 -0
  142. package/src/parser/declarations.ts +502 -0
  143. package/src/parser/expressions.ts +468 -0
  144. package/src/parser/helpers.ts +42 -0
  145. package/src/parser/literals.ts +207 -0
  146. package/src/parser/modules.ts +194 -0
  147. package/src/parser/operators.ts +181 -0
  148. package/src/parser/parser.ts +35 -2373
  149. package/src/parser/speculation.ts +9 -0
  150. package/src/parser/statements.ts +457 -0
  151. package/src/parser/types.ts +365 -0
  152. package/runtime/xt_runtime.c +0 -1372
@@ -1,115 +1,15 @@
1
1
  import { DiagnosticBag, DiagnosticCode } from "../diagnostics/diagnostic.js";
2
2
  import type { SourceFile } from "../diagnostics/source.js";
3
3
  import { KEYWORDS, Token, TokenKind } from "./token.js";
4
-
5
- const enum Char {
6
- Null = 0,
7
- Tab = 9,
8
- LineFeed = 10,
9
- VerticalTab = 11,
10
- FormFeed = 12,
11
- CarriageReturn = 13,
12
- Space = 32,
13
- Exclamation = 33,
14
- DoubleQuote = 34,
15
- Hash = 35,
16
- Dollar = 36,
17
- Percent = 37,
18
- Ampersand = 38,
19
- SingleQuote = 39,
20
- OpenParen = 40,
21
- CloseParen = 41,
22
- Asterisk = 42,
23
- Plus = 43,
24
- Comma = 44,
25
- Minus = 45,
26
- Dot = 46,
27
- Slash = 47,
28
- Zero = 48,
29
- Nine = 57,
30
- Colon = 58,
31
- Semicolon = 59,
32
- LessThan = 60,
33
- Equals = 61,
34
- GreaterThan = 62,
35
- Question = 63,
36
- At = 64,
37
- UpperA = 65,
38
- UpperB = 66,
39
- UpperE = 69,
40
- UpperF = 70,
41
- UpperO = 79,
42
- UpperX = 88,
43
- UpperZ = 90,
44
- OpenBracket = 91,
45
- Backslash = 92,
46
- CloseBracket = 93,
47
- Caret = 94,
48
- Underscore = 95,
49
- Backtick = 96,
50
- LowerA = 97,
51
- LowerB = 98,
52
- LowerE = 101,
53
- LowerF = 102,
54
- LowerN = 110,
55
- LowerO = 111,
56
- LowerR = 114,
57
- LowerT = 116,
58
- LowerU = 117,
59
- LowerX = 120,
60
- LowerZ = 122,
61
- OpenBrace = 123,
62
- Bar = 124,
63
- CloseBrace = 125,
64
- Tilde = 126,
65
- NonBreakingSpace = 0xa0,
66
- LineSeparator = 0x2028,
67
- ParagraphSeparator = 0x2029,
68
- ByteOrderMark = 0xfeff,
69
- }
70
-
71
- function isLineBreak(ch: number): boolean {
72
- return ch === Char.LineFeed || ch === Char.CarriageReturn || ch === Char.LineSeparator || ch === Char.ParagraphSeparator;
73
- }
74
-
75
- function isWhiteSpace(ch: number): boolean {
76
- return (
77
- ch === Char.Space ||
78
- ch === Char.Tab ||
79
- ch === Char.VerticalTab ||
80
- ch === Char.FormFeed ||
81
- ch === Char.NonBreakingSpace ||
82
- ch === Char.ByteOrderMark ||
83
- (ch > 0x1680 && WHITESPACE_UNICODE.has(ch))
84
- );
85
- }
86
-
87
- const WHITESPACE_UNICODE = new Set<number>([
88
- 0x1680, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200a, 0x202f, 0x205f,
89
- 0x3000,
90
- ]);
91
-
92
- function isDigit(ch: number): boolean {
93
- return ch >= Char.Zero && ch <= Char.Nine;
94
- }
95
-
96
- function isHexDigit(ch: number): boolean {
97
- return isDigit(ch) || (ch >= Char.LowerA && ch <= Char.LowerF) || (ch >= Char.UpperA && ch <= Char.UpperF);
98
- }
99
-
100
- function isIdentifierStart(ch: number): boolean {
101
- return (
102
- (ch >= Char.LowerA && ch <= Char.LowerZ) ||
103
- (ch >= Char.UpperA && ch <= Char.UpperZ) ||
104
- ch === Char.Dollar ||
105
- ch === Char.Underscore ||
106
- ch > 0x7f
107
- );
108
- }
109
-
110
- function isIdentifierPart(ch: number): boolean {
111
- return isIdentifierStart(ch) || isDigit(ch) || ch === Char.Backslash || ch > 0x7f;
112
- }
4
+ import {
5
+ Char,
6
+ isDigit,
7
+ isHexDigit,
8
+ isIdentifierPart,
9
+ isIdentifierStart,
10
+ isLineBreak,
11
+ isWhiteSpace,
12
+ } from "./char.js";
113
13
 
114
14
  /**
115
15
  * Hand-written scanner for the TypeScript language. It produces a flat token
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Shared state and low-level token plumbing for the parser.
3
+ *
4
+ * The parser's method groups (statements, expressions, types, modules) are
5
+ * defined as separate modules and installed onto `Parser.prototype`. This class
6
+ * holds the state they all operate on plus the token accessors every group
7
+ * needs.
8
+ */
9
+
10
+ import { DiagnosticBag, DiagnosticCode } from "../diagnostics/diagnostic.js";
11
+ import type { SourceFile } from "../diagnostics/source.js";
12
+ import { Scanner } from "../lexer/scanner.js";
13
+ import { Token, TokenKind } from "../lexer/token.js";
14
+ import { SyntaxKind } from "../ast/kinds.js";
15
+ import type { Identifier, StringLiteral } from "../ast/nodes.js";
16
+ import { SpeculationError } from "./speculation.js";
17
+
18
+ export interface ParserState {
19
+ index: number;
20
+ tokens: Token[];
21
+ pos: number;
22
+ consumed: number;
23
+ }
24
+
25
+ export class ParserContext {
26
+ readonly scanner: Scanner;
27
+ readonly diagnostics: DiagnosticBag;
28
+ readonly source: SourceFile;
29
+ tokens: Token[];
30
+ index = 0;
31
+ /** Non-zero while a speculative parse is running; errors abort the attempt. */
32
+ speculating = 0;
33
+ /** Monotonic count of tokens consumed; used to detect lack of progress. */
34
+ consumed = 0;
35
+
36
+ constructor(source: SourceFile, diagnostics: DiagnosticBag) {
37
+ this.source = source;
38
+ this.diagnostics = diagnostics;
39
+ this.scanner = new Scanner(source, diagnostics);
40
+ this.tokens = [this.scanner.nextToken()];
41
+ }
42
+
43
+ // -- token access ---------------------------------------------------------
44
+
45
+ get token(): Token {
46
+ return this.tokens[this.index]!;
47
+ }
48
+
49
+ /**
50
+ * Compare the current token kind without triggering TypeScript's control-flow
51
+ * narrowing of `this.token.kind`, which would otherwise be invalidated by the
52
+ * calls to `nextToken()` that occur between checks.
53
+ */
54
+ at(kind: TokenKind): boolean {
55
+ return this.tokens[this.index]!.kind === kind;
56
+ }
57
+
58
+ atAhead(k: number, kind: TokenKind): boolean {
59
+ return this.lookAhead(k).kind === kind;
60
+ }
61
+
62
+ lookAhead(k: number): Token {
63
+ while (this.tokens.length <= this.index + k) {
64
+ this.tokens.push(this.scanner.nextToken());
65
+ }
66
+ return this.tokens[this.index + k]!;
67
+ }
68
+
69
+ nextToken(): Token {
70
+ this.consumed++;
71
+ const current = this.token;
72
+ if (this.index + 1 < this.tokens.length) {
73
+ this.index++;
74
+ } else {
75
+ this.tokens = [this.scanner.nextToken()];
76
+ this.index = 0;
77
+ }
78
+ return current;
79
+ }
80
+
81
+ parseExpected(kind: TokenKind, message?: string): Token {
82
+ if (this.token.kind === kind) return this.nextToken();
83
+ this.error(
84
+ DiagnosticCode.ExpectedToken,
85
+ message ?? `Expected '${kind}' but found '${this.token.text || this.token.kind}'`,
86
+ this.token,
87
+ );
88
+ return this.token;
89
+ }
90
+
91
+ error(code: DiagnosticCode, message: string, token: Token = this.token): void {
92
+ this.diagnostics.error(code, message, { start: token.start, end: token.end }, this.source.fileName);
93
+ if (this.speculating > 0) throw new SpeculationError();
94
+ }
95
+
96
+ /**
97
+ * Run `fn` speculatively. Any diagnostic raised while it runs (which aborts
98
+ * the attempt via `SpeculationError`) is rolled back along with the scanner
99
+ * position, so the parser can try a different production cleanly.
100
+ */
101
+ tryParse<T>(fn: () => T): T | undefined {
102
+ const mark = this.diagnostics.mark();
103
+ const state = this.saveState();
104
+ this.speculating++;
105
+ let result: T | undefined;
106
+ let ok = false;
107
+ try {
108
+ result = fn();
109
+ ok = true;
110
+ } catch (error) {
111
+ if (!(error instanceof SpeculationError)) throw error;
112
+ } finally {
113
+ this.speculating--;
114
+ }
115
+ if (!ok) {
116
+ this.diagnostics.reset(mark);
117
+ this.restoreState(state);
118
+ return undefined;
119
+ }
120
+ return result;
121
+ }
122
+
123
+ isIdentifierLike(token: Token): boolean {
124
+ if (token.kind === TokenKind.Identifier) return true;
125
+ switch (token.kind) {
126
+ case TokenKind.AsKeyword:
127
+ case TokenKind.SatisfiesKeyword:
128
+ case TokenKind.FromKeyword:
129
+ case TokenKind.TypeKeyword:
130
+ case TokenKind.GetKeyword:
131
+ case TokenKind.SetKeyword:
132
+ case TokenKind.AsyncKeyword:
133
+ return true;
134
+ default:
135
+ return false;
136
+ }
137
+ }
138
+
139
+ parseIdentifier(allowKeywords = false): Identifier {
140
+ const token = this.token;
141
+ if (token.kind === TokenKind.Identifier || (allowKeywords && this.isIdentifierLike(token))) {
142
+ this.nextToken();
143
+ return { kind: SyntaxKind.Identifier, text: token.text, start: token.start, end: token.end };
144
+ }
145
+ this.error(DiagnosticCode.ExpectedIdentifier, `Expected identifier but found '${token.text || token.kind}'`);
146
+ if (token.kind !== TokenKind.EndOfFile) this.nextToken();
147
+ return { kind: SyntaxKind.Identifier, text: token.text || "missing", start: token.start, end: token.end };
148
+ }
149
+
150
+ parseIdentifierName(): Identifier {
151
+ return this.parseIdentifier(/* allowKeywords */ true);
152
+ }
153
+
154
+ parseSemicolon(): void {
155
+ if (this.at(TokenKind.Semicolon)) {
156
+ this.nextToken();
157
+ return;
158
+ }
159
+ if (this.canInsertSemicolon()) return;
160
+ this.error(DiagnosticCode.ExpectedToken, "Expected ';'", this.token);
161
+ }
162
+
163
+ canInsertSemicolon(): boolean {
164
+ return this.at(TokenKind.EndOfFile) || this.at(TokenKind.CloseBrace) || this.token.precededByLineBreak;
165
+ }
166
+
167
+ parseDelimitedList<T>(close: TokenKind, parseElement: () => T): T[] {
168
+ const items: T[] = [];
169
+ while (this.token.kind !== close && !this.at(TokenKind.EndOfFile)) {
170
+ const before = this.consumed;
171
+ items.push(parseElement());
172
+ if (this.at(TokenKind.Comma)) this.nextToken();
173
+ else break;
174
+ if (this.consumed === before && this.token.kind !== close) this.nextToken();
175
+ }
176
+ this.parseExpected(close);
177
+ return items;
178
+ }
179
+
180
+ stringLiteralFromToken(token: Token): StringLiteral {
181
+ return { kind: SyntaxKind.StringLiteral, text: token.text, raw: token.text, value: String(token.value ?? ""), start: token.start, end: token.end };
182
+ }
183
+
184
+ // -- state save / restore (for speculative parsing) -----------------------
185
+
186
+ saveState(): ParserState {
187
+ return { index: this.index, tokens: this.tokens.slice(), pos: this.scanner.position, consumed: this.consumed };
188
+ }
189
+
190
+ restoreState(state: ParserState): void {
191
+ this.index = state.index;
192
+ this.tokens = state.tokens;
193
+ this.scanner.seek(state.pos);
194
+ this.consumed = state.consumed;
195
+ }
196
+ }