Skip to content

Commit 6b9ce86

Browse files
fixes to opt-tool
1 parent 719ea6a commit 6b9ce86

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

genaisrc/myai.genai.mts

+22-18
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function parseOptFunctions(inputFile: WorkspaceFile) {
6464

6565
import * as fs from 'fs';
6666

67-
export async function mergeModifiedFunction(code :string, funs : { code: string, name: string }[], new_code : string) {
67+
export async function mergeModifiedFunction(code: string, funs: { code: string, name: string }[], new_code: string) {
6868
let name = function_name_from_code(new_code);
6969
let fun = funs.find(f => f.name === name);
7070
if (fun) {
@@ -74,30 +74,32 @@ export async function mergeModifiedFunction(code :string, funs : { code: string,
7474
return code;
7575
}
7676

77-
async function canCompileCode(inputFile : WorkspaceFile, code : string) {
77+
async function canCompileCode(inputFile: WorkspaceFile, new_code: string) {
7878

79+
//
7980
// move input file to a temp file
8081
// move code to the inputFile.filename
8182
// invoke ninja in the build directory: ninja -b build
8283
// move the temp file back to the original file
8384
// return true iff it succeeded
85+
//
8486

8587
let tempFile = inputFile.filename + ".tmp";
86-
let original_content = inputFile.content;
87-
await workspace.writeText(tempFile, inputFile.content);
88-
await workspace.writeText(inputFile.filename, code);
88+
let old_code = inputFile.content;
89+
await workspace.writeText(tempFile, old_code);
90+
await workspace.writeText(inputFile.filename, new_code);
8991
let result = await host.exec(`cmd /k "C:\Program\ Files/Microsoft\ Visual\ Studio/2022/Enterprise/Common7/Tools/VsDevCmd.bat" -arch=x64 & ninja`, { cwd: "build" });
90-
91-
// await fs.delete(tempFile);
92-
if (result.exitCode !== 0) {
93-
await workspace.writeText(inputFile.filename, original_content);
94-
console.log(result.stderr);
95-
return false;
92+
await workspace.writeText(inputFile.filename, old_code);
93+
if (result.exitCode == 0) {
94+
await workspace.writeText(tempFile, new_code);
95+
return true;
9696
}
97-
return true;
97+
console.log(result.stderr);
98+
return false;
99+
98100
}
99101

100-
export async function mergeCompileFunction(inputFile : WorkspaceFile, code : string, funs : { code: string, name: string }[], new_code_input : string) {
102+
export async function mergeCompileFunction(inputFile: WorkspaceFile, code: string, funs: { code: string, name: string }[], new_code_input: string) {
101103
let match_new_code = new_code_input.match(/```cpp([\s\S]*?)```/);
102104
if (!match_new_code) {
103105
console.log("Invalid new code");
@@ -110,8 +112,10 @@ export async function mergeCompileFunction(inputFile : WorkspaceFile, code : str
110112

111113
if (!fun) {
112114
console.log(`Function name '${name}' not found`);
113-
for (const fun of funs)
115+
console.log("Available functions: ");
116+
for (const fun of funs)
114117
console.log("'" + fun.name + "'");
118+
console.log(new_code);
115119
return code;
116120
}
117121
console.log("Updated function: " + name);
@@ -122,15 +126,15 @@ export async function mergeCompileFunction(inputFile : WorkspaceFile, code : str
122126
}
123127
let canCompile = await canCompileCode(inputFile, modified_code);
124128
console.log("Can compile: " + canCompile);
125-
if (canCompile)
129+
if (canCompile)
126130
return modified_code;
127131
return code;
128132
}
129133

130134
export async function mergeFunctionsFromList(inputCode: string, funs: { code: string, name: string }[], modifiedFunctionList: string[]) {
131135
let code = inputCode;
132-
for (const new_code of modifiedFunctionList)
133-
code = await mergeModifiedFunction(code, funs, new_code);
136+
for (const new_code of modifiedFunctionList)
137+
code = await mergeModifiedFunction(code, funs, new_code);
134138
return code;
135139
}
136140

@@ -140,7 +144,7 @@ export async function mergeFunctions(inputFile: WorkspaceFile) {
140144
return mergeFunctionsFromList(inputFile.content, funs, modifiedFunctionList);
141145
}
142146

143-
export async function invokeLLMOpt(code : string) {
147+
export async function invokeLLMOpt(code: string) {
144148
const answer = await runPrompt(
145149
(_) => {
146150
_.def("CODE", code);

genaisrc/myopttool.genai.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ for (const fun of funs) {
1515
new_code = await mergeCompileFunction(inputFile, new_code, funs, answer);
1616
}
1717

18-
await workspace.writeText(inputFile.filename + "opt.cpp", new_code);
18+
await workspace.writeText(inputFile.filename + ".opt.cpp", new_code);

0 commit comments

Comments
 (0)