Skip to content

Commit

Permalink
making it possible to check if vector are equal
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardicus committed Dec 17, 2023
1 parent f1afde4 commit 335e251
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ typedef struct libFunction {
intptr_t p; \
*sb = calloc(sz + 1, sizeof(stackval_t)); \
assert(*sb != NULL); \
p = ((intptr_t)*sb) % sizeof(stackval_t); \
p = ((intptr_t) * sb) % sizeof(stackval_t); \
if (p != 0) { \
p = (sizeof(stackval_t) - (p % sizeof(stackval_t))); \
} \
Expand All @@ -595,7 +595,7 @@ typedef struct libFunction {
heapval_t hpbv; \
*hb = calloc(hz + 2, sizeof(heapval_t)); \
assert(*hb != NULL); \
p = ((intptr_t)*hb) % sizeof(heapval_t); \
p = ((intptr_t) * hb) % sizeof(heapval_t); \
p = (sizeof(heapval_t) - (p % sizeof(heapval_t))); \
hpbv.sv.type = INT32TYPE; \
hpbv.sv.i = (int32_t)hz; \
Expand Down
39 changes: 39 additions & 0 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ expr_t *stackval_to_expression(stackval_t *sv, int alloc, EXPRESSION_PARAMS()) {
return newExp;
}

int evaluate_equal(expr_t *a, expr_t *b, EXPRESSION_PARAMS()) {
int result = 0;
ifCondition_t ifCond;

ifCond.type = CONDITION_EQ;
ifCond.left = a;
ifCond.right = b;

evaluate_condition(&ifCond, EXPRESSION_ARGS());

result = *PROVIDE_CONTEXT()->ax;

return result;
}

int evaluate_condition(ifCondition_t *cond, EXPRESSION_PARAMS()) {
void *sp = PROVIDE_CONTEXT()->sp;
size_t *sc = PROVIDE_CONTEXT()->sc;
Expand Down Expand Up @@ -443,6 +458,30 @@ int evaluate_condition(ifCondition_t *cond, EXPRESSION_PARAMS()) {
} else {
*ax = 0;
}
} else if (svLeft.type == VECTORTYPE && svRight.type == VECTORTYPE) {
argsList_t *walkA = svLeft.vec->content;
argsList_t *walkB = svRight.vec->content;
int res = svLeft.vec->length == svRight.vec->length;
while (res && walkA != NULL && walkB != NULL) {
ifCondition_t ifCond;
int tmpax = *PROVIDE_CONTEXT()->ax;
ifCond.type = CONDITION_EQ;

ifCond.left = walkA->arg;
ifCond.right = walkB->arg;

evaluate_condition(&ifCond, EXPRESSION_ARGS());
if (*PROVIDE_CONTEXT()->ax == 0) {
res = 0;
break;
}

*PROVIDE_CONTEXT()->ax = tmpax;

walkA = walkA->next;
walkB = walkB->next;
}
*ax = res;
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int evaluate_id_valid(char *id, void *stmt, void *next, PROVIDE_CONTEXT_ARGS(),
hashtable_t *argVals);
int evaluate_indexer(indexer_t *indexer, int max, int *idxStart, int *idxEnd, int *offset,
EXPRESSION_PARAMS());

int evaluate_equal(expr_t *a, expr_t *b, EXPRESSION_PARAMS());
void free_vector(vector_t *vec);

expr_t *copy_vector(vector_t *vec, int alloc, EXPRESSION_PARAMS());
Expand Down
22 changes: 21 additions & 1 deletion src/library/libstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ int ric_contains(LIBRARY_PARAMS()) {
char *argText = NULL;
cachepot_t *cachepot = NULL;
char *containText = NULL;
vector_t *containVec = NULL;
int32_t containInt = 0;
int32_t result = 0;
int searchForInt = 0; // Lazy, search for int or text.
Expand Down Expand Up @@ -945,6 +946,10 @@ int ric_contains(LIBRARY_PARAMS()) {
containInt = stv.i;
searchForInt = 1;
break;
case VECTORTYPE:
searchForInt = 0;
containVec = stv.vec;
break;
default: {
fprintf(stderr, "error: function '%s' expected text or integer as first argument.\n",
LIBRARY_FUNC_NAME());
Expand All @@ -966,12 +971,27 @@ int ric_contains(LIBRARY_PARAMS()) {
result = 1;
break;
}
} else {
} else if (containText != NULL) {
/* Check if is text */
if (stv.type == TEXT && strcmp(stv.t, containText) == 0) {
result = 1;
break;
}
} else if (containVec != NULL) {
if (stv.type == VECTORTYPE) {
expr_t a, b;

a.type = EXPR_TYPE_VECTOR;
b.type = EXPR_TYPE_VECTOR;

a.vec = stv.vec;
b.vec = containVec;

if (evaluate_equal(&a, &b, EXPRESSION_ARGS())) {
result = 1;
break;
}
}
}
walk = walk->next;
}
Expand Down

0 comments on commit 335e251

Please sign in to comment.