ref: c52cc17e882fbf6463f188acdeb5c1c75b3ff4a4
parent: bdcf22471658feadd37da3f16dbf98a95dc29dfa
author: Ali Gholami Rudi <ali@rudi.ir>
date: Wed Jun 4 05:49:13 EDT 2014
eval: report divide-by-zero errors
--- a/eval.c
+++ b/eval.c
@@ -91,6 +91,13 @@
return 0;
}
+static int nonzero(int n)
+{
+ if (!n)
+ errdie("neatroff: divide by zero\n");
+ return n;
+}
+
static int evalexpr(char **s)
{
int ret = evalatom(s);
@@ -100,11 +107,11 @@
else if (!evaljmp(s, '-'))
ret -= evalatom(s);
else if (!evaljmp(s, '/'))
- ret /= evalatom(s);
+ ret /= nonzero(evalatom(s));
else if (!evaljmp(s, '*'))
ret *= evalatom(s);
else if (!evaljmp(s, '%'))
- ret %= evalatom(s);
+ ret %= nonzero(evalatom(s));
else if (!evaljmp(s, '<'))
ret = !evaljmp(s, '=') ? ret <= evalatom(s) : ret < evalatom(s);
else if (!evaljmp(s, '>'))