aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/expressions.c70
1 files changed, 50 insertions, 20 deletions
diff --git a/src/expressions.c b/src/expressions.c
index 84d76cb..68049eb 100644
--- a/src/expressions.c
+++ b/src/expressions.c
@@ -32,6 +32,16 @@
#include "expressions.h"
/*
+ * All real-life architectures have negative integers in two's complement.
+ * C23 will demand that as well.
+ * If this would not be given, the result of multiplication and addition
+ * overflows would be undefined.
+ */
+#if (-1 & 3) != 3
+#warning "Negative integers are not in two's complement"
+#endif
+
+/*
* Number and operator stacks are static, so
* they can be passed to the undo token constructors.
* This is OK since we're currently singleton.
@@ -224,15 +234,14 @@ teco_expressions_calc(GError **error)
switch (op) {
case TECO_OP_POW: {
if (vleft_is_float || vright_is_float) {
+ /*
+ * NOTE: 0.0^*-1 will be Inf, instead of throwing an error.
+ */
teco_expressions_push(pow(vleft_is_float ? vleft.f : vleft.i,
vright_is_float ? vright.f : vright.i));
break;
}
- if (!vright.i) {
- teco_expressions_push(vleft.i < 0 ? -1 : 1);
- break;
- }
if (vright.i < 0) {
if (!vleft.i) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
@@ -266,34 +275,40 @@ teco_expressions_calc(GError **error)
break;
case TECO_OP_DIV:
- if (!(vright_is_float ? vright.f : vright.i)) {
- g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
- "Division by zero");
- return FALSE;
- }
-
if (vleft_is_float || vright_is_float) {
+ /*
+ * NOTE: 1.0/0 will be Inf instead of throwing an error.
+ */
teco_expressions_push((vleft_is_float ? vleft.f : vleft.i) /
(vright_is_float ? vright.f : vright.i));
break;
}
- teco_expressions_push(vleft.i / vright.i);
- break;
-
- case TECO_OP_MOD:
- if (!(vright_is_float ? vright.f : vright.i)) {
+ if (!vright.i) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
- "Remainder of division by zero");
+ "Division by zero is undefined");
return FALSE;
}
+ teco_expressions_push(vleft.i / vright.i);
+ break;
+
+ case TECO_OP_MOD:
if (vleft_is_float || vright_is_float) {
+ /*
+ * NOTE: 1.0^/0 will be NaN instead of throwing an error.
+ */
teco_expressions_push(fmod(vleft_is_float ? vleft.f : vleft.i,
vright_is_float ? vright.f : vright.i));
break;
}
+ if (!vright.i) {
+ g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
+ "Remainder of division by zero is undefined");
+ return FALSE;
+ }
+
teco_expressions_push(vleft.i % vright.i);
break;
@@ -318,13 +333,28 @@ teco_expressions_calc(GError **error)
break;
case TECO_OP_SHL:
- teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) <<
- (vright_is_float ? (teco_int_t)vright.f : vright.i));
+ if (vleft_is_float)
+ vleft.i = vleft.f;
+ if (vright_is_float)
+ vright.i = vright.f;
+ if (vright.i < 0)
+ /* undefined behavior in C, so we shift the other direction like in Lua */
+ teco_expressions_push(vleft.i >> -vright.i);
+ else
+ /* left shift larger than n bits is undefined - always return 0 */
+ teco_expressions_push(vright.i < sizeof(vleft.i)*8 ? (vleft.i << vright.i) : 0);
break;
case TECO_OP_SHR:
- teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) >>
- (vright_is_float ? (teco_int_t)vright.f : vright.i));
+ if (vleft_is_float)
+ vleft.i = vleft.f;
+ if (vright_is_float)
+ vright.i = vright.f;
+ if (vright.i < 0)
+ /* undefined behavior in C, so we shift the other direction like in Lua */
+ teco_expressions_push(-vright.i < sizeof(vleft.i)*8 ? (vleft.i << -vright.i) : 0);
+ else
+ teco_expressions_push(vleft.i >> vright.i);
break;
case TECO_OP_AND: