Avatar for user135362


Member since Oct 2021 • Last active Oct 2021
  • 3 conversations

Most recent activity

  • in JavaScript
    Avatar for user135362

    I don't quite understand the question. The comma operator is a normal operator valid in any full Expression (13.16 ). It is historically descended from Algol68's semicolon operator, and (despite the different precedence) semantically related to JS's && operator. Like && it processes the left argument and returns the right; unlike && it voids the left argument and proceeds unconditionally. In fact, I suspect a, b could be defined (semantically) as !a < 2 && b or some such stupidity.

    Syntactically it has only one oddity, which is that it, like the bare assignment operator, is also used for other things, such as forming VariableDeclarationLists (14.3.2) and ArgumentLists (13.3), which means that there are places where the semantic expectation is an expression but the syntax restricts you to an AssignmentExpression to resolve the ambiguity of which kind of comma is expected.

    In any case, none of this has anything to do with the conditions in control constructs, which are uniformly and uncomplicatedly Expressions (14.6,,, 14.7.4, 14.7.5, 14.12).

    There's also no case to be made that it is a semantic restriction of Espruino or its debugging mechanism, since the semantically identical while ((0, 0)); parses and executes flawlessly. This is nothing but an issue with Espruino's internal grammar, as far as I can see, either a typo (I hope) or an utterly capricious decision to alter the syntax of while () while not even making the same change for if ().

    To the argument that this feature of the language is not pretty and adds confusion, all I can say is that today it was recommended to me that I replace
    search: while (…) while (…) if (…) break search; (which Espruino does not implement) with
    (() => {while (…) while (…) if (…) return;})();. It does not appear that prettiness and lack of visual confusion is the driving force behind this project(!). And, for goodness' sake, if you really hate the comma operator so passionately, why allow it in if and switch?

    Orthogonality and lack of bugs are generally considered virtues in the programming language community; I'm honestly not sure what's going on here. If it's that efficiency trumps usability, then what's the point of Espruino at all? C, or indeed assembly, is easy enough to write; I came here in the expectation that things would “just work” without too much hassle :-}.

  • in JavaScript
    Avatar for user135362

    Yep, that's a definite option, thanks—does Espruino optimise parameterless functions called in place into low-overhead control flow? Is treating return as a value-carrying nonlocal exit the expected idiom?

    ((()=>{ … return;})()); is unquestionably uglier than foo: … break foo; though! :) )

  • in JavaScript
    Avatar for user135362

    Well, but here's the thing: how can a person write code for this beast if it doesn't follow the spec and doesn't provide any guidance for where it is going to diverge? It's not helping anyone if we have to write and constantly refer to a cheat sheet of arbitrary divergences from even the simplest parts of the language design.

    Don't get me wrong, I completely understand the idea of a motivated language subset, but arbitrarily outlawing a => (b) => x while allowing (a) => b => x, or forbidding while (a = b, c) while allowing if (a = b, c), what's that even for? Either these are bugs (and, with several language implementations under my belt, they seem more likely to be typos in the parser source than anything else), or some detailed documentation is needed so that users can know what is going to work and what is not and develop an intuition for why.

    As to why I'd appeal to the ECMA spec, it's not demented legalism on my part, it's that I've already noticed that people get yelled at if they don't. Indeed, it happened to me today—I'd assumed that everyone knew what the comma operator does in C-family languages, but apparently chapter and verse is required.

  • in JavaScript
    Avatar for user135362

    @Robin, I am attempting to report a parsing error, not a semantic error, and “while (0,0);” is a minimal reproduction. It is not intended to be a full, useful program; just the shortest and simplest snippet I can concoct that triggers the error, to assist with debugging. I did also explain what real examples would look like and why I care.

    I apologise for not including the link https://tc39.es/ecma262/multipage/ecmasc­ript-language-statements-and-declaration­s.html#sec-while-statement ; I truly didn't think it was necessary, because I thought everyone here would know what the comma operator is and does. Mea culpa.

    In any case, the standard, at that link, says this:

    WhileStatement[Yield, Await, Return] :
    while ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]

    (and similarly for for statements, which are also affected by the bug).

    I don't really understand your point about truthiness. “x = costlyAccessor(i++), p(x.a, x.b)” returns the value of p(…), not the value of x. Per https://tc39.es/ecma262/multipage/ecmasc­ript-language-expressions.html#sec-comma­-operator-runtime-semantics-evaluation :

    13.16.1 Runtime Semantics: Evaluation
    Expression : Expression , AssignmentExpression

    1. Let lref be the result of evaluating Expression.
    2. Perform ? GetValue(lref).
    3. Let rref be the result of evaluating AssignmentExpression.
    4. Return ? GetValue(rref).

    In short, it does the same thing as in every other language in the C family, evaluating the value on the left, then evaluating and returning the value on the right. The use in loop conditions is generally consider to be the principal motivation for the existence of the operator, since elsewhere you can usually accomplish the same thing with some unpacking and a semicolon. Admittedly it is more commonly used in for loops than in while loops, but again, I was seeking the easiest example to debug the grammar on.

  • in JavaScript
    Avatar for user135362

    Per the standard, the condition in loops is an Expression, but Espruino evidently parses it as AssignmentExpression, disallowing the comma operator. This is particularly painful contexts like

    while (x = costlyAccessor(i++), p(x.a, x.b)) …;

    Minimal reproduction:

    Uncaught SyntaxError: Got ',' expected ')'
     at line 1 col 8
    Uncaught SyntaxError: Got ',' expected ';'
     at line 1 col 7
    Uncaught SyntaxError: Got ',' expected ')'
     at line 1 col 11

    Oddly, if and switch have no such problem.

  • in JavaScript
    Avatar for user135362

    This may be a flaw in the Espruino documentation, then—it says there that the reason labels are not implemented is that they are bad practice, not because of the implementation strategy.

    My particular use case is of this form:
    search: for (…) for (…) if(…) break search; // found it!

    One could of course throw an exception, use “continue” variables, or any of a number of other solutions; they're all to varying degrees confusing or inefficient, and I just wondered what the received idiom is.

  • in JavaScript
    Avatar for user135362

    Espruino apparently does not implement labels because “they're generally considered bad practice”. Really? Goto is (still) broadly considered harmful, but JS does not provide goto; its labels are used for multilevel breaks (and continues), a feature that repairs the relative uselessness and occasional opacity of C's implicitly targeted break statements. It seems a little weird to implement break (which has also been argued against by control flow purists, though I think received opinion by now is that they were arguing more out of a desire to simplify their control flow analysis algorithms than any true readability problem) and not label.

    Anyway, I don't really mean to rant about language design philosophy, especially over a feature that I wouldn't notice was missing if it hadn't already been in the spec, but what's the recommended alternative on this platform?