diff src/pt-pr-code.cc @ 3665:0689afb1d001

[project @ 2000-05-11 19:07:56 by jwe]
author jwe
date Thu, 11 May 2000 19:10:09 +0000
parents b80bbb43a1a9
children 9b9efdcbdfd3
line wrap: on
line diff
--- a/src/pt-pr-code.cc	Mon May 01 19:35:22 2000 +0000
+++ b/src/pt-pr-code.cc	Thu May 11 19:10:09 2000 +0000
@@ -28,8 +28,11 @@
 #include <config.h>
 #endif
 
+#include <cctype>
+
 #include <iostream>
 
+#include "comment-list.h"
 #include "error.h"
 #include "ov-usr-fcn.h"
 #include "pr-output.h"
@@ -182,6 +185,8 @@
 void
 tree_print_code::visit_simple_for_command (tree_simple_for_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "for ";
@@ -205,10 +210,14 @@
   if (list)
     {
       increment_indent_level ();
+
       list->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "endfor";
@@ -217,6 +226,8 @@
 void
 tree_print_code::visit_complex_for_command (tree_complex_for_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "for [";
@@ -240,10 +251,14 @@
   if (list)
     {
       increment_indent_level ();
+
       list->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "endfor";
@@ -261,7 +276,9 @@
   if (cmd_list)
     {
       increment_indent_level ();
+
       cmd_list->accept (*this);
+
       decrement_indent_level ();
     }
 
@@ -271,6 +288,14 @@
 void
 tree_print_code::visit_octave_user_function_header (octave_user_function& fcn)
 {
+  octave_comment_list *leading_comment = fcn.leading_comment ();
+
+  if (leading_comment)
+    {
+      print_comment_list (leading_comment);
+      newline ();
+    }
+
   indent ();
 
   os << "function ";
@@ -341,8 +366,10 @@
 }
 
 void
-tree_print_code::visit_octave_user_function_trailer (octave_user_function&)
+tree_print_code::visit_octave_user_function_trailer (octave_user_function& fcn)
 {
+  print_indented_comment (fcn.trailing_comment ());
+
   indent ();
 
   os << "endfunction";
@@ -373,12 +400,12 @@
 
   newline ();
 
-  increment_indent_level ();
-
   tree_statement_list *list = cmd.commands ();
 
   if (list)
     {
+      increment_indent_level ();
+
       list->accept (*this);
 
       decrement_indent_level ();
@@ -388,6 +415,8 @@
 void
 tree_print_code::visit_if_command (tree_if_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "if ";
@@ -397,6 +426,8 @@
   if (list)
     list->accept (*this);
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "endif";
@@ -417,6 +448,8 @@
 	{
 	  if (! first_elt)
 	    {
+	      print_indented_comment (elt->leading_comment ());
+
 	      indent ();
 
 	      if (elt->is_else_clause ())
@@ -440,10 +473,16 @@
 
   print_parens (expr, "(");
 
+  bool expr_has_parens = false;
+
   tree_expression *e = expr.expression ();
 
   if (e)
-    e->accept (*this);
+    {
+      e->accept (*this);
+
+      expr_has_parens = e->is_postfix_indexed ();
+    }
 
   tree_argument_list *list = expr.arg_list ();
 
@@ -453,6 +492,8 @@
       list->accept (*this);
       os << ")";
     }
+  else if (expr_has_parens)
+    os << " ()";
 
   print_parens (expr, ")");
 }
@@ -773,6 +814,8 @@
 void
 tree_print_code::visit_statement (tree_statement& stmt)
 {
+  print_comment_list (stmt.comment_text ());
+
   tree_command *cmd = stmt.command ();
 
   if (cmd)
@@ -928,6 +971,8 @@
 void
 tree_print_code::visit_switch_case (tree_switch_case& cs)
 {
+  print_comment_list (cs.leading_comment ());
+
   indent ();
 
   if (cs.is_default_case ())
@@ -942,14 +987,16 @@
 
   newline ();
 
-  increment_indent_level ();
-
   tree_statement_list *list = cs.commands ();
 
   if (list)
     {
+      increment_indent_level ();
+
       list->accept (*this);
 
+      newline ();
+
       decrement_indent_level ();
     }
 }
@@ -973,6 +1020,8 @@
 void
 tree_print_code::visit_switch_command (tree_switch_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "switch ";
@@ -984,12 +1033,18 @@
 
   newline ();
 
-  increment_indent_level ();
-
   tree_switch_case_list *list = cmd.case_list ();
 
   if (list)
-    list->accept (*this);
+    {
+      increment_indent_level ();
+
+      list->accept (*this);
+
+      decrement_indent_level ();
+    }
+
+  print_indented_comment (cmd.leading_comment ());
 
   indent ();
 
@@ -999,9 +1054,11 @@
 void
 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
-  os << "try_catch";
+  os << "try";
 
   newline ();
 
@@ -1010,10 +1067,14 @@
   if (try_code)
     {
       increment_indent_level ();
+
       try_code->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.middle_comment ());
+
   indent ();
 
   os << "catch";
@@ -1025,10 +1086,14 @@
   if (catch_code)
     {
       increment_indent_level ();
+
       catch_code->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "end_try_catch";
@@ -1038,6 +1103,8 @@
 tree_print_code::visit_unwind_protect_command
   (tree_unwind_protect_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "unwind_protect";
@@ -1049,10 +1116,14 @@
   if (unwind_protect_code)
     {
       increment_indent_level ();
+
       unwind_protect_code->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.middle_comment ());
+
   indent ();
 
   os << "unwind_protect_cleanup";
@@ -1064,10 +1135,14 @@
   if (cleanup_code)
     {
       increment_indent_level ();
+
       cleanup_code->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "end_unwind_protect";
@@ -1076,6 +1151,8 @@
 void
 tree_print_code::visit_while_command (tree_while_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "while ";
@@ -1092,10 +1169,14 @@
   if (list)
     {
       increment_indent_level ();
+
       list->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "endwhile";
@@ -1104,6 +1185,8 @@
 void
 tree_print_code::visit_do_until_command (tree_do_until_command& cmd)
 {
+  print_comment_list (cmd.leading_comment ());
+
   indent ();
 
   os << "do";
@@ -1115,10 +1198,14 @@
   if (list)
     {
       increment_indent_level ();
+
       list->accept (*this);
+
       decrement_indent_level ();
     }
 
+  print_indented_comment (cmd.trailing_comment ());
+
   indent ();
 
   os << "until";
@@ -1186,6 +1273,91 @@
     os << txt;
 }
 
+void
+tree_print_code::print_comment_elt (const octave_comment_elt& elt)
+{
+  bool printed_something = false;
+
+  bool prev_char_was_newline = false;
+
+  string comment = elt.text ();
+
+  size_t len = comment.length ();
+
+  size_t i = 0;
+
+  while (i < len && comment[i++] == '\n')
+    ; /* Skip leading new lines. */
+  i--;
+
+  while (i < len)
+    {
+      char c = comment[i++];
+
+      if (c == '\n')
+	{
+	  if (prev_char_was_newline)
+	    os << "##";
+
+	  newline ();
+
+	  prev_char_was_newline = true;
+	}
+      else
+	{
+	  if (beginning_of_line)
+	    {
+	      printed_something = true;
+
+	      indent ();
+
+	      os << "##";
+
+	      if (! (isspace (c) || c == '!'))
+		os << " ";
+	    }
+
+	  os << (char) c;
+
+	  prev_char_was_newline = false;
+	}
+    }
+
+  if (printed_something && ! beginning_of_line)
+    newline ();
+}
+
+void
+tree_print_code::print_comment_list (octave_comment_list *comment_list)
+{
+  if (comment_list)
+    {
+      Pix p = comment_list->first ();
+
+      while (p)
+	{
+	  octave_comment_elt elt = comment_list->operator () (p);
+
+	  print_comment_elt (elt);
+
+	  comment_list->next (p);
+
+	  if (p)
+	    newline ();
+	}
+    }
+}
+
+void
+tree_print_code::print_indented_comment (octave_comment_list *comment_list)
+{
+  increment_indent_level ();
+
+  print_comment_list (comment_list);
+
+  decrement_indent_level ();
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***