suggestion: mapping symbolic infix ops to binary functions

Claus Reinke claus.reinke at talk21.com
Sat Jul 21 14:43:05 PDT 2012


Hi again,

my thread starter email seems to have disappeared (see below), 
perhaps this reply will get through. The topic is still user-defined
infix operations: in the short term, to experiment with proposed 
language extensions, in the long term, to shift this topic from tc39
to library authors.

I've now created a gist with an implementation sketch. It uses a 
modified esprima to parse infix operations, translating to Operator-
applications on the fly, then uses escodegen to generate source 
from the AST, and optionally evaluates the result (using a little 
node wrapper):

    map symbolic infix ops '(x .<op> y)' to 'Operation[".<op>"](x,y)'
    https://gist.github.com/3157251

Missing features include precedence and associativity (in this sketch, 
all infix ops are left-associative between 'logical or' and 'conditional 
expression'; also, all infix ops start with a '.', followed by a sequence 
of '.<>=!+-*%&|^/~?:#'). Still, what is left is sufficient to play with 
the examples, as shown in this sample source file

    https://gist.github.com/3157251#file_sample.js

    $ git clone git://gist.github.com/3157251.git
    $ cd 3157251/
    $ node infix.js sample.js --eval
    hi
    my name is obj
    undefined
    { x: 1, y: 0 }

Hope you like it,
Claus

--------------------------------------------------
From: "Claus Reinke" <..>
Sent: Wednesday, July 11, 2012 10:01 PM
To: <es-discuss at ..>
Subject: suggestion: mapping symbolic infix ops to binary functions

> The discussion of re-interpreting arrows in method contexts
> reminded me of an older idea: if we had user-defined infix
> operators in JS, then such features could be field-tested easily,
> without involving tc39.
> 
> ---------
> Basic idea (in a full spec, this would have to be fitted in the 
> existing binary operator precedence stack) - syntax: 
> 
>    SymbolicOperator :
>        any sequence of symbolic chars not valid in ES5
> 
>    BinaryOperatorExpression :
>        Expression SymbolicOperator Expression
> 
> Semantics: assuming a single predefined 'Operator' object, 
> extensible with function properties for each symbolic infix 
> operator, the phrase
> 
>    e1 <op> e2
> 
> would be rewritten (desugared) to
> 
>    Operator["<op>"](e1,e2)
> 
> ---------
> Example uses:
> 
> 1. to play with Irakli's idea, we could define
> 
>    Operator["#"] = 
>        function(obj,arr) { return arr(obj) };
> 
> to get a version of object property selection that provides
> its function argument with a this reference, so that
> 
>    { x: "hi" } # (obj => obj.x)    // would return "hi"
> 
> 2. to play with a simple variant of '<|', we could define
> 
>    Operator['<|'] = 
>        function(proto,obj){ obj.__proto__ = proto; return obj }
> 
> so that
> 
>    proto <| obj    // would set obj's __proto__ property
> 
> 3. to get a version of object property selection that does
> not fail over when subpaths don't exist, we could define
> 
>    Operator['.?'] = 
>        function(obj,prop){ return  ( obj || {} )[prop] }
> 
> so that
> 
>    (undefined .? "a") .? "b"    // would return undefined
> 
> [note that this example does not have an object as left operand,
> hence the use of 'Operator' in operator desugaring]
> 
> 4. to get a chainable version of object extension, we could define
> 
>    Operator['##'] =
>        function(obj,ext) { 
>            Object.keys(ext).forEach(function(k) { obj[k]=ext[k] });
>            return obj
>        }
> 
> so that
> 
>    ({} ## { x: 1 }) ## { y: 0 }     // would return { x: 1, y: 0}
> 
> and so on..
> 
> Any takers?-)
> 
> Claus
> http://clausreinke.github.com/
>

 


More information about the es-discuss mailing list