1.1 --- a/lib/__builtins__/int.py Sun Nov 20 00:09:02 2016 +0100
1.2 +++ b/lib/__builtins__/int.py Sun Nov 20 00:09:36 2016 +0100
1.3 @@ -77,14 +77,14 @@
1.4
1.5 def __rsub__(self, other):
1.6 "Return a new int for the operation."
1.7 - return _binary_op(self, other, native._int_rsub)
1.8 + return _binary_op(other, self, native._int_sub)
1.9
1.10 __mul__ = __rmul__ = __imul__
1.11 __div__ = __idiv__
1.12
1.13 def __rdiv__(self, other):
1.14 "Return a new int for the operation."
1.15 - return _binary_op(self, other, native._int_rdiv)
1.16 + return _binary_op(other, self, native._int_div)
1.17
1.18 def __floordiv__(self, other): pass
1.19 def __rfloordiv__(self, other): pass
1.20 @@ -94,13 +94,13 @@
1.21
1.22 def __rmod__(self, other):
1.23 "Return a new int for the operation."
1.24 - return _binary_op(self, other, native._int_rmod)
1.25 + return _binary_op(other, self, native._int_mod)
1.26
1.27 __pow__ = __ipow__
1.28
1.29 def __rpow__(self, other):
1.30 "Return a new int for the operation."
1.31 - return _binary_op(self, other, native._int_rpow)
1.32 + return _binary_op(other, self, native._int_pow)
1.33
1.34 __and__ = __rand__ = __iand__
1.35 __or__ = __ror__ = __ior__
1.36 @@ -133,7 +133,11 @@
1.37 def __invert__(self): pass
1.38 def __neg__(self): pass
1.39 def __pos__(self): pass
1.40 - def __str__(self): pass
1.41 +
1.42 + def __str__(self):
1.43 + "Return a string representation."
1.44 + return native._int_str(self)
1.45 +
1.46 def __lshift__(self): pass
1.47 def __rlshift__(self): pass
1.48 def __rshift__(self): pass
1.49 @@ -143,6 +147,6 @@
1.50
1.51 def __bool__(self):
1.52 "Return whether this int is non-zero."
1.53 - return _negate(native._int_eq(self, 0))
1.54 + return native._int_ne(self, 0)
1.55
1.56 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/lib/native.py Sun Nov 20 00:09:02 2016 +0100
3.2 +++ b/lib/native.py Sun Nov 20 00:09:36 2016 +0100
3.3 @@ -33,11 +33,6 @@
3.4 def _int_pow(self, other): pass
3.5 def _int_sub(self, other): pass
3.6
3.7 -def _int_rdiv(self, other): pass
3.8 -def _int_rmod(self, other): pass
3.9 -def _int_rpow(self, other): pass
3.10 -def _int_rsub(self, other): pass
3.11 -
3.12 def _int_and(self, other): pass
3.13 def _int_or(self, other): pass
3.14 def _int_xor(self, other): pass
3.15 @@ -45,6 +40,9 @@
3.16 def _int_lt(self, other): pass
3.17 def _int_gt(self, other): pass
3.18 def _int_eq(self, other): pass
3.19 +def _int_ne(self, other): pass
3.20 +
3.21 +def _int_str(self): pass
3.22
3.23 def _str_add(self, other): pass
3.24 def _str_lt(self, other): pass
6.1 --- a/templates/native.c Sun Nov 20 00:09:02 2016 +0100
6.2 +++ b/templates/native.c Sun Nov 20 00:09:36 2016 +0100
6.3 @@ -1,5 +1,8 @@
6.4 #include <stdlib.h> /* calloc, exit */
6.5 #include <unistd.h> /* read, write */
6.6 +#include <math.h> /* ceil, log10, pow */
6.7 +#include <string.h> /* strcmp, strlen */
6.8 +#include <stdio.h> /* snprintf */
6.9 #include "types.h"
6.10 #include "exceptions.h"
6.11 #include "ops.h"
6.12 @@ -8,6 +11,24 @@
6.13 #include "progtypes.h"
6.14 #include "main.h"
6.15
6.16 +/* Utility functions. */
6.17 +
6.18 +inline __attr __new_int(int i)
6.19 +{
6.20 + /* Create a new integer and mutate the __data__ attribute. */
6.21 + __attr attr = __new(&__InstanceTable___builtins___int_int, &__builtins___int_int, sizeof(__obj___builtins___int_int));
6.22 + attr.value->attrs[__pos___data__].intvalue = i;
6.23 + return attr;
6.24 +}
6.25 +
6.26 +inline __attr __new_str(char *s)
6.27 +{
6.28 + /* Create a new string and mutate the __data__ attribute. */
6.29 + __attr attr = __new(&__InstanceTable___builtins___str_string, &__builtins___str_string, sizeof(__obj___builtins___str_string));
6.30 + attr.value->attrs[__pos___data__].strvalue = s;
6.31 + return attr;
6.32 +}
6.33 +
6.34 /* Native functions. */
6.35
6.36 __attr __fn_native__exit(__attr __args[])
6.37 @@ -61,9 +82,13 @@
6.38 {
6.39 #define self (__args[1])
6.40 #define other (__args[2])
6.41 + /* self.__data__ and other.__data__ interpreted as int */
6.42 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.43 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.44
6.45 - /* NOTE: To be written. */
6.46 - return __builtins___none_None;
6.47 + /* Return the new integer. */
6.48 + /* NOTE: No overflow test applied. */
6.49 + return __new_int(i + j);
6.50 #undef self
6.51 #undef other
6.52 }
6.53 @@ -72,9 +97,13 @@
6.54 {
6.55 #define self (__args[1])
6.56 #define other (__args[2])
6.57 + /* self.__data__ and other.__data__ interpreted as int */
6.58 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.59 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.60
6.61 - /* NOTE: To be written. */
6.62 - return __builtins___none_None;
6.63 + /* Return the new integer. */
6.64 + /* NOTE: No overflow test applied. */
6.65 + return __new_int(i - j);
6.66 #undef self
6.67 #undef other
6.68 }
6.69 @@ -83,9 +112,13 @@
6.70 {
6.71 #define self (__args[1])
6.72 #define other (__args[2])
6.73 + /* self.__data__ and other.__data__ interpreted as int */
6.74 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.75 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.76
6.77 - /* NOTE: To be written. */
6.78 - return __builtins___none_None;
6.79 + /* Return the new integer. */
6.80 + /* NOTE: No overflow test applied. */
6.81 + return __new_int(i * j);
6.82 #undef self
6.83 #undef other
6.84 }
6.85 @@ -94,9 +127,13 @@
6.86 {
6.87 #define self (__args[1])
6.88 #define other (__args[2])
6.89 + /* self.__data__ and other.__data__ interpreted as int */
6.90 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.91 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.92
6.93 - /* NOTE: To be written. */
6.94 - return __builtins___none_None;
6.95 + /* Return the new integer. */
6.96 + /* NOTE: No overflow test applied. */
6.97 + return __new_int(i / j);
6.98 #undef self
6.99 #undef other
6.100 }
6.101 @@ -105,9 +142,13 @@
6.102 {
6.103 #define self (__args[1])
6.104 #define other (__args[2])
6.105 + /* self.__data__ and other.__data__ interpreted as int */
6.106 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.107 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.108
6.109 - /* NOTE: To be written. */
6.110 - return __builtins___none_None;
6.111 + /* Return the new integer. */
6.112 + /* NOTE: No overflow test applied. */
6.113 + return __new_int(i % j);
6.114 #undef self
6.115 #undef other
6.116 }
6.117 @@ -116,9 +157,13 @@
6.118 {
6.119 #define self (__args[1])
6.120 #define other (__args[2])
6.121 + /* self.__data__ and other.__data__ interpreted as int */
6.122 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.123 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.124
6.125 - /* NOTE: To be written. */
6.126 - return __builtins___none_None;
6.127 + /* Return the new integer. */
6.128 + /* NOTE: No overflow test applied. */
6.129 + return __new_int((int) pow(i, j));
6.130 #undef self
6.131 #undef other
6.132 }
6.133 @@ -127,9 +172,13 @@
6.134 {
6.135 #define self (__args[1])
6.136 #define other (__args[2])
6.137 + /* self.__data__ and other.__data__ interpreted as int */
6.138 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.139 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.140
6.141 - /* NOTE: To be written. */
6.142 - return __builtins___none_None;
6.143 + /* Return the new integer. */
6.144 + /* NOTE: No overflow test applied. */
6.145 + return __new_int(i & j);
6.146 #undef self
6.147 #undef other
6.148 }
6.149 @@ -138,9 +187,13 @@
6.150 {
6.151 #define self (__args[1])
6.152 #define other (__args[2])
6.153 + /* self.__data__ and other.__data__ interpreted as int */
6.154 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.155 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.156
6.157 - /* NOTE: To be written. */
6.158 - return __builtins___none_None;
6.159 + /* Return the new integer. */
6.160 + /* NOTE: No overflow test applied. */
6.161 + return __new_int(i | j);
6.162 #undef self
6.163 #undef other
6.164 }
6.165 @@ -149,53 +202,13 @@
6.166 {
6.167 #define self (__args[1])
6.168 #define other (__args[2])
6.169 -
6.170 - /* NOTE: To be written. */
6.171 - return __builtins___none_None;
6.172 - #undef self
6.173 - #undef other
6.174 -}
6.175 -
6.176 -__attr __fn_native__int_rsub(__attr __args[])
6.177 -{
6.178 - #define self (__args[1])
6.179 - #define other (__args[2])
6.180 -
6.181 - /* NOTE: To be written. */
6.182 - return __builtins___none_None;
6.183 - #undef self
6.184 - #undef other
6.185 -}
6.186 -
6.187 -__attr __fn_native__int_rdiv(__attr __args[])
6.188 -{
6.189 - #define self (__args[1])
6.190 - #define other (__args[2])
6.191 + /* self.__data__ and other.__data__ interpreted as int */
6.192 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.193 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.194
6.195 - /* NOTE: To be written. */
6.196 - return __builtins___none_None;
6.197 - #undef self
6.198 - #undef other
6.199 -}
6.200 -
6.201 -__attr __fn_native__int_rmod(__attr __args[])
6.202 -{
6.203 - #define self (__args[1])
6.204 - #define other (__args[2])
6.205 -
6.206 - /* NOTE: To be written. */
6.207 - return __builtins___none_None;
6.208 - #undef self
6.209 - #undef other
6.210 -}
6.211 -
6.212 -__attr __fn_native__int_rpow(__attr __args[])
6.213 -{
6.214 - #define self (__args[1])
6.215 - #define other (__args[2])
6.216 -
6.217 - /* NOTE: To be written. */
6.218 - return __builtins___none_None;
6.219 + /* Return the new integer. */
6.220 + /* NOTE: No overflow test applied. */
6.221 + return __new_int(i ^ j);
6.222 #undef self
6.223 #undef other
6.224 }
6.225 @@ -204,9 +217,12 @@
6.226 {
6.227 #define self (__args[1])
6.228 #define other (__args[2])
6.229 + /* self.__data__ and other.__data__ interpreted as int */
6.230 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.231 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.232
6.233 - /* NOTE: To be written. */
6.234 - return __builtins___none_None;
6.235 + /* Return a boolean result. */
6.236 + return i < j ? __builtins___boolean_True : __builtins___boolean_False;
6.237 #undef self
6.238 #undef other
6.239 }
6.240 @@ -215,9 +231,12 @@
6.241 {
6.242 #define self (__args[1])
6.243 #define other (__args[2])
6.244 + /* self.__data__ and other.__data__ interpreted as int */
6.245 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.246 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.247
6.248 - /* NOTE: To be written. */
6.249 - return __builtins___none_None;
6.250 + /* Return a boolean result. */
6.251 + return i > j ? __builtins___boolean_True : __builtins___boolean_False;
6.252 #undef self
6.253 #undef other
6.254 }
6.255 @@ -226,9 +245,43 @@
6.256 {
6.257 #define self (__args[1])
6.258 #define other (__args[2])
6.259 + /* self.__data__ and other.__data__ interpreted as int */
6.260 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.261 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.262
6.263 - /* NOTE: To be written. */
6.264 - return __builtins___none_None;
6.265 + /* Return a boolean result. */
6.266 + return i == j ? __builtins___boolean_True : __builtins___boolean_False;
6.267 + #undef self
6.268 + #undef other
6.269 +}
6.270 +
6.271 +__attr __fn_native__int_ne(__attr __args[])
6.272 +{
6.273 + #define self (__args[1])
6.274 + #define other (__args[2])
6.275 + /* self.__data__ and other.__data__ interpreted as int */
6.276 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.277 + int j = __load_via_object(other.value, __pos___data__).intvalue;
6.278 +
6.279 + /* Return a boolean result. */
6.280 + return i != j ? __builtins___boolean_True : __builtins___boolean_False;
6.281 + #undef self
6.282 + #undef other
6.283 +}
6.284 +
6.285 +__attr __fn_native__int_str(__attr __args[])
6.286 +{
6.287 + #define self (__args[1])
6.288 + /* self.__data__ interpreted as int */
6.289 + int i = __load_via_object(self.value, __pos___data__).intvalue;
6.290 + int n = i != 0 ? (int) ceil(log10(i+1)) + 1 : 2;
6.291 + char *s = calloc(n, sizeof(char));
6.292 +
6.293 + if (i < 0) n++;
6.294 + snprintf(s, n, "%d", i);
6.295 +
6.296 + /* Return a new string. */
6.297 + return __new_str(s);
6.298 #undef self
6.299 #undef other
6.300 }
6.301 @@ -237,9 +290,17 @@
6.302 {
6.303 #define self (__args[1])
6.304 #define other (__args[2])
6.305 + /* self.__data__, other.__data__ interpreted as string */
6.306 + char *s = __load_via_object(self.value, __pos___data__).strvalue;
6.307 + char *o = __load_via_object(other.value, __pos___data__).strvalue;
6.308 + int n = strlen(s) + strlen(o) + 1;
6.309 + char *r = calloc(n, sizeof(char));
6.310
6.311 - /* NOTE: To be written. */
6.312 - return __builtins___none_None;
6.313 + strncpy(r, s, n);
6.314 + strncpy(r + strlen(s), o, n - strlen(s));
6.315 +
6.316 + /* Return a new string. */
6.317 + return __new_str(r);
6.318 #undef self
6.319 #undef other
6.320 }
6.321 @@ -248,9 +309,12 @@
6.322 {
6.323 #define self (__args[1])
6.324 #define other (__args[2])
6.325 + /* self.__data__, other.__data__ interpreted as string */
6.326 + char *s = __load_via_object(self.value, __pos___data__).strvalue;
6.327 + char *o = __load_via_object(other.value, __pos___data__).strvalue;
6.328
6.329 - /* NOTE: To be written. */
6.330 - return __builtins___none_None;
6.331 + /* NOTE: Using simple byte-level string operations. */
6.332 + return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False;
6.333 #undef self
6.334 #undef other
6.335 }
6.336 @@ -259,9 +323,12 @@
6.337 {
6.338 #define self (__args[1])
6.339 #define other (__args[2])
6.340 + /* self.__data__, other.__data__ interpreted as string */
6.341 + char *s = __load_via_object(self.value, __pos___data__).strvalue;
6.342 + char *o = __load_via_object(other.value, __pos___data__).strvalue;
6.343
6.344 - /* NOTE: To be written. */
6.345 - return __builtins___none_None;
6.346 + /* NOTE: Using simple byte-level string operations. */
6.347 + return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False;
6.348 #undef self
6.349 #undef other
6.350 }
6.351 @@ -270,9 +337,12 @@
6.352 {
6.353 #define self (__args[1])
6.354 #define other (__args[2])
6.355 + /* self.__data__, other.__data__ interpreted as string */
6.356 + char *s = __load_via_object(self.value, __pos___data__).strvalue;
6.357 + char *o = __load_via_object(other.value, __pos___data__).strvalue;
6.358
6.359 - /* NOTE: To be written. */
6.360 - return __builtins___none_None;
6.361 + /* NOTE: Using simple byte-level string operations. */
6.362 + return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False;
6.363 #undef self
6.364 #undef other
6.365 }
6.366 @@ -280,25 +350,28 @@
6.367 __attr __fn_native__str_len(__attr __args[])
6.368 {
6.369 #define self (__args[1])
6.370 + /* self.__data__ interpreted as string */
6.371 + char *s = __load_via_object(self.value, __pos___data__).strvalue;
6.372
6.373 - /* NOTE: To be written. */
6.374 - return __builtins___none_None;
6.375 + /* Return the new integer. */
6.376 + return __new_int(strlen(s));
6.377 #undef self
6.378 }
6.379
6.380 __attr __fn_native__str_nonempty(__attr __args[])
6.381 {
6.382 #define self (__args[1])
6.383 + /* self.__data__ interpreted as string */
6.384 + char *s = __load_via_object(self.value, __pos___data__).strvalue;
6.385
6.386 - /* NOTE: To be written. */
6.387 - return __builtins___none_None;
6.388 + return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False;
6.389 #undef self
6.390 }
6.391
6.392 __attr __fn_native__list_init(__attr __args[])
6.393 {
6.394 #define size (__args[1])
6.395 - /* size.__data__ interpreted as int */
6.396 + /* size.__data__ interpreted as fragment */
6.397 __fragment *data = calloc(__load_via_object(size.value, __pos___data__).intvalue, sizeof(__attr));
6.398 __attr attr = {0, .data=data};
6.399
6.400 @@ -312,12 +385,8 @@
6.401 /* self.__data__ interpreted as fragment */
6.402 unsigned int size = __load_via_object(self.value, __pos___data__).data->size;
6.403
6.404 - /* Create a new integer and mutate the __data__ attribute. */
6.405 - __attr length = __new(&__InstanceTable___builtins___int_int, &__builtins___int_int, sizeof(__obj___builtins___int_int));
6.406 - length.value->attrs[__pos___data__].intvalue = size;
6.407 -
6.408 /* Return the new integer. */
6.409 - return length;
6.410 + return __new_int(size);
6.411 #undef self
6.412 }
6.413
6.414 @@ -333,9 +402,12 @@
6.415 {
6.416 #define self (__args[1])
6.417 #define index (__args[2])
6.418 + /* self.__data__ interpreted as fragment */
6.419 + __attr *elements = __load_via_object(self.value, __pos___data__).data->attrs;
6.420 + /* index.__data__ interpreted as int */
6.421 + int i = __load_via_object(index.value, __pos___data__).intvalue;
6.422
6.423 - /* NOTE: To be written. */
6.424 - return __builtins___none_None;
6.425 + return elements[i];
6.426 #undef self
6.427 #undef index
6.428 }
6.429 @@ -352,7 +424,7 @@
6.430 __attr __fn_native__tuple_init(__attr __args[])
6.431 {
6.432 #define size (__args[1])
6.433 - /* size.__data__ interpreted as int */
6.434 + /* size.__data__ interpreted as fragment */
6.435 __fragment *data = calloc(__load_via_object(size.value, __pos___data__).intvalue, sizeof(__attr));
6.436 __attr attr = {0, .data=data};
6.437
6.438 @@ -363,9 +435,11 @@
6.439 __attr __fn_native__tuple_len(__attr __args[])
6.440 {
6.441 #define self (__args[1])
6.442 + /* self.__data__ interpreted as fragment */
6.443 + unsigned int size = __load_via_object(self.value, __pos___data__).data->size;
6.444
6.445 - /* NOTE: To be written. */
6.446 - return __builtins___none_None;
6.447 + /* Return the new integer. */
6.448 + return __new_int(size);
6.449 #undef self
6.450 }
6.451
6.452 @@ -373,9 +447,12 @@
6.453 {
6.454 #define self (__args[1])
6.455 #define index (__args[2])
6.456 + /* self.__data__ interpreted as fragment */
6.457 + __attr *elements = __load_via_object(self.value, __pos___data__).data->attrs;
6.458 + /* index.__data__ interpreted as int */
6.459 + int i = __load_via_object(index.value, __pos___data__).intvalue;
6.460
6.461 - /* NOTE: To be written. */
6.462 - return __builtins___none_None;
6.463 + return elements[i];
6.464 #undef self
6.465 #undef index
6.466 }
6.467 @@ -386,9 +463,9 @@
6.468 #define cls (__args[2])
6.469
6.470 if (__is_instance(obj.value) && __HASATTR(__get_class(obj.value), __TYPEPOS(cls.value), __TYPECODE(cls.value)))
6.471 - return obj;
6.472 + return __builtins___boolean_True;
6.473 else
6.474 - return __builtins___none_None;
6.475 + return __builtins___boolean_False;
6.476 #undef obj
6.477 #undef cls
6.478 }
6.479 @@ -408,8 +485,12 @@
6.480 {
6.481 #define fd (__args[1])
6.482 #define str (__args[2])
6.483 + /* fd.__data__ interpreted as int */
6.484 + int i = __load_via_object(fd.value, __pos___data__).intvalue;
6.485 + /* str.__data__ interpreted as string */
6.486 + char *s = __load_via_object(str.value, __pos___data__).strvalue;
6.487
6.488 - /* NOTE: To be written. */
6.489 + write(i, s, sizeof(char) * strlen(s));
6.490 return __builtins___none_None;
6.491 #undef fd
6.492 #undef str