core: split up core

This commit is contained in:
hellerve 2018-02-27 14:14:14 +01:00
parent b7ea8d1487
commit 1ec7ee184d
12 changed files with 196 additions and 179 deletions

View File

@ -1,6 +1,8 @@
#include <core.h>
#include <SDL.h>
#include <carp_memory.h>
#include <core.h>
typedef struct {
SDL_Window *window;
SDL_Renderer *renderer;

View File

@ -1,3 +1,5 @@
(system-include "carp_string.h")
(defmodule String
(register = (Fn [&String &String] Bool))

View File

@ -1,3 +1,5 @@
#include <stdbool.h>
// Bool
bool Bool_copy(bool* b) {
return *b;
@ -27,4 +29,3 @@ string Bool_format(string* str, bool b) {
snprintf(buffer, size, *str, b);
return buffer;
}

View File

@ -1,3 +1,6 @@
#include <math.h>
#include <limits.h>
double Double__PLUS_(double x, double y) { return x + y; }
double Double__MINUS_(double x, double y) { return x - y; }
double Double__MUL_(double x, double y) { return x * y; }
@ -27,7 +30,7 @@ double Double_from_MINUS_float(float x) {
}
double Double_abs(double x) {
return fabs(x);
return x > 0.0 ? x : -x;
}
double Double_acos(double x) {

View File

@ -1,3 +1,6 @@
#include <math.h>
#include <limits.h>
float Float__PLUS_(float x, float y) { return x + y; }
float Float__MINUS_(float x, float y) { return x - y; }
float Float__MUL_(float x, float y) { return x * y; }

View File

@ -1,3 +1,5 @@
#include <math.h>
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; }
@ -12,7 +14,7 @@ bool Int__GT_(int x, int y) { return x > y; }
int Int_inc(int x) { return x + 1; }
int Int_dec(int x) { return x - 1; }
int Int_abs(int x) { return abs(x); }
int Int_abs(int x) { return x > 0 ? x : -x; }
int Int_bit_MINUS_shift_MINUS_left(int x, int y) { return x << y; }
int Int_bit_MINUS_shift_MINUS_right(int x, int y) { return x >> y; }
int Int_bit_MINUS_and(int x, int y) { return x & y; }

View File

@ -1,4 +1,7 @@
#include <core.h>
#pragma once
#include <stdio.h>
#include <carp_string.h>
void IO_println(string *s) { puts(*s); }
void IO_print(string *s) { printf("%s", *s); }

View File

@ -1,3 +1,5 @@
#include <math.h>
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; }
@ -11,7 +13,7 @@ bool Long__GT_(long x, long y) { return x > y; }
long Long_inc(long x) { return x + 1; }
long Long_dec(long x) { return x - 1; }
long Long_abs(long x) { return labs(x); }
long Long_abs(long x) { return x > 0 ? x : -x; }
long Long_bit_MINUS_shift_MINUS_left(long x, long y) { return x << y; }
long Long_bit_MINUS_shift_MINUS_right(long x, long y) { return x >> y; }
long Long_bit_MINUS_and(long x, long y) { return x & y; }

73
core/carp_memory.h Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#include <stdbool.h>
#include <stdlib.h>
#ifdef LOG_MEMORY
#include <stdio.h>
long malloc_balance_counter = 0;
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);
}
malloc_balance_counter++;
return ptr;
}
void logged_free(void *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"); */
/* } */
/* else if(malloc_balance_counter < 0) { */
/* printf("malloc is %ld, that is bad!\n", malloc_balance_counter); */
/* } */
}
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
log_memory_balance = value;
}
#define CARP_MALLOC(size) logged_malloc(size)
#define CARP_FREE(ptr) logged_free(ptr)
long Debug_memory_MINUS_balance() {
return malloc_balance_counter;
}
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
malloc_balance_counter = 0;
}
#else
#define CARP_MALLOC(size) malloc(size)
#define CARP_FREE(ptr) free(ptr)
#include <stdio.h>
long Debug_memory_MINUS_balance() {
printf("Error - calling 'memory-balance' without compiling with LOG_MEMORY enabled.\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.\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.\n");
exit(1);
}
#endif

94
core/carp_string.h Normal file
View File

@ -0,0 +1,94 @@
#pragma once
#include <string.h>
#include <carp_memory.h>
#include <core.h>
void String_delete(string s) {
CARP_FREE(s);
}
string String_copy(string *s) {
size_t len = strlen(*s) + 1;
string ptr = CARP_MALLOC(len);
if (ptr == NULL) {
return NULL;
}
return (string) memcpy(ptr, *s, len);
}
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;
string buffer = CARP_MALLOC(total);
snprintf(buffer, total, "%s%s", a, b);
CARP_FREE(a);
CARP_FREE(b);
return buffer;
}
int String_count(string *s) {
return strlen(*s);
}
// Replace with 'copy' later:
string String_duplicate(string *s) {
return String_copy(s);
}
char* String_cstr(string *s) {
return *s;
}
string String_str(string *s) {
int n = strlen(*s) + 4;
string buffer = CARP_MALLOC(n);
snprintf(buffer, n, "@\"%s\"", *s);
return buffer;
}
char String_char_MINUS_at(string* s, int i) {
return (*s)[i];
}
string String_format(string *str, string *s) {
int size = snprintf(NULL, 0, *str, *s)+1;
string buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, *s);
return buffer;
}
Array String_chars(string *s) {
Array chars;
chars.len = strlen(*s);
chars.data = String_copy(s);
return chars;
}
string String_from_MINUS_chars(Array a) {
string s = CARP_MALLOC(a.len+1);
memmove(s, a.data, a.len);
s[a.len] = '\0';
return s;
}
string String_tail(string* s) {
int len = strlen(*s);
string news = CARP_MALLOC(len);
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
return news;
}
string String_empty() {
string s = CARP_MALLOC(1);
s[0] = '\0';
return s;
}

View File

@ -1,4 +1,7 @@
#include <core.h>
#include <time.h>
#include <unistd.h>
#include <carp_memory.h>
void System_exit(int code) {
exit(code);

View File

@ -1,86 +1,11 @@
#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>
#include <limits.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <stdbool.h>
typedef char* string;
#ifdef LOG_MEMORY
long malloc_balance_counter = 0;
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);
}
malloc_balance_counter++;
return ptr;
}
void logged_free(void *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"); */
/* } */
/* else if(malloc_balance_counter < 0) { */
/* printf("malloc is %ld, that is bad!\n", malloc_balance_counter); */
/* } */
}
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
log_memory_balance = value;
}
#define CARP_MALLOC(size) logged_malloc(size)
#define CARP_FREE(ptr) logged_free(ptr)
long Debug_memory_MINUS_balance() {
return malloc_balance_counter;
}
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
malloc_balance_counter = 0;
}
#else
#define CARP_MALLOC(size) malloc(size)
#define CARP_FREE(ptr) free(ptr)
long Debug_memory_MINUS_balance() {
printf("Error - calling 'memory-balance' without compiling with LOG_MEMORY enabled.\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.\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.\n");
exit(1);
}
#endif
// Array
typedef struct {
size_t len;
@ -94,100 +19,4 @@ bool not(bool b) {
bool and(bool x, bool y) { return x && y; }
bool or(bool x, bool y) { return x || y; }
void String_delete(string s) {
CARP_FREE(s);
}
string String_copy(string *s) {
size_t len = strlen(*s) + 1;
string ptr = CARP_MALLOC(len);
if (ptr == NULL) {
return NULL;
}
return (string) memcpy(ptr, *s, len);
}
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;
string buffer = CARP_MALLOC(total);
snprintf(buffer, total, "%s%s", a, b);
CARP_FREE(a);
CARP_FREE(b);
return buffer;
}
int String_count(string *s) {
return strlen(*s);
}
// Replace with 'copy' later:
string String_duplicate(string *s) {
return String_copy(s);
}
char* String_cstr(string *s) {
return *s;
}
string String_str(string *s) {
int n = strlen(*s) + 1;
string buffer = CARP_MALLOC(n);
snprintf(buffer, n, "%s", *s);
return buffer;
}
string String_prn(string *s) {
int n = strlen(*s) + 4;
string buffer = CARP_MALLOC(n);
snprintf(buffer, n, "@\"%s\"", *s);
return buffer;
}
char String_char_MINUS_at(string* s, int i) {
return (*s)[i];
}
string String_format(string *str, string *s) {
int size = snprintf(NULL, 0, *str, *s)+1;
string buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, *s);
return buffer;
}
Array String_chars(string *s) {
Array chars;
chars.len = strlen(*s);
chars.data = String_copy(s);
return chars;
}
string String_from_MINUS_chars(Array a) {
string s = CARP_MALLOC(a.len+1);
memmove(s, a.data, a.len);
s[a.len] = '\0';
return s;
}
string String_tail(string* s) {
int len = strlen(*s);
string news = CARP_MALLOC(len);
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
return news;
}
string String_empty() {
string s = CARP_MALLOC(1);
s[0] = '\0';
return s;
}
#endif