// executes (loops over) a Lambda expression: tries to run to termination,
// or returns after having emitted further remote lookup/reduction requests
// the return val is a buffer with the result stack's (concatenated) content
struct ccnl_buf_s*
Krivine_reduction(struct ccnl_relay_s *ccnl, char *expression,
                  int start_locally,
                  struct configuration_s **config,
                  struct ccnl_prefix_s *prefix, int suite)
{
    int steps = 0, halt = 0, restart = 1;
    int len = strlen("CLOSURE(HALT);RESOLVENAME()") + strlen(expression) + 1;
    char *dummybuf;

    DEBUGMSG(TRACE, "Krivine_reduction()\n");

    if (!*config && strlen(expression) == 0)
        return 0;
    dummybuf = ccnl_malloc(2000);
    if (!*config) {
        char *prog;
        struct environment_s *global_dict = NULL;

        prog = ccnl_malloc(len*sizeof(char));
        sprintf(prog, "CLOSURE(HALT);RESOLVENAME(%s)", expression);
        setup_global_environment(&global_dict);
        DEBUGMSG(DEBUG, "PREFIX %s\n", ccnl_prefix_to_path(prefix));
        *config = new_config(ccnl, prog, global_dict,
                             start_locally,
                             prefix, ccnl->km->configid, suite);
        DBL_LINKED_LIST_ADD(ccnl->km->configuration_list, (*config));
        restart = 0;
        --ccnl->km->configid;
    }
    DEBUGMSG(INFO, "Prog: %s\n", (*config)->prog);

    while ((*config)->prog && !halt) {
        char *oldprog = (*config)->prog;
        steps++;
        DEBUGMSG(DEBUG, "Step %d (%d/%d): %s\n", steps,
                 stack_len((*config)->argument_stack),
                 stack_len((*config)->result_stack), (*config)->prog);
        (*config)->prog = ZAM_term(ccnl, *config, &halt, dummybuf, &restart);
        ccnl_free(oldprog);
    }
    ccnl_free(dummybuf);

    if (halt < 0) { //HALT < 0 means pause computation
        DEBUGMSG(INFO,"Pause computation: %d\n", -(*config)->configid);
        return NULL;
    }

    //HALT > 0 means computation finished
    DEBUGMSG(INFO, "end-of-computation (%d/%d)\n",
             stack_len((*config)->argument_stack),
             stack_len((*config)->result_stack));
/*
    print_argument_stack((*config)->argument_stack);
    print_result_stack((*config)->result_stack);
*/

    return Krivine_exportResultStack(ccnl, *config);
}
Example #2
0
int stack_top(stack_t *s, void *value)
{
        if (stack_len(s) > 0) {
                memcpy(value, s->data + s->ele_size * s->top, s->ele_size);
                return 0;
        }
        return -1;
}
Example #3
0
File: op.c Project: BoscoTin/AS
void exec_op_code(Stack* s, int op, int n)
{
	int output = 0;
	int a, b;
	switch (op)
	{
		case OPCODE_LEN:
			output = (int)stack_len(s);
			break;
		case OPCODE_TOP:
			output = stack_top(s);
			break;
		case OPCODE_POP:
			output = stack_pop(s);
			break;
		case OPCODE_PUSH:
			output = stack_push(s, n);
			break;
		case OPCODE_ADD:
			a = stack_pop(s);
			b = stack_pop(s);
			output = a + b;
			stack_push(s, output);
			break;
		case OPCODE_SUB:
			a = stack_pop(s);
			b = stack_pop(s);
			output = a - b;
			stack_push(s, output);
			break;
		case OPCODE_MUL:
			a = stack_pop(s);
			b = stack_pop(s);
			output = a * b;
			stack_push(s, output);
			break;
		case OPCODE_DIV:
			a = stack_pop(s);
			b = stack_pop(s);
			output = a / b;
			stack_push(s, output);
			break;
		case OPCODE_MOD:
			a = stack_pop(s);
			b = stack_pop(s);
			output = a % b;
			stack_push(s, output);
			break;
		case OPCODE_EXIT:
			exit(1);
			break;
		default:
			puts("Unknown Command");
	}
	printf("%d\n", output);
}
Example #4
0
int stack_pop(stack_t *s, void *value)
{
        /*  Value that is popped from the stack is placed in value parameter,
         *  @param s: Pointer to stack_t type variable
         *  @param x: Pointer to a variable to store the value popped from the stack
         *  Returns:  Zero if stack is not empty when stack_pop() is called,
         *              -1 Otherwise
         */
        if (stack_len(s) > 0) {
                memcpy(value, s->data + s->ele_size * s->top, s->ele_size);
                s->top--;
                return 0;
        } else {
                return -1;
        }
}
Example #5
0
int stack_push(stack_t *s, void *x)
{
        /*  Pushes an element on to the stack
         *  @param s: Pointer to stack_t type variable
         *  @param x: Value to Push on to the stack
         *  Returns : Zero if stack is not full when stack_push() is called,
         *              -1 Otherwise
         */
        if (stack_len(s) < s->capacity) {
                s->top++;
                memcpy(s->data + s->ele_size * s->top, x, s->ele_size);
                return 0;
        } else {
                return -1;
        }
}
Example #6
0
void sortLinkedList_Nums(Node *head) {
	Node *temp;
	int i;
	/* since the compare dereferences temp->next you'll have to verify that it is not NULL */
	for (i = 0; i < stack_len(head); i++) {
		for (temp = head; temp && temp->next; temp = temp->next) {
			int32_t* blah = (int32_t *) temp->data;
			int32_t * blahNext = (int32_t *) temp->next->data;
			if (*blah > *blahNext) {
				/* no need for a whole node, since you only copy a pointer */
				int32_t *cp;
				cp = temp->data;
				temp->data = temp->next->data;
				temp->next->data = cp;
			}

		}
	}

}
Example #7
0
void sortLinkedList_Names(Node *head) {
	Node *temp;
	int i;
	/* since the compare dereferences temp->next you'll have to verify that it is not NULL */
	for (i = 0; i < stack_len(head); i++) {
		for (temp = head; temp && temp->next; temp = temp->next) {
			person* blah = (person *) temp->data;
			person * blahNext = (person *) temp->next->data;
			if (strcmp(blah->name, blahNext->name) > 0) {
				/* no need for a whole node, since you only copy a pointer */
				person *cp;
				cp = temp->data;
				temp->data = temp->next->data;
				temp->next->data = cp;
			}

		}
	}

}
Example #8
0
File: daemon.c Project: qnaal/fls
static bool daemon_serve(int s, char *cmd) {
  /* Do <cmd> for client connected on <s>. */
  static Node *null=NULL, **stack=&null;
  char buf[FILEPATH_MAX];
  bool keep_running=true;

  if( strcmp(cmd, CMD_PUSH) == 0 ) {
    char *status;
    if( stack_len(stack) >= STACK_MAX ) {
      printf("daemon: push request failed (stack full)\n");
      status = MSG_ERROR;
      strcpy(buf, MSG_ERR_STACK_FULL);
    } else {
      soc_w(s, MSG_SUCCESS);
      if( soc_r(s, buf, FILEPATH_MAX) <= 0 ) {
	printf("daemon: push request failed (read error)\n");
	status = MSG_ERROR;
	strcpy(buf, MSG_ERR_LENGTH);
      } else {
	status = MSG_SUCCESS;
	stack_push(buf, stack);
	printf("daemon: PUSH `%s'\n", buf);
      }
    }
    soc_w(s, status);
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_POP) == 0 ) {
    char *status;
    if( stack_len(stack) > 0 ) {
      status = MSG_SUCCESS;
      sprintf(buf, "%s", stack_peek(stack));
      stack_drop(stack);
      printf("daemon: POP `%s'\n", buf);
    } else {
      printf("daemon: tried to pop from empty stack\n");
      status = MSG_ERROR;
      sprintf(buf, MSG_ERR_STACK_EMPTY);
    }
    soc_w(s, status);
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_PEEK) == 0 ) {
    char *status;
    if( stack_len(stack) > 0 ) {
      status = MSG_SUCCESS;
      sprintf(buf, "%s", stack_peek(stack));
    } else {
      status = MSG_ERROR;
      sprintf(buf, MSG_ERR_STACK_EMPTY);
    }
    soc_w(s, status);
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_PICK) == 0 ) {
    char *picked;
    soc_w(s, MSG_SUCCESS);
    soc_r(s, buf, MSG_MAX);
    picked = stack_nth(atoi(buf), stack);
    if( picked == NULL ) {
      soc_w(s, MSG_ERROR);
      soc_w(s, "stack is not quite that deep");
    } else {
      soc_w(s, MSG_SUCCESS);
      soc_w(s, picked);
    }

  } else if( strcmp(cmd, CMD_SIZE) == 0 ) {
    sprintf(buf, "%d", stack_len(stack));
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_STOP) == 0 ) {
    printf("daemon: Shutting down...\n");
    soc_w(s, MSG_SUCCESS);
    keep_running = false;

  } else {
    char msg[MSG_MAX + FILEPATH_MAX];
    sprintf(msg, "unknown command `%s'", cmd);
    soc_w(s, msg);
  }

  return keep_running;
}