mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-23 06:10:02 +08:00
- Remove C emission, debugging options, and automatic backend fallback. - Complete LLVM lowering and native bootstrap coverage across supported targets. - Link executables and libraries with precompiled C runtime packs, including iOS and Android.
62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
// Mapping a recursive variant before the whole union preserves separate
|
|
// layout ids for bin and logical. Later inferred literals can share either.
|
|
type Expr =
|
|
| { kind: "number"; value: number }
|
|
| { kind: "bin"; op: "+" | "==="; left: Expr; right: Expr }
|
|
| { kind: "logical"; op: "&&" | "||"; left: Expr; right: Expr }
|
|
| { kind: "nullish"; left: Expr; right: Expr }
|
|
| { kind: "ternary"; cond: Expr; then: Expr; else_: Expr };
|
|
|
|
function binary(value: Expr & { kind: "bin" }): Expr { return value; }
|
|
|
|
function inferred(left: Expr, right: Expr): Expr {
|
|
const value = { kind: "bin" as const, op: "+" as const, left, right };
|
|
return value;
|
|
}
|
|
|
|
function callback(left: Expr, right: Expr): () => Expr {
|
|
return () => ({ kind: "bin" as const, op: "+" as const, left, right });
|
|
}
|
|
|
|
function build(left: Expr, right: Expr, conditional: boolean): Expr {
|
|
const missing = conditional ? {
|
|
kind: "ternary" as const,
|
|
cond: { kind: "bin" as const, op: "===" as const, left, right },
|
|
then: left,
|
|
else_: right,
|
|
} : left;
|
|
return missing;
|
|
}
|
|
|
|
function inspect(root: Expr): void {
|
|
const pending = [root];
|
|
while (pending.length !== 0) {
|
|
const e = pending.pop()!;
|
|
if (e.kind === "logical" || e.kind === "nullish") {
|
|
console.log(e.kind);
|
|
pending.push(e.right, e.left);
|
|
} else if (e.kind === "ternary") {
|
|
pending.push(e.else_, e.then, e.cond);
|
|
} else {
|
|
console.log(e.kind);
|
|
}
|
|
}
|
|
}
|
|
const left: Expr = { kind: "number", value: 1 };
|
|
const right: Expr = { kind: "number", value: 2 };
|
|
console.log(binary({ kind: "bin", op: "+", left, right }).kind);
|
|
inspect(build(left, right, true));
|
|
inspect(build(left, right, false));
|
|
inspect(inferred(left, right));
|
|
inspect(callback(left, right)());
|
|
inspect({ kind: "logical", op: "&&", left, right });
|
|
inspect({ kind: "nullish", left, right });
|
|
|
|
// Native compilation of array search synthesizes a branching expression
|
|
// with a nested binary comparison, exercising this conversion in the compiler.
|
|
function packageSegment(path: string): number {
|
|
return path.split("/").lastIndexOf("node_modules");
|
|
}
|
|
console.log(packageSegment("/workspace/node_modules/package"));
|
|
console.log(packageSegment("/workspace/src"));
|