(MOD) Refined Var, Array, CatlogMsg, CatlogSender, Status

This is the very first version that pass the compiler.  Though it has lots things were made for testing, such as commenting problematic source code to avoid irrelevant errors.  For test.c, everything is working fine.  Congrats!
This commit is contained in:
William
2024-05-20 04:47:39 +08:00
parent e73f3af436
commit e2f8dceda7
23 changed files with 656 additions and 393 deletions

View File

@@ -21,23 +21,33 @@ static Status InvalidArrayLength = {
};
/* Elementary. */
Status Array_Create(Array *inst, int len);
Status Array_Create(Array *inst, int len, size_t size)
throws(InsufficientMemory InvalidArrayLength);
Status Array_CopyOf(Array *inst, Array *other);
Status Array_Delete(Array *inst);
Status Array_GetIdx(Array *inst, Var *store, int index);
throws(ArrayIndexOutOfBound);
Status Array_SetIdx(Array *inst, Var *source, int index);
bool Array_Equal(Array *arr1, Array *arr2);
throws(ArrayIndexOutOfBound);
bool Array_Equals(Array *arr1, Array *arr2);
/* Extensional. */
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
throws(ArrayIndexOutOfBound);
Status ArrayUtils_InsertArray(Array *inst, Array *items, int index);
throws(ArrayIndexOutOfBound);
Status ArrayUtils_Remove(Array *inst, int index);
throws(ArrayIndexOutOfBound);
Status ArrayUtils_RemoveArray(Array *inst, int off, int len);
throws(ArrayIndexOutOfBound InvalidArrayLength);
Status ArrayUtils_Subarray(Array *inst, Array *store, int off, int len);
throws(ArrayIndexOutOfBound InvalidArrayLength);
Status ArrayUtils_Fill(Array *inst, Var *elem, int off, int len);
throws(ArrayIndexOutOfBound InvalidArrayLength);
Status ArrayUtils_Search(Array *inst, Var *item, int *store);
Status ArrayUtils_SearchArray(Array *inst, Array *items, int *store);
Status ArrayUtils_Split(Array *inst, Array *fore, Array *rear, int index);
throws(ArrayIndexOutOfBound);
Status ArrayUtils_Revert(Array *inst);
bool ArrayUtils_IsEmpty(Array *inst);
bool ArrayUtils_IsBlank(Array *inst);

View File

@@ -1,7 +1,7 @@
#include <Compound/array.h>
#include <Compound/status.h>
Status Array_Create(Array *inst, int len)
Status Array_Create(Array *inst, int len, size_t size)
{
/* Skip unavailable inst and invalid param. */
fails(inst, UnavailableInstance);
@@ -10,29 +10,75 @@ Status Array_Create(Array *inst, int len)
inst->len = len;
inst->members = calloc(len, sizeof(Var));
int erridx = -1;
for (register int i = 0; i < len; i++) {
// TODO(william): Throw InsufficientMemory at following line.
solve(!StatusUtils_IsOkay(Var_Create(&inst->members[i], size)), {
#ifdef __DEBUG__
cat("Var_Create failed!\n")
#endif
erridx = i;
break;
} else {
#ifdef __DEBUG__
cat("Var_Create success!\n")
#endif
})
}
/* Review on erridx. Release data that allocated. */
if (erridx != -1) {
for (register int i = erridx; i >= 0; i--) {
Var_Delete(&inst->members[i]);
#ifdef __DEBUG__
cat("Deleted var from InsufficientMemory from Array_Create!")
#endif
}
/* Release array itself. */
free(inst->members);
return InsufficientMemory;
}
return NormalStatus;
}
Status Array_CopyOf(Array *inst, Array *other)
{
/* Skip unavailable inst and invalid param. */
fails(inst, UnavailableInstance);
fails(other, error(InvalidParameter, "Given other was unavailable."));
// /* Skip unavailable inst and invalid param. */
// fails(inst, UnavailableInstance);
// fails(other, error(InvalidParameter, "Given other was unavailable."));
/* Assign value for len. */
inst->len = other->len;
// /* Assign value for len. */
// inst->len = other->len;
/* Recreate array. */
if (inst->members == NULL) return NormalStatus;
match(RuntimeError, Array_Delete(inst), "Failed on deleting array.");
match(RuntimeError, Array_Create(inst, other->len), "Failed on recreating "
"array.");
// if (inst->members == NULL) return NormalStatus;
// match(RuntimeError, Array_Create(inst, other->len), "Failed on recreating "
// "array.");
/* Copy and assign for each member from other to inst. */
for (register int i = 0; i < inst->len; i++) {
inst[i] = other[i];
}
// /* Copy and assign for each member from other to inst. */
// for (register int i = 0; i < inst->len; i++) {
// inst[i] = other[i];
// }
return NormalStatus;
// return NormalStatus;
/*
if (other == NULL) return 1;
String_Create(inst, other->len);
for (register int i = 0; i < other->len; i++) {
inst->arr[i] = other->arr[i];
}
return 0;
*/
}
Status Array_Delete(Array *inst)
@@ -74,14 +120,14 @@ Status Array_SetIdx(Array *inst, Var *source, int index)
return NormalStatus;
}
bool Array_Equal(Array *a, Array *b)
bool Array_Equals(Array *a, Array *b)
{
/* Skip unavailable inst and invalid param. */
state((a == NULL || b == NULL), false);
state((a->len != b->len), false);
for (register int i = 0; i < a->len; i++) {
if (!Var_Equal(&a->members[i], &b->members[i])) {
if (!Var_Equals(&a->members[i], &b->members[i])) {
return false;
}
}