added statis map object and used it to implement builtins. Also, new objects File and Directory

This commit is contained in:
cozis
2022-03-10 19:00:30 +01:00
parent 5b097e2b76
commit 8deec027d1
19 changed files with 824 additions and 1045 deletions
+53
View File
@@ -0,0 +1,53 @@
#include "objects.h"
typedef struct {
Object base;
DIR *dir;
} DirObject;
static _Bool dir_free(Object *obj, Error *error);
static TypeObject t_dir = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "Directory",
.size = sizeof(DirObject),
.free = dir_free,
};
_Bool Object_IsDir(Object *obj)
{
return obj->type == &t_dir;
}
Object *Object_FromDIR(DIR *handle, Heap *heap, Error *error)
{
DirObject *dob = (DirObject*) Heap_Malloc(heap, &t_dir, error);
if(dob == NULL)
return NULL;
dob->dir = handle;
return (Object*) dob;
}
DIR *Object_ToDIR(Object *obj, Error *error)
{
if(!Object_IsDir(obj))
{
Error_Report(error, 0, "Object is not a directory");
return NULL;
}
return ((DirObject*) obj)->dir;
}
static _Bool dir_free(Object *obj, Error *error)
{
DirObject *dob = (DirObject*) obj;
if(closedir(dob->dir) == 0)
return 1;
Error_Report(error, 0, "Failed to close directory");
return 0;
}