1- use crate :: { KitParser , Rule , error:: CompilationError } ;
21use crate :: error:: CompileResult ;
2+ use crate :: { KitParser , Rule , error:: CompilationError } ;
33use pest:: Parser ;
44
55use std:: collections:: HashSet ;
6+ use std:: fmt:: Write ;
67use std:: path:: { Path , PathBuf } ;
78use std:: process:: Command ;
89
9- use crate :: codegen:: ast:: * ;
10+ use crate :: codegen:: ast:: { Block , Expr , Function , Include , Program , Stmt } ;
1011use crate :: codegen:: compiler:: { CompilerMeta , CompilerOptions , Toolchain } ;
1112use crate :: codegen:: inference:: TypeInferencer ;
1213use crate :: codegen:: parser:: Parser as CodeParser ;
@@ -60,7 +61,7 @@ impl Compiler {
6061 }
6162 }
6263
63- self . includes = includes . clone ( ) ;
64+ self . includes . clone_from ( & includes ) ;
6465
6566 Ok ( Program {
6667 includes,
@@ -70,10 +71,10 @@ impl Compiler {
7071 }
7172
7273 /// Generate C code from the AST and write it to the output path
73- fn transpile_with_program ( & mut self , prog : Program ) {
74- let c_code = self . generate_c_code ( & prog) ;
74+ fn transpile_with_program ( & mut self , prog : & Program ) {
75+ let c_code = self . generate_c_code ( prog) ;
7576 if let Err ( e) = std:: fs:: write ( & self . c_output , c_code) {
76- panic ! ( "Failed to write output: {}" , e ) ;
77+ panic ! ( "Failed to write output: {e}" ) ;
7778 }
7879 }
7980
@@ -134,7 +135,7 @@ impl Compiler {
134135
135136 // emit unique headers
136137 for hdr in seen_headers {
137- out . push_str ( & format ! ( "#include {} \n " , hdr ) ) ;
138+ writeln ! ( out , "#include {hdr}" ) . unwrap ( ) ;
138139 }
139140 out. push ( '\n' ) ;
140141
@@ -213,16 +214,15 @@ impl Compiler {
213214 . inferencer
214215 . store
215216 . resolve ( * inferred)
216- . map ( |t| t. to_c_repr ( ) . name )
217- . unwrap_or_else ( |_| "auto" . to_string ( ) ) ;
217+ . map_or_else ( |_| "auto" . to_string ( ) , |t| t. to_c_repr ( ) . name ) ;
218218
219219 match init {
220220 Some ( expr) => {
221221 let init_str = self . transpile_expr ( expr) ;
222- format ! ( "{} {} = {};\n " , ty_str , name , init_str )
222+ format ! ( "{ty_str } {name } = {init_str };\n " )
223223 }
224224 None => {
225- format ! ( "{} {};\n " , ty_str , name )
225+ format ! ( "{ty_str } {name };\n " )
226226 }
227227 }
228228 }
@@ -260,16 +260,12 @@ impl Compiler {
260260 let mut s = if let Expr :: RangeLiteral { start, end } = iter {
261261 let start_str = self . transpile_expr ( start) ;
262262 let end_str = self . transpile_expr ( end) ;
263- format ! (
264- "for (int {} = {}; {} < {}; ++{}) " ,
265- var, start_str, var, end_str, var
266- )
263+ format ! ( "for (int {var} = {start_str}; {var} < {end_str}; ++{var}) " )
267264 } else {
268265 let iter_str = self . transpile_expr ( iter) ;
269- format ! ( "for (int {} = 0; {} < {}; ++{}) " , var , var , iter_str , var )
266+ format ! ( "for (int {var } = 0; {var } < {iter_str }; ++{var }) " )
270267 } ;
271268 s. push_str ( & self . transpile_block ( body) ) ;
272- s. push ( '\n' ) ;
273269 s
274270 }
275271 Stmt :: Break => "break;\n " . to_string ( ) ,
@@ -300,7 +296,7 @@ impl Compiler {
300296 . map ( |a| self . transpile_expr ( a) )
301297 . collect :: < Vec < _ > > ( )
302298 . join ( ", " ) ;
303- format ! ( "{}({})" , callee , args_str )
299+ format ! ( "{callee }({args_str })" )
304300 }
305301 Expr :: UnaryOp { op, expr, ty : _ } => {
306302 let expr_str = self . transpile_expr ( expr) ;
@@ -314,7 +310,7 @@ impl Compiler {
314310 } => {
315311 let left_str = self . transpile_expr ( left) ;
316312 let right_str = self . transpile_expr ( right) ;
317- format ! ( "({} {} {})" , left_str , op. to_c_str( ) , right_str )
313+ format ! ( "({left_str } {} {right_str })" , op. to_c_str( ) )
318314 }
319315 Expr :: Assign {
320316 op,
@@ -324,7 +320,7 @@ impl Compiler {
324320 } => {
325321 let left_str = self . transpile_expr ( left) ;
326322 let right_str = self . transpile_expr ( right) ;
327- format ! ( "{} {} {}" , left_str , op. to_c_str( ) , right_str )
323+ format ! ( "{left_str } {} {right_str }" , op. to_c_str( ) )
328324 }
329325 Expr :: If {
330326 cond,
@@ -335,7 +331,7 @@ impl Compiler {
335331 let cond_str = self . transpile_expr ( cond) ;
336332 let then_str = self . transpile_expr ( then_branch) ;
337333 let else_str = self . transpile_expr ( else_branch) ;
338- format ! ( "({} ? {} : {})" , cond_str , then_str , else_str )
334+ format ! ( "({cond_str } ? {then_str } : {else_str })" )
339335 }
340336 Expr :: RangeLiteral { .. } => {
341337 // Should technically not be used alone, but return something safe to avoid panic
@@ -348,7 +344,7 @@ impl Compiler {
348344 let mut prog = self . parse ( ) ?;
349345
350346 self . inferencer . infer_program ( & mut prog) ?;
351- self . transpile_with_program ( prog) ;
347+ self . transpile_with_program ( & prog) ;
352348
353349 let detected = Toolchain :: executable_path ( ) . ok_or ( CompilationError :: ToolchainNotFound ) ?;
354350
0 commit comments