Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created February 16, 2014 15:09
Show Gist options
  • Select an option

  • Save pasberth/9035794 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/9035794 to your computer and use it in GitHub Desktop.
sweet.js でのアレ
sjs --module ./mod.js --output fn.built.js fn.js
sjs --module ./mod.js --output fn2.built.js fn2.js
sjs --module ./mod.js --output fn3.built.js fn3.js
fn x y { x };
square = fn x { x * x };
cube = fn x { square(x) * x };
fill = fn container [liquid = "coffee"] {
"Filling the #{container} with #{liquid}..."
};
macro setdefault {
rule { $var = $init } => { if ($var == null) { $var = $init } }
}
macro setdefaults {
rule { { } } => {}
rule { { $var = $init $repeat ... } } => { setdefault $var = $init ; setdefaults { $repeat ... } }
}
macro setdefaults1 {
rule { { = } } => {}
rule { { $var $vars ... = $init $inits ... } } => { setdefault $var = $init ; setdefaults1 { $vars ... = $inits ... } }
}
macro fn {
rule { $params ... [$($option = $init) ...] { $body ... } } => { (function ($params ... $option ...) { setdefaults1 { $option ... = $init ... } ; return ($body ...) }) }
rule { $params ... { $body ... } } => { (function ($params ...) { return ($body ...) }) }
}
macro when {
rule {
$cond { $body ... }
} => {
{
if ($cond) { $body ... }
}
}
}
export fn;
export when;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment