diff --git a/src/lib/builtins/basic.c b/src/lib/builtins/basic.c index 33450a4..a6ae85a 100644 --- a/src/lib/builtins/basic.c +++ b/src/lib/builtins/basic.c @@ -38,6 +38,7 @@ #include "string.h" #include "buffer.h" #include "../utils/defs.h" +#include "../common/defs.h" #include "../objects/objects.h" #include "../runtime/runtime.h" #include "../compiler/compile.h" @@ -71,13 +72,10 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object return -1; } - int n; - path = Object_GetString(o_path, &n); + path = Object_GetString(o_path, &path_len); if (path == NULL) return -1; - ASSERT(n >= 0); - path_len = (size_t) n; } char full_path[1024]; @@ -291,7 +289,7 @@ static int bin_error(Runtime *runtime, Object **argv, unsigned int argc, Object return -1; } - int length; + size_t length; const char *string; string = Object_GetString(argv[0], &length); @@ -316,18 +314,18 @@ void bins_basic_init(StaticMapSlot slots[]) } StaticMapSlot bins_basic[] = { - { "Type", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "None", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "int", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "bool", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "float", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "String", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "List", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "Map", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "File", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "Dir", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, - { "math", SM_SMAP, .as_smap = bins_math, }, + { TYPENAME_TYPE, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_NONE, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_INT, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_BOOL, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_FLOAT, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_STRING, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_LIST, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_MAP, SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, + { TYPENAME_FILE, 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, }, { "files", SM_SMAP, .as_smap = bins_files, }, { "buffer", SM_SMAP, .as_smap = bins_buffer, }, { "string", SM_SMAP, .as_smap = bins_string, }, diff --git a/src/lib/builtins/math.c b/src/lib/builtins/math.c index a3fc806..4a61465 100644 --- a/src/lib/builtins/math.c +++ b/src/lib/builtins/math.c @@ -119,4 +119,4 @@ StaticMapSlot bins_math[] = { { "pow", SM_FUNCT, .as_funct = bin_pow, .argc = 1, }, { "sqrt", SM_FUNCT, .as_funct = bin_sqrt, .argc = 1, }, { NULL, SM_END, {}, {} }, -}; \ No newline at end of file +}; diff --git a/src/lib/builtins/string.c b/src/lib/builtins/string.c index 7a64b13..05c353b 100644 --- a/src/lib/builtins/string.c +++ b/src/lib/builtins/string.c @@ -21,19 +21,19 @@ static int bin_ord(Runtime *runtime, Object **argv, unsigned int argc, Object *r return -1; } - const char *string; - int n; - string = Object_GetString(argv[0], &n); + const char *string; + size_t length; + string = Object_GetString(argv[0], &length); ASSERT(string != NULL); - if(n == 0) + if(length == 0) { Error_Report(error, 0, "Argument #%d is an empty string", 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); ASSERT(k >= 0); @@ -114,17 +114,17 @@ static int bin_cat(Runtime *runtime, Object **argv, unsigned int argc, Object *r for(unsigned int i = 0, written = 0; i < argc; i += 1) { - int n; - const char *s = Object_GetString(argv[i], &n); + size_t length; + const char *s = Object_GetString(argv[i], &length); - memcpy(buffer + written, s, n); - written += n; + memcpy(buffer + written, s, length); + written += length; } buffer[total_count] = '\0'; result = Object_FromString(buffer, total_count, Runtime_GetHeap(runtime), error); - + if(starting != buffer) free(buffer); diff --git a/src/lib/common/defs.h b/src/lib/common/defs.h new file mode 100644 index 0000000..5a5fa99 --- /dev/null +++ b/src/lib/common/defs.h @@ -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" \ No newline at end of file diff --git a/src/lib/objects/o_bool.c b/src/lib/objects/o_bool.c index 76516fb..563bc48 100644 --- a/src/lib/objects/o_bool.c +++ b/src/lib/objects/o_bool.c @@ -31,6 +31,7 @@ #include #include "objects.h" #include "../utils/defs.h" +#include "../common/defs.h" static void print(Object *obj, FILE *fp); 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 = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "bool", + .name = TYPENAME_BOOL, .size = sizeof(Object), .hash = hash, .copy = copy, @@ -89,9 +90,9 @@ static _Bool op_eql(Object *self, Object *other) bool Object_GetBool(Object *obj) { if(!Object_IsBool(obj)) { - Error_Panic("%s expected a bool object, but " - "an %s was provided", __func__, - Object_GetName(obj)); + Error_Panic("%s expected a " TYPENAME_BOOL + " object, but an %s was provided", + __func__, Object_GetName(obj)); return -1; } diff --git a/src/lib/objects/o_buffer.c b/src/lib/objects/o_buffer.c index 982b686..9cc43cc 100644 --- a/src/lib/objects/o_buffer.c +++ b/src/lib/objects/o_buffer.c @@ -30,8 +30,9 @@ #include #include -#include "../utils/defs.h" #include "objects.h" +#include "../utils/defs.h" +#include "../common/defs.h" typedef struct { size_t refs, size; @@ -52,7 +53,7 @@ static _Bool buffer_free(Object *self, Error *error); static TypeObject t_buffer = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "buffer", + .name = TYPENAME_BUFFER, .size = sizeof(BufferObject), .select = buffer_select, .insert = buffer_insert, @@ -119,7 +120,7 @@ Object *Object_SliceBuffer(Object *obj, size_t offset, size_t length, Heap *heap { if(!Object_IsBuffer(obj)) { - Error_Report(error, 0, "Not a buffer"); + Error_Report(error, 0, "Not a " TYPENAME_BUFFER); return NULL; } @@ -150,7 +151,7 @@ void *Object_GetBuffer(Object *obj, size_t *size) { if(!Object_IsBuffer(obj)) { - Error_Panic("Not a buffer"); + Error_Panic("Not a " TYPENAME_BUFFER); return NULL; } diff --git a/src/lib/objects/o_closure.c b/src/lib/objects/o_closure.c index 3d82d18..3f37fc4 100644 --- a/src/lib/objects/o_closure.c +++ b/src/lib/objects/o_closure.c @@ -44,7 +44,7 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *err); static TypeObject t_closure = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "closure", + .name = "Closure", .size = sizeof(ClosureObject), .select = select_, .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) { - Error_Report(error, 0, "Object is not a closure"); + Error_Report(error, 0, "Object is not a Closure"); return NULL; } diff --git a/src/lib/objects/o_dir.c b/src/lib/objects/o_dir.c index ae52208..506e792 100644 --- a/src/lib/objects/o_dir.c +++ b/src/lib/objects/o_dir.c @@ -29,6 +29,7 @@ */ #include "objects.h" +#include "../common/defs.h" typedef struct { Object base; @@ -39,7 +40,7 @@ static _Bool dir_free(Object *obj, Error *error); static TypeObject t_dir = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "Directory", + .name = TYPENAME_DIRECTORY, .size = sizeof(DirObject), .free = dir_free, }; @@ -70,8 +71,9 @@ Object *Object_FromDIR(DIR *handle, Heap *heap, Error *error) DIR *Object_GetDIR(Object *obj) { if(!Object_IsDir(obj)) { - Error_Panic("%s expected a directory object, but an %s " - "was provided", __func__, Object_GetName(obj)); + Error_Panic("%s expected a " TYPENAME_DIRECTORY + " object, but an %s was provided", + __func__, Object_GetName(obj)); return NULL; } diff --git a/src/lib/objects/o_file.c b/src/lib/objects/o_file.c index 69d43ed..0b2b9c8 100644 --- a/src/lib/objects/o_file.c +++ b/src/lib/objects/o_file.c @@ -29,6 +29,7 @@ */ #include "objects.h" +#include "../common/defs.h" typedef struct { Object base; @@ -39,7 +40,7 @@ static _Bool file_free(Object *self, Error *error); static TypeObject t_file = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "File", + .name = TYPENAME_FILE, .size = sizeof(FileObject), .free = file_free, }; @@ -57,8 +58,9 @@ _Bool Object_IsFile(Object *obj) FILE *Object_GetStream(Object *obj) { if(!Object_IsDir(obj)) { - Error_Panic("%s expected a file object, but an %s " - "was provided", __func__, Object_GetName(obj)); + Error_Panic("%s expected a " TYPENAME_FILE + "object, but an %s was provided", + __func__, Object_GetName(obj)); return NULL; } diff --git a/src/lib/objects/o_float.c b/src/lib/objects/o_float.c index 47561da..8ca59f1 100644 --- a/src/lib/objects/o_float.c +++ b/src/lib/objects/o_float.c @@ -32,6 +32,7 @@ #include "objects.h" #include "../utils/defs.h" #include "../utils/hash.h" +#include "../common/defs.h" static void print(Object *obj, FILE *fp); static _Bool op_eql(Object *self, Object *other); @@ -45,7 +46,7 @@ typedef struct { static TypeObject t_float = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "float", + .name = TYPENAME_FLOAT, .size = sizeof (FloatObject), .hash = hash, .copy = copy, @@ -86,9 +87,9 @@ static _Bool op_eql(Object *self, Object *other) double Object_GetFloat(Object *obj) { if(!Object_IsFloat(obj)) { - Error_Panic("%s expected a float object, but " - "an %s was provided", __func__, - Object_GetName(obj)); + Error_Panic("%s expected a " TYPENAME_FLOAT + " object, but an %s was provided", + __func__, Object_GetName(obj)); return 0.0; } diff --git a/src/lib/objects/o_int.c b/src/lib/objects/o_int.c index 03ff8b2..1f69773 100644 --- a/src/lib/objects/o_int.c +++ b/src/lib/objects/o_int.c @@ -32,6 +32,7 @@ #include "objects.h" #include "../utils/defs.h" #include "../utils/hash.h" +#include "../common/defs.h" static void print(Object *obj, FILE *fp); static _Bool op_eql(Object *self, Object *other); @@ -45,7 +46,7 @@ typedef struct { static TypeObject t_int = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "int", + .name = TYPENAME_INT, .size = sizeof(IntObject), .hash = hash, .copy = copy, @@ -92,9 +93,9 @@ Object *Object_FromInt(long long int val, Heap *heap, Error *error) long long int Object_GetInt(Object *obj) { if(!Object_IsInt(obj)) { - Error_Panic("%s expected an int object, but " - "an %s was provided", __func__, - Object_GetName(obj)); + Error_Panic("%s expected an " TYPENAME_INT + " object, but an %s was provided", + __func__, Object_GetName(obj)); return 0; } @@ -123,4 +124,4 @@ static _Bool op_eql(Object *self, Object *other) i2 = (IntObject*) other; return i1->val == i2->val; -} \ No newline at end of file +} diff --git a/src/lib/objects/o_list.c b/src/lib/objects/o_list.c index 3528726..24684c1 100644 --- a/src/lib/objects/o_list.c +++ b/src/lib/objects/o_list.c @@ -29,8 +29,9 @@ */ #include -#include "../utils/defs.h" #include "objects.h" +#include "../utils/defs.h" +#include "../common/defs.h" typedef struct { Object base; @@ -49,7 +50,7 @@ static int hash(Object *self); static TypeObject t_list = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "list", + .name = TYPENAME_LIST, .size = sizeof(ListObject), .copy = copy, .hash = hash, diff --git a/src/lib/objects/o_map.c b/src/lib/objects/o_map.c index aa7be39..b0c8a71 100644 --- a/src/lib/objects/o_map.c +++ b/src/lib/objects/o_map.c @@ -28,8 +28,9 @@ ** +--------------------------------------------------------------------------+ */ -#include "../utils/defs.h" #include "objects.h" +#include "../utils/defs.h" +#include "../common/defs.h" typedef struct { Object base; @@ -50,7 +51,7 @@ static int hash(Object *self); static TypeObject t_map = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "map", + .name = TYPENAME_MAP, .size = sizeof(MapObject), .copy = copy, .hash = hash, diff --git a/src/lib/objects/o_none.c b/src/lib/objects/o_none.c index 73356ca..65a4076 100644 --- a/src/lib/objects/o_none.c +++ b/src/lib/objects/o_none.c @@ -31,6 +31,7 @@ #include #include "objects.h" #include "../utils/defs.h" +#include "../common/defs.h" static _Bool op_eql(Object *self, Object *other); static void print(Object *obj, FILE *fp); @@ -39,7 +40,7 @@ static Object *copy(Object *self, Heap *heap, Error *err); static TypeObject t_none = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "none", + .name = TYPENAME_NONE, .size = sizeof(Object), .hash = hash, .copy = copy, diff --git a/src/lib/objects/o_string.c b/src/lib/objects/o_string.c index 2500b62..6924ce2 100644 --- a/src/lib/objects/o_string.c +++ b/src/lib/objects/o_string.c @@ -29,10 +29,11 @@ */ #include +#include "objects.h" #include "../utils/defs.h" #include "../utils/hash.h" #include "../utils/utf8.h" -#include "objects.h" +#include "../common/defs.h" typedef struct { Object base; @@ -51,7 +52,7 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *error); static TypeObject t_string = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "string", + .name = TYPENAME_STRING, .size = sizeof(StringObject), .hash = hash, .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); } -const char *Object_GetString(Object *obj, int *size) +const char *Object_GetString(Object *obj, size_t *size) { if(!Object_IsString(obj)) { - Error_Panic("%s expected a string object, but " - "an %s was provided", __func__, - Object_GetName(obj)); + Error_Panic("%s expected a " TYPENAME_STRING + " object, but an %s was provided", + __func__, Object_GetName(obj)); return NULL; } diff --git a/src/lib/objects/objects.c b/src/lib/objects/objects.c index e092e87..4ec6869 100644 --- a/src/lib/objects/objects.c +++ b/src/lib/objects/objects.c @@ -30,12 +30,13 @@ #include "objects.h" #include "../utils/defs.h" +#include "../common/defs.h" static _Bool op_eql(Object *self, Object *other); TypeObject t_type = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, - .name = "type", + .name = TYPENAME_TYPE, .size = sizeof(TypeObject), .op_eql = op_eql, }; diff --git a/src/lib/objects/objects.h b/src/lib/objects/objects.h index 36d56a6..94b5a02 100644 --- a/src/lib/objects/objects.h +++ b/src/lib/objects/objects.h @@ -56,10 +56,8 @@ struct TypeObject { Object base; - // Any. - const char *name; - unsigned int size; - //AtomicType atomic; + const char *name; + size_t size; bool (*init)(Object *self, Error *err); bool (*free)(Object *self, Error *err); @@ -67,7 +65,6 @@ struct TypeObject { 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); void (*print)(Object *self, FILE *fp); - unsigned int (*deepsize)(const Object *self); // Collections. Object *(*select)(Object *self, Object *key, Heap *heap, Error *err); @@ -75,9 +72,7 @@ struct TypeObject { bool (*insert)(Object *self, Object *key, Object *val, Heap *heap, Error *err); int (*count)(Object *self); - bool (*op_eql)(Object *self, Object *other); - - // All. + bool (*op_eql)(Object *self, Object *other); 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); }; @@ -100,11 +95,11 @@ void *Heap_GetPointer(Heap *heap); unsigned int Heap_GetSize(Heap *heap); const TypeObject* Object_GetType(const Object *obj); -const char* Object_GetName(const Object *obj); -int Object_Hash (Object *obj); -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); -void Object_Print (Object *obj, FILE *fp); +const char* Object_GetName(const Object *obj); +int Object_Hash (Object *obj); +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); +void Object_Print(Object *obj, FILE *fp); Object* Object_Select(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); @@ -151,7 +146,7 @@ bool Object_IsDir(Object *obj); long long int Object_GetInt (Object *obj); bool Object_GetBool (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); FILE *Object_GetStream(Object *obj); void *Object_GetBuffer(Object *obj, size_t *size); diff --git a/src/lib/runtime/runtime.c b/src/lib/runtime/runtime.c index ab43011..3340851 100644 --- a/src/lib/runtime/runtime.c +++ b/src/lib/runtime/runtime.c @@ -31,10 +31,10 @@ #include #include #include +#include "runtime.h" #include "../utils/path.h" #include "../utils/defs.h" #include "../utils/stack.h" -#include "runtime.h" #define MAX_FRAME_STACK 16 #define MAX_FRAMES 16 diff --git a/src/lib/utils/error.h b/src/lib/utils/error.h index dae04da..61eb07a 100644 --- a/src/lib/utils/error.h +++ b/src/lib/utils/error.h @@ -33,7 +33,6 @@ #include typedef struct Error Error; - struct Error { void (*on_report)(Error *err); _Bool occurred,