diff --git a/Array/include/array.h b/Array/include/array.h index 422d1ba..af2da29 100644 --- a/Array/include/array.h +++ b/Array/include/array.h @@ -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_CopyOf(Array *inst, Array *other); void Array_Delete(Array *inst); -Status Array_GetIdx(Array *inst, Var *store, int index); -Status Array_SetIdx(Array *inst, Var *source, int index); +// Status Array_GetIdx(Array *inst, Var *store, int index); +// Status Array_SetIdx(Array *inst, Var *source, int index); bool Array_Equals(Array *arr1, Array *arr2); Status ArrayUtils_Insert(Array *inst, Var *item, int index); diff --git a/Array/src/array.c b/Array/src/array.c index cbdd137..648e253 100644 --- a/Array/src/array.c +++ b/Array/src/array.c @@ -91,7 +91,7 @@ Status Array_CopyOf(Array *inst, Array *other) /* Release the array inst. */ free(inst->members); - return errstat; + return apply(errstat); } /* Assign rest of the struct members. */ @@ -115,9 +115,37 @@ void Array_Delete(Array *inst) 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); diff --git a/Status/include/status.h b/Status/include/status.h index bac29be..b1b1b10 100644 --- a/Status/include/status.h +++ b/Status/include/status.h @@ -165,14 +165,6 @@ DEFSTATUS(UnavailableObject, 1, "An unavailable object was presented.", 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, "An invalid parameter was presented.", STATUS_ERROR, &InvalidObject); @@ -189,10 +181,26 @@ DEFSTATUS(IntegerOverFlow, 1, "An integer had overflowed.", STATUS_ERROR, &ArithmeticError); +DEFSTATUS(InvalidOperation, 1, + "An invalid operation was detected.", + STATUS_ERROR, &ErrorStatus); + DEFSTATUS(RuntimeError, 1, "A runtime error occurred.", 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, "Cannot create the instance.", STATUS_ERROR, &RuntimeError); diff --git a/common.h b/common.h index 8bd2f6a..4f06b92 100644 --- a/common.h +++ b/common.h @@ -6,6 +6,13 @@ # 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. */ # define nameof(obj) #obj @@ -37,10 +44,10 @@ # define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b } /* 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. */ -# define fail(e) { notok(e, return apply(_);) } +# define fail(e) { notok(e, return _;) } /* Return v when passing a failing e. */ # define vfail(e, v) { notok(e, return v;) } @@ -64,6 +71,10 @@ /* Cast Var "var" into builtin type in C specified with "type". */ # 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, ...) {\ // /* Duplicate everything from cap. */\ // va_list ptr;\ diff --git a/test.c b/test.c index 20e03b4..988f36d 100644 --- a/test.c +++ b/test.c @@ -1,3 +1,4 @@ +#include "Status/include/status.h" #include #include #include @@ -20,6 +21,52 @@ __attribute__((destructor)) void __DESTRUCT__() {} 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(¤t, 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(¤t, buff), !_.value, { + return annot(_, "Failed to literalise Var current."); + }) + + (void)printf("%s\n", buff); + + // Var_Delete(¤t); + } + + 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;