Token macros
Hygienic-style token templates: max, clamp and generated getter functions.
+ <stdio.h>
@m max($a, $b)
(($a) > ($b) ? ($a) : ($b))
@m clamp($x, $lo, $hi)
@max($lo, ($x) < ($hi) ? ($x) : ($hi))
@m getter($name, $field)
f $name(s: *Shape) double
< s->$field
s Shape w,h:double
@getter(width, w)
@getter(height, h)
f main()
s: Shape = {3, 4}
printf("max %d clamp %d %d\n", @max(3, 9), @clamp(15, 0, 10), @clamp(-4, 0, 10))
printf("w x h = %g %g\n", width(&s), height(&s))
< 0
#include <stdio.h>
typedef struct Shape Shape;
struct Shape {
double w,h;
};
double width(Shape *s) {
return s->w;
}
double height(Shape *s) {
return s->h;
}
int main(void) {
Shape s = {3, 4};
printf("max %d clamp %d %d\n", ((3) > (9) ? (3) : (9)), ((0) > ((15) < (10) ? (15) : (10)) ? (0) : ((15) < (10) ? (15) : (10))),
((0) > ((-4) < (10) ? (-4) : (10)) ? (0) : ((-4) < (10) ? (-4) : (10))));
printf("w x h = %g %g\n", width(&s), height(&s));
return 0;
}
Output
max 9 clamp 10 0
w x h = 3 4
What it shows
@m, captures