3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-04 08:39:57 +00:00
z3/genaisrc/treesitter.genai.mts
Nikolaj Bjorner 9fad15e2ca adding mergeopt
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2025-02-13 15:00:05 -08:00

37 lines
984 B
TypeScript

script({
title: "Extract functions from files using treesitter",
files: "src/muz/spacer/spacer_qe_project.cpp"
})
function get_functions(captures : QueryCapture[], code : string) {
return captures.map(({ name, node }) => ({
code : node.text
}))
}
const inputFile = env.files[0];
const { captures: functions } = await parsers.code(
inputFile,
`(function_definition) @function`
);
let funs = get_functions(functions, inputFile.content);
for (const fun of funs) {
// todo put files in a different directory
let name = fun.code.split('(')[0].trim();
name = name
.replace(/::/g, '_')
.replace(/ /g, '_')
.replace(/\*/g, '');
console.log(name);
let outputFile = "slice_" + path.basename(inputFile.filename) + name + ".cpp";
outputFile = path.join("code_slices", outputFile);
await workspace.writeText(outputFile, `//Extracted ${name} in ${inputFile.filename}\n${fun.code}\n\n`);
}