xbintsc 0.1.3 → 0.1.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.
- package/README.md +14 -13
- package/dist/src/ast/common.d.ts +33 -0
- package/dist/src/ast/common.js +7 -0
- package/dist/src/ast/common.js.map +1 -0
- package/dist/src/ast/declarations.d.ts +110 -0
- package/dist/src/ast/declarations.js +5 -0
- package/dist/src/ast/declarations.js.map +1 -0
- package/dist/src/ast/expressions.d.ts +219 -0
- package/dist/src/ast/expressions.js +6 -0
- package/dist/src/ast/expressions.js.map +1 -0
- package/dist/src/ast/kinds.d.ts +160 -0
- package/dist/src/ast/kinds.js +171 -0
- package/dist/src/ast/kinds.js.map +1 -0
- package/dist/src/ast/modules.d.ts +61 -0
- package/dist/src/ast/modules.js +5 -0
- package/dist/src/ast/modules.js.map +1 -0
- package/dist/src/ast/nodes.d.ts +11 -839
- package/dist/src/ast/nodes.js +11 -237
- package/dist/src/ast/nodes.js.map +1 -1
- package/dist/src/ast/operators.d.ts +71 -0
- package/dist/src/ast/operators.js +77 -0
- package/dist/src/ast/operators.js.map +1 -0
- package/dist/src/ast/statements.d.ts +120 -0
- package/dist/src/ast/statements.js +5 -0
- package/dist/src/ast/statements.js.map +1 -0
- package/dist/src/ast/types.d.ts +123 -0
- package/dist/src/ast/types.js +6 -0
- package/dist/src/ast/types.js.map +1 -0
- package/dist/src/codegen/generator/calls.d.ts +22 -0
- package/dist/src/codegen/generator/calls.js +301 -0
- package/dist/src/codegen/generator/calls.js.map +1 -0
- package/dist/src/codegen/generator/context.d.ts +45 -0
- package/dist/src/codegen/generator/context.js +128 -0
- package/dist/src/codegen/generator/context.js.map +1 -0
- package/dist/src/codegen/generator/expressions.d.ts +22 -0
- package/dist/src/codegen/generator/expressions.js +383 -0
- package/dist/src/codegen/generator/expressions.js.map +1 -0
- package/dist/src/codegen/generator/generator.d.ts +22 -0
- package/dist/src/codegen/generator/generator.js +21 -0
- package/dist/src/codegen/generator/generator.js.map +1 -0
- package/dist/src/codegen/generator/module.d.ts +15 -0
- package/dist/src/codegen/generator/module.js +114 -0
- package/dist/src/codegen/generator/module.js.map +1 -0
- package/dist/src/codegen/generator/state.d.ts +40 -0
- package/dist/src/codegen/generator/state.js +84 -0
- package/dist/src/codegen/generator/state.js.map +1 -0
- package/dist/src/codegen/generator/statements.d.ts +22 -0
- package/dist/src/codegen/generator/statements.js +425 -0
- package/dist/src/codegen/generator/statements.js.map +1 -0
- package/dist/src/codegen/generator/tables.d.ts +19 -0
- package/dist/src/codegen/generator/tables.js +153 -0
- package/dist/src/codegen/generator/tables.js.map +1 -0
- package/dist/src/codegen/llvm.d.ts +4 -13
- package/dist/src/codegen/llvm.js +4 -1159
- package/dist/src/codegen/llvm.js.map +1 -1
- package/dist/src/driver/compiler.js +13 -4
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/lexer/char.d.ts +78 -0
- package/dist/src/lexer/char.js +106 -0
- package/dist/src/lexer/char.js.map +1 -0
- package/dist/src/lexer/scanner.js +1 -98
- package/dist/src/lexer/scanner.js.map +1 -1
- package/dist/src/parser/context.d.ts +58 -0
- package/dist/src/parser/context.js +174 -0
- package/dist/src/parser/context.js.map +1 -0
- package/dist/src/parser/declarations.d.ts +26 -0
- package/dist/src/parser/declarations.js +461 -0
- package/dist/src/parser/declarations.js.map +1 -0
- package/dist/src/parser/expressions.d.ts +27 -0
- package/dist/src/parser/expressions.js +437 -0
- package/dist/src/parser/expressions.js.map +1 -0
- package/dist/src/parser/helpers.d.ts +7 -0
- package/dist/src/parser/helpers.js +36 -0
- package/dist/src/parser/helpers.js.map +1 -0
- package/dist/src/parser/literals.d.ts +16 -0
- package/dist/src/parser/literals.js +193 -0
- package/dist/src/parser/literals.js.map +1 -0
- package/dist/src/parser/modules.d.ts +12 -0
- package/dist/src/parser/modules.js +175 -0
- package/dist/src/parser/modules.js.map +1 -0
- package/dist/src/parser/operators.d.ts +12 -0
- package/dist/src/parser/operators.js +172 -0
- package/dist/src/parser/operators.js.map +1 -0
- package/dist/src/parser/parser.d.ts +17 -122
- package/dist/src/parser/parser.js +18 -2307
- package/dist/src/parser/parser.js.map +1 -1
- package/dist/src/parser/speculation.d.ts +6 -0
- package/dist/src/parser/speculation.js +10 -0
- package/dist/src/parser/speculation.js.map +1 -0
- package/dist/src/parser/statements.d.ts +28 -0
- package/dist/src/parser/statements.js +402 -0
- package/dist/src/parser/statements.js.map +1 -0
- package/dist/src/parser/types.d.ts +21 -0
- package/dist/src/parser/types.js +338 -0
- package/dist/src/parser/types.js.map +1 -0
- package/dist/tests/e2e/compile.test.js +122 -2
- package/dist/tests/e2e/compile.test.js.map +1 -1
- package/dist/tests/parser/parser.test.js +9 -1
- package/dist/tests/parser/parser.test.js.map +1 -1
- package/package.json +1 -1
- package/runtime/rt.h +32 -0
- package/runtime/rt_internal.h +106 -0
- package/runtime/xt_alloc.c +38 -0
- package/runtime/xt_builtins.c +192 -0
- package/runtime/xt_containers.c +225 -0
- package/runtime/xt_io.c +133 -0
- package/runtime/xt_stdlib.c +502 -0
- package/runtime/xt_values.c +291 -0
- package/scripts/build-runtime.mjs +8 -1
- package/src/ast/common.ts +210 -0
- package/src/ast/declarations.ts +130 -0
- package/src/ast/expressions.ts +275 -0
- package/src/ast/kinds.ts +176 -0
- package/src/ast/modules.ts +73 -0
- package/src/ast/nodes.ts +11 -1101
- package/src/ast/operators.ts +76 -0
- package/src/ast/statements.ts +148 -0
- package/src/ast/types.ts +173 -0
- package/src/codegen/generator/calls.ts +355 -0
- package/src/codegen/generator/context.ts +156 -0
- package/src/codegen/generator/expressions.ts +431 -0
- package/src/codegen/generator/generator.ts +35 -0
- package/src/codegen/generator/module.ts +139 -0
- package/src/codegen/generator/state.ts +126 -0
- package/src/codegen/generator/statements.ts +474 -0
- package/src/codegen/generator/tables.ts +171 -0
- package/src/codegen/llvm.ts +4 -1305
- package/src/driver/compiler.ts +17 -5
- package/src/lexer/char.ts +116 -0
- package/src/lexer/scanner.ts +9 -109
- package/src/parser/context.ts +196 -0
- package/src/parser/declarations.ts +502 -0
- package/src/parser/expressions.ts +465 -0
- package/src/parser/helpers.ts +42 -0
- package/src/parser/literals.ts +207 -0
- package/src/parser/modules.ts +194 -0
- package/src/parser/operators.ts +181 -0
- package/src/parser/parser.ts +35 -2369
- package/src/parser/speculation.ts +9 -0
- package/src/parser/statements.ts +457 -0
- package/src/parser/types.ts +365 -0
- package/runtime/xt_runtime.c +0 -682
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static tables and small pure helpers used by the LLVM generator.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
AssignmentOperator,
|
|
7
|
+
BinaryOperator,
|
|
8
|
+
SyntaxKind,
|
|
9
|
+
type Node,
|
|
10
|
+
} from "../../ast/nodes.js";
|
|
11
|
+
|
|
12
|
+
export const BINARY_RUNTIME: Record<string, string | undefined> = {
|
|
13
|
+
[BinaryOperator.Add]: "xt_add",
|
|
14
|
+
[BinaryOperator.Subtract]: "xt_sub",
|
|
15
|
+
[BinaryOperator.Multiply]: "xt_mul",
|
|
16
|
+
[BinaryOperator.Divide]: "xt_div",
|
|
17
|
+
[BinaryOperator.Remainder]: "xt_mod",
|
|
18
|
+
[BinaryOperator.Exponent]: "xt_pow",
|
|
19
|
+
[BinaryOperator.LessThan]: "xt_lt",
|
|
20
|
+
[BinaryOperator.LessThanEquals]: "xt_le",
|
|
21
|
+
[BinaryOperator.GreaterThan]: "xt_gt",
|
|
22
|
+
[BinaryOperator.GreaterThanEquals]: "xt_ge",
|
|
23
|
+
[BinaryOperator.EqualsEquals]: "xt_eq",
|
|
24
|
+
[BinaryOperator.ExclamationEquals]: "xt_ne",
|
|
25
|
+
[BinaryOperator.EqualsEqualsEquals]: "xt_seq",
|
|
26
|
+
[BinaryOperator.ExclamationEqualsEquals]: "xt_sne",
|
|
27
|
+
[BinaryOperator.Ampersand]: "xt_bit_and",
|
|
28
|
+
[BinaryOperator.Bar]: "xt_bit_or",
|
|
29
|
+
[BinaryOperator.Caret]: "xt_bit_xor",
|
|
30
|
+
[BinaryOperator.LessThanLessThan]: "xt_shl",
|
|
31
|
+
[BinaryOperator.GreaterThanGreaterThan]: "xt_shr",
|
|
32
|
+
[BinaryOperator.GreaterThanGreaterThanGreaterThan]: "xt_ushr",
|
|
33
|
+
[BinaryOperator.In]: "xt_in",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const CONSOLE_METHODS: Record<string, string> = {
|
|
37
|
+
log: "xt_console_log",
|
|
38
|
+
info: "xt_console_info",
|
|
39
|
+
warn: "xt_console_warn",
|
|
40
|
+
error: "xt_console_error",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const MATH_FUNCTIONS = new Set<string>([
|
|
44
|
+
"abs", "floor", "ceil", "round", "trunc", "sqrt", "cbrt", "pow", "exp", "log", "log2", "log10",
|
|
45
|
+
"sin", "cos", "tan", "asin", "acos", "atan", "atan2", "hypot", "sign", "random", "min", "max",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
export const MATH_CONSTANTS: Record<string, number> = {
|
|
49
|
+
PI: Math.PI,
|
|
50
|
+
E: Math.E,
|
|
51
|
+
LN2: Math.LN2,
|
|
52
|
+
LN10: Math.LN10,
|
|
53
|
+
LOG2E: Math.LOG2E,
|
|
54
|
+
LOG10E: Math.LOG10E,
|
|
55
|
+
SQRT2: Math.SQRT2,
|
|
56
|
+
SQRT1_2: Math.SQRT1_2,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const GLOBAL_FUNCTIONS: Record<string, string> = {
|
|
60
|
+
parseInt: "xt_parse_int",
|
|
61
|
+
parseFloat: "xt_parse_float",
|
|
62
|
+
isNaN: "xt_is_nan",
|
|
63
|
+
isFinite: "xt_is_finite",
|
|
64
|
+
Number: "xt_number_ctor",
|
|
65
|
+
String: "xt_string_ctor",
|
|
66
|
+
Boolean: "xt_boolean_ctor",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const BUILTIN_METHODS = new Set<string>([
|
|
70
|
+
"push", "pop", "shift", "unshift", "join", "slice", "indexOf", "includes", "map", "forEach", "filter",
|
|
71
|
+
"reduce", "concat", "reverse", "charAt", "charCodeAt", "substring", "substr", "split", "toUpperCase",
|
|
72
|
+
"toLowerCase", "trim", "replace", "repeat", "startsWith", "endsWith",
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
export const ASSIGNMENT_OPERATORS = new Set<string>([
|
|
76
|
+
"=",
|
|
77
|
+
"+=",
|
|
78
|
+
"-=",
|
|
79
|
+
"*=",
|
|
80
|
+
"/=",
|
|
81
|
+
"%=",
|
|
82
|
+
"**=",
|
|
83
|
+
"<<=",
|
|
84
|
+
">>=",
|
|
85
|
+
">>>=",
|
|
86
|
+
"&=",
|
|
87
|
+
"|=",
|
|
88
|
+
"^=",
|
|
89
|
+
"&&=",
|
|
90
|
+
"||=",
|
|
91
|
+
"??=",
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
export function isAssignmentOperator(operator: string): boolean {
|
|
95
|
+
return ASSIGNMENT_OPERATORS.has(operator);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function compoundToBinary(operator: AssignmentOperator): BinaryOperator {
|
|
99
|
+
const text = operator.slice(0, -1);
|
|
100
|
+
return text as BinaryOperator;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function propertyNameText(name: Node): string {
|
|
104
|
+
switch (name.kind) {
|
|
105
|
+
case SyntaxKind.Identifier:
|
|
106
|
+
case SyntaxKind.PrivateIdentifier:
|
|
107
|
+
return (name as unknown as { text: string }).text;
|
|
108
|
+
case SyntaxKind.StringLiteral:
|
|
109
|
+
return (name as unknown as { value: string }).value;
|
|
110
|
+
case SyntaxKind.NumericLiteral:
|
|
111
|
+
return String((name as unknown as { value: number }).value);
|
|
112
|
+
default:
|
|
113
|
+
return "";
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Human readable syntax kind for diagnostics (const enums have no reverse map). */
|
|
118
|
+
export function kindName(kind: number): string {
|
|
119
|
+
const entry = KIND_NAMES.get(kind);
|
|
120
|
+
return entry ?? String(kind);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const KIND_NAMES = new Map<number, string>([
|
|
124
|
+
[SyntaxKind.CallExpression, "call expression"],
|
|
125
|
+
[SyntaxKind.NewExpression, "new expression"],
|
|
126
|
+
[SyntaxKind.ClassDeclaration, "class declaration"],
|
|
127
|
+
[SyntaxKind.ClassExpression, "class expression"],
|
|
128
|
+
[SyntaxKind.EnumDeclaration, "enum declaration"],
|
|
129
|
+
[SyntaxKind.SwitchStatement, "switch statement"],
|
|
130
|
+
[SyntaxKind.TryStatement, "try statement"],
|
|
131
|
+
[SyntaxKind.SpreadElement, "spread element"],
|
|
132
|
+
[SyntaxKind.ThisKeyword, "this expression"],
|
|
133
|
+
[SyntaxKind.RegularExpressionLiteral, "regular expression"],
|
|
134
|
+
[SyntaxKind.TaggedTemplateExpression, "tagged template"],
|
|
135
|
+
[SyntaxKind.DeleteExpression, "delete expression"],
|
|
136
|
+
[SyntaxKind.AwaitExpression, "await expression"],
|
|
137
|
+
[SyntaxKind.YieldExpression, "yield expression"],
|
|
138
|
+
[SyntaxKind.SwitchStatement, "switch statement"],
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
export function utf8Bytes(text: string): number[] {
|
|
142
|
+
const bytes: number[] = [];
|
|
143
|
+
for (const character of text) {
|
|
144
|
+
const code = character.codePointAt(0)!;
|
|
145
|
+
if (code < 0x80) bytes.push(code);
|
|
146
|
+
else if (code < 0x800) {
|
|
147
|
+
bytes.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
|
|
148
|
+
} else if (code < 0x10000) {
|
|
149
|
+
bytes.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
|
|
150
|
+
} else {
|
|
151
|
+
bytes.push(
|
|
152
|
+
0xf0 | (code >> 18),
|
|
153
|
+
0x80 | ((code >> 12) & 0x3f),
|
|
154
|
+
0x80 | ((code >> 6) & 0x3f),
|
|
155
|
+
0x80 | (code & 0x3f),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return bytes;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function escapeBytes(bytes: readonly number[]): string {
|
|
163
|
+
return bytes
|
|
164
|
+
.map((byte) => {
|
|
165
|
+
if (byte === 0x22) return '\\22';
|
|
166
|
+
if (byte === 0x5c) return '\\5C';
|
|
167
|
+
if (byte >= 0x20 && byte < 0x7f) return String.fromCharCode(byte);
|
|
168
|
+
return `\\${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
169
|
+
})
|
|
170
|
+
.join("");
|
|
171
|
+
}
|