Last active
September 13, 2015 15:54
-
-
Save weapp/9135e1c31aefbcabcc0f to your computer and use it in GitHub Desktop.
Macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/mateuspv/45bfb3e10a84e34b0074 | |
/** | |
Sweet.js version: 0.7.2 | |
`this` nickname `@` | |
Expected: | |
@ -> this | |
@something -> this.something | |
example: | |
function hello (tag) { | |
var element = @document.querySelectorAll(tag); | |
console.log(element, @); | |
Object.prototype.toString.call(@, 5); | |
return @; | |
} | |
*/ | |
macro @ { | |
rule { ; } => { this; } | |
rule { , } => { this, } | |
rule { [$exp:expr] } => { this[$exp] } | |
rule { $exp } => { this.$exp } | |
rule { } => { this } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro :: { | |
rule infix { | |
$lhs:ident | [$rhs:expr] | |
} => { | |
$lhs.prototype[$rhs] | |
} | |
rule infix { | |
$lhs:expr | $rhs:expr | |
} => { | |
$lhs.prototype.$rhs | |
} | |
} | |
export ::; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/metaraine/fae4f7d3496c92714c23 | |
// Sweet.js v0.7.4 | |
// $ npm install --save sweet-compose | |
macro compose { | |
rule infix { | |
$outer:expr | $inner:expr | |
} => { | |
function() { | |
return $outer($inner.apply(this, arguments)); | |
} | |
} | |
} | |
function add(x,y) { return x+y; } | |
function double(x) { return x*2; } | |
alert((double compose add)(2,3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//require https://github.com/ubolonton/js-csp | |
macro (<-) { | |
rule infix { $id:ident | $op:expr } | |
=> { | |
if (typeof $op === 'Channel') | |
{ $id = yield take($op); if ($id === null) { return ;} } | |
else | |
{ yield put($id, $op); } | |
} | |
rule { $id } => { $id.close() } | |
} | |
macro (await){ | |
rule { $lit:lit } => { yield timeout($lit)} | |
rule {} => { yield } | |
} | |
function* player(name, table) { | |
while (true) { | |
var ball; | |
ball <- table | |
ball.hits += 1; | |
console.log(name + " " + ball.hits); | |
await 100; | |
table <- ball; | |
} | |
} | |
go(function* () { | |
var table = chan(); | |
go(player, ["ping", table]); | |
go(player, ["pong", table]); | |
table <- {hits: 0}; | |
await 1000; | |
<- table; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro (<-) { | |
rule infix { $res:ident | $ch:expr } | |
=> { $res = await $ch.take(); if ($res === null) { return ;} } | |
rule { $ch } => { $ch.close() } | |
} | |
macro (<<) { | |
rule infix { $ch:ident | $op:expr } => { await $ch.put($op); } | |
} | |
macro (await){ | |
rule {} => { yield } | |
} | |
function* player(name, table) { | |
while (true) { | |
var ball; | |
ball <- table; | |
ball.hits += 1; | |
console.log(name + " " + ball.hits); | |
await csp.timeout(1000); | |
table << ball; | |
} | |
} | |
go(function* () { | |
var table = csp.chan(); | |
go(player, ["ping", table]); | |
go(player, ["pong", table]); | |
table << {hits: 0}; | |
await csp.timeout(1000); | |
<- table; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/jugglinmike/8551293 | |
// Use suite.js to define a JavaScript macro for a "not in" operator | |
// http://sweetjs.org/ | |
macro nin { | |
rule infix { | |
$key:expr | $obj:expr | |
} => { | |
!($key in $obj) | |
} | |
} | |
// Now, | |
if (key nin obj) { | |
return null; | |
} | |
// ...compiles to: | |
if (!(key in obj)) { | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/natefaubion/c70704618bb618cc636c | |
operator (|>) 14 left { $lhs, $rhs } => #{ thread $lhs $rhs } | |
export (|>); | |
macro thread { | |
rule { $lhs ($method:part()) } => { | |
$method($lhs) | |
} | |
rule { $lhs ($method:part($args:expr (,) ...)) } => { | |
$method($args (,) ..., $lhs) | |
} | |
case { _ _ ($tok ...) } => { | |
throwSyntaxError('|>', 'Expected function call', #{ $tok ... }) | |
} | |
} | |
// We can be a little loose with our parsing since `operator` guarantees | |
// that we have a fully expanded, valid expression. | |
macro part { | |
rule { ($e ...) . $next:part } | |
rule { ($e ...) } | |
rule { $name:ident[$e ...] . $next:part } | |
rule { $name:ident($e ...) . $next:part } | |
rule { $name:ident . $next:part } | |
rule { $name:ident[$e ...] } | |
rule { $name:ident } | |
rule { new $ctr:part } | |
} | |
/* | |
arg |> foo[0].bar(42) | |
|> baz("hello", "there") | |
|> quux.foo().bar() | |
|> new Foo() | |
// Expands to: | |
// new Foo(quux.foo().bar(baz('hello', 'there', foo[0].bar(42, arg)))); | |
arg |> foo() |> bar | |
// SyntaxError: [|>] Expected function call | |
// 31: arg |> foo() |> bar | |
// ^ | |
macro test { | |
rule { } => { to_fn_call(42) } | |
} | |
arg |> foo() |> test | |
// Expands to: | |
// to_fn_call(42, foo(arg)); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment