Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Memorizing C operator precedence Date: Sat, 30 Apr 2022 07:05:39 -0700 Organization: A noiseless patient Spider Lines: 187 Message-ID: <86v8uq4qfg.fsf@linuxsc.com> References: <20220413163721.82@kylheku.com> <86levs6c6n.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="947a81ba8a661bedfa1d3062f5b6bffa"; logging-data="27619"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/grzvylyAPD7ck9qO+SlFu/Aer4FI7F+Y=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:0PO2y1yuwFAsHKlZ2dRfqmEiFiE= sha1:X/5mG4G87WbkxeG6gMC6l2XWm8E= Xref: csiph.com comp.lang.c:166031 Alan Mackenzie writes: > Tim Rentsch wrote: [...] I should note here that I have read through your previous comments but don't have any response to them at this time. >> .... but it's already way below threshold for any reasonable >> standard of code review. > > This is simply untrue. My statement is a statement of opinion, not a statement of fact, and so it is neither true nor false -- it is just my opinion. > That piece of code has been reviewed many times, including by > me, and presents no particular difficulties in understanding. > > I don't think it could be written better. Different people have different metrics for "better". I'm interested to hear reactions from other people on this question. To give some additional context, below is the function forw_comment, taken from syntax.c in emacs 28.1, and which containts the if() expression (or something very much like it) under discussion. What do other comp.lang.c folks think of the code below? Does it present any difficulties in understanding it? Can or should it be revised to make it "better"? If yes, what kinds of changes would make it better? Any other comments are also welcome. (The excerpt below starts with a function-level comment, and after that has one C function definition. There is no indentation except what was present in the original code, and there is nothing more in the posting after the excerpt.) /* Jump over a comment, assuming we are at the beginning of one. FROM is the current position. FROM_BYTE is the bytepos corresponding to FROM. Do not move past STOP (a charpos). The comment over which we have to jump is of style STYLE (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE). NESTING should be positive to indicate the nesting at the beginning for nested comments and should be zero or negative else. ST_COMMENT_STYLE cannot be nested. PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character (or 0 if the search cannot start in the middle of a two-character). If successful, return true and store the charpos of the comment's end into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR. Else, return false and store the charpos STOP into *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the current nesting (as defined for state->incomment) in *INCOMMENT_PTR. Should the last character scanned in an incomplete comment be a possible first character of a two character construct, we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise, we store Smax into *last_syntax_ptr. The comment end is the last character of the comment rather than the character just after the comment. Global syntax data is assumed to initially be valid for FROM and remains valid for forward search starting at the returned position. */ static bool forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop, EMACS_INT nesting, int style, int prev_syntax, ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr, EMACS_INT *incomment_ptr, int *last_syntax_ptr) { unsigned short int quit_count = 0; int c, c1; enum syntaxcode code; int syntax, other_syntax; if (nesting <= 0) nesting = -1; /* Enter the loop in the middle so that we find a 2-char comment ender if we start in the middle of it. */ syntax = prev_syntax; code = syntax & 0xff; if (syntax != 0 && from < stop) goto forw_incomment; while (1) { if (from == stop) { *incomment_ptr = nesting; *charpos_ptr = from; *bytepos_ptr = from_byte; *last_syntax_ptr = (code == Sescape || code == Scharquote || SYNTAX_FLAGS_COMEND_FIRST (syntax) || (nesting > 0 && SYNTAX_FLAGS_COMSTART_FIRST (syntax))) ? syntax : Smax ; return 0; } c = FETCH_CHAR_AS_MULTIBYTE (from_byte); syntax = SYNTAX_WITH_FLAGS (c); code = syntax & 0xff; if (code == Sendcomment && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ? (nesting > 0 && --nesting == 0) : nesting < 0) && !(comment_end_can_be_escaped && char_quoted (from, from_byte))) /* We have encountered a comment end of the same style as the comment sequence which began this comment section. */ break; if (code == Scomment_fence && style == ST_COMMENT_STYLE) /* We have encountered a comment end of the same style as the comment sequence which began this comment section. */ break; if (nesting > 0 && code == Scomment && SYNTAX_FLAGS_COMMENT_NESTED (syntax) && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style) /* We have encountered a nested comment of the same style as the comment sequence which began this comment section. */ nesting++; if (comment_end_can_be_escaped && (code == Sescape || code == Scharquote)) { inc_both (&from, &from_byte); UPDATE_SYNTAX_TABLE_FORWARD (from); if (from == stop) continue; /* Failure */ } inc_both (&from, &from_byte); UPDATE_SYNTAX_TABLE_FORWARD (from); forw_incomment: if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax) && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte), other_syntax = SYNTAX_WITH_FLAGS (c1), SYNTAX_FLAGS_COMEND_SECOND (other_syntax)) && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)) ? nesting > 0 : nesting < 0)) { syntax = Smax; /* So that "|#" (lisp) can not return the syntax of "#" in *last_syntax_ptr. */ if (--nesting <= 0) /* We have encountered a comment end of the same style as the comment sequence which began this comment section. */ break; else { inc_both (&from, &from_byte); UPDATE_SYNTAX_TABLE_FORWARD (from); } } if (nesting > 0 && from < stop && SYNTAX_FLAGS_COMSTART_FIRST (syntax) && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte), other_syntax = SYNTAX_WITH_FLAGS (c1), SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)) && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))) /* We have encountered a nested comment of the same style as the comment sequence which began this comment section. */ { syntax = Smax; /* So that "#|#" isn't also a comment ender. */ inc_both (&from, &from_byte); UPDATE_SYNTAX_TABLE_FORWARD (from); nesting++; } rarely_quit (++quit_count); } *charpos_ptr = from; *bytepos_ptr = from_byte; *last_syntax_ptr = Smax; /* Any syntactic power the last byte had is used up. */ return 1; }