Carp/core/prelude.h

224 lines
4.0 KiB
C
Raw Normal View History

2017-06-26 12:15:03 +03:00
#ifndef PRELUDE_H
#define PRELUDE_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <assert.h>
2017-06-30 11:46:40 +03:00
void *logged_malloc(size_t size) {
void *ptr = malloc(size);
printf("MALLOC: %p (%ld bytes)\n", ptr, size);
return ptr;
}
void logged_free(void *ptr) {
printf("FREE: %p\n", ptr);
free(ptr);
}
2017-06-30 15:21:31 +03:00
#define LOG_MEMORY 0
2017-06-30 11:46:40 +03:00
#if LOG_MEMORY
#define CARP_MALLOC(size) logged_malloc(size)
#define CARP_FREE(ptr) logged_free(ptr)
#else
#define CARP_MALLOC(size) malloc(size)
#define CARP_FREE(ptr) free(ptr)
#endif
2017-06-26 12:15:03 +03:00
typedef char* string;
bool not(bool b) {
return !b;
}
2017-06-30 12:37:03 +03:00
int Int__PLUS_(x, y) { return x + y; }
int Int__MINUS_(x, y) { return x - y; }
int Int__MUL_(x, y) { return x * y; }
int Int__DIV_(x, y) { return x / y; }
int Int__EQ_(x, y) { return x == y; }
int Int__LT_(x, y) { return x < y; }
int Int__GT_(x, y) { return x > y; }
2017-06-26 12:15:03 +03:00
int Int_inc(int x) { return x + 1; }
int Int_dec(int x) { return x - 1; }
2017-09-08 13:24:57 +03:00
int Int_copy(int *x) { return *x; }
2017-06-26 12:15:03 +03:00
#define Double__PLUS_(x, y) ((x) + (y))
#define Double__MINUS_(x, y) ((x) - (y))
#define Double__MUL_(x, y) ((x) * (y))
#define Double__DIV_(x, y) ((x) / (y))
#define Float__PLUS_(x, y) ((x) + (y))
#define Float__MINUS_(x, y) ((x) - (y))
#define Float__MUL_(x, y) ((x) * (y))
#define Float__DIV_(x, y) ((x) / (y))
#define and(x, y) ((x) && (y))
#define or(x, y) ((x) || (y))
void IO_println(string *s) { puts(*s); }
void IO_print(string *s) { printf("%s", *s); }
string IO_get_MINUS_line() {
size_t size = 1024;
2017-06-30 12:00:36 +03:00
char *buffer = CARP_MALLOC(size);
2017-06-26 12:15:03 +03:00
getline(&buffer, &size, stdin);
return buffer;
}
int Int_from_MINUS_string(string *s) {
return atoi(*s);
2017-06-26 12:15:03 +03:00
}
int Int_mod(int x, int divider) {
return x % divider;
}
void Int_seed(int seed) {
srand(seed);
}
int Int_random() {
return rand();
}
int Int_random_MINUS_between(int lower, int upper) {
int diff = upper - lower;
return lower + (rand() % diff);
}
string Int_str(int x) {
2017-10-10 21:13:58 +03:00
char *buffer = CARP_MALLOC(64);
snprintf(buffer, 64, "%d", x);
return buffer;
2017-06-26 12:15:03 +03:00
}
bool Int_mask(int a, int b) {
return a & b;
}
void String_delete(string s) {
2017-06-30 12:00:36 +03:00
CARP_FREE(s);
2017-06-26 12:15:03 +03:00
}
string String_copy(string *s) {
char *ptr = strdup(*s);
#if LOG_MEMORY
printf("STRDUP: %p\n", ptr);
#endif
return ptr;
2017-06-26 12:15:03 +03:00
}
bool String__EQ_(string *a, string *b) {
return strcmp(*a, *b) == 0;
}
string String_append(string a, string b) {
int la = strlen(a);
int lb = strlen(b);
int total = la + lb + 1;
2017-06-30 12:00:36 +03:00
string buffer = CARP_MALLOC(total);
2017-06-26 12:15:03 +03:00
snprintf(buffer, total, "%s%s", a, b);
2017-06-30 12:00:36 +03:00
CARP_FREE(a);
CARP_FREE(b);
2017-06-26 12:15:03 +03:00
return buffer;
}
int String_count(string *s) {
return strlen(*s);
}
// Replace with 'copy' later:
string String_duplicate(string *s) {
return strdup(*s);
}
char* String_cstr(string *s) {
return *s;
}
2017-10-10 21:13:58 +03:00
string String_str(string *s) {
2017-10-12 09:38:53 +03:00
int n = strlen(*s) + 4;
2017-10-10 21:13:58 +03:00
string buffer = malloc(n);
2017-10-12 09:38:53 +03:00
snprintf(buffer, n, "@\"%s\"", *s);
2017-10-10 21:13:58 +03:00
return buffer;
}
2017-06-26 12:15:03 +03:00
string Char_str(char c) {
2017-10-10 21:13:58 +03:00
char *buffer = CARP_MALLOC(3);
snprintf(buffer, 3, "\\%c", c);
2017-06-26 12:15:03 +03:00
return buffer;
}
int exmod__bleh(int x) {
return x * 1000;
}
// Double.toInt : Double -> Int
int Double_toInt(double x) {
return (int)x;
}
double Double_fromInt(int x) {
return (double)x;
}
double Double_sin(double x) {
return sin(x);
}
double Double_cos(double x) {
return cos(x);
}
2017-10-10 21:13:58 +03:00
string Double_str(double x) {
char *buffer = CARP_MALLOC(32);
snprintf(buffer, 32, "%f", x);
return buffer;
}
2017-06-26 12:15:03 +03:00
int Float_toInt(double x) {
return (int)x;
}
2017-10-10 21:13:58 +03:00
string Float_str(float x) {
char *buffer = CARP_MALLOC(32);
snprintf(buffer, 32, "%ff", x);
2017-10-10 21:13:58 +03:00
return buffer;
}
// Bool
string Bool_str(bool b) {
if(b) {
return strdup("true");
} else {
return strdup("false");
}
}
2017-06-26 12:15:03 +03:00
// Array
typedef struct {
int len;
void *data;
} Array;
void System_exit(int code) {
exit(code);
}
2017-06-30 12:00:36 +03:00
void System_CARP_FREE__string_MUL_(void *p) {
CARP_FREE(p);
2017-06-26 12:15:03 +03:00
}
int System_time() {
return time(0);
}
#endif