(MOD) Removed Array_GetIdx and Array_SetIdx due to better approach was discovered.

(MOD) Defined 2 more statuses:  InvalidOperation, InvalidOperationBetweenAliveAndNonAlive.

(MOD) Defined 1 more macro:  assign.
This commit is contained in:
2024-06-26 20:20:04 +08:00
parent a63063de42
commit 310586ab86
5 changed files with 109 additions and 15 deletions

View File

@@ -15,8 +15,8 @@ DEFSTATUS(InvalidArrayLength, 1, "Given length is invalid.", STATUS_ERROR, &Erro
Status Array_Create(Array *inst, int len, size_t size); Status Array_Create(Array *inst, int len, size_t size);
Status Array_CopyOf(Array *inst, Array *other); Status Array_CopyOf(Array *inst, Array *other);
void Array_Delete(Array *inst); void Array_Delete(Array *inst);
Status Array_GetIdx(Array *inst, Var *store, int index); // Status Array_GetIdx(Array *inst, Var *store, int index);
Status Array_SetIdx(Array *inst, Var *source, int index); // Status Array_SetIdx(Array *inst, Var *source, int index);
bool Array_Equals(Array *arr1, Array *arr2); bool Array_Equals(Array *arr1, Array *arr2);
Status ArrayUtils_Insert(Array *inst, Var *item, int index); Status ArrayUtils_Insert(Array *inst, Var *item, int index);

View File

@@ -91,7 +91,7 @@ Status Array_CopyOf(Array *inst, Array *other)
/* Release the array inst. */ /* Release the array inst. */
free(inst->members); free(inst->members);
return errstat; return apply(errstat);
} }
/* Assign rest of the struct members. */ /* Assign rest of the struct members. */
@@ -115,9 +115,37 @@ void Array_Delete(Array *inst)
inst->len = 0; inst->len = 0;
} }
Status Array_GetIdx(Array *inst, Var *store, int index); // Status Array_GetIdx(Array *inst, Var *store, int index)
// {
// nonull(inst, apply(UnavailableInstance));
// nonull(store, apply(annot(UnavailableParameter,
// "Given store was unavailable.")));
// state(!store->alive, apply(annot(InstanceNotAlive,
// "Cannot store into a non-alive var.")));
// state(index >= inst->len || index < 0, apply(ArrayIndexOutOfBound));
// // *store = inst->members[index];
// store->addr = inst->members[index].addr;
// store->size = inst->members[index].size;
// return apply(NormalStatus);
// }
Status Array_SetIdx(Array *inst, Var *source, int index); // Status Array_SetIdx(Array *inst, Var *source, int index)
// {
// nonull(inst, apply(UnavailableInstance));
// nonull(source, apply(annot(UnavailableParameter,
// "Given store was unavailable.")));
// state(!source->alive, apply(annot(InstanceNotAlive,
// "Cannot assign with a non-alive var.")));
// state(index >= inst->len || index < 0, apply(ArrayIndexOutOfBound));
// // inst->members[index] = *source;
// inst->members[index].addr = source->addr;
// inst->members[index].size = source->size;
// return apply(NormalStatus);
// }
bool Array_Equals(Array *arr1, Array *arr2); bool Array_Equals(Array *arr1, Array *arr2);

View File

@@ -165,14 +165,6 @@ DEFSTATUS(UnavailableObject, 1,
"An unavailable object was presented.", "An unavailable object was presented.",
STATUS_ERROR, &ErrorStatus); STATUS_ERROR, &ErrorStatus);
DEFSTATUS(InstanceStillAlive, 1,
"Given instance was yet alive.",
STATUS_ERROR, &ErrorStatus);
DEFSTATUS(InstanceNotAlive, 1,
"Given instance for reallocation was not alive.",
STATUS_ERROR, &ErrorStatus);
DEFSTATUS(InvalidParameter, 1, DEFSTATUS(InvalidParameter, 1,
"An invalid parameter was presented.", "An invalid parameter was presented.",
STATUS_ERROR, &InvalidObject); STATUS_ERROR, &InvalidObject);
@@ -189,10 +181,26 @@ DEFSTATUS(IntegerOverFlow, 1,
"An integer had overflowed.", "An integer had overflowed.",
STATUS_ERROR, &ArithmeticError); STATUS_ERROR, &ArithmeticError);
DEFSTATUS(InvalidOperation, 1,
"An invalid operation was detected.",
STATUS_ERROR, &ErrorStatus);
DEFSTATUS(RuntimeError, 1, DEFSTATUS(RuntimeError, 1,
"A runtime error occurred.", "A runtime error occurred.",
STATUS_ERROR, &ErrorStatus); STATUS_ERROR, &ErrorStatus);
DEFSTATUS(InstanceStillAlive, 1,
"Given instance was yet alive.",
STATUS_ERROR, &RuntimeError);
DEFSTATUS(InstanceNotAlive, 1,
"Given instance for reallocation was not alive.",
STATUS_ERROR, &RuntimeError);
DEFSTATUS(InvalidOperationBetweenAliveAndNonAlive, 1,
"Given two instances were incompatible with each other for any operation.",
STATUS_ERROR, &InvalidOperation);
DEFSTATUS(InstanceCreatingFailure, 1, DEFSTATUS(InstanceCreatingFailure, 1,
"Cannot create the instance.", "Cannot create the instance.",
STATUS_ERROR, &RuntimeError); STATUS_ERROR, &RuntimeError);

View File

@@ -6,6 +6,13 @@
# define EMPTY {0} # define EMPTY {0}
/*
PLEASE BE NOTICE:
MAGIC MACRO CANNOT USE "APPLY" AT RETURNING.
IT SHOULD ALWAYS RETURN THE LOCATION INDICATOR WHERE
IT CAME FROM.
*/
/* Get the literal. */ /* Get the literal. */
# define nameof(obj) #obj # define nameof(obj) #obj
@@ -37,10 +44,10 @@
# define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b } # define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b }
/* Return e when passing a failing e commented with c. */ /* Return e when passing a failing e commented with c. */
# define fails(e, c) { notok(e, return apply(annot(_, c));) } # define fails(e, c) { notok(e, return annot(_, c);) }
/* Return e when passing a failing e. */ /* Return e when passing a failing e. */
# define fail(e) { notok(e, return apply(_);) } # define fail(e) { notok(e, return _;) }
/* Return v when passing a failing e. */ /* Return v when passing a failing e. */
# define vfail(e, v) { notok(e, return v;) } # define vfail(e, v) { notok(e, return v;) }
@@ -64,6 +71,10 @@
/* Cast Var "var" into builtin type in C specified with "type". */ /* Cast Var "var" into builtin type in C specified with "type". */
# define cast(var, type) (*(type *)var.addr) # define cast(var, type) (*(type *)var.addr)
/* Assign var with value under type "type".
REQUIRE BOTH VAR AND VALUE TO BE ALIVE. */
# define assign(var, value, type) { cast(var, type) = cast(value, type); }
// # define lambda(param, body, capfmt, ...) {\ // # define lambda(param, body, capfmt, ...) {\
// /* Duplicate everything from cap. */\ // /* Duplicate everything from cap. */\
// va_list ptr;\ // va_list ptr;\

47
test.c
View File

@@ -1,3 +1,4 @@
#include "Status/include/status.h"
#include <Compound/array.h> #include <Compound/array.h>
#include <Compound/catlog.h> #include <Compound/catlog.h>
#include <Compound/common.h> #include <Compound/common.h>
@@ -20,6 +21,52 @@ __attribute__((destructor))
void __DESTRUCT__() {} void __DESTRUCT__() {}
Status Main(void) Status Main(void)
{
Array arr;
fail(Array_Create(&arr, 8, sizeof(int)));
for (register int i = 0; i < arr.len; i++) {
Var current;
// fails(Var_Create(&current, arr.members[0].size),
// "Failed to create Var current.");
state(!current.alive || !arr.members[i].alive,
apply(InvalidOperationBetweenAliveAndNonAlive));
assign(current, arr.members[i], int);
char buff[LITERALISATION_LENGTH_MAXIMUM] = EMPTY;
unsure(Var_Literalise(&current, buff), !_.value, {
return annot(_, "Failed to literalise Var current.");
})
(void)printf("%s\n", buff);
// Var_Delete(&current);
}
cat("Get:");
Var get = EMPTY;
fails(Var_Create(&get, arr.members[4].size), "Failed to create Var \"get\".");
state(!get.alive || !arr.members[4].alive,
apply(InvalidOperationBetweenAliveAndNonAlive));
assign(get, arr.members[4], int);
char buff[LITERALISATION_LENGTH_MAXIMUM] = EMPTY;
unsure(Var_Literalise(&get, buff), !_.value, {
return annot(_, "Failed to literalise Var current.");
})
Var_Delete(&get);
// Array_Delete(&arr);
return apply(NormalStatus);
}
Status MainArrayCreateAndDeleteWithTraditionalMemberAccessing(void)
{ {
// const int len = 8; // const int len = 8;