Skip to content

Instantly share code, notes, and snippets.

@ozten
Forked from disnet/scheme.sjs
Last active September 6, 2019 06:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozten/c9330efe9ee16ce4da3c to your computer and use it in GitHub Desktop.
Save ozten/c9330efe9ee16ce4da3c to your computer and use it in GitHub Desktop.
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