-
-
Save ozten/c9330efe9ee16ce4da3c to your computer and use it in GitHub Desktop.
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 $if { | |
rule { | |
($x...) | |
} => { | |
if (relCar($x...)) | |
} | |
} | |
macro $while { | |
rule { | |
($x...) | |
} => { | |
while (relCar($x...)) | |
} | |
} | |
// naive form: | |
/* | |
macro rel { | |
rule {$x} => { $x } | |
rule {($x:ident $op $y:ident $rest ...)} => { | |
$x $op $y && $rel($y $rest ...) | |
} | |
} | |
*/ | |
// better: works even with the presence of &&, |, etc. | |
macro relGen { | |
rule { | |
($cmp...) | |
} => { | |
macro relCar { | |
$(rule { | |
($x $cmp $y $rest $[...]) | |
} => { | |
$x $cmp $y relCdr($y $rest $[...]) | |
})... | |
rule { | |
$x | |
} => { | |
$x | |
} | |
} | |
macro relCdr { | |
$(rule { | |
($y $cmp $z) | |
} => { && $y $cmp $z | |
})... | |
/* since the following two sequences are of equal length, order is important */ | |
rule { | |
($y: ident $op $z: ident $rest $[...]) | |
} => { | |
$op relCar($z $rest $[...]) | |
} | |
$(rule { | |
($y: ident $cmp $z: ident $rest $[...]) | |
} => { && $y $cmp $z relCdr($z $rest $[...]) | |
})... | |
rule { | |
$y | |
} => { | |
$y | |
} | |
} | |
} | |
} | |
relGen(>= <= > < != !== === ==) | |
$while(1 > 2 > 3 >= 4 <= 5 > 6) { | |
console.log("foo"); | |
} | |
$if(1 < 2 < 3 < 4 < 5 < 6) { | |
console.log("bar"); | |
} | |
$if(1 < 2 && 3 < 4 < 5 || 1 == 2 == 3) { | |
console.log("bar"); | |
} | |
// result: | |
/* | |
while (1 > 2 && 2 > 3 && 3 >= 4 && 4 <= 5 && 5 > 6) { | |
console.log('foo'); | |
} | |
if (1 < 2 && 2 < 3 && 3 < 4 && 4 < 5 && 5 < 6) { | |
console.log('bar'); | |
} | |
if (1 < 2 && 3 < 4 && 4 < 5 || 1 == 2 && 2 == 3) { | |
console.log('bar'); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment