paul@293 | 1 | #include <stdlib.h> /* abs, exit */ |
paul@173 | 2 | #include <unistd.h> /* read, write */ |
paul@306 | 3 | #include <limits.h> /* INT_MAX, INT_MIN */ |
paul@198 | 4 | #include <math.h> /* ceil, log10, pow */ |
paul@209 | 5 | #include <string.h> /* strcmp, strncpy, strlen */ |
paul@198 | 6 | #include <stdio.h> /* snprintf */ |
paul@306 | 7 | #include <errno.h> /* errno */ |
paul@161 | 8 | #include "types.h" |
paul@161 | 9 | #include "exceptions.h" |
paul@161 | 10 | #include "ops.h" |
paul@161 | 11 | #include "progconsts.h" |
paul@161 | 12 | #include "progops.h" |
paul@161 | 13 | #include "progtypes.h" |
paul@161 | 14 | #include "main.h" |
paul@161 | 15 | |
paul@198 | 16 | /* Utility functions. */ |
paul@198 | 17 | |
paul@231 | 18 | static __attr __new_int(int i) |
paul@198 | 19 | { |
paul@198 | 20 | /* Create a new integer and mutate the __data__ attribute. */ |
paul@198 | 21 | __attr attr = __new(&__InstanceTable___builtins___int_int, &__builtins___int_int, sizeof(__obj___builtins___int_int)); |
paul@198 | 22 | attr.value->attrs[__pos___data__].intvalue = i; |
paul@198 | 23 | return attr; |
paul@198 | 24 | } |
paul@198 | 25 | |
paul@231 | 26 | static __attr __new_str(char *s) |
paul@198 | 27 | { |
paul@198 | 28 | /* Create a new string and mutate the __data__ attribute. */ |
paul@198 | 29 | __attr attr = __new(&__InstanceTable___builtins___str_string, &__builtins___str_string, sizeof(__obj___builtins___str_string)); |
paul@198 | 30 | attr.value->attrs[__pos___data__].strvalue = s; |
paul@198 | 31 | return attr; |
paul@198 | 32 | } |
paul@198 | 33 | |
paul@283 | 34 | static __attr __new_list(__fragment *f) |
paul@283 | 35 | { |
paul@283 | 36 | /* Create a new list and mutate the __data__ attribute. */ |
paul@283 | 37 | __attr attr = __new(&__InstanceTable___builtins___list_list, &__builtins___list_list, sizeof(__obj___builtins___list_list)); |
paul@283 | 38 | attr.value->attrs[__pos___data__].seqvalue = f; |
paul@283 | 39 | return attr; |
paul@283 | 40 | } |
paul@283 | 41 | |
paul@283 | 42 | static __fragment *__fragment_append(__fragment *data, __attr * const value) |
paul@283 | 43 | { |
paul@283 | 44 | __fragment *newdata = data; |
paul@283 | 45 | unsigned int size = data->size, capacity = data->capacity; |
paul@283 | 46 | unsigned int n; |
paul@283 | 47 | |
paul@283 | 48 | /* Re-allocate the fragment if the capacity has been reached. */ |
paul@283 | 49 | if (size >= capacity) |
paul@283 | 50 | { |
paul@283 | 51 | /* NOTE: Consider various restrictions on capacity increases. */ |
paul@283 | 52 | n = capacity ? capacity * 2 : 1; |
paul@283 | 53 | newdata = (__fragment *) __REALLOCATE(data, __FRAGMENT_SIZE(n)); |
paul@283 | 54 | newdata->capacity = n; |
paul@283 | 55 | } |
paul@283 | 56 | |
paul@283 | 57 | /* Insert the new element and increment the list size. */ |
paul@283 | 58 | newdata->attrs[size] = *value; |
paul@283 | 59 | newdata->size = size + 1; |
paul@283 | 60 | |
paul@283 | 61 | return newdata; |
paul@283 | 62 | } |
paul@283 | 63 | |
paul@167 | 64 | /* Native functions. */ |
paul@167 | 65 | |
paul@167 | 66 | __attr __fn_native__exit(__attr __args[]) |
paul@167 | 67 | { |
paul@208 | 68 | __attr * const status = &__args[1]; |
paul@167 | 69 | |
paul@208 | 70 | exit(__load_via_object(status->value, __pos___data__).intvalue); |
paul@167 | 71 | return __builtins___none_None; |
paul@167 | 72 | } |
paul@167 | 73 | |
paul@182 | 74 | __attr __fn_native__get_argv(__attr __args[]) |
paul@182 | 75 | { |
paul@208 | 76 | __attr * const status = &__args[1]; |
paul@182 | 77 | |
paul@182 | 78 | /* NOTE: To be written. */ |
paul@182 | 79 | return __builtins___none_None; |
paul@182 | 80 | } |
paul@182 | 81 | |
paul@182 | 82 | __attr __fn_native__get_path(__attr __args[]) |
paul@182 | 83 | { |
paul@208 | 84 | __attr * const status = &__args[1]; |
paul@182 | 85 | |
paul@182 | 86 | /* NOTE: To be written. */ |
paul@182 | 87 | return __builtins___none_None; |
paul@182 | 88 | } |
paul@182 | 89 | |
paul@161 | 90 | __attr __fn_native__is(__attr __args[]) |
paul@161 | 91 | { |
paul@208 | 92 | __attr * const x = &__args[1]; |
paul@208 | 93 | __attr * const y = &__args[2]; |
paul@161 | 94 | |
paul@208 | 95 | return x->value == y->value ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 96 | } |
paul@161 | 97 | |
paul@161 | 98 | __attr __fn_native__is_not(__attr __args[]) |
paul@161 | 99 | { |
paul@208 | 100 | __attr * const x = &__args[1]; |
paul@208 | 101 | __attr * const y = &__args[2]; |
paul@161 | 102 | |
paul@208 | 103 | return x->value != y->value ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 104 | } |
paul@161 | 105 | |
paul@161 | 106 | __attr __fn_native__int_add(__attr __args[]) |
paul@161 | 107 | { |
paul@303 | 108 | __attr * const _data = &__args[1]; |
paul@208 | 109 | __attr * const other = &__args[2]; |
paul@303 | 110 | /* _data and other interpreted as int */ |
paul@303 | 111 | int i = _data->intvalue; |
paul@303 | 112 | int j = other->intvalue; |
paul@161 | 113 | |
paul@306 | 114 | /* Test for overflow. */ |
paul@306 | 115 | if (((i > 0) && (j > 0) && (i > INT_MAX - j)) || |
paul@306 | 116 | ((i < 0) && (j < 0) && (i < INT_MIN - j))) |
paul@306 | 117 | |
paul@306 | 118 | __raise_overflow_error(); |
paul@306 | 119 | |
paul@198 | 120 | /* Return the new integer. */ |
paul@198 | 121 | return __new_int(i + j); |
paul@161 | 122 | } |
paul@161 | 123 | |
paul@161 | 124 | __attr __fn_native__int_sub(__attr __args[]) |
paul@161 | 125 | { |
paul@303 | 126 | __attr * const _data = &__args[1]; |
paul@208 | 127 | __attr * const other = &__args[2]; |
paul@303 | 128 | /* _data and other interpreted as int */ |
paul@303 | 129 | int i = _data->intvalue; |
paul@303 | 130 | int j = other->intvalue; |
paul@161 | 131 | |
paul@306 | 132 | /* Test for overflow. */ |
paul@306 | 133 | if (((i < 0) && (j > 0) && (i < INT_MIN + j)) || |
paul@306 | 134 | ((i > 0) && (j < 0) && (i > INT_MAX + j))) |
paul@306 | 135 | |
paul@306 | 136 | __raise_overflow_error(); |
paul@306 | 137 | |
paul@198 | 138 | /* Return the new integer. */ |
paul@198 | 139 | return __new_int(i - j); |
paul@161 | 140 | } |
paul@161 | 141 | |
paul@161 | 142 | __attr __fn_native__int_mul(__attr __args[]) |
paul@161 | 143 | { |
paul@303 | 144 | __attr * const _data = &__args[1]; |
paul@208 | 145 | __attr * const other = &__args[2]; |
paul@303 | 146 | /* _data and other interpreted as int */ |
paul@303 | 147 | int i = _data->intvalue; |
paul@303 | 148 | int j = other->intvalue; |
paul@161 | 149 | |
paul@306 | 150 | /* Test for overflow. */ |
paul@306 | 151 | if (((i > 0) && (j > 0) && (i > INT_MAX / j)) || |
paul@306 | 152 | ((i < 0) && (j < 0) && (i > INT_MAX / j)) || |
paul@306 | 153 | ((i < 0) && (j > 0) && (i < INT_MIN / j)) || |
paul@306 | 154 | ((i > 0) && (j < 0) && (j < INT_MIN / i))) |
paul@306 | 155 | |
paul@306 | 156 | __raise_overflow_error(); |
paul@306 | 157 | |
paul@198 | 158 | /* Return the new integer. */ |
paul@198 | 159 | return __new_int(i * j); |
paul@161 | 160 | } |
paul@161 | 161 | |
paul@161 | 162 | __attr __fn_native__int_div(__attr __args[]) |
paul@161 | 163 | { |
paul@303 | 164 | __attr * const _data = &__args[1]; |
paul@208 | 165 | __attr * const other = &__args[2]; |
paul@303 | 166 | /* _data and other interpreted as int */ |
paul@303 | 167 | int i = _data->intvalue; |
paul@303 | 168 | int j = other->intvalue; |
paul@161 | 169 | |
paul@306 | 170 | /* Test for division by zero or overflow. */ |
paul@306 | 171 | if (j == 0) |
paul@306 | 172 | __raise_zero_division_error(); |
paul@306 | 173 | else if ((j == -1) && (i == INT_MIN)) |
paul@306 | 174 | __raise_overflow_error(); |
paul@306 | 175 | |
paul@198 | 176 | /* Return the new integer. */ |
paul@198 | 177 | return __new_int(i / j); |
paul@161 | 178 | } |
paul@161 | 179 | |
paul@161 | 180 | __attr __fn_native__int_mod(__attr __args[]) |
paul@161 | 181 | { |
paul@303 | 182 | __attr * const _data = &__args[1]; |
paul@208 | 183 | __attr * const other = &__args[2]; |
paul@303 | 184 | /* _data and other interpreted as int */ |
paul@303 | 185 | int i = _data->intvalue; |
paul@303 | 186 | int j = other->intvalue; |
paul@161 | 187 | |
paul@306 | 188 | /* Test for division by zero or overflow. */ |
paul@306 | 189 | if (j == 0) |
paul@306 | 190 | __raise_zero_division_error(); |
paul@306 | 191 | else if ((j == -1) && (i == INT_MIN)) |
paul@306 | 192 | __raise_overflow_error(); |
paul@306 | 193 | |
paul@198 | 194 | /* Return the new integer. */ |
paul@198 | 195 | return __new_int(i % j); |
paul@161 | 196 | } |
paul@161 | 197 | |
paul@207 | 198 | __attr __fn_native__int_neg(__attr __args[]) |
paul@207 | 199 | { |
paul@303 | 200 | __attr * const _data = &__args[1]; |
paul@303 | 201 | /* _data interpreted as int */ |
paul@303 | 202 | int i = _data->intvalue; |
paul@207 | 203 | |
paul@306 | 204 | /* Test for overflow. */ |
paul@306 | 205 | if (i == INT_MIN) |
paul@306 | 206 | __raise_overflow_error(); |
paul@306 | 207 | |
paul@207 | 208 | /* Return the new integer. */ |
paul@207 | 209 | return __new_int(-i); |
paul@207 | 210 | } |
paul@207 | 211 | |
paul@161 | 212 | __attr __fn_native__int_pow(__attr __args[]) |
paul@161 | 213 | { |
paul@303 | 214 | __attr * const _data = &__args[1]; |
paul@208 | 215 | __attr * const other = &__args[2]; |
paul@303 | 216 | /* _data and other interpreted as int */ |
paul@303 | 217 | int i = _data->intvalue; |
paul@303 | 218 | int j = other->intvalue; |
paul@306 | 219 | int k; |
paul@306 | 220 | |
paul@306 | 221 | errno = 0; |
paul@306 | 222 | k = (int) pow(i, j); |
paul@306 | 223 | |
paul@306 | 224 | /* Test for overflow. */ |
paul@306 | 225 | |
paul@306 | 226 | if (errno == ERANGE) |
paul@306 | 227 | __raise_overflow_error(); |
paul@161 | 228 | |
paul@198 | 229 | /* Return the new integer. */ |
paul@306 | 230 | return __new_int(k); |
paul@161 | 231 | } |
paul@161 | 232 | |
paul@161 | 233 | __attr __fn_native__int_and(__attr __args[]) |
paul@161 | 234 | { |
paul@303 | 235 | __attr * const _data = &__args[1]; |
paul@208 | 236 | __attr * const other = &__args[2]; |
paul@303 | 237 | /* _data and other interpreted as int */ |
paul@303 | 238 | int i = _data->intvalue; |
paul@303 | 239 | int j = other->intvalue; |
paul@161 | 240 | |
paul@198 | 241 | /* Return the new integer. */ |
paul@198 | 242 | /* NOTE: No overflow test applied. */ |
paul@198 | 243 | return __new_int(i & j); |
paul@161 | 244 | } |
paul@161 | 245 | |
paul@161 | 246 | __attr __fn_native__int_or(__attr __args[]) |
paul@161 | 247 | { |
paul@303 | 248 | __attr * const _data = &__args[1]; |
paul@208 | 249 | __attr * const other = &__args[2]; |
paul@303 | 250 | /* _data and other interpreted as int */ |
paul@303 | 251 | int i = _data->intvalue; |
paul@303 | 252 | int j = other->intvalue; |
paul@161 | 253 | |
paul@198 | 254 | /* Return the new integer. */ |
paul@198 | 255 | /* NOTE: No overflow test applied. */ |
paul@198 | 256 | return __new_int(i | j); |
paul@161 | 257 | } |
paul@161 | 258 | |
paul@161 | 259 | __attr __fn_native__int_xor(__attr __args[]) |
paul@161 | 260 | { |
paul@303 | 261 | __attr * const _data = &__args[1]; |
paul@208 | 262 | __attr * const other = &__args[2]; |
paul@303 | 263 | /* _data and other interpreted as int */ |
paul@303 | 264 | int i = _data->intvalue; |
paul@303 | 265 | int j = other->intvalue; |
paul@161 | 266 | |
paul@198 | 267 | /* Return the new integer. */ |
paul@198 | 268 | /* NOTE: No overflow test applied. */ |
paul@198 | 269 | return __new_int(i ^ j); |
paul@161 | 270 | } |
paul@161 | 271 | |
paul@161 | 272 | __attr __fn_native__int_lt(__attr __args[]) |
paul@161 | 273 | { |
paul@303 | 274 | __attr * const _data = &__args[1]; |
paul@208 | 275 | __attr * const other = &__args[2]; |
paul@303 | 276 | /* _data and other interpreted as int */ |
paul@303 | 277 | int i = _data->intvalue; |
paul@303 | 278 | int j = other->intvalue; |
paul@161 | 279 | |
paul@198 | 280 | /* Return a boolean result. */ |
paul@198 | 281 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 282 | } |
paul@161 | 283 | |
paul@161 | 284 | __attr __fn_native__int_gt(__attr __args[]) |
paul@161 | 285 | { |
paul@303 | 286 | __attr * const _data = &__args[1]; |
paul@208 | 287 | __attr * const other = &__args[2]; |
paul@303 | 288 | /* _data and other interpreted as int */ |
paul@303 | 289 | int i = _data->intvalue; |
paul@303 | 290 | int j = other->intvalue; |
paul@161 | 291 | |
paul@198 | 292 | /* Return a boolean result. */ |
paul@198 | 293 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 294 | } |
paul@161 | 295 | |
paul@161 | 296 | __attr __fn_native__int_eq(__attr __args[]) |
paul@161 | 297 | { |
paul@303 | 298 | __attr * const _data = &__args[1]; |
paul@208 | 299 | __attr * const other = &__args[2]; |
paul@303 | 300 | /* _data and other interpreted as int */ |
paul@303 | 301 | int i = _data->intvalue; |
paul@303 | 302 | int j = other->intvalue; |
paul@161 | 303 | |
paul@198 | 304 | /* Return a boolean result. */ |
paul@198 | 305 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@198 | 306 | } |
paul@198 | 307 | |
paul@198 | 308 | __attr __fn_native__int_ne(__attr __args[]) |
paul@198 | 309 | { |
paul@303 | 310 | __attr * const _data = &__args[1]; |
paul@208 | 311 | __attr * const other = &__args[2]; |
paul@303 | 312 | /* _data and other interpreted as int */ |
paul@303 | 313 | int i = _data->intvalue; |
paul@303 | 314 | int j = other->intvalue; |
paul@198 | 315 | |
paul@198 | 316 | /* Return a boolean result. */ |
paul@198 | 317 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@198 | 318 | } |
paul@198 | 319 | |
paul@198 | 320 | __attr __fn_native__int_str(__attr __args[]) |
paul@198 | 321 | { |
paul@303 | 322 | __attr * const _data = &__args[1]; |
paul@303 | 323 | /* _data interpreted as int */ |
paul@303 | 324 | int i = _data->intvalue; |
paul@293 | 325 | int n = i != 0 ? (int) ceil(log10(abs(i)+1)) + 1 : 2; |
paul@260 | 326 | char *s = (char *) __ALLOCATE(n, sizeof(char)); |
paul@198 | 327 | |
paul@198 | 328 | if (i < 0) n++; |
paul@198 | 329 | snprintf(s, n, "%d", i); |
paul@198 | 330 | |
paul@198 | 331 | /* Return a new string. */ |
paul@198 | 332 | return __new_str(s); |
paul@161 | 333 | } |
paul@161 | 334 | |
paul@161 | 335 | __attr __fn_native__str_add(__attr __args[]) |
paul@161 | 336 | { |
paul@303 | 337 | __attr * const _data = &__args[1]; |
paul@208 | 338 | __attr * const other = &__args[2]; |
paul@303 | 339 | /* _data, other interpreted as string */ |
paul@303 | 340 | char *s = _data->strvalue; |
paul@303 | 341 | char *o = other->strvalue; |
paul@198 | 342 | int n = strlen(s) + strlen(o) + 1; |
paul@260 | 343 | char *r = (char *) __ALLOCATE(n, sizeof(char)); |
paul@161 | 344 | |
paul@198 | 345 | strncpy(r, s, n); |
paul@292 | 346 | strncpy(r + strlen(s), o, n - strlen(s)); /* should null terminate */ |
paul@198 | 347 | |
paul@198 | 348 | /* Return a new string. */ |
paul@198 | 349 | return __new_str(r); |
paul@161 | 350 | } |
paul@161 | 351 | |
paul@161 | 352 | __attr __fn_native__str_lt(__attr __args[]) |
paul@161 | 353 | { |
paul@303 | 354 | __attr * const _data = &__args[1]; |
paul@208 | 355 | __attr * const other = &__args[2]; |
paul@303 | 356 | /* _data, other interpreted as string */ |
paul@303 | 357 | char *s = _data->strvalue; |
paul@303 | 358 | char *o = other->strvalue; |
paul@161 | 359 | |
paul@198 | 360 | /* NOTE: Using simple byte-level string operations. */ |
paul@198 | 361 | return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 362 | } |
paul@161 | 363 | |
paul@161 | 364 | __attr __fn_native__str_gt(__attr __args[]) |
paul@161 | 365 | { |
paul@303 | 366 | __attr * const _data = &__args[1]; |
paul@208 | 367 | __attr * const other = &__args[2]; |
paul@303 | 368 | /* _data, other interpreted as string */ |
paul@303 | 369 | char *s = _data->strvalue; |
paul@303 | 370 | char *o = other->strvalue; |
paul@161 | 371 | |
paul@198 | 372 | /* NOTE: Using simple byte-level string operations. */ |
paul@198 | 373 | return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 374 | } |
paul@161 | 375 | |
paul@161 | 376 | __attr __fn_native__str_eq(__attr __args[]) |
paul@161 | 377 | { |
paul@303 | 378 | __attr * const _data = &__args[1]; |
paul@208 | 379 | __attr * const other = &__args[2]; |
paul@303 | 380 | /* _data, other interpreted as string */ |
paul@303 | 381 | char *s = _data->strvalue; |
paul@303 | 382 | char *o = other->strvalue; |
paul@161 | 383 | |
paul@198 | 384 | /* NOTE: Using simple byte-level string operations. */ |
paul@198 | 385 | return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 386 | } |
paul@161 | 387 | |
paul@161 | 388 | __attr __fn_native__str_len(__attr __args[]) |
paul@161 | 389 | { |
paul@303 | 390 | __attr * const _data = &__args[1]; |
paul@303 | 391 | /* _data interpreted as string */ |
paul@303 | 392 | char *s = _data->strvalue; |
paul@161 | 393 | |
paul@198 | 394 | /* Return the new integer. */ |
paul@198 | 395 | return __new_int(strlen(s)); |
paul@161 | 396 | } |
paul@161 | 397 | |
paul@161 | 398 | __attr __fn_native__str_nonempty(__attr __args[]) |
paul@161 | 399 | { |
paul@303 | 400 | __attr * const _data = &__args[1]; |
paul@303 | 401 | /* _data interpreted as string */ |
paul@303 | 402 | char *s = _data->strvalue; |
paul@161 | 403 | |
paul@198 | 404 | return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 405 | } |
paul@161 | 406 | |
paul@296 | 407 | __attr __fn_native__str_ord(__attr __args[]) |
paul@296 | 408 | { |
paul@303 | 409 | __attr * const _data = &__args[1]; |
paul@303 | 410 | /* _data interpreted as string */ |
paul@303 | 411 | char *s = _data->strvalue; |
paul@296 | 412 | |
paul@296 | 413 | return __new_int((unsigned int) s[0]); |
paul@296 | 414 | } |
paul@296 | 415 | |
paul@292 | 416 | __attr __fn_native__str_substr(__attr __args[]) |
paul@292 | 417 | { |
paul@303 | 418 | __attr * const _data = &__args[1]; |
paul@292 | 419 | __attr * const start = &__args[2]; |
paul@292 | 420 | __attr * const size = &__args[3]; |
paul@303 | 421 | /* _data interpreted as string */ |
paul@303 | 422 | char *s = _data->strvalue, *sub; |
paul@292 | 423 | /* start.__data__ interpreted as int */ |
paul@292 | 424 | int i = __load_via_object(start->value, __pos___data__).intvalue; |
paul@292 | 425 | /* size.__data__ interpreted as int */ |
paul@292 | 426 | int l = __load_via_object(size->value, __pos___data__).intvalue; |
paul@292 | 427 | |
paul@292 | 428 | /* Reserve space for a new string. */ |
paul@292 | 429 | sub = (char *) __ALLOCATE(l + 1, sizeof(char)); |
paul@292 | 430 | strncpy(sub, s + i, l); /* does not null terminate but final byte should be zero */ |
paul@292 | 431 | return __new_str(sub); |
paul@292 | 432 | } |
paul@292 | 433 | |
paul@161 | 434 | __attr __fn_native__list_init(__attr __args[]) |
paul@161 | 435 | { |
paul@208 | 436 | __attr * const size = &__args[1]; |
paul@208 | 437 | /* size.__data__ interpreted as int */ |
paul@208 | 438 | unsigned int n = __load_via_object(size->value, __pos___data__).intvalue; |
paul@283 | 439 | __attr attr = {0, .seqvalue=__new_fragment(n)}; |
paul@206 | 440 | |
paul@283 | 441 | /* Return the __data__ attribute. */ |
paul@161 | 442 | return attr; |
paul@206 | 443 | } |
paul@206 | 444 | |
paul@227 | 445 | __attr __fn_native__list_setsize(__attr __args[]) |
paul@227 | 446 | { |
paul@303 | 447 | __attr * const _data = &__args[1]; |
paul@227 | 448 | __attr * const size = &__args[2]; |
paul@303 | 449 | /* _data interpreted as list */ |
paul@303 | 450 | __fragment *data = _data->seqvalue; |
paul@227 | 451 | /* size.__data__ interpreted as int */ |
paul@227 | 452 | unsigned int n = __load_via_object(size->value, __pos___data__).intvalue; |
paul@227 | 453 | |
paul@227 | 454 | data->size = n; |
paul@227 | 455 | return __builtins___none_None; |
paul@227 | 456 | } |
paul@227 | 457 | |
paul@206 | 458 | __attr __fn_native__list_append(__attr __args[]) |
paul@206 | 459 | { |
paul@208 | 460 | __attr * const self = &__args[1]; |
paul@208 | 461 | __attr * const value = &__args[2]; |
paul@206 | 462 | /* self.__data__ interpreted as list */ |
paul@280 | 463 | __fragment *data = __load_via_object(self->value, __pos___data__).seqvalue; |
paul@283 | 464 | __fragment *newdata = __fragment_append(data, value); |
paul@215 | 465 | |
paul@215 | 466 | /* Replace the __data__ attribute if appropriate. */ |
paul@215 | 467 | if (newdata != data) |
paul@280 | 468 | __store_via_object(self->value, __pos___data__, ((__attr) {0, .seqvalue=newdata})); |
paul@206 | 469 | return __builtins___none_None; |
paul@206 | 470 | } |
paul@206 | 471 | |
paul@206 | 472 | __attr __fn_native__list_concat(__attr __args[]) |
paul@206 | 473 | { |
paul@208 | 474 | __attr * const self = &__args[1]; |
paul@215 | 475 | __attr * const other = &__args[2]; |
paul@303 | 476 | /* self.__data__, other interpreted as list */ |
paul@280 | 477 | __fragment *data = __load_via_object(self->value, __pos___data__).seqvalue; |
paul@303 | 478 | __fragment *other_data = other->seqvalue; |
paul@215 | 479 | __fragment *newdata = data; |
paul@206 | 480 | unsigned int size = data->size, capacity = data->capacity; |
paul@206 | 481 | unsigned int other_size = other_data->size; |
paul@206 | 482 | unsigned int i, j, n; |
paul@206 | 483 | |
paul@206 | 484 | /* Re-allocate the fragment if the capacity has been reached. */ |
paul@206 | 485 | if (size + other_size >= capacity) |
paul@206 | 486 | { |
paul@206 | 487 | n = size + other_size; |
paul@260 | 488 | newdata = (__fragment *) __REALLOCATE(data, __FRAGMENT_SIZE(n)); |
paul@215 | 489 | newdata->capacity = n; |
paul@206 | 490 | } |
paul@206 | 491 | |
paul@206 | 492 | /* Copy the elements from the other list and increment the list size. */ |
paul@206 | 493 | for (i = size, j = 0; j < other_size; i++, j++) |
paul@215 | 494 | newdata->attrs[i] = other_data->attrs[j]; |
paul@215 | 495 | newdata->size = n; |
paul@215 | 496 | |
paul@215 | 497 | /* Replace the __data__ attribute if appropriate. */ |
paul@215 | 498 | if (newdata != data) |
paul@280 | 499 | __store_via_object(self->value, __pos___data__, ((__attr) {0, .seqvalue=newdata})); |
paul@206 | 500 | return __builtins___none_None; |
paul@161 | 501 | } |
paul@161 | 502 | |
paul@161 | 503 | __attr __fn_native__list_len(__attr __args[]) |
paul@161 | 504 | { |
paul@303 | 505 | __attr * const _data = &__args[1]; |
paul@303 | 506 | /* _data interpreted as fragment */ |
paul@303 | 507 | unsigned int size = _data->seqvalue->size; |
paul@161 | 508 | |
paul@182 | 509 | /* Return the new integer. */ |
paul@198 | 510 | return __new_int(size); |
paul@161 | 511 | } |
paul@161 | 512 | |
paul@161 | 513 | __attr __fn_native__list_nonempty(__attr __args[]) |
paul@161 | 514 | { |
paul@303 | 515 | __attr * const _data = &__args[1]; |
paul@161 | 516 | |
paul@303 | 517 | return _data->seqvalue->size ? __builtins___boolean_True : __builtins___boolean_False; |
paul@161 | 518 | } |
paul@161 | 519 | |
paul@161 | 520 | __attr __fn_native__list_element(__attr __args[]) |
paul@161 | 521 | { |
paul@303 | 522 | __attr * const _data = &__args[1]; |
paul@208 | 523 | __attr * const index = &__args[2]; |
paul@303 | 524 | /* _data interpreted as fragment */ |
paul@303 | 525 | __attr *elements = _data->seqvalue->attrs; |
paul@198 | 526 | /* index.__data__ interpreted as int */ |
paul@208 | 527 | int i = __load_via_object(index->value, __pos___data__).intvalue; |
paul@161 | 528 | |
paul@198 | 529 | return elements[i]; |
paul@161 | 530 | } |
paul@161 | 531 | |
paul@227 | 532 | __attr __fn_native__list_setelement(__attr __args[]) |
paul@161 | 533 | { |
paul@303 | 534 | __attr * const _data = &__args[1]; |
paul@227 | 535 | __attr * const index = &__args[2]; |
paul@227 | 536 | __attr * const value = &__args[3]; |
paul@303 | 537 | /* _data interpreted as fragment */ |
paul@303 | 538 | __attr *elements = _data->seqvalue->attrs; |
paul@227 | 539 | /* index.__data__ interpreted as int */ |
paul@227 | 540 | int i = __load_via_object(index->value, __pos___data__).intvalue; |
paul@161 | 541 | |
paul@227 | 542 | /* Set the element. */ |
paul@227 | 543 | elements[i] = *value; |
paul@161 | 544 | return __builtins___none_None; |
paul@161 | 545 | } |
paul@161 | 546 | |
paul@206 | 547 | __attr __fn_native__buffer_str(__attr __args[]) |
paul@206 | 548 | { |
paul@303 | 549 | __attr * const _data = &__args[1]; |
paul@303 | 550 | /* _data interpreted as buffer */ |
paul@303 | 551 | __fragment *data = _data->seqvalue; |
paul@209 | 552 | unsigned int size = 0, i, j, n; |
paul@209 | 553 | char *s, *o; |
paul@206 | 554 | |
paul@206 | 555 | /* Calculate the size of the string. */ |
paul@206 | 556 | for (i = 0; i < data->size; i++) |
paul@253 | 557 | size += strlen(__load_via_object(data->attrs[i].value, __pos___data__).strvalue); |
paul@206 | 558 | |
paul@206 | 559 | /* Reserve space for a new string. */ |
paul@260 | 560 | s = (char *) __ALLOCATE(size + 1, sizeof(char)); |
paul@206 | 561 | |
paul@206 | 562 | /* Build a single string from the buffer contents. */ |
paul@209 | 563 | for (i = 0, j = 0; i < data->size; i++) |
paul@209 | 564 | { |
paul@209 | 565 | o = __load_via_object(data->attrs[i].value, __pos___data__).strvalue; |
paul@209 | 566 | n = strlen(o); |
paul@292 | 567 | strncpy(s + j, o, n); /* does not null terminate but final byte should be zero */ |
paul@209 | 568 | j += n; |
paul@209 | 569 | } |
paul@206 | 570 | |
paul@206 | 571 | /* Return a new string. */ |
paul@206 | 572 | return __new_str(s); |
paul@206 | 573 | } |
paul@206 | 574 | |
paul@230 | 575 | __attr __fn_native__get_using(__attr __args[]) |
paul@230 | 576 | { |
paul@230 | 577 | __attr * const callable = &__args[1]; |
paul@230 | 578 | __attr * const instance = &__args[2]; |
paul@230 | 579 | |
paul@230 | 580 | return __replace_context(instance->value, *callable); |
paul@230 | 581 | } |
paul@230 | 582 | |
paul@228 | 583 | __attr __fn_native__object_getattr(__attr __args[]) |
paul@228 | 584 | { |
paul@228 | 585 | __attr * const obj = &__args[1]; |
paul@228 | 586 | __attr * const name = &__args[2]; |
paul@228 | 587 | __attr * const _default = &__args[3]; |
paul@228 | 588 | |
paul@228 | 589 | /* NOTE: To be written. */ |
paul@228 | 590 | return __builtins___none_None; |
paul@228 | 591 | } |
paul@228 | 592 | |
paul@231 | 593 | static int __issubclass(__ref obj, __attr cls) |
paul@231 | 594 | { |
paul@231 | 595 | return (__HASATTR(obj, __TYPEPOS(cls.value), __TYPECODE(cls.value))); |
paul@231 | 596 | } |
paul@231 | 597 | |
paul@161 | 598 | __attr __fn_native__isinstance(__attr __args[]) |
paul@161 | 599 | { |
paul@208 | 600 | __attr * const obj = &__args[1]; |
paul@208 | 601 | __attr * const cls = &__args[2]; |
paul@161 | 602 | |
paul@231 | 603 | /* cls must be a class. */ |
paul@231 | 604 | if (__is_instance(obj->value) && __issubclass(__get_class(obj->value), *cls)) |
paul@231 | 605 | return __builtins___boolean_True; |
paul@231 | 606 | else |
paul@231 | 607 | return __builtins___boolean_False; |
paul@231 | 608 | } |
paul@231 | 609 | |
paul@231 | 610 | __attr __fn_native__issubclass(__attr __args[]) |
paul@231 | 611 | { |
paul@231 | 612 | __attr * const obj = &__args[1]; |
paul@231 | 613 | __attr * const cls = &__args[2]; |
paul@231 | 614 | |
paul@231 | 615 | /* obj and cls must be classes. */ |
paul@231 | 616 | if (__issubclass(obj->value, *cls)) |
paul@198 | 617 | return __builtins___boolean_True; |
paul@161 | 618 | else |
paul@198 | 619 | return __builtins___boolean_False; |
paul@161 | 620 | } |
paul@161 | 621 | |
paul@173 | 622 | __attr __fn_native__read(__attr __args[]) |
paul@173 | 623 | { |
paul@208 | 624 | __attr * const fd = &__args[1]; |
paul@208 | 625 | __attr * const n = &__args[2]; |
paul@173 | 626 | |
paul@173 | 627 | /* NOTE: To be written. */ |
paul@173 | 628 | return __builtins___none_None; |
paul@173 | 629 | } |
paul@173 | 630 | |
paul@173 | 631 | __attr __fn_native__write(__attr __args[]) |
paul@173 | 632 | { |
paul@208 | 633 | __attr * const fd = &__args[1]; |
paul@208 | 634 | __attr * const str = &__args[2]; |
paul@198 | 635 | /* fd.__data__ interpreted as int */ |
paul@208 | 636 | int i = __load_via_object(fd->value, __pos___data__).intvalue; |
paul@198 | 637 | /* str.__data__ interpreted as string */ |
paul@208 | 638 | char *s = __load_via_object(str->value, __pos___data__).strvalue; |
paul@173 | 639 | |
paul@198 | 640 | write(i, s, sizeof(char) * strlen(s)); |
paul@173 | 641 | return __builtins___none_None; |
paul@173 | 642 | } |
paul@173 | 643 | |
paul@173 | 644 | /* Module initialisation. */ |
paul@173 | 645 | |
paul@161 | 646 | void __main_native() |
paul@161 | 647 | { |
paul@161 | 648 | } |