@@ -74,7 +74,11 @@ export async function mergeModifiedFunction(code: string, funs: { code: string,
74
74
return code ;
75
75
}
76
76
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 ) {
78
82
79
83
//
80
84
// move input file to a temp file
@@ -88,15 +92,47 @@ async function canCompileCode(inputFile: WorkspaceFile, new_code: string) {
88
92
let old_code = inputFile . content ;
89
93
await workspace . writeText ( tempFile , old_code ) ;
90
94
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" } ) ;
92
96
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 ) {
94
105
await workspace . writeText ( tempFile , new_code ) ;
95
106
return true ;
96
107
}
97
- console . log ( result . stderr ) ;
98
108
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.
99
120
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 ] ;
100
136
}
101
137
102
138
export 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
106
142
return code ;
107
143
}
108
144
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 ) ;
126
174
}
127
- let canCompile = await canCompileCode ( inputFile , modified_code ) ;
128
- console . log ( "Can compile: " + canCompile ) ;
129
- if ( canCompile )
130
- return modified_code ;
131
175
return code ;
132
176
}
133
177
@@ -153,7 +197,13 @@ export async function invokeLLMOpt(code: string) {
153
197
and software engineering principles enables you to produce robust, efficient, and
154
198
maintainable code in any scenario.
155
199
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.`
157
207
} , {
158
208
system : [ ] ,
159
209
systemSafety : false
0 commit comments