summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Gilles <sgilles@math.umd.edu>2017-10-26 21:06:43 -0400
committerOri Bernstein <ori@markovcorp.com>2017-10-27 11:52:01 -0700
commit455101bdeb02a67e9821258ed21ccac7ac1716c3 (patch)
treedce2e876db2ea1028a29b72e8c950d270ffd894c
parent715cf398c47f4bb36df95a06f506cc714e4933e6 (diff)
downloadlibtermdraw-455101bdeb02a67e9821258ed21ccac7ac1716c3.tar.gz
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.
-rw-r--r--test/tifmt.myr12
-rw-r--r--tifmt.myr27
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))