undar-lang/docs/plex_orented_c_example.c

94 lines
2.1 KiB
C

#define __UNDAR_ARENA_SIZE__ 64000
#include "../libc.h"
#include "../strbuf.h"
#include <stdio.h>
typedef struct Point Point;
struct Point {
u32 x;
u32 y;
};
Point *
Point_init(Arena *a, u32 x, u32 y)
{
Point *this = (Point *)aalloc(a, sizeof(Point));
this->x = x;
this->y = y;
return this;
}
char*
Point_toS(Arena *a, Point *this)
{
u32 __UNDAR_FN_CHECKPOINT_REF__ = a->count;
StrBuf *buf = StrBuf_init(a);
StrBuf_append(a, buf, "[x:");
StrBuf_append(a, buf,nat_to_string(a, this->x));
StrBuf_append(a, buf, ", y:");
StrBuf_append(a, buf,nat_to_string(a, this->y));
StrBuf_append(a, buf, "]");
ARENA_RETURN_STRBUF(a, __UNDAR_FN_CHECKPOINT_REF__, buf);
}
typedef struct Rect Rect;
struct Rect {
Point top_left;
Point bottom_right;
u32 width;
u32 height;
};
Rect *
Rect_init(Arena *a, Point *tl, Point *br, u32 width, u32 height)
{
Rect *this = (Rect *)aalloc(a, sizeof(Rect));
mcpy(&this->top_left, tl, sizeof(Point));
mcpy(&this->bottom_right, br, sizeof(Point));
this->width = width;
this->height = height;
return this;
}
char*
Rect_toS(Arena *a, Rect *this)
{
u32 __UNDAR_FN_CHECKPOINT_REF__ = a->count;
StrBuf *buf = StrBuf_init(a);
StrBuf_append(a, buf, "[top_left: ");
StrBuf_append(a, buf,Point_toS(a,&this->top_left));
StrBuf_append(a, buf,", bottom_right: ");
StrBuf_append(a, buf,Point_toS(a,&this->bottom_right));
StrBuf_append(a, buf, ", width:");
StrBuf_append(a, buf,nat_to_string(a,this->width));
StrBuf_append(a, buf, ", height:");
StrBuf_append(a, buf,nat_to_string(a,this->height));
StrBuf_append(a, buf, "]");
ARENA_RETURN_STRBUF(a, __UNDAR_FN_CHECKPOINT_REF__, buf);
}
Rect *
create_geometry(Arena *a)
{
u32 __UNDAR_FN_CHECKPOINT_REF__ = a->count;
Point *tl = Point_init(a, 10, 20);
Point *br = Point_init(a, 100, 200);
Rect *final_rect = Rect_init(a, tl, br, 90, 180);
ARENA_RETURN(a, __UNDAR_FN_CHECKPOINT_REF__, final_rect, Rect);
}
int main() {
u8 tape[__UNDAR_ARENA_SIZE__];
Arena arena = {tape, 0, __UNDAR_ARENA_SIZE__};
Rect *r = create_geometry(&arena);
printf("%s\n", Rect_toS(&arena, r));
return 0;
}