3

From the previous question I learned how to extend JavaScript language to support more operators (created by me).

There @Benjamin used Esprima and created # operator. Using Esprima we can do the following:

esprima.parse("10 # 2")

That returns this object:

{
    "type": "Program",
    "body": [
        {
            "type": "ExpressionStatement",
            "expression": {
                "type": "BinaryExpression",
                "operator": "#",
                "left": {
                    "type": "Literal",
                    "value": 10,
                    "raw": "10"
                },
                "right": {
                    "type": "Literal",
                    "value": 2,
                    "raw": "2"
                }
            }
        }
    ]
}

But if I replace # with it throws this error:

Error: Line 1: Unexpected token ILLEGAL

Why is # supported and not? Would it be possible to support unicode characters when parsing a string like "2 ∘ 3"?

Is there any way to force Esprima to accept unicode characters?

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 3
    This is much more about esprima than it is about JavaScript. You **can't** add operators to JavaScript itself. – T.J. Crowder Jan 20 '14 at 16:33
  • @T.J.Crowder I hope that not all people will say the same. :-) – Ionică Bizău Jan 20 '14 at 16:48
  • @IonicăBizău are you sure you configured Esprima correctly? Note, in that question I didn't work with Esprima I __changed Esprima's source code__ in order to accept "#" as part of the grammar. – Benjamin Gruenbaum Jan 20 '14 at 17:42
  • Also, very recently Sweet.js added support for infix operators too, you might find that easier than using Esprima. – Benjamin Gruenbaum Jan 20 '14 at 17:44
  • 1
    @BenjaminGruenbaum Ah, I didn't notice that... Can you add a good answer, again? :-) – Ionică Bizău Jan 20 '14 at 17:47
  • @IonicăBizău I'm sick (like 39.4C with 5 different prescription drugs sick) right now so whatever I'll write will come out as a blabbering mess. If you don't get an answer until I'm better off I'll gladly post one. – Benjamin Gruenbaum Jan 20 '14 at 17:51
  • @BenjaminGruenbaum Hope you will be well soon! Thank you for noticing me that you edited the source. I will take a look there. – Ionică Bizău Jan 20 '14 at 17:52
  • 1
    @T.J.Crowder Yet my friend, you can't add operators to JS itself _yet_ :) – Benjamin Gruenbaum Jan 20 '14 at 17:53
  • @BenjaminGruenbaum: You know something I don't? (Well, of course you do, but specifically...?) I think the bar to adding operator overloading or similar in JS is going to be *really* high. It took a huge amount of discussion to sort out the prototype operator and the arrow function syntax... – T.J. Crowder Jan 21 '14 at 07:54
  • 1
    IonicăBizău: You don't need Ben for this (get better soon @BenjaminGruenbaum!). He's already shown you *exactly* what to do in his truly [comprehensive answer](http://stackoverflow.com/a/20764137/157247) earlier. Just follow the steps again using your desired operator. – T.J. Crowder Jan 21 '14 at 07:57
  • @T.J.Crowder Yeah, I didn't notice that Ben modified the Esprima code. I also [modified it to accept unicode characters](https://github.com/IonicaBizau/JavaScript-custom-operators/commit/3766ee3264430957d04a4e07fc5d4ae3030d8afa) as operators. And hope you like [**the result**](https://github.com/IonicaBizau/JavaScript-custom-operators). :-) – Ionică Bizău Jan 21 '14 at 12:36
  • @IonicăBizău: Cool. You should post that as an answer (and then accept it in a day or so). – T.J. Crowder Jan 21 '14 at 12:47
  • @T.J.Crowder Operator overloading is planned for ES7 with value types. Adding new operators is not planned at the moment though :) – Benjamin Gruenbaum Jan 21 '14 at 13:41

0 Answers0