diff --git a/compile.c b/compile.c index 7df5ccf..a4dbc8e 100644 --- a/compile.c +++ b/compile.c @@ -171,6 +171,12 @@ CORD compile(ast_t *ast) } return CORD_cat(code, ";"); } + case Min: { + return CORD_asprintf("min(%r, %r)", compile(Match(ast, Min)->lhs), compile(Match(ast, Min)->rhs)); + } + case Max: { + return CORD_asprintf("max(%r, %r)", compile(Match(ast, Max)->lhs), compile(Match(ast, Max)->rhs)); + } // Min, Max, // Array, Table, TableEntry, case Array: { diff --git a/nextlang.h b/nextlang.h index 2718ef6..c41968c 100644 --- a/nextlang.h +++ b/nextlang.h @@ -57,7 +57,22 @@ #define xor(x, y) ((x) ^ (y)) #define mod(x, n) ((x) % (n)) #define mod1(x, n) (((x) % (n)) + (__typeof(x))1) +#define __cmp(x, y) (_Generic(x, CORD: CORD_cmp(x, y), char*: strcmp(x, y), const char*: strcmp(x, y), default: (x > 0) - (y > 0))) +#define __lt(x, y) (_Generic(x, int8_t: x < y, int16_t: x < y, int32_t: x < y, int64_t: x < y, float: x < y, double: x < y, char: x < y, bool: x < y, \ + default: __cmp(x, y) < 0)) +#define __le(x, y) (_Generic(x, int8_t: x <= y, int16_t: x <= y, int32_t: x <= y, int64_t: x <= y, float: x <= y, double: x <= y, char: x <= y, bool: x <= y, \ + default: __cmp(x, y) <= 0)) +#define __ge(x, y) (_Generic(x, int8_t: x >= y, int16_t: x >= y, int32_t: x >= y, int64_t: x >= y, float: x >= y, double: x >= y, char: x >= y, bool: x >= y, \ + default: __cmp(x, y) >= 0)) +#define __gt(x, y) (_Generic(x, int8_t: x > y, int16_t: x > y, int32_t: x > y, int64_t: x > y, float: x > y, double: x > y, char: x > y, bool: x > y, \ + default: __cmp(x, y) > 0)) +#define __eq(x, y) (_Generic(x, int8_t: x == y, int16_t: x == y, int32_t: x == y, int64_t: x == y, float: x == y, double: x == y, char: x == y, bool: x == y, \ + default: __cmp(x, y) == 0)) +#define __ne(x, y) (_Generic(x, int8_t: x != y, int16_t: x != y, int32_t: x != y, int64_t: x != y, float: x != y, double: x != y, char: x != y, bool: x != y, \ + default: __cmp(x, y) != 0)) +#define min(x, y) ({ __declare(__min_lhs, x); __declare(__min_rhs, y); __le(__min_lhs, __min_rhs) ? __min_lhs : __min_rhs; }) +#define max(x, y) ({ __declare(__min_lhs, x); __declare(__min_rhs, y); __ge(__min_lhs, __min_rhs) ? __min_lhs : __min_rhs; }) -#define say(str) puts(CORD_to_const_char_star(str)) +#define say(str) puts(CORD_to_const_char_star(__cord(str))) // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0