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