binjs-ref icon indicating copy to clipboard operation
binjs-ref copied to clipboard

Representation of negative integer literals in the v1 format is odd

Open vdjeric opened this issue 7 years ago • 6 comments

Unary expression to negate seems very inefficient

vdjeric avatar Dec 12 '18 02:12 vdjeric

This kind of sounds like a parser error. Normally, -1 should be represented as a negative float in multipart (and as a 8 bit integer in entropy). Do you have a minimal example to reproduce this?

Yoric avatar Dec 12 '18 10:12 Yoric

yes, it's from parser. not sure if this is "error" tho.

$ node
> console.log(JSON.stringify(require('shift-parser').parseScript("-1"), undefined, 2))
{
  "type": "Script",
  "directives": [],
  "statements": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "UnaryExpression",
        "operator": "-",
        "operand": {
          "type": "LiteralNumericExpression",
          "value": 1
        }
      }
    }
  ]
}

we can apply some filter in shift.rs or somewhere.

arai-a avatar Dec 17 '18 11:12 arai-a

maybe we could solve this at the same time as https://github.com/binast/binjs-ref/issues/229 ?

arai-a avatar Dec 17 '18 11:12 arai-a

FWIW this is a standard representation of negative numbers in JS parsers, as syntax spec technically doesn't define negative numbers. I agree it can be more efficient in custom formats like BinJS, but it's not really odd.

RReverser avatar Dec 17 '18 20:12 RReverser

Actually disregard the deleted comment, I didn't see that Shift already converts any keys to strings.

RReverser avatar Dec 17 '18 20:12 RReverser

Would anyone be interested in rewriting negative number translation to get rid of such oddities?

@vdjeric maybe?

Yoric avatar Jan 29 '19 17:01 Yoric