@thi.ng/shader-ast
Version:
DSL to define shader code in TypeScript and cross-compile to GLSL, JS and other targets
50 lines (49 loc) • 984 B
JavaScript
import { decl, scope } from "./scope.js";
const ifThen = (test, truthy, falsey) => ({
tag: "if",
type: "void",
test,
t: scope(truthy),
f: falsey ? scope(falsey) : void 0
});
const ternary = (test, t, f) => ({
tag: "ternary",
type: t.type,
test,
t,
f
});
function forLoop(...args) {
const [init, test, iter, body] = args.length === 2 ? [void 0, args[0], void 0, args[1]] : args.length === 3 ? [args[0], args[1], void 0, args[2]] : args;
return {
tag: "for",
type: "void",
init: init ? decl(init) : void 0,
test: test(init),
iter: iter ? iter(init) : void 0,
scope: scope(body(init))
};
}
const whileLoop = (test, body) => ({
tag: "while",
type: "void",
test,
scope: scope(body)
});
const __ctrl = (id) => ({
tag: "ctrl",
type: "void",
id
});
const brk = __ctrl("break");
const cont = __ctrl("continue");
const discard = __ctrl("discard");
export {
brk,
cont,
discard,
forLoop,
ifThen,
ternary,
whileLoop
};