Skip to content

Instantly share code, notes, and snippets.

@tuchida
Created February 20, 2013 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuchida/4992852 to your computer and use it in GitHub Desktop.
Save tuchida/4992852 to your computer and use it in GitHub Desktop.
macro(sweet.js) for asm.js
macro asmfn {
case $name:ident ($params ...) { $body ... } => {
function $name(asmfn_params($params ...)) {
'use asm';
asmfn_convparams($params ...)
$body ...
}
}
// case $name:ident ($params ...): $ret_type { $body ... } => {
// macro ret {
// case $ret:expr => {
// return asmfn_type(($ret), $ret_type)
// }
// }
// }
}
macro asmfn_params {
// TODO : joinSyntax
case ,($params ...) => {
asmfn_params($params ...)
}
case ($param:ident : $type:ident) => {
$param
}
case ($param:ident) => {
$param
}
case ($param:ident : $type:ident, $rest ...) => {
$param, asmfn_params($rest ...)
}
case ($param:ident, $rest ...) => {
$param, asmfn_params($rest ...)
}
}
macro asmfn_convparams {
case ($param:ident : $type:ident) => {
$param = asmfn_type($param, $type);
}
case ($param:ident) => {
;
}
case ($param:ident : $type:ident, $rest ...) => {
asmfn_convparams($param : $type) asmfn_convparams($rest ...)
}
case ($param:ident, $rest ...) => {
asmfn_convparams($rest ...)
}
}
macro asmfn_type {
case ($param, int) => {
$param|0
}
case ($param, double) => {
+$param
}
}
asmfn calc(a: int, b: int, c, d: double, msg) {
var res = (a + b) / d;
if (c) {
res *= 2;
}
if (res > 0x7fffffff) {
console.log(msg);
}
return (res + 10);
}
// function calc(a, b, c, d, msg) {
// 'use asm';
// a = a | 0;
// b = b | 0;
// d = +d;
// ;
// var res = (a + b) / d;
// if (c) {
// res *= 2;
// }
// if (res > 2147483647) {
// console.log(msg);
// }
// return res + 10;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment