2626
2727 info!("constants: {:?}", chunk.constants);
2828 info!("names: {:?}", chunk.names);
29+ info!("annotations: {:?}", chunk.annotations);
30+ info!("functions count: {:?}", chunk.functions.len());
2931 ```
3032
3133 Output:
@@ -230,7 +232,7 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
230232 self . chunk . emit ( OpCode :: JumpIfFalse , 0 ) ;
231233 let jf = self . chunk . instructions . len ( ) - 1 ;
232234 self . eat ( TokenType :: Colon ) ;
233- self . stmt ( ) ;
235+ self . compile_block ( ) ; // ← AQUÍ
234236 self . chunk . emit ( OpCode :: Jump , loop_start) ;
235237 self . patch ( jf) ;
236238 self . loop_starts . pop ( ) ;
@@ -250,8 +252,7 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
250252 self . chunk . emit ( OpCode :: JumpIfFalse , 0 ) ;
251253 let jf = self . chunk . instructions . len ( ) - 1 ;
252254 self . eat ( TokenType :: Colon ) ;
253- self . stmt ( ) ;
254- self . chunk . emit ( OpCode :: PopTop , 0 ) ;
255+ self . compile_block ( ) ;
255256
256257 match self . peek ( ) {
257258 Some ( TokenType :: Elif ) => {
@@ -268,8 +269,7 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
268269 let jmp = self . chunk . instructions . len ( ) - 1 ;
269270 self . patch ( jf) ;
270271 self . eat ( TokenType :: Colon ) ;
271- self . stmt ( ) ;
272- self . chunk . emit ( OpCode :: PopTop , 0 ) ;
272+ self . compile_block ( ) ; // ← usa block aquí
273273 self . patch ( jmp) ;
274274 }
275275 _ => { self . patch ( jf) ; }
@@ -294,8 +294,8 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
294294 self . chunk . emit ( OpCode :: StoreName , idx) ;
295295
296296 self . eat ( TokenType :: Colon ) ;
297- self . stmt ( ) ;
298- self . chunk . emit ( OpCode :: PopTop , 0 ) ;
297+ self . compile_block ( ) ; // ← correcto
298+
299299 self . chunk . emit ( OpCode :: Jump , loop_start) ;
300300 self . patch ( fi) ;
301301 self . loop_starts . pop ( ) ;
@@ -313,6 +313,31 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
313313 if matches ! ( self . peek( ) , Some ( k) if k == kind) { self . advance ( ) ; true } else { false }
314314 }
315315
316+ fn compile_block ( & mut self ) {
317+ self . eat_if ( TokenType :: Indent ) ;
318+
319+ while !self . at_end ( ) {
320+ if matches ! ( self . peek( ) , Some ( TokenType :: Dedent ) ) {
321+ self . advance ( ) ;
322+ break ;
323+ }
324+
325+ if matches ! ( self . peek( ) , Some ( TokenType :: Newline | TokenType :: Nl ) ) {
326+ self . advance ( ) ;
327+ continue ;
328+ }
329+
330+ let is_compound = matches ! ( self . peek( ) ,
331+ Some ( TokenType :: For | TokenType :: If | TokenType :: While | TokenType :: Def ) ) ;
332+
333+ self . stmt ( ) ; // ← ya lo tienes, perfecto
334+
335+ if !self . at_end ( ) && !is_compound {
336+ self . chunk . emit ( OpCode :: PopTop , 0 ) ;
337+ }
338+ }
339+ }
340+
316341 fn dict_literal ( & mut self ) {
317342 let mut pairs = 0u16 ;
318343 while !matches ! ( self . peek( ) , Some ( TokenType :: Rbrace ) | None ) {
@@ -588,15 +613,17 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
588613 }
589614
590615 fn compile_body ( & mut self , params : & [ String ] ) -> SSAChunk {
591- let ( saved_chunk, saved_ver) = ( std :: mem :: take ( & mut self . chunk ) , std :: mem :: take ( & mut self . ssa_versions ) ) ;
592- for p in params { self . ssa_versions . insert ( p . clone ( ) , 0 ) ; }
593-
594- loop {
595- let is_return = matches ! ( self . peek ( ) , Some ( TokenType :: Return ) ) ;
596- self . stmt ( ) ;
597- if is_return || self . at_end ( ) { break ; }
616+ let ( mut saved_chunk, saved_ver) = (
617+ std :: mem :: take ( & mut self . chunk ) ,
618+ std :: mem :: take ( & mut self . ssa_versions ) ,
619+ ) ;
620+
621+ for p in params {
622+ self . ssa_versions . insert ( p . clone ( ) , 0 ) ;
598623 }
599-
624+
625+ self . compile_block ( ) ; // ← SOLO esto
626+
600627 let body = std:: mem:: take ( & mut self . chunk ) ;
601628 ( self . chunk , self . ssa_versions ) = ( saved_chunk, saved_ver) ;
602629 body
0 commit comments