undar-lang/test/plex.c

92 lines
1.9 KiB
C

#include "undar.h"
typedef struct Point Point;
struct Point {
unsigned int x;
unsigned int y;
};
Point *
Point_new(Arena *a, unsigned int x, unsigned int y)
{
Point *this = (Point *)aalloc(a, sizeof(Point));
this->x = x;
this->y = y;
return this;
}
char*
Point_toS(Arena *a, Point *this)
{
unsigned int ckpt = a->count;
char *s = (char*)&a->tape[a->count];
int len = sprintf(s, "[x:%d, y:%d]",
this->x,
this->y);
s[len] = '\0';
a->count += len + 1;
ARENA_RETURN_ARRAY(a, ckpt, s, char, len + 1);
}
typedef struct Rect Rect;
struct Rect {
Point top_left;
Point bottom_right;
unsigned int width;
unsigned int height;
};
Rect *
Rect_new(Arena *a, Point *tl, Point *br, unsigned int width, unsigned int height)
{
Rect *this = (Rect *)aalloc(a, sizeof(Rect));
memcpy(&this->top_left, tl, sizeof(Point));
memcpy(&this->bottom_right, br, sizeof(Point));
this->width = width;
this->height = height;
return this;
}
char*
Rect_toS(Arena *a, Rect *this)
{
unsigned int ckpt = a->count;
char *s1 = Point_toS(a, &this->top_left);
char *s2 = Point_toS(a, &this->bottom_right);
char *s = (char*)&a->tape[a->count];
int len = sprintf(s, "[top_left: %s, bottom_right: %s, width:%d, height:%d]",
s1,
s2,
this->width,
this->height);
s[len] = '\0';
a->count += len + 1;
ARENA_RETURN_ARRAY(a, ckpt, s, char, len + 1);
}
Rect *
create_geometry(Arena *a)
{
unsigned int ckpt = a->count;
Rect *final_rect;
Point *temp_tl;
Point *temp_br;
temp_tl = Point_new(a, 10, 20);
temp_br = Point_new(a, 100, 200);
final_rect = Rect_new(a, temp_tl, temp_br, 90, 180);
ARENA_RETURN(a, ckpt, final_rect, Rect);
}
int main() {
unsigned char tape[64000];
Arena a = {tape, 0, 64000};
Rect *r = create_geometry(&a);
printf("%s\n", Rect_toS(&a, r));
return 0;
}