diff --git a/CMakeLists.txt b/CMakeLists.txt index 01c00142e..88a3109ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ file(GLOB std_srcs src/std/ucs2.c src/std/thread.c src/std/process.c + src/std/num.c ) if(CMAKE_SYSTEM_NAME MATCHES "Darwin") diff --git a/Makefile b/Makefile index 50dfb4d27..28de81d19 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ RUNTIME = src/gc.o STD = src/std/array.o src/std/buffer.o src/std/bytes.o src/std/cast.o src/std/date.o src/std/error.o src/std/debug.o \ src/std/file.o src/std/fun.o src/std/maps.o src/std/math.o src/std/obj.o src/std/random.o src/std/regexp.o \ src/std/socket.o src/std/string.o src/std/sys.o src/std/types.o src/std/ucs2.o src/std/thread.o src/std/process.o \ - src/std/track.o + src/std/track.o src/std/num.o HL = src/code.o src/jit.o src/main.o src/module.o src/debugger.o src/profile.o diff --git a/src/std/num.c b/src/std/num.c new file mode 100644 index 000000000..33805b645 --- /dev/null +++ b/src/std/num.c @@ -0,0 +1,107 @@ +#include +#include +#include + +// I64 + +HL_PRIM int64 hl_num_i64_of_int( int i ) { + return i; +} +DEFINE_PRIM(_I64, num_i64_of_int, _I32); + +HL_PRIM int64 hl_num_i64_max() { + return LLONG_MAX; +} +DEFINE_PRIM(_I64, num_i64_max, _NO_ARG); + +HL_PRIM int64 hl_num_i64_min() { + return LLONG_MIN; +} +DEFINE_PRIM(_I64, num_i64_min, _NO_ARG); + +HL_PRIM int64 hl_num_i64_of_string( vstring *s ) { + if( !s ) + hl_null_access(); + return strtoll(hl_to_utf8(s->bytes), NULL, 10); +} +DEFINE_PRIM(_I64, num_i64_of_string, _STRING); + +HL_PRIM int64 hl_num_i64_add( int64 a, int64 b ) { + return a + b; +} +DEFINE_PRIM(_I64, num_i64_add, _I64 _I64); + +HL_PRIM int64 hl_num_i64_sub( int64 a, int64 b ) { + return a - b; +} +DEFINE_PRIM(_I64, num_i64_sub, _I64 _I64); + +HL_PRIM int64 hl_num_i64_mul( int64 a, int64 b ) { + return a * b; +} +DEFINE_PRIM(_I64, num_i64_mul, _I64 _I64); + +HL_PRIM int64 hl_num_i64_mod( int64 a, int64 b ) { + return a % b; +} +DEFINE_PRIM(_I64, num_i64_mod, _I64 _I64); + +HL_PRIM int64 hl_num_i64_div( int64 a, int64 b ) { + return a / b; +} +DEFINE_PRIM(_I64, num_i64_div, _I64 _I64); + +HL_PRIM int64 hl_num_i64_logand( int64 a, int64 b ) { + return a & b; +} +DEFINE_PRIM(_I64, num_i64_logand, _I64 _I64); + +HL_PRIM int64 hl_num_i64_logor( int64 a, int64 b ) { + return a | b; +} +DEFINE_PRIM(_I64, num_i64_logor, _I64 _I64); + +HL_PRIM int64 hl_num_i64_logxor( int64 a, int64 b ) { + return a ^ b; +} +DEFINE_PRIM(_I64, num_i64_logxor, _I64 _I64); + +HL_PRIM int64 hl_num_i64_shift_left( int64 a, int b ) { + return a << b; +} +DEFINE_PRIM(_I64, num_i64_shift_left, _I64 _I32); + +HL_PRIM int64 hl_num_i64_shift_right( int64 a, int b ) { + return a >> b; +} +DEFINE_PRIM(_I64, num_i64_shift_right, _I64 _I32); + +HL_PRIM int64 hl_num_i64_lognot( int64 a ) { + return ~a; +} +DEFINE_PRIM(_I64, num_i64_lognot, _I64); + +HL_PRIM bool hl_num_i64_eq( int64 a, int64 b ) { + return a == b; +} +DEFINE_PRIM(_BOOL, num_i64_eq, _I64 _I64); + +HL_PRIM bool hl_num_i64_lt( int64 a, int64 b ) { + return a < b; +} +DEFINE_PRIM(_BOOL, num_i64_lt, _I64 _I64); + +HL_PRIM bool hl_num_i64_gt( int64 a, int64 b ) { + return a > b; +} +DEFINE_PRIM(_BOOL, num_i64_gt, _I64 _I64); + +HL_PRIM bool hl_num_i64_lte( int64 a, int64 b ) { + return a <= b; +} +DEFINE_PRIM(_BOOL, num_i64_lte, _I64 _I64); + +HL_PRIM bool hl_num_i64_gte( int64 a, int64 b ) { + return a >= b; +} +DEFINE_PRIM(_BOOL, num_i64_gte, _I64 _I64); \ No newline at end of file diff --git a/src/std/ucs2.c b/src/std/ucs2.c index b009801dc..d34667231 100644 --- a/src/std/ucs2.c +++ b/src/std/ucs2.c @@ -105,7 +105,7 @@ int utoi( const uchar *str, uchar **end ) { int ucmp( const uchar *a, const uchar *b ) { while(true) { - int d = (unsigned)*a - (unsigned)*b; + int d = (unsigned)*a - (unsigned)*b; if( d ) return d; if( !*a ) return 0; a++; @@ -202,7 +202,15 @@ HL_PRIM int uvszprintf( uchar *out, int out_size, const uchar *fmt, va_list argl switch( c ) { case 'd': cfmt[i++] = 0; - size = sprintf(tmp,cfmt,va_arg(arglist,int)); + if( cfmt[i - 3] == 'l') { + if( cfmt[i - 4] == 'l' ) { + size = sprintf(tmp,cfmt,va_arg(arglist,long long)); + } else { + size = sprintf(tmp,cfmt,va_arg(arglist,long)); + } + } else { + size = sprintf(tmp,cfmt,va_arg(arglist,int)); + } goto sprintf_add; case 'f': cfmt[i++] = 0;