cleaning up
This commit is contained in:
+14
-16
@@ -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, },
|
||||
|
||||
@@ -22,18 +22,18 @@ static int bin_ord(Runtime *runtime, Object **argv, unsigned int argc, Object *r
|
||||
}
|
||||
|
||||
const char *string;
|
||||
int n;
|
||||
string = Object_GetString(argv[0], &n);
|
||||
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,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)
|
||||
{
|
||||
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';
|
||||
|
||||
@@ -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"
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <string.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,8 +30,9 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,8 +29,9 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <string.h>
|
||||
#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,
|
||||
|
||||
@@ -29,10 +29,11 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -56,10 +56,8 @@ struct TypeObject {
|
||||
|
||||
Object base;
|
||||
|
||||
// Any.
|
||||
const char *name;
|
||||
unsigned int size;
|
||||
//AtomicType atomic;
|
||||
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);
|
||||
@@ -76,8 +73,6 @@ struct TypeObject {
|
||||
int (*count)(Object *self);
|
||||
|
||||
bool (*op_eql)(Object *self, Object *other);
|
||||
|
||||
// All.
|
||||
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);
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
@@ -31,10 +31,10 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#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
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef struct Error Error;
|
||||
|
||||
struct Error {
|
||||
void (*on_report)(Error *err);
|
||||
_Bool occurred,
|
||||
|
||||
Reference in New Issue
Block a user