#ifndef UNDAR_LIBC_H #define UNDAR_LIBC_H #if defined(__has_include) #if __has_include() #define HAVE_STDINT 1 #endif #if __has_include() #define HAVE_STDBOOL 1 #endif #if __has_include() #define HAVE_STDDEF 1 #endif #endif #ifdef HAVE_STDINT #include typedef uint8_t u8; typedef int8_t i8; typedef uint16_t u16; typedef int16_t i16; typedef uint32_t u32; typedef int32_t i32; typedef int32_t r32; typedef float f32; #else typedef unsigned char u8; typedef signed char i8; typedef unsigned short u16; typedef signed short i16; typedef unsigned int u32; typedef signed int i32; typedef signed int r32; typedef float f32; #endif #ifdef HAVE_STDBOOL #include #else #define true 1 #define false 0 typedef u8 bool; #endif #ifdef HAVE_STDDEF #include #define nil NULL #else #define nil ((void *)0) #endif #define I8_MIN -128 #define I8_MAX 127 #define U8_MAX 255 #define I16_MIN -32768 #define I16_MAX 32767 #define U16_MAX 65535 #define I32_MIN -2147483648 #define I32_MAX 2147483647 #define U32_MAX 4294967295 #define FIXED_CONST 65536.0f #define AS_INT(v) ((i32)(v)) #define AS_NAT(v) ((u32)(v)) #define AS_REAL(v) ((i32)(v)) #define FLOAT_TO_REAL(v) (((i32)(v)) * FIXED_CONST) #define REAL_TO_FLOAT(v) (((f32)(v)) / FIXED_CONST) #define USED(x) ((void)(x)) #define MAX_LEN_REAL32 12 #define MAX_LEN_INT32 11 typedef struct arena_s Arena; struct arena_s { u8 *tape; u32 count; u32 capacity; }; void mcpy(void *dest, void *src, u32 n); i32 scpy(char *to, const char *from, u32 length); bool seq(const char *s1, const char *s2); bool sleq(const char *s1, const char *s2, u32 length); u32 slen(const char *str); u32 snlen(const char *str, u32 max_len); void *aalloc(Arena *arena, u32 size); void *aend(Arena *arena); void *areturn(Arena *arena, u32 checkpoint, const void *src, u32 size); u32 afree(Arena *arena); char *ascpy(Arena *arena, const char *start, u32 length); void *amcpy(Arena *arena, void *from, u32 length); #define ARENA_RETURN(arena, ckpt, src_ptr, type) \ return (type *)areturn(arena, (ckpt), (src_ptr), sizeof(type)) #define ARENA_RETURN_ARRAY(arena, ckpt, src_ptr, type, count) \ return (type *)areturn(arena, (ckpt), (src_ptr), sizeof(type) * (count)) #define ARENA_RETURN_STR(arena, ckpt, src_ptr) \ return (char *)areturn(arena, (ckpt), (src_ptr), slen(src_ptr)) #define ARENA_RETURN_STRBUF(arena, ckpt, sbuf) \ char *to_return = StrBuf_toS(arena, sbuf); \ return (char *)areturn(arena, (ckpt), (to_return), slen(to_return)) r32 int_to_real(i32 i); i32 real_to_int(r32 f); r32 float_to_real(f32 f); f32 real_to_float(r32 r); r32 real_add(r32 a, r32 b); r32 real_sub(r32 a, r32 b); r32 real_mul(r32 a, r32 b); r32 real_div(r32 a, r32 b); r32 real_eq(r32 a, r32 b); r32 real_ne(r32 a, r32 b); r32 real_lt(r32 a, r32 b); r32 real_le(r32 a, r32 b); r32 real_gt(r32 a, r32 b); r32 real_ge(r32 a, r32 b); r32 real_neg(r32 f); r32 real_abs(r32 f); char *int_to_string(Arena *a, i32 v); char *nat_to_string(Arena *a, u32 v); char *real_to_string(Arena *a, r32 q); #endif