Mercurial > hg > octave-shane
changeset 2961:8a77d52c9d02
[project @ 1997-05-12 01:36:04 by jwe]
author | jwe |
---|---|
date | Mon, 12 May 1997 01:36:24 +0000 |
parents | 194b50e4725b |
children | 5e0fe4c5d52f |
files | src/parse.y src/pt-exp-base.h src/pt-pr-code.cc src/pt-pr-code.h |
diffstat | 4 files changed, 53 insertions(+), 73 deletions(-) [+] |
line wrap: on
line diff
--- a/src/parse.y +++ b/src/parse.y @@ -1427,7 +1427,7 @@ { if (Vwarn_assign_as_truth_value && expr->is_assignment_expression () - && expr->is_in_parens () < 2) + && expr->paren_count () < 2) { warning ("suggest parenthesis around assignment used as truth value"); }
--- a/src/pt-exp-base.h +++ b/src/pt-exp-base.h @@ -42,7 +42,7 @@ public: tree_expression (int l = -1, int c = -1) - : tree (l, c), in_parens (0) { } + : tree (l, c), num_parens (0) { } virtual ~tree_expression (void) { } @@ -72,7 +72,8 @@ virtual bool is_logically_true (const char *); - virtual bool is_in_parens (void) { return in_parens; } + virtual int paren_count (void) const + { return num_parens; } virtual void mark_for_possible_ans_assign (void); @@ -80,16 +81,26 @@ virtual octave_variable_reference reference (void); - virtual string oper (void) const { return "<unknown>"; } + virtual string oper (void) const + { return "<unknown>"; } virtual string original_text (void) const; - expression *mark_in_parens (void) { in_parens++; return this; } + tree_expression *mark_in_parens (void) + { + num_parens++; + return this; + } protected: - // Nonzero if this expression appears inside parentheses. - int in_parens; + // A count of the number of times this expression appears directly + // inside a set of parentheses. + // + // (((e1)) + e2) ==> 2 for expression e1 + // ==> 1 for expression ((e1)) + e2 + // ==> 0 for expression e2 + int num_parens; }; #endif
--- a/src/pt-pr-code.cc +++ b/src/pt-pr-code.cc @@ -70,10 +70,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); tree_expression *op1 = expr.lhs (); @@ -87,8 +84,7 @@ if (op2) op2->accept (*this); - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -104,10 +100,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); tree_expression *op1 = expr.base (); @@ -132,8 +125,7 @@ op2->accept (*this); } - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -333,16 +325,12 @@ { indent (); - bool in_parens = id.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (id, "("); string nm = id.name (); os << (nm.empty () ? string ("(empty)") : nm); - if (in_parens) - os << ")"; + print_parens (id, ")"); } void @@ -420,10 +408,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); tree_indirect_ref *id = expr.ident (); @@ -439,8 +424,7 @@ os << ")"; } - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -448,18 +432,14 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); // The name of the indirect ref includes the sub-elements. string nm = expr.name (); os << (nm.empty () ? string ("(empty)") : nm); - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -467,10 +447,7 @@ { indent (); - bool in_parens = lst.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (lst, "("); os << "["; @@ -493,8 +470,7 @@ os << "]"; - if (in_parens) - os << ")"; + print_parens (lst, ")"); } void @@ -524,10 +500,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); tree_return_list *lhs = expr.left_hand_side (); @@ -551,8 +524,7 @@ if (rhs) rhs->accept (*this); - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -574,15 +546,11 @@ { indent (); - bool in_parens = val.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (val, "("); val.print_raw (os, true, print_original_text); - if (in_parens) - os << ")"; + print_parens (val, ")"); } void @@ -687,10 +655,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); tree_expression *e = expr.operand (); @@ -699,8 +664,7 @@ os << expr.oper (); - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -708,10 +672,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); os << expr.oper (); @@ -720,8 +681,7 @@ if (e) e->accept (*this); - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -759,10 +719,7 @@ { indent (); - bool in_parens = expr.is_in_parens (); - - if (in_parens) - os << "("; + print_parens (expr, "("); if (! expr.is_ans_assign ()) { @@ -788,8 +745,7 @@ if (rhs) rhs->accept (*this); - if (in_parens) - os << ")"; + print_parens (expr, ")"); } void @@ -1158,6 +1114,15 @@ curr_print_indent_level = 0; } +void +tree_print_code::print_parens (const tree_expression& expr, const char *txt) +{ + int n = expr.paren_count (); + + for (int i = 0; i < n; i++) + os << txt; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/pt-pr-code.h +++ b/src/pt-pr-code.h @@ -31,6 +31,8 @@ #include "pt-walk.h" +class tree_expression; + // How to print the code that the parse trees represent. class @@ -160,6 +162,8 @@ void reset (void); + void print_parens (const tree_expression& expr, const char *txt); + // Must create with an output stream! tree_print_code (void);