@@ -325,15 +325,21 @@ def _visit_reassign(self, node):
325325
326326 def _visit_glosure_anon (self , node ):
327327 self ._indent += 1
328+ body_indent = self ._indent
328329 params_str , body_str = self ._make_function_body (node .children [0 ], node .children [1 ])
329330 self ._indent -= 1
330- return f"(glosure ({ params_str } ) (begin\n { body_str } ))" if body_str else f"(glosure ({ params_str } ) (begin))"
331+ if body_str :
332+ return f"(glosure ({ params_str } ) (begin\n { body_str } \n { INDENT * body_indent } ))"
333+ return f"(glosure ({ params_str } ) (begin))"
331334
332335 def _visit_lambda_anon (self , node ):
333336 self ._indent += 1
337+ body_indent = self ._indent
334338 params_str , body_str = self ._make_function_body (node .children [0 ], node .children [1 ])
335339 self ._indent -= 1
336- return f"(lambda ({ params_str } ) (begin\n { body_str } ))" if body_str else f"(lambda ({ params_str } ) (begin))"
340+ if body_str :
341+ return f"(lambda ({ params_str } ) (begin\n { body_str } \n { INDENT * body_indent } ))"
342+ return f"(lambda ({ params_str } ) (begin))"
337343
338344 def _visit_return_stmt (self , node ):
339345 return self ._visit (node .children [0 ])
@@ -359,7 +365,7 @@ def _make_function_body(self, param_node, block_node):
359365 body_str = "\n " .join (body_lines )
360366 return params_str , body_str
361367
362- def _make_method (self , keyword , dotted_name , params_str , body_str ):
368+ def _make_method (self , keyword , dotted_name , params_str , body_str , body_indent = 0 ):
363369 parts = dotted_name .split ("." )
364370 method_name = parts [- 1 ]
365371 obj_parts = parts [:- 1 ]
@@ -371,7 +377,10 @@ def _make_method(self, keyword, dotted_name, params_str, body_str):
371377 target = f"(at { target } '{ part } ')"
372378 set_target = target
373379 func_name = f"__{ method_name } "
374- func_body = f"(begin\n { body_str } )" if body_str else "(begin)"
380+ if body_str :
381+ func_body = f"(begin\n { body_str } \n { INDENT * body_indent } )"
382+ else :
383+ func_body = "(begin)"
375384 func_def = f"({ keyword } { func_name } ({ params_str } ) { func_body } )"
376385 set_line = f"(set { set_target } '{ method_name } ' { func_name } )"
377386 idt = INDENT * self ._indent
@@ -384,11 +393,15 @@ def _visit_function_def(self, node):
384393 param_node = node .children [1 ]
385394 block_node = node .children [2 ]
386395 self ._indent += 1
396+ body_indent = self ._indent
387397 params_str , body_str = self ._make_function_body (param_node , block_node )
388398 self ._indent -= 1
389399 if "." in name :
390- return self ._make_method ("defunction" , name , params_str , body_str )
391- func_body = f"(begin\n { body_str } )" if body_str else "(begin)"
400+ return self ._make_method ("defunction" , name , params_str , body_str , body_indent )
401+ if body_str :
402+ func_body = f"(begin\n { body_str } \n { INDENT * body_indent } )"
403+ else :
404+ func_body = "(begin)"
392405 return f"(defunction { name } ({ params_str } ) { func_body } )"
393406
394407 def _visit_defun_def (self , node ):
@@ -398,11 +411,15 @@ def _visit_defun_def(self, node):
398411 param_node = node .children [1 ]
399412 block_node = node .children [2 ]
400413 self ._indent += 1
414+ body_indent = self ._indent
401415 params_str , body_str = self ._make_function_body (param_node , block_node )
402416 self ._indent -= 1
403417 if "." in name :
404- return self ._make_method ("defun" , name , params_str , body_str )
405- func_body = f"(begin\n { body_str } )" if body_str else "(begin)"
418+ return self ._make_method ("defun" , name , params_str , body_str , body_indent )
419+ if body_str :
420+ func_body = f"(begin\n { body_str } \n { INDENT * body_indent } )"
421+ else :
422+ func_body = "(begin)"
406423 return f"(defun { name } ({ params_str } ) { func_body } )"
407424
408425 def _visit_param_list (self , node ):
@@ -423,8 +440,9 @@ def _visit_for_stmt(self, node):
423440 cond_str = self ._visit (cond )
424441 inc_str = self ._visit (inc )
425442 self ._indent += 1
443+ body_indent = self ._indent
426444 body_str = self ._visit (body )
427- result = f"(for { init_str } { cond_str } { inc_str } (begin\n { INDENT * self . _indent } { body_str } ))"
445+ result = f"(for { init_str } { cond_str } { inc_str } (begin\n { INDENT * body_indent } { body_str } \n { INDENT * body_indent } ))"
428446 self ._indent -= 1
429447 return result
430448
@@ -433,25 +451,28 @@ def _visit_foreach_stmt(self, node):
433451 vl = node .children [1 ].value
434452 expr_str = self ._visit (node .children [2 ])
435453 self ._indent += 1
454+ body_indent = self ._indent
436455 body_str = self ._visit (node .children [3 ])
437- result = f"(foreach { idx } { vl } { expr_str } (begin\n { INDENT * self . _indent } { body_str } ))"
456+ result = f"(foreach { idx } { vl } { expr_str } (begin\n { INDENT * body_indent } { body_str } \n { INDENT * body_indent } ))"
438457 self ._indent -= 1
439458 return result
440459
441460 def _visit_while_stmt (self , node ):
442461 cond = self ._visit (node .children [0 ])
443462 self ._indent += 1
463+ body_indent = self ._indent
444464 body = self ._visit (node .children [1 ])
445- result = f"(while { cond } (begin\n { INDENT * self . _indent } { body } ))"
465+ result = f"(while { cond } (begin\n { INDENT * body_indent } { body } \n { INDENT * body_indent } ))"
446466 self ._indent -= 1
447467 return result
448468
449469 def _visit_if_stmt (self , node ):
450470 children = node .children
451471 cond = self ._visit (children [0 ])
452472 self ._indent += 1
473+ then_indent = self ._indent
453474 then_block = self ._visit (children [1 ])
454- then_result = f"(if { cond } (begin\n { INDENT * self . _indent } { then_block } )"
475+ then_result = f"(if { cond } (begin\n { INDENT * then_indent } { then_block } \n { INDENT * then_indent } )"
455476 self ._indent -= 1
456477
457478 if len (children ) == 2 :
@@ -460,8 +481,9 @@ def _visit_if_stmt(self, node):
460481 remaining = children [2 :]
461482 if len (remaining ) % 2 == 1 :
462483 self ._indent += 1
484+ else_indent = self ._indent
463485 last_block = self ._visit (remaining [- 1 ])
464- rest = f"(begin\n { INDENT * self . _indent } { last_block } )"
486+ rest = f"(begin\n { INDENT * else_indent } { last_block } \n { INDENT * else_indent } )"
465487 self ._indent -= 1
466488 pairs = remaining [:- 1 ]
467489 else :
@@ -471,8 +493,9 @@ def _visit_if_stmt(self, node):
471493 for i in range (len (pairs ) - 2 , - 1 , - 2 ):
472494 else_cond = self ._visit (pairs [i ])
473495 self ._indent += 1
496+ else_indent = self ._indent
474497 else_block = self ._visit (pairs [i + 1 ])
475- else_part = f"(if { else_cond } (begin\n { INDENT * self . _indent } { else_block } )"
498+ else_part = f"(if { else_cond } (begin\n { INDENT * else_indent } { else_block } \n { INDENT * else_indent } )"
476499 self ._indent -= 1
477500 if rest :
478501 rest = f"{ else_part } \n { INDENT * self ._indent } { rest } )"
0 commit comments