aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-10 17:36:04 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-10 17:36:04 -0500
commitb099d51d2b0305295518522313ec3b594c66331f (patch)
treef7896c5a83ba6ffca1ff5c4fba78c68f8ed6252a
parentb341e0481c3ba0ea0c60ead1c0bbf0b7eb8d8aaa (diff)
Min/max
-rw-r--r--compile.c6
-rw-r--r--nextlang.h17
2 files changed, 22 insertions, 1 deletions
diff --git a/compile.c b/compile.c
index 7df5ccfc..a4dbc8eb 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 2718ef6d..c41968ca 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