wgsl-plus
Version:
A WGSL preprocessor, prettifier, minifier, obfuscator, and compiler with C-style macros, conditional compilation, file linking, and multi-format output for WebGPU shaders.
76 lines (75 loc) • 3.42 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = collectDeclaredIdentifiersAndEntryPoints;
const next_name_1 = __importDefault(require("./next-name"));
function collectDeclaredIdentifiersAndEntryPoints(tokens) {
const identifierMap = new Map(); // For top-level identifiers
const bindingMap = new Map();
const entryPointNames = new Set();
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (token.type === "directive") {
const match = token.value.match(/^#(\w+)\s+"([^"]+)"/);
if (match) {
const directiveType = match[1];
const name = match[2];
if (directiveType === "binding") {
if (!identifierMap.has(name)) {
const newName = (0, next_name_1.default)();
identifierMap.set(name, newName);
}
bindingMap.set(name, identifierMap.get(name));
}
else if (directiveType === "entrypoint") {
entryPointNames.add(name);
}
}
}
else if (token.type === "keyword") {
if (token.value === "fn" && i + 1 < tokens.length && tokens[i + 1].type === "identifier") {
const funcName = tokens[i + 1].value;
if (!(entryPointNames.has(funcName) || (entryPointNames.size === 0 && funcName === "main"))) {
if (!identifierMap.has(funcName)) {
identifierMap.set(funcName, (0, next_name_1.default)());
}
}
i++;
if (i + 1 < tokens.length && tokens[i + 1].value === "(") {
i += 2;
while (i < tokens.length && tokens[i].value !== ")") {
if (tokens[i].type === "identifier" &&
i + 1 < tokens.length &&
tokens[i + 1].value === ":") {
const paramName = tokens[i].value;
if (!identifierMap.has(paramName)) {
identifierMap.set(paramName, (0, next_name_1.default)());
}
i++;
}
i++;
}
}
}
else if ((token.value === "let" || token.value === "var" || token.value === "const") &&
i + 1 < tokens.length &&
tokens[i + 1].type === "identifier") {
const varName = tokens[i + 1].value;
if (!identifierMap.has(varName)) {
identifierMap.set(varName, (0, next_name_1.default)());
}
i++;
}
else if (token.value === "struct" && i + 1 < tokens.length && tokens[i + 1].type === "identifier") {
const structName = tokens[i + 1].value;
if (!identifierMap.has(structName)) {
identifierMap.set(structName, (0, next_name_1.default)());
}
i++;
}
}
}
return { identifierMap, bindingMap };
}
;