Pick operator

Tim Disney tim.disney at gmail.com
Sat Jun 20 07:19:57 UTC 2015


On Fri, Jun 19, 2015 at 9:30 PM, Bob Myers <rtm at gol.com> wrote:

> I suspect this is beyond the capabilities of sweet, but haven't really
> tried.



I think it's doable with sweet. You need to use infix macros which are a
bit more complicated than standard macros though.

Here's a first stab.

```
macro # {
    // let p #= o
    rule infix { let $prop | = $obj:expr } => {
        let $prop = $obj.$prop
    }
    // p = 42 # o
    rule infix { $prop = $val:expr | $obj:expr } => {
        ($obj.$prop || $val)
    }
    // [ p1, p2 ] # o
    rule infix { [ $prop (,) ... ] | $obj:expr } => {
        [ $($obj.$prop) (,) ...]
    }
    // { p1, p2 } # o
    rule infix { { $prop (,) ... } | $obj:expr } => {
        {
        $($prop : $obj.$prop) (,) ...
        }
    }
    // p # if o
    rule infix { $prop:ident | if $obj:expr } => {
        $obj && typeof $obj === "object" ? $obj.$prop : void 0
    }
    // q # (p # o)
    rule infix { $prop:ident | $obj:expr } => { $obj.$prop }
}

macro @ {
    // { p1, p2 } @ [1, 2]
    rule infix { { $props (,) ... } | [ $el:expr (,) ... ] } => {
        {
            $($props : $el) (,) ...
        }
    }
}
```

This dosn't handle `{ p1, p22 # p2 } # o` but I think that totally possible
to handle.


-- 
-Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150620/26baebb4/attachment-0001.html>


More information about the es-discuss mailing list