@@ -90,6 +90,35 @@ impl<'a> Code<'a> {
9090 ) ) ;
9191 self . free_reg ( condition) ;
9292 }
93+ Statement :: For { init, condition, update, body } => {
94+ if let Some ( SimpleStatement :: Expression ( expr) ) = init {
95+ let reg = self . lower_expr ( expr, ValueContext :: Untyped ) ;
96+ self . free_reg ( reg) ;
97+ }
98+ let cond_label = self . following_instr ( 0 ) ;
99+ if let Some ( condition) = condition {
100+ let condition = self . lower_expr ( condition, ValueContext :: Scalar ) ;
101+ let body_label = self . following_instr ( 1 ) ;
102+ let while_label =
103+ self . bc
104+ . emit ( Instruction :: branch ( condition. reg ( ) , body_label, Label ( 0 ) ) ) ;
105+ self . free_reg ( condition) ;
106+ self . lower_body ( body) ;
107+ if let Some ( SimpleStatement :: Expression ( expr) ) = update {
108+ let reg = self . lower_expr ( expr, ValueContext :: Untyped ) ;
109+ self . free_reg ( reg) ;
110+ }
111+ self . bc . emit ( Instruction :: jump ( cond_label) ) ;
112+ self . bc . nth ( while_label) . args . branch . 2 = self . following_instr ( 0 ) ;
113+ } else {
114+ self . lower_body ( body) ;
115+ if let Some ( SimpleStatement :: Expression ( expr) ) = update {
116+ let reg = self . lower_expr ( expr, ValueContext :: Untyped ) ;
117+ self . free_reg ( reg) ;
118+ }
119+ self . bc . emit ( Instruction :: jump ( cond_label) ) ;
120+ }
121+ }
93122 Statement :: Simple ( SimpleStatement :: Expression ( expr) ) => {
94123 let reg = self . lower_expr ( expr, ValueContext :: Untyped ) ;
95124 self . free_reg ( reg) ;
0 commit comments