core: add .clang-format (references #499)

This commit is contained in:
hellerve 2019-10-30 08:47:36 +01:00
parent dd9c4616e8
commit 65946224f0
17 changed files with 828 additions and 922 deletions

19
.clang-format Normal file
View File

@ -0,0 +1,19 @@
---
BasedOnStyle: Google
AlignEscapedNewlinesLeft: 'true'
AlignTrailingComments: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLoopsOnASingleLine: 'true'
BinPackParameters: 'true'
ColumnLimit: '80'
PointerAlignment: Left
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpacesInContainerLiterals: 'true'
Standard: Cpp11
TabWidth: '4'
IndentWidth: '4'
UseTab: Never

View File

@ -7,3 +7,7 @@ then
cabal build
fi
fi
if which clang-format > /dev/null || false
then
clang-format -i core/*.h
fi

View File

@ -7,27 +7,17 @@ SDL_Event SDL_Event_init() {
return e;
}
int SDL_Event_type(SDL_Event *e) {
return e->type;
}
int SDL_Event_type(SDL_Event *e) { return e->type; }
SDL_Keycode SDL_Event_keycode(SDL_Event *e) {
return e->key.keysym.sym;
}
SDL_Keycode SDL_Event_keycode(SDL_Event *e) { return e->key.keysym.sym; }
bool SDL_Event__EQ_(SDL_EventType a, SDL_EventType b) {
return a == b;
}
bool SDL_Event__EQ_(SDL_EventType a, SDL_EventType b) { return a == b; }
SDL_Event SDL_Event_copy(SDL_Event *e) {
return *e;
}
SDL_Event SDL_Event_copy(SDL_Event *e) { return *e; }
// Keycode
SDL_Keycode SDL_Keycode_copy(SDL_Keycode* a) {
return *a;
}
SDL_Keycode SDL_Keycode_copy(SDL_Keycode *a) { return *a; }
String SDL_Keycode_str(SDL_Keycode a) {
char *buffer = CARP_MALLOC(32);
@ -70,26 +60,14 @@ SDL_Color SDL_rgba(int r, int g, int b, int a) {
return p;
}
int SDL_Color_r(SDL_Color *col) {
return (int)(col->r);
}
int SDL_Color_r(SDL_Color *col) { return (int)(col->r); }
int SDL_Color_g(SDL_Color *col) {
return (int)(col->g);
}
int SDL_Color_g(SDL_Color *col) { return (int)(col->g); }
int SDL_Color_b(SDL_Color *col) {
return (int)(col->b);
}
int SDL_Color_b(SDL_Color *col) { return (int)(col->b); }
int SDL_Color_a(SDL_Color *col) {
return (int)(col->a);
}
int SDL_Color_a(SDL_Color *col) { return (int)(col->a); }
void* SDL_SurfacePixels(SDL_Surface* s) {
return s->pixels;
}
void *SDL_SurfacePixels(SDL_Surface *s) { return s->pixels; }
int SDL_SurfacePitch(SDL_Surface* s) {
return s->pitch;
}
int SDL_SurfacePitch(SDL_Surface *s) { return s->pitch; }

View File

@ -1,5 +1,5 @@
double get_MINUS_time_MINUS_elapsed() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
}

View File

@ -1,20 +1,10 @@
// Bool
bool Bool_copy(const bool* b) {
return *b;
}
bool Bool_copy(const bool* b) { return *b; }
bool Bool__EQ_(bool a, bool b) {
return a == b;
}
bool Bool__EQ_(bool a, bool b) { return a == b; }
bool Bool_not(bool a) {
return !a;
}
bool Bool_not(bool a) { return !a; }
bool Bool_and(bool a, bool b) {
return a && b;
}
bool Bool_and(bool a, bool b) { return a && b; }
bool Bool_or(bool a, bool b) {
return a && b;
}
bool Bool_or(bool a, bool b) { return a && b; }

View File

@ -1,29 +1,17 @@
bool Char__EQ_(char a, char b) {
return a == b;
}
bool Char__EQ_(char a, char b) { return a == b; }
bool Char__LT_(char a, char b) {
return a < b;
}
bool Char__LT_(char a, char b) { return a < b; }
bool Char__GT_(char a, char b) {
return a > b;
}
bool Char__GT_(char a, char b) { return a > b; }
int Char_to_MINUS_int(char c) {
return (int)(unsigned char)c;
}
int Char_to_MINUS_int(char c) { return (int)(unsigned char)c; }
char Char_from_MINUS_int(int i) {
return (char)i;
}
char Char_from_MINUS_int(int i) { return (char)i; }
char Char_copy(const char *c) {
return *c;
}
char Char_copy(const char *c) { return *c; }
String PtrChar_str(const char *c) {
size_t len = strlen(c) + 1;
String ptr = CARP_MALLOC(len);
return (String) memcpy(ptr, c, len);
return (String)memcpy(ptr, c, len);
}

View File

@ -9,16 +9,12 @@ bool Double__GT_(double x, double y) { return x > y; }
bool Double__EQ_(double x, double y) { return x == y; }
double Double_neg(double x) { return -x; }
double Double_copy(const double *x) { return *x; }
double Double_copy(const double* x) { return *x; }
// Double.toInt : Double -> Int
int Double_to_MINUS_int(double x) {
return (int)x;
}
int Double_to_MINUS_int(double x) { return (int)x; }
double Double_from_MINUS_int(int x) {
return (double)x;
}
double Double_from_MINUS_int(int x) { return (double)x; }
long Double_to_MINUS_bytes(double x) {
long y;
@ -26,102 +22,52 @@ long Double_to_MINUS_bytes(double x) {
return y;
}
float Double_to_MINUS_float(double x) {
return (float)x;
}
float Double_to_MINUS_float(double x) { return (float)x; }
double Double_from_MINUS_float(float x) {
return (double)x;
}
double Double_from_MINUS_float(float x) { return (double)x; }
long Double_to_MINUS_long(double x) {
return (long)x;
}
long Double_to_MINUS_long(double x) { return (long)x; }
double Double_from_MINUS_long(long x) {
return (double)x;
}
double Double_from_MINUS_long(long x) { return (double)x; }
double Double_abs(double x) {
return x > 0.0 ? x : -x;
}
double Double_abs(double x) { return x > 0.0 ? x : -x; }
double Double_acos(double x) {
return acos(x);
}
double Double_acos(double x) { return acos(x); }
double Double_asin(double x) {
return asin(x);
}
double Double_asin(double x) { return asin(x); }
double Double_atan(double x) {
return atan(x);
}
double Double_atan(double x) { return atan(x); }
double Double_atan2(double y, double x) {
return atan2(y, x);
}
double Double_atan2(double y, double x) { return atan2(y, x); }
double Double_cos(double x) {
return cos(x);
}
double Double_cos(double x) { return cos(x); }
double Double_cosh(double x) {
return cosh(x);
}
double Double_cosh(double x) { return cosh(x); }
double Double_sin(double x) {
return sin(x);
}
double Double_sin(double x) { return sin(x); }
double Double_sinh(double x) {
return sinh(x);
}
double Double_sinh(double x) { return sinh(x); }
double Double_tanh(double x) {
return tanh(x);
}
double Double_tanh(double x) { return tanh(x); }
double Double_exp(double x) {
return exp(x);
}
double Double_exp(double x) { return exp(x); }
double Double_frexp(double x, int* exponent) {
return frexp(x, exponent);
}
double Double_frexp(double x, int* exponent) { return frexp(x, exponent); }
double Double_ldexp(double x, int exponent) {
return ldexp(x, exponent);
}
double Double_ldexp(double x, int exponent) { return ldexp(x, exponent); }
double Double_log(double x) {
return log(x);
}
double Double_log(double x) { return log(x); }
double Double_log10(double x) {
return log10(x);
}
double Double_log10(double x) { return log10(x); }
double Double_modf(double x, double* integer) {
return modf(x, integer);
}
double Double_modf(double x, double* integer) { return modf(x, integer); }
double Double_pow(double x, double y) {
return pow(x, y);
}
double Double_pow(double x, double y) { return pow(x, y); }
double Double_sqrt(double x) {
return sqrt(x);
}
double Double_sqrt(double x) { return sqrt(x); }
double Double_ceil(double x) {
return ceil(x);
}
double Double_ceil(double x) { return ceil(x); }
double Double_floor(double x) {
return floor(x);
}
double Double_floor(double x) { return floor(x); }
double Double_mod(double x, double y) {
return fmod(x, y);
}
double Double_mod(double x, double y) { return fmod(x, y); }

View File

@ -9,15 +9,11 @@ bool Float__GT_(float x, float y) { return x > y; }
bool Float__EQ_(float x, float y) { return x == y; }
float Float_neg(float x) { return -x; }
float Float_copy(const float *x) { return *x; }
float Float_copy(const float* x) { return *x; }
int Float_to_MINUS_int(float x) {
return (int)x;
}
int Float_to_MINUS_int(float x) { return (int)x; }
float Float_from_MINUS_int(int x) {
return (float)x;
}
float Float_from_MINUS_int(int x) { return (float)x; }
int Float_to_MINUS_bytes(float x) {
int y;
@ -25,90 +21,46 @@ int Float_to_MINUS_bytes(float x) {
return y;
}
float Float_abs(float x) {
return fabsf(x);
}
float Float_abs(float x) { return fabsf(x); }
float Float_acos(float x) {
return acosf(x);
}
float Float_acos(float x) { return acosf(x); }
float Float_asin(float x) {
return asinf(x);
}
float Float_asin(float x) { return asinf(x); }
float Float_atan(float x) {
return atanf(x);
}
float Float_atan(float x) { return atanf(x); }
float Float_atan2(float y, float x) {
return atan2f(y, x);
}
float Float_atan2(float y, float x) { return atan2f(y, x); }
float Float_cos(float x) {
return cosf(x);
}
float Float_cos(float x) { return cosf(x); }
float Float_cosh(float x) {
return coshf(x);
}
float Float_cosh(float x) { return coshf(x); }
float Float_sin(float x) {
return sinf(x);
}
float Float_sin(float x) { return sinf(x); }
float Float_sinh(float x) {
return sinhf(x);
}
float Float_sinh(float x) { return sinhf(x); }
float Float_tan(float x) {
return tanf(x);
}
float Float_tan(float x) { return tanf(x); }
float Float_tanh(float x) {
return tanhf(x);
}
float Float_tanh(float x) { return tanhf(x); }
float Float_exp(float x) {
return expf(x);
}
float Float_exp(float x) { return expf(x); }
float Float_frexp(float x, int* exponent) {
return frexpf(x, exponent);
}
float Float_frexp(float x, int* exponent) { return frexpf(x, exponent); }
float Float_ldexp(float x, int exponent) {
return ldexpf(x, exponent);
}
float Float_ldexp(float x, int exponent) { return ldexpf(x, exponent); }
float Float_log(float x) {
return logf(x);
}
float Float_log(float x) { return logf(x); }
float Float_log10(float x) {
return log10f(x);
}
float Float_log10(float x) { return log10f(x); }
float Float_modf(float x, float * integer) {
return modff(x, integer);
}
float Float_modf(float x, float* integer) { return modff(x, integer); }
float Float_pow(float x, float y) {
return powf(x, y);
}
float Float_pow(float x, float y) { return powf(x, y); }
float Float_sqrt(float x) {
return sqrtf(x);
}
float Float_sqrt(float x) { return sqrtf(x); }
float Float_ceil(float x) {
return ceilf(x);
}
float Float_ceil(float x) { return ceilf(x); }
float Float_floor(float x) {
return floorf(x);
}
float Float_floor(float x) { return floorf(x); }
float Float_mod(float x, float y) {
return fmodf(x, y);
}
float Float_mod(float x, float y) { return fmodf(x, y); }

View File

@ -1,14 +1,14 @@
int CARP_INT_MAX = INT_MAX;
int CARP_INT_MIN = INT_MIN;
int Int__PLUS_(int x, int y) { return x + y; }
int Int__MINUS_(int x, int y) { return x - y; }
int Int__MUL_(int x, int y) { return x * y; }
int Int__DIV_(int x, int y) { return x / y; }
bool Int__EQ_(int x, int y) { return x == y; }
bool Int__LT_(int x, int y) { return x < y; }
bool Int__GT_(int x, int y) { return x > y; }
int Int_neg(int x) { return -x; }
int Int__PLUS_(int x, int y) { return x + y; }
int Int__MINUS_(int x, int y) { return x - y; }
int Int__MUL_(int x, int y) { return x * y; }
int Int__DIV_(int x, int y) { return x / y; }
bool Int__EQ_(int x, int y) { return x == y; }
bool Int__LT_(int x, int y) { return x < y; }
bool Int__GT_(int x, int y) { return x > y; }
int Int_neg(int x) { return -x; }
int Int_inc(int x) { return x + 1; }
int Int_dec(int x) { return x - 1; }
@ -22,10 +22,6 @@ int Int_bit_MINUS_not(int x) { return ~x; }
int Int_copy(const int *x) { return *x; }
int Int_mod(int x, int divider) {
return x % divider;
}
int Int_mod(int x, int divider) { return x % divider; }
bool Int_mask(int a, int b) {
return a & b;
}
bool Int_mask(int a, int b) { return a & b; }

View File

@ -4,7 +4,7 @@ void IO_print(String *s) { printf("%s", *s); }
void IO_errorln(String *s) { fprintf(stderr, "%s\n", *s); }
void IO_error(String *s) { fprintf(stderr, "%s", *s); }
char IO_EOF = (char) EOF;
char IO_EOF = (char)EOF;
#ifdef _WIN32
// getline isn't a C standard library function so it's missing on windows
@ -19,37 +19,27 @@ size_t getline(char **lineptr, size_t *n, FILE *stream) {
}
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (c == EOF) { return -1; }
if (*lineptr == NULL) {
*lineptr = malloc(128);
if (*lineptr == NULL) {
return -1;
}
if (*lineptr == NULL) { return -1; }
*n = 128;
}
pos = 0;
while(c != EOF) {
while (c != EOF) {
if (pos + 1 >= *n) {
size_t new_size = *n + (*n >> 2);
if (new_size < 128) {
new_size = 128;
}
if (new_size < 128) { new_size = 128; }
char *new_ptr = CARP_REALLOC(*lineptr, new_size);
if (new_ptr == NULL) {
return -1;
}
if (new_ptr == NULL) { return -1; }
*n = new_size;
*lineptr = new_ptr;
}
((unsigned char *)(*lineptr))[pos ++] = c;
if (c == '\n') {
break;
}
((unsigned char *)(*lineptr))[pos++] = c;
if (c == '\n') { break; }
c = fgetc(stream);
}
@ -70,35 +60,30 @@ String IO_read_MINUS_file(const String *filename) {
long length;
FILE *f = fopen(*filename, "rb");
if(f) {
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = CARP_MALLOC (length + 1);
if (buffer) {
fread (buffer, 1, length, f);
if (f) {
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = CARP_MALLOC(length + 1);
if (buffer) {
fread(buffer, 1, length, f);
buffer[length] = '\0';
} else {
printf("Failed to open buffer from file: %s\n", *filename);
buffer = String_empty();
}
fclose (f);
fclose(f);
} else {
printf("Failed to open file: %s\n", *filename);
buffer = String_empty();
}
return buffer;
}
char IO_fgetc(FILE *f) {
return (char) fgetc(f);
}
char IO_fgetc(FILE *f) { return (char)fgetc(f); }
void IO_fclose(FILE *f) {
fclose(f);
}
void IO_fclose(FILE *f) { fclose(f); }
FILE *IO_fopen(const String *filename, const String *mode) {
return fopen(*filename, *mode);

View File

@ -1,15 +1,21 @@
long Long__PLUS_(long x, long y) { return x + y; }
long Long__MINUS_(long x, long y) { return x - y; }
long Long__MUL_(long x, long y) { return x * y; }
long Long__DIV_(long x, long y) { return x / y; }
long Long__PLUS_(long x, long y) { return x + y; }
long Long__MINUS_(long x, long y) { return x - y; }
long Long__MUL_(long x, long y) { return x * y; }
long Long__DIV_(long x, long y) { return x / y; }
#ifndef _WIN32
bool Long_safe_MINUS_add(long x, long y, long* res) { return __builtin_saddl_overflow(x, y, res); }
bool Long_safe_MINUS_sub(long x, long y, long* res) { return __builtin_ssubl_overflow(x, y, res); }
bool Long_safe_MINUS_mul(long x, long y, long* res) { return __builtin_smull_overflow(x, y, res); }
bool Long_safe_MINUS_add(long x, long y, long* res) {
return __builtin_saddl_overflow(x, y, res);
}
bool Long_safe_MINUS_sub(long x, long y, long* res) {
return __builtin_ssubl_overflow(x, y, res);
}
bool Long_safe_MINUS_mul(long x, long y, long* res) {
return __builtin_smull_overflow(x, y, res);
}
#endif
bool Long__EQ_(long x, long y) { return x == y; }
bool Long__LT_(long x, long y) { return x < y; }
bool Long__GT_(long x, long y) { return x > y; }
bool Long__EQ_(long x, long y) { return x == y; }
bool Long__LT_(long x, long y) { return x < y; }
bool Long__GT_(long x, long y) { return x > y; }
long Long_neg(long x) { return -x; }
long Long_inc(long x) { return x + 1; }
@ -22,24 +28,14 @@ long Long_bit_MINUS_or(long x, long y) { return x | y; }
long Long_bit_MINUS_xor(long x, long y) { return x ^ y; }
long Long_bit_MINUS_not(long x) { return ~x; }
long Long_copy(const long *x) { return *x; }
long Long_copy(const long* x) { return *x; }
long Long_mod(long x, long divider) {
return x % divider;
}
long Long_mod(long x, long divider) { return x % divider; }
void Long_seed(long seed) {
srand(seed);
}
void Long_seed(long seed) { srand(seed); }
bool Long_mask(long a, long b) {
return a & b;
}
bool Long_mask(long a, long b) { return a & b; }
int Long_to_MINUS_int(long a) {
return (int) a;
}
int Long_to_MINUS_int(long a) { return (int)a; }
long Long_from_MINUS_int(int a) {
return (long) a;
}
long Long_from_MINUS_int(int a) { return (long)a; }

View File

@ -5,21 +5,18 @@ bool log_memory_balance = false;
void *logged_malloc(size_t size) {
void *ptr = malloc(size);
if(log_memory_balance) {
printf("MALLOC: %p (%ld bytes)\n", ptr, size);
}
if (log_memory_balance) { printf("MALLOC: %p (%ld bytes)\n", ptr, size); }
malloc_balance_counter++;
return ptr;
}
void logged_free(void *ptr) {
if(log_memory_balance) {
printf("FREE: %p\n", ptr);
}
if (log_memory_balance) { printf("FREE: %p\n", ptr); }
free(ptr);
malloc_balance_counter--;
/* if(malloc_balance_counter == 0) { */
/* printf("malloc is balanced! (this should be the last thing you see)\n"); */
/* printf("malloc is balanced! (this should be the last thing you
* see)\n"); */
/* } */
/* else if(malloc_balance_counter < 0) { */
/* printf("malloc is %ld, that is bad!\n", malloc_balance_counter); */
@ -34,9 +31,7 @@ void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
#define CARP_FREE(ptr) logged_free(ptr)
#define CARP_REALLOC(ptr, size) realloc(ptr, size)
long Debug_memory_MINUS_balance() {
return malloc_balance_counter;
}
long Debug_memory_MINUS_balance() { return malloc_balance_counter; }
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
malloc_balance_counter = 0;
@ -46,14 +41,14 @@ void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
#ifdef CHECK_ALLOCATIONS
void* CARP_MALLOC(size_t size) {
void* res = malloc(size);
if (!res) abort();
return res;
void* res = malloc(size);
if (!res) abort();
return res;
}
void* CARP_REALLOC(void * ptr, size_t size) {
void* res = realloc(ptr, size);
if (!res) abort();
return res;
void* CARP_REALLOC(void* ptr, size_t size) {
void* res = realloc(ptr, size);
if (!res) abort();
return res;
}
#else
#define CARP_MALLOC(size) malloc(size)
@ -63,18 +58,24 @@ void* CARP_REALLOC(void * ptr, size_t size) {
#define CARP_FREE(ptr) free(ptr)
long Debug_memory_MINUS_balance() {
printf("Error - calling 'memory-balance' without compiling with LOG_MEMORY enabled (--log-memory).\n");
printf(
"Error - calling 'memory-balance' without compiling with LOG_MEMORY "
"enabled (--log-memory).\n");
exit(1);
return 0;
}
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
printf("Error - calling 'reset-memory-balance!' without compiling with LOG_MEMORY enabled (--log-memory).\n");
printf(
"Error - calling 'reset-memory-balance!' without compiling with "
"LOG_MEMORY enabled (--log-memory).\n");
exit(1);
}
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
printf("Error - calling 'log-memory-balance!' without compiling with LOG_MEMORY enabled (--log-memory).\n");
printf(
"Error - calling 'log-memory-balance!' without compiling with "
"LOG_MEMORY enabled (--log-memory).\n");
exit(1);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,9 @@
bool Int_safe_MINUS_add(int x, int y, int* res) { return __builtin_sadd_overflow(x, y, res); }
bool Int_safe_MINUS_sub(int x, int y, int* res) { return __builtin_ssub_overflow(x, y, res); }
bool Int_safe_MINUS_mul(int x, int y, int* res) { return __builtin_smul_overflow(x, y, res); }
bool Int_safe_MINUS_add(int x, int y, int* res) {
return __builtin_sadd_overflow(x, y, res);
}
bool Int_safe_MINUS_sub(int x, int y, int* res) {
return __builtin_ssub_overflow(x, y, res);
}
bool Int_safe_MINUS_mul(int x, int y, int* res) {
return __builtin_smul_overflow(x, y, res);
}

View File

@ -5,22 +5,21 @@ String String_allocate(int len, char byte) {
*
* String_alloc(10, "a") == "aaaaaaaaaa"
*/
String ptr = CARP_MALLOC(len+1);
String ptr = CARP_MALLOC(len + 1);
memset(ptr, byte, len);
ptr[len] = '\0';
return ptr;
}
void String_delete(String s) {
CARP_FREE(s);
}
void String_delete(String s) { CARP_FREE(s); }
void String_string_MINUS_set_BANG_(String *s, int i, char ch) {
CHK_INDEX(i, strlen(*s));
(*s)[i] = ch;
}
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, const String *src) {
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i,
const String *src) {
char *dest = (*into) + i;
int lsrc = strlen(*src);
/* given a string and indices
@ -52,14 +51,14 @@ void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, const String *s
*
* so this write is safe
*/
CHK_INDEX(i+lsrc, strlen(*into)+1);
CHK_INDEX(i + lsrc, strlen(*into) + 1);
memcpy(dest, *src, lsrc);
}
String String_copy(const String *s) {
size_t len = strlen(*s) + 1;
String ptr = CARP_MALLOC(len);
return (String) memcpy(ptr, *s, len);
return (String)memcpy(ptr, *s, len);
}
bool String__EQ_(const String *a, const String *b) {
@ -83,17 +82,11 @@ String String_append(const String *a, const String *b) {
return buffer;
}
int String_length(const String *s) {
return strlen(*s);
}
int String_length(const String *s) { return strlen(*s); }
char* String_cstr(const String *s) {
return *s;
}
char *String_cstr(const String *s) { return *s; }
String String_str(const String *s) {
return String_copy(s);
}
String String_str(const String *s) { return String_copy(s); }
String String_prn(const String *s) {
int n = strlen(*s) + 4;
@ -102,12 +95,10 @@ String String_prn(const String *s) {
return buffer;
}
char String_char_MINUS_at(const String* s, int i) {
return (*s)[i];
}
char String_char_MINUS_at(const String *s, int i) { return (*s)[i]; }
String String_format(const String *str, const String *s) {
int size = snprintf(NULL, 0, *str, *s)+1;
int size = snprintf(NULL, 0, *str, *s) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, *s);
return buffer;
@ -122,17 +113,17 @@ Array String_chars(const String *s) {
}
String String_from_MINUS_chars(const Array *a) {
String s = CARP_MALLOC(a->len+1);
String s = CARP_MALLOC(a->len + 1);
memcpy(s, a->data, a->len);
s[a->len] = '\0';
return s;
}
String String_tail(const String* s) {
String String_tail(const String *s) {
int len = strlen(*s);
String news = CARP_MALLOC(len);
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
memcpy(news, (*s) + 1, len - 1);
news[len - 1] = '\0';
return news;
}
@ -145,15 +136,15 @@ String String_empty() {
String Bool_str(bool b) {
const String true_str = "true";
const String false_str = "false";
if(b) {
if (b) {
return String_copy(&true_str);
} else {
return String_copy(&false_str);
}
}
String Bool_format(const String* str, bool b) {
int size = snprintf(NULL, 0, *str, b)+1;
String Bool_format(const String *str, bool b) {
int size = snprintf(NULL, 0, *str, b) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
return buffer;
@ -171,87 +162,81 @@ String Char_prn(char c) {
return buffer;
}
String Char_format(const String* str, char b) {
int size = snprintf(NULL, 0, *str, b)+1;
String Char_format(const String *str, char b) {
int size = snprintf(NULL, 0, *str, b) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
return buffer;
}
String Double_str(double x) {
int size = snprintf(NULL, 0, "%g", x)+1;
int size = snprintf(NULL, 0, "%g", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%g", x);
return buffer;
}
String Double_format(const String* s, double x) {
int size = snprintf(NULL, 0, *s, x)+1;
String Double_format(const String *s, double x) {
int size = snprintf(NULL, 0, *s, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *s, x);
return buffer;
}
String Float_str(float x) {
int size = snprintf(NULL, 0, "%gf", x)+1;
int size = snprintf(NULL, 0, "%gf", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%gf", x);
return buffer;
}
String Float_format(const String* str, float x) {
int size = snprintf(NULL, 0, *str, x)+1;
String Float_format(const String *str, float x) {
int size = snprintf(NULL, 0, *str, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
String Int_str(int x) {
int size = snprintf(NULL, 0, "%d", x)+1;
int size = snprintf(NULL, 0, "%d", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%d", x);
return buffer;
}
String Int_format(const String* str, int x) {
int size = snprintf(NULL, 0, *str, x)+1;
String Int_format(const String *str, int x) {
int size = snprintf(NULL, 0, *str, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
int Int_from_MINUS_string(const String *s) {
return atoi(*s);
}
int Int_from_MINUS_string(const String *s) { return atoi(*s); }
String Long_str(long x) {
int size = snprintf(NULL, 0, "%ldl", x)+1;
int size = snprintf(NULL, 0, "%ldl", x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%ldl", x);
return buffer;
}
String Long_format(const String* str, long x) {
int size = snprintf(NULL, 0, *str, x)+1;
String Long_format(const String *str, long x) {
int size = snprintf(NULL, 0, *str, x) + 1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
long Long_from_MINUS_string(const String *s) {
return atol(*s);
}
long Long_from_MINUS_string(const String *s) { return atol(*s); }
int String_index_MINUS_of_MINUS_from(const String *s, char c, int i) {
/* Return index of first occurrence of `c` in `s` AFTER index i
* Returns -1 if not found
*/
++i; // skip first character as we want AFTER i
++i; // skip first character as we want AFTER i
int len = strlen(*s);
for (; i<len; ++i) {
if (c == (*s)[i]) {
return i;
}
for (; i < len; ++i) {
if (c == (*s)[i]) { return i; }
}
return -1;
}

View File

@ -1,11 +1,7 @@
void System_free(void *p) {
CARP_FREE(p);
}
void System_free(void *p) { CARP_FREE(p); }
int System_time() {
return time(0);
}
int System_time() { return time(0); }
#ifdef _WIN32
void System_sleep_MINUS_seconds(int t) {
@ -16,36 +12,26 @@ void System_sleep_MINUS_micros(int t) {
// TODO!
}
double System_nanotime() {
return 0;
}
double System_nanotime() { return 0; }
#else
void System_sleep_MINUS_seconds(int t) {
sleep(t);
}
void System_sleep_MINUS_seconds(int t) { sleep(t); }
void System_sleep_MINUS_micros(int t) {
usleep(t);
}
void System_sleep_MINUS_micros(int t) { usleep(t); }
double System_nanotime() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
}
#endif
void System_system(const String *command) {
system(*command);
}
void System_system(const String *command) { system(*command); }
Array System_args;
String* System_get_MINUS_arg(int idx) {
String *System_get_MINUS_arg(int idx) {
assert(idx < System_args.len);
return &(((String*)System_args.data)[idx]);
return &(((String *)System_args.data)[idx]);
}
int System_get_MINUS_args_MINUS_len() {
return System_args.len;
}
int System_get_MINUS_args_MINUS_len() { return System_args.len; }

View File

@ -1,26 +1,27 @@
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#if defined(WIN32) || defined(_WIN32) || \
defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
typedef char* String;
typedef char* Pattern;
typedef char *String;
typedef char *Pattern;
#if defined NDEBUG
#define CHK_INDEX(i,n)
#define CHK_INDEX(i, n)
#else
#define CHK_INDEX(i,n) \
do { \
size_t __si = (size_t)i; \
size_t __ni = (size_t)n; \
if (!(__si < __ni)) { \
printf(__FILE__ ":%u: bad index: %zd < %zd\n", \
__LINE__, (ssize_t)i, (ssize_t)n); \
abort(); \
} \
}while (0)
#define CHK_INDEX(i, n) \
do { \
size_t __si = (size_t)i; \
size_t __ni = (size_t)n; \
if (!(__si < __ni)) { \
printf(__FILE__ ":%u: bad index: %zd < %zd\n", __LINE__, \
(ssize_t)i, (ssize_t)n); \
abort(); \
} \
} while (0)
#endif
// Array
@ -38,4 +39,4 @@ typedef struct {
void *copy;
} Lambda;
typedef void* LambdaEnv;
typedef void *LambdaEnv;