Skip to content

A negative or integer literal used as a call receiver is not parenthesised: -2.5.abs() negates the result, and 5.toString() is a syntax error #100

Description

@matt-edmondson

What's wrong

Coder/Languages/LanguageGeneratorBase.cs:559-563: GenerateCallExpression writes GenerateInternal(callExpr.Receiver) followed by . and never wraps the receiver.

PR #92 added the LeadingSign handling (around line 699), but it only covers the unary-minus operand. A signed literal used as a receiver still loses its grouping.

Failure scenario

Signed literal receiver

new CallExpression(Literal.DecimalValue(-2.5), "__abs__") (or "abs") generates -2.5.abs(). C#, C++, Go, Rust, Python and JavaScript all do this, and C# writes -2.5d.abs().

Unary minus binds looser than member access, so this means -(2.5.abs()). In Python, -2.5.__abs__() evaluates to -2.5, while (-2.5).__abs__() gives 2.5. The generated code runs and returns the wrong value, with no error.

Integer literal receiver

new CallExpression(Literal.Number(5), "toString") generates 5.toString(). The output doesn't parse:

  • Node: SyntaxError: Invalid or unexpected token
  • Python: SyntaxError: invalid decimal literal

-5.toString() is also a SyntaxError in Node.

I reproduced these by running the built ktsu.Coder.dll and executing its output with python3 and node.

Suggested fix / acceptance criteria

  • In GenerateCallExpression, parenthesise a receiver that is a literal with a leading sign. The existing LeadingSign helper can do this.
  • Also parenthesise a bare integer literal receiver, at least for Python and JavaScript.
  • Add generator tests:
    • (-2.5).abs() for every language
    • (5).toString() for JavaScript and Python

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreadyFully specified; implement as written

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions