Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/4] Reorder binary operations


The terminfo format goes out of its way to make binary operations
read left-to-right, see e.g. ncurses-6.0' tinfo/lib_tparm.c:652.
---
 test/tifmt.myr | 12 +++++++++---
 tifmt.myr      | 27 ++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/test/tifmt.myr b/test/tifmt.myr
index cd02fd5..0fd1dcf 100644
--- a/test/tifmt.myr
+++ b/test/tifmt.myr
@@ -30,10 +30,16 @@ const main = {
 
 		/* numeric */
 		[.name="arith-add",	.fn={ctx; binop(ctx, 'd', '+', 10, 21, "31")}],
-		[.name="arith-sub",	.fn={ctx; binop(ctx, 'd', '-', 21, 10, "-11")}],
+		[.name="arith-sub",	.fn={ctx; binop(ctx, 'd', '-', 10, 21, "-11")}],
 		[.name="arith-mul",	.fn={ctx; binop(ctx, 'd', '*', 10, 21, "210")}],
-		[.name="arith-div",	.fn={ctx; binop(ctx, 'd', '/', 9, 100, "11")}],
-		[.name="arith-mod",	.fn={ctx; binop(ctx, 'd', 'm', 9, 100, "1")}],
+		[.name="arith-div",	.fn={ctx; binop(ctx, 'd', '/', 100, 9, "11")}],
+		[.name="arith-mod",	.fn={ctx; binop(ctx, 'd', 'm', 100, 9, "1")}],
+
+		/* comparison */
+		[.name="comp-lt-1",	.fn={ctx; binop(ctx, 'd', '<', 0, 1, "1")}],
+		[.name="comp-lt-2",	.fn={ctx; binop(ctx, 'd', '<', 0, 999, "1")}],
+		[.name="comp-lt-3",	.fn={ctx; binop(ctx, 'd', '<', 2, -2, "0")}],
+		[.name="comp-gt-1",	.fn={ctx; binop(ctx, 'd', '>', 156, 100, "1")}],
 
 		/* bitwise */
 		[.name="bit-and",	.fn={ctx; binop(ctx, 'x','&', 0x60, 0xc0, "40")}],
diff --git a/tifmt.myr b/tifmt.myr
index 29f0831..4ae92b8 100644
--- a/tifmt.myr
+++ b/tifmt.myr
@@ -39,10 +39,27 @@ const eval = {f, sb, str
 		| 'l':	pushi(f, pops(f).len)
 		/* arithmetic */
 		| '+':	pushi(f, popi(f) + popi(f))
-		| '-':	pushi(f, popi(f) - popi(f))
+		| '-':
+			var b = popi(f)
+			var a = popi(f)
+			pushi(f, a - b)
 		| '*':	pushi(f, popi(f) * popi(f))
-		| '/':	pushi(f, popi(f) / popi(f))
-		| 'm':	pushi(f, popi(f) % popi(f))
+		| '/':
+			var d = popi(f)
+			var n = popi(f)
+			if d == 0
+				pushi(f, 0)
+			else
+				pushi(f, n / d)
+			;;
+		| 'm':
+			var m = popi(f)
+			var n = popi(f)
+			if m == 0
+				pushi(f, 0)
+			else
+				pushi(f, n % m)
+			;;
 		/* bitwise */
 		| '&':	pushi(f, popi(f) & popi(f))
 		| '|':	pushi(f, popi(f) | popi(f))
@@ -50,8 +67,8 @@ const eval = {f, sb, str
 		| '~':	pushi(f, ~ popi(f))
 		/* relational false*/
 		| '=':	pushi(f, btoi(popi(f) == popi(f)))
-		| '<':	pushi(f, btoi(popi(f) < popi(f)))
-		| '>':	pushi(f, btoi(popi(f) > popi(f)))
+		| '<':	pushi(f, btoi(popi(f) > popi(f)))
+		| '>':	pushi(f, btoi(popi(f) < popi(f)))
 		/* logical */
 		| 'A':	pushi(f, btoi(popi(f) != 0 && popi(f) != 0))
 		| 'O':	pushi(f, btoi(popi(f) != 0 || popi(f) != 0))
-- 
2.14.3


References:
[PATCH 0/4] Fixes to allow using terminfo for colors on st"S. Gilles" <sgilles@xxxxxxxxxxxx>