paul@354 | 1 | /* Native functions for integer operations. |
paul@354 | 2 | |
paul@569 | 3 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@354 | 4 | |
paul@354 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@354 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@354 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@354 | 8 | version. |
paul@354 | 9 | |
paul@354 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@354 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@354 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@354 | 13 | details. |
paul@354 | 14 | |
paul@354 | 15 | You should have received a copy of the GNU General Public License along with |
paul@354 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@354 | 17 | */ |
paul@354 | 18 | |
paul@354 | 19 | #include <math.h> /* ceil, log10, pow */ |
paul@583 | 20 | #include <stdio.h> /* fdopen, snprintf */ |
paul@354 | 21 | #include <errno.h> /* errno */ |
paul@583 | 22 | #include <string.h> /* strlen */ |
paul@354 | 23 | #include "native/common.h" |
paul@354 | 24 | #include "types.h" |
paul@354 | 25 | #include "exceptions.h" |
paul@354 | 26 | #include "ops.h" |
paul@354 | 27 | #include "progconsts.h" |
paul@354 | 28 | #include "progops.h" |
paul@354 | 29 | #include "progtypes.h" |
paul@354 | 30 | #include "main.h" |
paul@354 | 31 | |
paul@354 | 32 | /* Integer operations. */ |
paul@354 | 33 | |
paul@763 | 34 | __attr __fn_native_int_int_add(__attr __self, __attr self, __attr other) |
paul@354 | 35 | { |
paul@758 | 36 | /* self and other interpreted as int */ |
paul@763 | 37 | int i = __TOINT(self); |
paul@763 | 38 | int j = __TOINT(other); |
paul@354 | 39 | |
paul@354 | 40 | /* Test for overflow. */ |
paul@758 | 41 | if (((i > 0) && (j > 0) && (i > __MAXINT - j)) || |
paul@758 | 42 | ((i < 0) && (j < 0) && (i < __MININT - j))) |
paul@354 | 43 | |
paul@354 | 44 | __raise_overflow_error(); |
paul@354 | 45 | |
paul@354 | 46 | /* Return the new integer. */ |
paul@354 | 47 | return __new_int(i + j); |
paul@354 | 48 | } |
paul@354 | 49 | |
paul@763 | 50 | __attr __fn_native_int_int_sub(__attr __self, __attr self, __attr other) |
paul@354 | 51 | { |
paul@758 | 52 | /* self and other interpreted as int */ |
paul@763 | 53 | int i = __TOINT(self); |
paul@763 | 54 | int j = __TOINT(other); |
paul@354 | 55 | |
paul@354 | 56 | /* Test for overflow. */ |
paul@758 | 57 | if (((i < 0) && (j > 0) && (i < __MININT + j)) || |
paul@758 | 58 | ((i > 0) && (j < 0) && (i > __MAXINT + j))) |
paul@354 | 59 | |
paul@354 | 60 | __raise_overflow_error(); |
paul@354 | 61 | |
paul@354 | 62 | /* Return the new integer. */ |
paul@354 | 63 | return __new_int(i - j); |
paul@354 | 64 | } |
paul@354 | 65 | |
paul@763 | 66 | __attr __fn_native_int_int_mul(__attr __self, __attr self, __attr other) |
paul@354 | 67 | { |
paul@758 | 68 | /* self and other interpreted as int */ |
paul@763 | 69 | int i = __TOINT(self); |
paul@763 | 70 | int j = __TOINT(other); |
paul@354 | 71 | |
paul@354 | 72 | /* Test for overflow. */ |
paul@758 | 73 | if (((i > 0) && (j > 0) && (i > __MAXINT / j)) || |
paul@758 | 74 | ((i < 0) && (j < 0) && (i > __MAXINT / j)) || |
paul@758 | 75 | ((i < 0) && (j > 0) && (i < __MININT / j)) || |
paul@758 | 76 | ((i > 0) && (j < 0) && (j < __MININT / i))) |
paul@354 | 77 | |
paul@354 | 78 | __raise_overflow_error(); |
paul@354 | 79 | |
paul@354 | 80 | /* Return the new integer. */ |
paul@354 | 81 | return __new_int(i * j); |
paul@354 | 82 | } |
paul@354 | 83 | |
paul@763 | 84 | __attr __fn_native_int_int_div(__attr __self, __attr self, __attr other) |
paul@354 | 85 | { |
paul@758 | 86 | /* self and other interpreted as int */ |
paul@763 | 87 | int i = __TOINT(self); |
paul@763 | 88 | int j = __TOINT(other); |
paul@354 | 89 | |
paul@354 | 90 | /* Test for division by zero or overflow. */ |
paul@354 | 91 | if (j == 0) |
paul@354 | 92 | __raise_zero_division_error(); |
paul@758 | 93 | else if ((j == -1) && (i == __MININT)) |
paul@354 | 94 | __raise_overflow_error(); |
paul@354 | 95 | |
paul@354 | 96 | /* Return the new integer. */ |
paul@354 | 97 | return __new_int(i / j); |
paul@354 | 98 | } |
paul@354 | 99 | |
paul@763 | 100 | __attr __fn_native_int_int_mod(__attr __self, __attr self, __attr other) |
paul@354 | 101 | { |
paul@758 | 102 | /* self and other interpreted as int */ |
paul@763 | 103 | int i = __TOINT(self); |
paul@763 | 104 | int j = __TOINT(other); |
paul@354 | 105 | |
paul@354 | 106 | /* Test for division by zero or overflow. */ |
paul@354 | 107 | if (j == 0) |
paul@354 | 108 | __raise_zero_division_error(); |
paul@758 | 109 | else if ((j == -1) && (i == __MININT)) |
paul@354 | 110 | __raise_overflow_error(); |
paul@354 | 111 | |
paul@354 | 112 | /* Return the new integer. */ |
paul@354 | 113 | return __new_int(i % j); |
paul@354 | 114 | } |
paul@354 | 115 | |
paul@763 | 116 | __attr __fn_native_int_int_neg(__attr __self, __attr self) |
paul@354 | 117 | { |
paul@758 | 118 | /* self interpreted as int */ |
paul@763 | 119 | int i = __TOINT(self); |
paul@354 | 120 | |
paul@354 | 121 | /* Test for overflow. */ |
paul@758 | 122 | if (i == __MININT) |
paul@354 | 123 | __raise_overflow_error(); |
paul@354 | 124 | |
paul@354 | 125 | /* Return the new integer. */ |
paul@354 | 126 | return __new_int(-i); |
paul@354 | 127 | } |
paul@354 | 128 | |
paul@763 | 129 | __attr __fn_native_int_int_pow(__attr __self, __attr self, __attr other) |
paul@354 | 130 | { |
paul@758 | 131 | /* self and other interpreted as int */ |
paul@763 | 132 | int i = __TOINT(self); |
paul@763 | 133 | int j = __TOINT(other); |
paul@354 | 134 | int k; |
paul@354 | 135 | |
paul@354 | 136 | errno = 0; |
paul@354 | 137 | k = (int) pow(i, j); |
paul@354 | 138 | |
paul@354 | 139 | /* Test for overflow. */ |
paul@354 | 140 | |
paul@354 | 141 | if (errno == ERANGE) |
paul@354 | 142 | __raise_overflow_error(); |
paul@354 | 143 | |
paul@354 | 144 | /* Return the new integer. */ |
paul@354 | 145 | return __new_int(k); |
paul@354 | 146 | } |
paul@354 | 147 | |
paul@763 | 148 | __attr __fn_native_int_int_and(__attr __self, __attr self, __attr other) |
paul@354 | 149 | { |
paul@758 | 150 | /* self and other interpreted as int */ |
paul@763 | 151 | int i = __TOINT(self); |
paul@763 | 152 | int j = __TOINT(other); |
paul@354 | 153 | |
paul@354 | 154 | /* Return the new integer. */ |
paul@354 | 155 | /* NOTE: No overflow test applied. */ |
paul@354 | 156 | return __new_int(i & j); |
paul@354 | 157 | } |
paul@354 | 158 | |
paul@763 | 159 | __attr __fn_native_int_int_not(__attr __self, __attr self) |
paul@354 | 160 | { |
paul@758 | 161 | /* self interpreted as int */ |
paul@763 | 162 | int i = __TOINT(self); |
paul@354 | 163 | |
paul@354 | 164 | /* Return the new integer. */ |
paul@354 | 165 | return __new_int(~i); |
paul@354 | 166 | } |
paul@354 | 167 | |
paul@763 | 168 | __attr __fn_native_int_int_or(__attr __self, __attr self, __attr other) |
paul@354 | 169 | { |
paul@758 | 170 | /* self and other interpreted as int */ |
paul@763 | 171 | int i = __TOINT(self); |
paul@763 | 172 | int j = __TOINT(other); |
paul@354 | 173 | |
paul@354 | 174 | /* Return the new integer. */ |
paul@354 | 175 | /* NOTE: No overflow test applied. */ |
paul@354 | 176 | return __new_int(i | j); |
paul@354 | 177 | } |
paul@354 | 178 | |
paul@763 | 179 | __attr __fn_native_int_int_xor(__attr __self, __attr self, __attr other) |
paul@354 | 180 | { |
paul@758 | 181 | /* self and other interpreted as int */ |
paul@763 | 182 | int i = __TOINT(self); |
paul@763 | 183 | int j = __TOINT(other); |
paul@354 | 184 | |
paul@354 | 185 | /* Return the new integer. */ |
paul@354 | 186 | /* NOTE: No overflow test applied. */ |
paul@354 | 187 | return __new_int(i ^ j); |
paul@354 | 188 | } |
paul@354 | 189 | |
paul@763 | 190 | __attr __fn_native_int_int_lt(__attr __self, __attr self, __attr other) |
paul@354 | 191 | { |
paul@758 | 192 | /* self and other interpreted as int */ |
paul@763 | 193 | int i = __TOINT(self); |
paul@763 | 194 | int j = __TOINT(other); |
paul@354 | 195 | |
paul@354 | 196 | /* Return a boolean result. */ |
paul@354 | 197 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 198 | } |
paul@354 | 199 | |
paul@763 | 200 | __attr __fn_native_int_int_gt(__attr __self, __attr self, __attr other) |
paul@354 | 201 | { |
paul@758 | 202 | /* self and other interpreted as int */ |
paul@763 | 203 | int i = __TOINT(self); |
paul@763 | 204 | int j = __TOINT(other); |
paul@354 | 205 | |
paul@354 | 206 | /* Return a boolean result. */ |
paul@354 | 207 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 208 | } |
paul@354 | 209 | |
paul@763 | 210 | __attr __fn_native_int_int_eq(__attr __self, __attr self, __attr other) |
paul@354 | 211 | { |
paul@758 | 212 | /* self and other interpreted as int */ |
paul@763 | 213 | int i = __TOINT(self); |
paul@763 | 214 | int j = __TOINT(other); |
paul@354 | 215 | |
paul@354 | 216 | /* Return a boolean result. */ |
paul@354 | 217 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 218 | } |
paul@354 | 219 | |
paul@763 | 220 | __attr __fn_native_int_int_ne(__attr __self, __attr self, __attr other) |
paul@354 | 221 | { |
paul@758 | 222 | /* self and other interpreted as int */ |
paul@763 | 223 | int i = __TOINT(self); |
paul@763 | 224 | int j = __TOINT(other); |
paul@354 | 225 | |
paul@354 | 226 | /* Return a boolean result. */ |
paul@354 | 227 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 228 | } |
paul@354 | 229 | |
paul@763 | 230 | __attr __fn_native_int_int_str(__attr __self, __attr self) |
paul@354 | 231 | { |
paul@758 | 232 | /* self interpreted as int */ |
paul@763 | 233 | int i = __TOINT(self); |
paul@763 | 234 | |
paul@354 | 235 | /* Employ a buffer big enough to fit the largest integer plus an extra |
paul@354 | 236 | character, a minus sign, and the null terminator. */ |
paul@758 | 237 | unsigned int n = (int) log10(__MAXINT) + 3; |
paul@354 | 238 | char *s = (char *) __ALLOCATE(n, sizeof(char)); |
paul@354 | 239 | |
paul@354 | 240 | snprintf(s, n, "%d", i); |
paul@354 | 241 | |
paul@354 | 242 | /* Return a new string. */ |
paul@583 | 243 | return __new_str(s, strlen(s)); |
paul@354 | 244 | } |
paul@354 | 245 | |
paul@354 | 246 | /* Module initialisation. */ |
paul@354 | 247 | |
paul@354 | 248 | void __main_native_int() |
paul@354 | 249 | { |
paul@354 | 250 | } |