cleaning up

This commit is contained in:
Francesco Cozzuto
2022-08-24 18:01:32 +02:00
parent cd74dda4a9
commit d610a973f4
19 changed files with 100 additions and 80 deletions
+14 -16
View File
@@ -38,6 +38,7 @@
#include "string.h" #include "string.h"
#include "buffer.h" #include "buffer.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../common/defs.h"
#include "../objects/objects.h" #include "../objects/objects.h"
#include "../runtime/runtime.h" #include "../runtime/runtime.h"
#include "../compiler/compile.h" #include "../compiler/compile.h"
@@ -71,13 +72,10 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
return -1; return -1;
} }
int n; path = Object_GetString(o_path, &path_len);
path = Object_GetString(o_path, &n);
if (path == NULL) if (path == NULL)
return -1; return -1;
ASSERT(n >= 0);
path_len = (size_t) n;
} }
char full_path[1024]; char full_path[1024];
@@ -291,7 +289,7 @@ static int bin_error(Runtime *runtime, Object **argv, unsigned int argc, Object
return -1; return -1;
} }
int length; size_t length;
const char *string; const char *string;
string = Object_GetString(argv[0], &length); string = Object_GetString(argv[0], &length);
@@ -316,18 +314,18 @@ void bins_basic_init(StaticMapSlot slots[])
} }
StaticMapSlot bins_basic[] = { StaticMapSlot bins_basic[] = {
{ "Type", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_TYPE, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "None", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_NONE, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "int", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_INT, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "bool", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_BOOL, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "float", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_FLOAT, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "String", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_STRING, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "List", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_LIST, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "Map", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_MAP, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "File", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_FILE, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "Dir", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { TYPENAME_DIRECTORY, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "math", SM_SMAP, .as_smap = bins_math, },
{ "math", SM_SMAP, .as_smap = bins_math, },
{ "files", SM_SMAP, .as_smap = bins_files, }, { "files", SM_SMAP, .as_smap = bins_files, },
{ "buffer", SM_SMAP, .as_smap = bins_buffer, }, { "buffer", SM_SMAP, .as_smap = bins_buffer, },
{ "string", SM_SMAP, .as_smap = bins_string, }, { "string", SM_SMAP, .as_smap = bins_string, },
+8 -8
View File
@@ -22,18 +22,18 @@ static int bin_ord(Runtime *runtime, Object **argv, unsigned int argc, Object *r
} }
const char *string; const char *string;
int n; size_t length;
string = Object_GetString(argv[0], &n); string = Object_GetString(argv[0], &length);
ASSERT(string != NULL); ASSERT(string != NULL);
if(n == 0) if(length == 0)
{ {
Error_Report(error, 0, "Argument #%d is an empty string", 1); Error_Report(error, 0, "Argument #%d is an empty string", 1);
return -1; return -1;
} }
int k = utf8_sequence_to_utf32_codepoint(string,n,&ret); int k = utf8_sequence_to_utf32_codepoint(string, length, &ret);
UNUSED(k); UNUSED(k);
ASSERT(k >= 0); ASSERT(k >= 0);
@@ -114,11 +114,11 @@ static int bin_cat(Runtime *runtime, Object **argv, unsigned int argc, Object *r
for(unsigned int i = 0, written = 0; i < argc; i += 1) for(unsigned int i = 0, written = 0; i < argc; i += 1)
{ {
int n; size_t length;
const char *s = Object_GetString(argv[i], &n); const char *s = Object_GetString(argv[i], &length);
memcpy(buffer + written, s, n); memcpy(buffer + written, s, length);
written += n; written += length;
} }
buffer[total_count] = '\0'; buffer[total_count] = '\0';
+15
View File
@@ -0,0 +1,15 @@
#define TYPENAME_TYPE "Type"
#define TYPENAME_NONE "None"
#define TYPENAME_INT "int"
#define TYPENAME_BOOL "bool"
#define TYPENAME_FLOAT "float"
#define TYPENAME_MAP "Map"
#define TYPENAME_LIST "List"
#define TYPENAME_STRING "String"
#define TYPENAME_BUFFER "Buffer"
#define TYPENAME_FILE "File"
#define TYPENAME_DIRECTORY "Directory"
+5 -4
View File
@@ -31,6 +31,7 @@
#include <string.h> #include <string.h>
#include "objects.h" #include "objects.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../common/defs.h"
static void print(Object *obj, FILE *fp); static void print(Object *obj, FILE *fp);
static _Bool op_eql(Object *self, Object *other); static _Bool op_eql(Object *self, Object *other);
@@ -39,7 +40,7 @@ static Object *copy(Object *self, Heap *heap, Error *err);
static TypeObject t_bool = { static TypeObject t_bool = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "bool", .name = TYPENAME_BOOL,
.size = sizeof(Object), .size = sizeof(Object),
.hash = hash, .hash = hash,
.copy = copy, .copy = copy,
@@ -89,9 +90,9 @@ static _Bool op_eql(Object *self, Object *other)
bool Object_GetBool(Object *obj) bool Object_GetBool(Object *obj)
{ {
if(!Object_IsBool(obj)) { if(!Object_IsBool(obj)) {
Error_Panic("%s expected a bool object, but " Error_Panic("%s expected a " TYPENAME_BOOL
"an %s was provided", __func__, " object, but an %s was provided",
Object_GetName(obj)); __func__, Object_GetName(obj));
return -1; return -1;
} }
+5 -4
View File
@@ -30,8 +30,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../utils/defs.h"
#include "objects.h" #include "objects.h"
#include "../utils/defs.h"
#include "../common/defs.h"
typedef struct { typedef struct {
size_t refs, size; size_t refs, size;
@@ -52,7 +53,7 @@ static _Bool buffer_free(Object *self, Error *error);
static TypeObject t_buffer = { static TypeObject t_buffer = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "buffer", .name = TYPENAME_BUFFER,
.size = sizeof(BufferObject), .size = sizeof(BufferObject),
.select = buffer_select, .select = buffer_select,
.insert = buffer_insert, .insert = buffer_insert,
@@ -119,7 +120,7 @@ Object *Object_SliceBuffer(Object *obj, size_t offset, size_t length, Heap *heap
{ {
if(!Object_IsBuffer(obj)) if(!Object_IsBuffer(obj))
{ {
Error_Report(error, 0, "Not a buffer"); Error_Report(error, 0, "Not a " TYPENAME_BUFFER);
return NULL; return NULL;
} }
@@ -150,7 +151,7 @@ void *Object_GetBuffer(Object *obj, size_t *size)
{ {
if(!Object_IsBuffer(obj)) if(!Object_IsBuffer(obj))
{ {
Error_Panic("Not a buffer"); Error_Panic("Not a " TYPENAME_BUFFER);
return NULL; return NULL;
} }
+2 -2
View File
@@ -44,7 +44,7 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *err);
static TypeObject t_closure = { static TypeObject t_closure = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "closure", .name = "Closure",
.size = sizeof(ClosureObject), .size = sizeof(ClosureObject),
.select = select_, .select = select_,
.walk = walk, .walk = walk,
@@ -59,7 +59,7 @@ Object *Object_NewClosure(Object *parent, Object *new_map, Heap *heap, Error *er
if(parent != NULL && parent->type != &t_closure) if(parent != NULL && parent->type != &t_closure)
{ {
Error_Report(error, 0, "Object is not a closure"); Error_Report(error, 0, "Object is not a Closure");
return NULL; return NULL;
} }
+5 -3
View File
@@ -29,6 +29,7 @@
*/ */
#include "objects.h" #include "objects.h"
#include "../common/defs.h"
typedef struct { typedef struct {
Object base; Object base;
@@ -39,7 +40,7 @@ static _Bool dir_free(Object *obj, Error *error);
static TypeObject t_dir = { static TypeObject t_dir = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "Directory", .name = TYPENAME_DIRECTORY,
.size = sizeof(DirObject), .size = sizeof(DirObject),
.free = dir_free, .free = dir_free,
}; };
@@ -70,8 +71,9 @@ Object *Object_FromDIR(DIR *handle, Heap *heap, Error *error)
DIR *Object_GetDIR(Object *obj) DIR *Object_GetDIR(Object *obj)
{ {
if(!Object_IsDir(obj)) { if(!Object_IsDir(obj)) {
Error_Panic("%s expected a directory object, but an %s " Error_Panic("%s expected a " TYPENAME_DIRECTORY
"was provided", __func__, Object_GetName(obj)); " object, but an %s was provided",
__func__, Object_GetName(obj));
return NULL; return NULL;
} }
+5 -3
View File
@@ -29,6 +29,7 @@
*/ */
#include "objects.h" #include "objects.h"
#include "../common/defs.h"
typedef struct { typedef struct {
Object base; Object base;
@@ -39,7 +40,7 @@ static _Bool file_free(Object *self, Error *error);
static TypeObject t_file = { static TypeObject t_file = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "File", .name = TYPENAME_FILE,
.size = sizeof(FileObject), .size = sizeof(FileObject),
.free = file_free, .free = file_free,
}; };
@@ -57,8 +58,9 @@ _Bool Object_IsFile(Object *obj)
FILE *Object_GetStream(Object *obj) FILE *Object_GetStream(Object *obj)
{ {
if(!Object_IsDir(obj)) { if(!Object_IsDir(obj)) {
Error_Panic("%s expected a file object, but an %s " Error_Panic("%s expected a " TYPENAME_FILE
"was provided", __func__, Object_GetName(obj)); "object, but an %s was provided",
__func__, Object_GetName(obj));
return NULL; return NULL;
} }
+5 -4
View File
@@ -32,6 +32,7 @@
#include "objects.h" #include "objects.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/hash.h" #include "../utils/hash.h"
#include "../common/defs.h"
static void print(Object *obj, FILE *fp); static void print(Object *obj, FILE *fp);
static _Bool op_eql(Object *self, Object *other); static _Bool op_eql(Object *self, Object *other);
@@ -45,7 +46,7 @@ typedef struct {
static TypeObject t_float = { static TypeObject t_float = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "float", .name = TYPENAME_FLOAT,
.size = sizeof (FloatObject), .size = sizeof (FloatObject),
.hash = hash, .hash = hash,
.copy = copy, .copy = copy,
@@ -86,9 +87,9 @@ static _Bool op_eql(Object *self, Object *other)
double Object_GetFloat(Object *obj) double Object_GetFloat(Object *obj)
{ {
if(!Object_IsFloat(obj)) { if(!Object_IsFloat(obj)) {
Error_Panic("%s expected a float object, but " Error_Panic("%s expected a " TYPENAME_FLOAT
"an %s was provided", __func__, " object, but an %s was provided",
Object_GetName(obj)); __func__, Object_GetName(obj));
return 0.0; return 0.0;
} }
+5 -4
View File
@@ -32,6 +32,7 @@
#include "objects.h" #include "objects.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/hash.h" #include "../utils/hash.h"
#include "../common/defs.h"
static void print(Object *obj, FILE *fp); static void print(Object *obj, FILE *fp);
static _Bool op_eql(Object *self, Object *other); static _Bool op_eql(Object *self, Object *other);
@@ -45,7 +46,7 @@ typedef struct {
static TypeObject t_int = { static TypeObject t_int = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "int", .name = TYPENAME_INT,
.size = sizeof(IntObject), .size = sizeof(IntObject),
.hash = hash, .hash = hash,
.copy = copy, .copy = copy,
@@ -92,9 +93,9 @@ Object *Object_FromInt(long long int val, Heap *heap, Error *error)
long long int Object_GetInt(Object *obj) long long int Object_GetInt(Object *obj)
{ {
if(!Object_IsInt(obj)) { if(!Object_IsInt(obj)) {
Error_Panic("%s expected an int object, but " Error_Panic("%s expected an " TYPENAME_INT
"an %s was provided", __func__, " object, but an %s was provided",
Object_GetName(obj)); __func__, Object_GetName(obj));
return 0; return 0;
} }
+3 -2
View File
@@ -29,8 +29,9 @@
*/ */
#include <string.h> #include <string.h>
#include "../utils/defs.h"
#include "objects.h" #include "objects.h"
#include "../utils/defs.h"
#include "../common/defs.h"
typedef struct { typedef struct {
Object base; Object base;
@@ -49,7 +50,7 @@ static int hash(Object *self);
static TypeObject t_list = { static TypeObject t_list = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "list", .name = TYPENAME_LIST,
.size = sizeof(ListObject), .size = sizeof(ListObject),
.copy = copy, .copy = copy,
.hash = hash, .hash = hash,
+3 -2
View File
@@ -28,8 +28,9 @@
** +--------------------------------------------------------------------------+ ** +--------------------------------------------------------------------------+
*/ */
#include "../utils/defs.h"
#include "objects.h" #include "objects.h"
#include "../utils/defs.h"
#include "../common/defs.h"
typedef struct { typedef struct {
Object base; Object base;
@@ -50,7 +51,7 @@ static int hash(Object *self);
static TypeObject t_map = { static TypeObject t_map = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "map", .name = TYPENAME_MAP,
.size = sizeof(MapObject), .size = sizeof(MapObject),
.copy = copy, .copy = copy,
.hash = hash, .hash = hash,
+2 -1
View File
@@ -31,6 +31,7 @@
#include <string.h> #include <string.h>
#include "objects.h" #include "objects.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../common/defs.h"
static _Bool op_eql(Object *self, Object *other); static _Bool op_eql(Object *self, Object *other);
static void print(Object *obj, FILE *fp); static void print(Object *obj, FILE *fp);
@@ -39,7 +40,7 @@ static Object *copy(Object *self, Heap *heap, Error *err);
static TypeObject t_none = { static TypeObject t_none = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "none", .name = TYPENAME_NONE,
.size = sizeof(Object), .size = sizeof(Object),
.hash = hash, .hash = hash,
.copy = copy, .copy = copy,
+7 -6
View File
@@ -29,10 +29,11 @@
*/ */
#include <string.h> #include <string.h>
#include "objects.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/hash.h" #include "../utils/hash.h"
#include "../utils/utf8.h" #include "../utils/utf8.h"
#include "objects.h" #include "../common/defs.h"
typedef struct { typedef struct {
Object base; Object base;
@@ -51,7 +52,7 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *error);
static TypeObject t_string = { static TypeObject t_string = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "string", .name = TYPENAME_STRING,
.size = sizeof(StringObject), .size = sizeof(StringObject),
.hash = hash, .hash = hash,
.count = count, .count = count,
@@ -112,12 +113,12 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *error)
return Object_FromString(str->body + byteoffset, codelength, heap, error); return Object_FromString(str->body + byteoffset, codelength, heap, error);
} }
const char *Object_GetString(Object *obj, int *size) const char *Object_GetString(Object *obj, size_t *size)
{ {
if(!Object_IsString(obj)) { if(!Object_IsString(obj)) {
Error_Panic("%s expected a string object, but " Error_Panic("%s expected a " TYPENAME_STRING
"an %s was provided", __func__, " object, but an %s was provided",
Object_GetName(obj)); __func__, Object_GetName(obj));
return NULL; return NULL;
} }
+2 -1
View File
@@ -30,12 +30,13 @@
#include "objects.h" #include "objects.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../common/defs.h"
static _Bool op_eql(Object *self, Object *other); static _Bool op_eql(Object *self, Object *other);
TypeObject t_type = { TypeObject t_type = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC }, .base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "type", .name = TYPENAME_TYPE,
.size = sizeof(TypeObject), .size = sizeof(TypeObject),
.op_eql = op_eql, .op_eql = op_eql,
}; };
+3 -8
View File
@@ -56,10 +56,8 @@ struct TypeObject {
Object base; Object base;
// Any.
const char *name; const char *name;
unsigned int size; size_t size;
//AtomicType atomic;
bool (*init)(Object *self, Error *err); bool (*init)(Object *self, Error *err);
bool (*free)(Object *self, Error *err); bool (*free)(Object *self, Error *err);
@@ -67,7 +65,6 @@ struct TypeObject {
Object* (*copy)(Object *self, Heap *heap, Error *err); Object* (*copy)(Object *self, Heap *heap, Error *err);
int (*call)(Object *self, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *err); int (*call)(Object *self, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *err);
void (*print)(Object *self, FILE *fp); void (*print)(Object *self, FILE *fp);
unsigned int (*deepsize)(const Object *self);
// Collections. // Collections.
Object *(*select)(Object *self, Object *key, Heap *heap, Error *err); Object *(*select)(Object *self, Object *key, Heap *heap, Error *err);
@@ -76,8 +73,6 @@ struct TypeObject {
int (*count)(Object *self); int (*count)(Object *self);
bool (*op_eql)(Object *self, Object *other); bool (*op_eql)(Object *self, Object *other);
// All.
void (*walk) (Object *self, void (*callback)(Object **referer, void *userp), void *userp); void (*walk) (Object *self, void (*callback)(Object **referer, void *userp), void *userp);
void (*walkexts)(Object *self, void (*callback)(void **referer, unsigned int size, void *userp), void *userp); void (*walkexts)(Object *self, void (*callback)(void **referer, unsigned int size, void *userp), void *userp);
}; };
@@ -104,7 +99,7 @@ const char* Object_GetName(const Object *obj);
int Object_Hash (Object *obj); int Object_Hash (Object *obj);
Object* Object_Copy (Object *obj, Heap *heap, Error *err); Object* Object_Copy (Object *obj, Heap *heap, Error *err);
int Object_Call (Object *obj, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *err); int Object_Call (Object *obj, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *err);
void Object_Print (Object *obj, FILE *fp); void Object_Print(Object *obj, FILE *fp);
Object* Object_Select(Object *coll, Object *key, Heap *heap, Error *err); Object* Object_Select(Object *coll, Object *key, Heap *heap, Error *err);
Object* Object_Delete(Object *coll, Object *key, Heap *heap, Error *err); Object* Object_Delete(Object *coll, Object *key, Heap *heap, Error *err);
bool Object_Insert(Object *coll, Object *key, Object *val, Heap *heap, Error *err); bool Object_Insert(Object *coll, Object *key, Object *val, Heap *heap, Error *err);
@@ -151,7 +146,7 @@ bool Object_IsDir(Object *obj);
long long int Object_GetInt (Object *obj); long long int Object_GetInt (Object *obj);
bool Object_GetBool (Object *obj); bool Object_GetBool (Object *obj);
double Object_GetFloat(Object *obj); double Object_GetFloat(Object *obj);
const char *Object_GetString(Object *obj, int *size); const char *Object_GetString(Object *obj, size_t *size);
DIR *Object_GetDIR(Object *obj); DIR *Object_GetDIR(Object *obj);
FILE *Object_GetStream(Object *obj); FILE *Object_GetStream(Object *obj);
void *Object_GetBuffer(Object *obj, size_t *size); void *Object_GetBuffer(Object *obj, size_t *size);
+1 -1
View File
@@ -31,10 +31,10 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "runtime.h"
#include "../utils/path.h" #include "../utils/path.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/stack.h" #include "../utils/stack.h"
#include "runtime.h"
#define MAX_FRAME_STACK 16 #define MAX_FRAME_STACK 16
#define MAX_FRAMES 16 #define MAX_FRAMES 16
-1
View File
@@ -33,7 +33,6 @@
#include <stdarg.h> #include <stdarg.h>
typedef struct Error Error; typedef struct Error Error;
struct Error { struct Error {
void (*on_report)(Error *err); void (*on_report)(Error *err);
_Bool occurred, _Bool occurred,