@@ -74,7 +74,11 @@ export async function mergeModifiedFunction(code: string, funs: { code: string,
7474 return code ;
7575}
7676
77- async function canCompileCode ( inputFile : WorkspaceFile , new_code : string ) {
77+ class ShellOutputRef {
78+ shellOutput : ShellOutput ;
79+ }
80+
81+ async function canCompileCode ( inputFile : WorkspaceFile , new_code : string , result : ShellOutputRef ) {
7882
7983 //
8084 // move input file to a temp file
@@ -88,15 +92,47 @@ async function canCompileCode(inputFile: WorkspaceFile, new_code: string) {
8892 let old_code = inputFile . content ;
8993 await workspace . writeText ( tempFile , old_code ) ;
9094 await workspace . writeText ( inputFile . filename , new_code ) ;
91- let result = await host . exec ( `cmd /k "C:\ Program\ Files/Microsoft\ Visual\ Studio/2022/Enterprise/Common7/Tools/VsDevCmd.bat" -arch=x64 & ninja` , { cwd : "build" } ) ;
95+ result . shellOutput = await host . exec ( `cmd /k "C:/ Program\ Files/Microsoft\ Visual\ Studio/2022/Enterprise/Common7/Tools/VsDevCmd.bat" -arch=x64 & ninja` , { cwd : "build" } ) ;
9296 await workspace . writeText ( inputFile . filename , old_code ) ;
93- if ( result . exitCode == 0 ) {
97+ console . log ( result . shellOutput . stdout ) ;
98+ console . log ( result . shellOutput . stderr ) ;
99+ let has_failed = result . shellOutput . stdout . search ( "failed" ) ;
100+ if ( has_failed != - 1 ) {
101+ console . log ( "Failed to compile" ) ;
102+ return false ;
103+ }
104+ if ( result . shellOutput . exitCode == 0 ) {
94105 await workspace . writeText ( tempFile , new_code ) ;
95106 return true ;
96107 }
97- console . log ( result . stderr ) ;
98108 return false ;
109+ }
110+
111+ async function llmFixCompilerErrors ( inputFile : WorkspaceFile , new_code : string , result : ShellOutput ) {
112+ let answer = await runPrompt (
113+ ( _ ) => {
114+ _ . def ( "CODE" , new_code ) ;
115+ _ . def ( "OUTPUT" , result . stderr + result . stdout ) ;
116+ _ . $ `You are a highly experienced compiler engineer with over 20 years of expertise,
117+ specializing in C and C++ programming. Your deep knowledge of best coding practices
118+ and software engineering principles enables you to produce robust, efficient, and
119+ maintainable code in any scenario.
99120
121+ Please modify the original code in <CODE> to ensure that it compiles without any errors.
122+ The compiler produced the output <OUTPUT> when building <CODE>.
123+ `
124+ } , {
125+ system : [ ] ,
126+ systemSafety : false
127+ }
128+ ) ;
129+ let new_code_input = answer . text ;
130+ let match_new_code = new_code_input . match ( / ` ` ` c p p ( [ \s \S ] * ?) ` ` ` / ) ;
131+ if ( ! match_new_code ) {
132+ console . log ( "Invalid new code" ) ;
133+ return new_code ;
134+ }
135+ return match_new_code [ 1 ] ;
100136}
101137
102138export async function mergeCompileFunction ( inputFile : WorkspaceFile , code : string , funs : { code : string , name : string } [ ] , new_code_input : string ) {
@@ -106,28 +142,36 @@ export async function mergeCompileFunction(inputFile: WorkspaceFile, code: strin
106142 return code ;
107143 }
108144 let new_code = match_new_code [ 1 ] ;
109-
110- let name = function_name_from_code ( new_code ) ;
111- let fun = funs . find ( f => f . name == name ) ;
112-
113- if ( ! fun ) {
114- console . log ( `Function name '${ name } ' not found` ) ;
115- console . log ( "Available functions: " ) ;
116- for ( const fun of funs )
117- console . log ( "'" + fun . name + "'" ) ;
118- console . log ( new_code ) ;
119- return code ;
120- }
121- console . log ( "Updated function: " + name ) ;
122- let modified_code = code . replace ( fun . code , new_code ) ;
123- if ( code == modified_code ) {
124- console . log ( "No change in function: " + name ) ;
125- return code ;
145+ let retry_count = 0 ;
146+
147+ while ( retry_count < 2 ) {
148+ let name = function_name_from_code ( new_code ) ;
149+ let fun = funs . find ( f => f . name == name ) ;
150+
151+ if ( ! fun ) {
152+ console . log ( `Function name '${ name } ' not found` ) ;
153+ console . log ( "Available functions: " ) ;
154+ for ( const fun of funs )
155+ console . log ( "'" + fun . name + "'" ) ;
156+ console . log ( new_code ) ;
157+ return code ;
158+ }
159+ console . log ( "Updated function: " + name ) ;
160+ let modified_code = code . replace ( fun . code , new_code ) ;
161+ if ( code == modified_code ) {
162+ console . log ( "No change in function: " + name ) ;
163+ return code ;
164+ }
165+ let result = new ShellOutputRef ( ) ;
166+ let canCompile = await canCompileCode ( inputFile , modified_code , result ) ;
167+ console . log ( "Can compile: " + canCompile ) ;
168+ if ( canCompile )
169+ return modified_code ;
170+ if ( retry_count > 0 )
171+ break ;
172+ retry_count ++ ;
173+ new_code = await llmFixCompilerErrors ( inputFile , new_code , result . shellOutput ) ;
126174 }
127- let canCompile = await canCompileCode ( inputFile , modified_code ) ;
128- console . log ( "Can compile: " + canCompile ) ;
129- if ( canCompile )
130- return modified_code ;
131175 return code ;
132176}
133177
@@ -153,7 +197,13 @@ export async function invokeLLMOpt(code: string) {
153197 and software engineering principles enables you to produce robust, efficient, and
154198 maintainable code in any scenario.
155199
156- Please modify the original code in <CODE> to ensure that it uses best practices for optimal code execution.' `
200+ Please modify the original code in <CODE> to ensure that it uses best practices for optimal code execution.
201+
202+ - do not use assert. Instead use SASSERT.
203+ - do not change function signatures.
204+ - do not use std::vector.
205+ - do not add new comments.
206+ - do not split functions into multiple functions.`
157207 } , {
158208 system : [ ] ,
159209 systemSafety : false
0 commit comments