Skip to content

Commit

Permalink
Lib function: popIdx. Works on lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardicus committed Dec 20, 2023
1 parent 335e251 commit 5bf7a8f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 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
1 change: 1 addition & 0 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libFunction_t ric_library[] = {
DECLARE_LIB_FUNCTION("push", 2, ric_push),
DECLARE_LIB_FUNCTION("pop", 1, ric_pop),
DECLARE_LIB_FUNCTION("popFirst", 1, ric_pop_first),
DECLARE_LIB_FUNCTION("popIdx", 2, ric_pop_idx),
DECLARE_LIB_FUNCTION("len", 1, ric_len),
DECLARE_LIB_FUNCTION("contains", 2, ric_contains),
DECLARE_LIB_FUNCTION("keys", 1, ric_keys),
Expand Down
82 changes: 81 additions & 1 deletion src/library/libstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,86 @@ int ric_pop(LIBRARY_PARAMS()) {
return 0;
}

int ric_pop_idx(LIBRARY_PARAMS()) {
stackval_t stv;
vector_t *vec = NULL;
argsList_t *walk;
argsList_t *prev;
int idx = 0;
void *sp = PROVIDE_CONTEXT()->sp;
size_t *sc = PROVIDE_CONTEXT()->sc;
void *hp = PROVIDE_CONTEXT()->hp;
heapval_t *hpv = NULL;
int dummy;
int walk_count = 0;

/* Get vector reference */
POP_VAL(&stv, sp, sc);

switch (stv.type) {
case VECTORTYPE:
vec = stv.vec;
break;
default: {
fprintf(stderr, "error: function call '%s' got an unexpected first argument.\n",
LIBRARY_FUNC_NAME());
exit(1);
} break;
}

/* Get idx */
POP_VAL(&stv, sp, sc);

switch (stv.type) {
case INT32TYPE:
idx = stv.i;
break;
default: {
fprintf(stderr, "error: function call '%s' got an unexpected first argument.\n",
LIBRARY_FUNC_NAME());
exit(1);
} break;
}

if (vec->length == 0 || idx >= vec->length) {
/* This is not very good.. Guess I will return 0 then. */
PUSH_INT(0, sp, sc);
return 0;
}

if (idx < 0) {
/* Negative index, wrap around */
idx = (vec->length + idx % vec->length);
}

walk = vec->content;
prev = NULL;
while (walk->next != NULL) {
if (walk_count == idx) {
break;
}

prev = walk;
walk = walk->next;
walk_count++;
}

if (prev == NULL) {
vec->content = walk->next;
} else {
prev->next = walk->next;
}
evaluate_expression(walk->arg, EXPRESSION_ARGS());
POP_VAL(&stv, sp, sc);

ALLOC_HEAP(&stv, hp, &hpv, &dummy);
push_stackval(&stv, PROVIDE_CONTEXT());

// Decrease vector size
vec->length--;
return 0;
}

int ric_pop_first(LIBRARY_PARAMS()) {
stackval_t stv;
vector_t *vec = NULL;
Expand Down Expand Up @@ -1345,7 +1425,7 @@ int ric_sort(LIBRARY_PARAMS()) {

vecContent = vec->content;

if (vecContent->length > 0) {
if (vec->length > 0 && vecContent->length > 0) {

if (vecContent->arg->type == EXPR_TYPE_IVAL) {

Expand Down
1 change: 1 addition & 0 deletions src/library/libstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ int ric_append(LIBRARY_PARAMS());
int ric_push(LIBRARY_PARAMS());
int ric_pop(LIBRARY_PARAMS());
int ric_pop_first(LIBRARY_PARAMS());
int ric_pop_idx(LIBRARY_PARAMS());
int ric_len(LIBRARY_PARAMS());
int ric_contains(LIBRARY_PARAMS());
int ric_keys(LIBRARY_PARAMS());
Expand Down

0 comments on commit 5bf7a8f

Please sign in to comment.