@@ -56,7 +56,7 @@ ASTNode* parse_program() {
5656 statements [stmt_count ++ ] = parse_statement ();
5757
5858 // After a statement, we must have a newline or EOF.
59- if (current_token -> type != TOK_NEWLINE && current_token -> type != TOK_EOF ) {
59+ if (current_token -> type != TOK_EOF && current_token -> type != TOK_EOF ) {
6060 consume (TOK_NEWLINE , "Expected newline after statement" );
6161 }
6262 }
@@ -169,30 +169,52 @@ ASTNode* parse_statement() {
169169 return each ;
170170 }
171171 else if (match (KW_IF )) {
172- advance (); // consume 'if'
173- ASTNode * condition = parse_expression ();
174- consume (TOK_COLON , "Expected ':'" );
175- consume (TOK_NEWLINE , "Expected newline after if condition" );
176-
177- int body_count = 0 ;
178- ASTNode * * body = malloc (sizeof (ASTNode * ) * 100 );
172+ advance (); // consume 'if'
173+ ASTNode * condition = parse_expression ();
174+ consume (TOK_COLON , "Expected ':'" );
175+ consume (TOK_NEWLINE , "Expected newline after if condition" );
176+
177+ // Parse 'then' body (stop at 'else' or 'end')
178+ int then_body_count = 0 ;
179+ ASTNode * * then_body = malloc (sizeof (ASTNode * ) * 100 );
180+ while (current_token && current_token -> type != TOK_END && current_token -> type != KW_ELSE ) {
181+ if (current_token -> type == TOK_NEWLINE ) {
182+ advance ();
183+ continue ;
184+ }
185+ then_body [then_body_count ++ ] = parse_statement ();
186+ }
187+
188+ // Parse optional 'else' block
189+ ASTNode * * else_body = NULL ;
190+ int else_body_count = 0 ;
191+ if (match (KW_ELSE )) {
192+ advance (); // consume 'else'
193+ consume (TOK_COLON , "Expected ':' after else" );
194+ consume (TOK_NEWLINE , "Expected newline after else" );
195+
196+ else_body = malloc (sizeof (ASTNode * ) * 100 );
179197 while (current_token && current_token -> type != TOK_END ) {
180198 if (current_token -> type == TOK_NEWLINE ) {
181199 advance ();
182200 continue ;
183201 }
184- body [ body_count ++ ] = parse_statement ();
202+ else_body [ else_body_count ++ ] = parse_statement ();
185203 }
186- consume (TOK_END , "Expected 'end' to close if" );
187-
188- ASTNode * if_stmt = malloc (sizeof (ASTNode ));
189- if_stmt -> type = AST_IF ;
190- if_stmt -> line = current_token -> line ;
191- if_stmt -> data .loop .condition = condition ;
192- if_stmt -> data .loop .body = body ;
193- if_stmt -> data .loop .body_count = body_count ;
194- return if_stmt ;
195204 }
205+
206+ consume (TOK_END , "Expected 'end' to close if" );
207+
208+ ASTNode * if_node = malloc (sizeof (ASTNode ));
209+ if_node -> type = AST_IF ;
210+ if_node -> line = condition -> line ;
211+ if_node -> data .if_stmt .condition = condition ;
212+ if_node -> data .if_stmt .then_body = then_body ;
213+ if_node -> data .if_stmt .then_body_count = then_body_count ;
214+ if_node -> data .if_stmt .else_body = else_body ; // NULL if no else
215+ if_node -> data .if_stmt .else_body_count = else_body_count ;
216+ return if_node ;
217+ }
196218 else if (match (KW_GIVE )) {
197219 advance (); // consume 'give'
198220 ASTNode * value = parse_expression ();
@@ -599,12 +621,19 @@ void print_ast(ASTNode* node, int indent) {
599621 }
600622 break ;
601623 case AST_IF :
602- printf ("IF\n" );
603- print_ast (node -> data .loop .condition , indent + 1 );
604- for (int i = 0 ; i < node -> data .loop .body_count ; i ++ ) {
605- print_ast (node -> data .loop .body [i ], indent + 1 );
606- }
607- break ;
624+ printf ("IF\n" );
625+ print_ast (node -> data .if_stmt .condition , indent + 1 );
626+ printf ("%*sTHEN:\n" , indent * 2 , "" );
627+ for (int i = 0 ; i < node -> data .if_stmt .then_body_count ; i ++ ) {
628+ print_ast (node -> data .if_stmt .then_body [i ], indent + 1 );
629+ }
630+ if (node -> data .if_stmt .else_body ) {
631+ printf ("%*sELSE:\n" , indent * 2 , "" );
632+ for (int i = 0 ; i < node -> data .if_stmt .else_body_count ; i ++ ) {
633+ print_ast (node -> data .if_stmt .else_body [i ], indent + 1 );
634+ }
635+ }
636+ break ;
608637 case AST_LOOP :
609638 printf ("LOOP\n" );
610639 print_ast (node -> data .loop .condition , indent + 1 );
0 commit comments