From 335e2519c7fa6677dc45ebd2230385104f201a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rickard=20Hallerb=C3=A4ck?= Date: Sun, 17 Dec 2023 22:54:53 +0100 Subject: [PATCH] making it possible to check if vector are equal --- src/ast.h | 4 ++-- src/eval.c | 39 +++++++++++++++++++++++++++++++++++++++ src/eval.h | 2 +- src/library/libstd.c | 22 +++++++++++++++++++++- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/ast.h b/src/ast.h index 1b5f1ae..241d916 100644 --- a/src/ast.h +++ b/src/ast.h @@ -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))); \ } \ @@ -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; \ diff --git a/src/eval.c b/src/eval.c index 923551d..fd30e23 100644 --- a/src/eval.c +++ b/src/eval.c @@ -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; @@ -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; } diff --git a/src/eval.h b/src/eval.h index 34885ba..799dad9 100644 --- a/src/eval.h +++ b/src/eval.h @@ -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()); diff --git a/src/library/libstd.c b/src/library/libstd.c index ee7f2a7..64e7b79 100644 --- a/src/library/libstd.c +++ b/src/library/libstd.c @@ -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. @@ -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()); @@ -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; }