Пример #1
0
struct eval *
eval_new(symlook_fn symlook, void *context)
{
	struct eval *eval;
	struct tok *em;

	if ((eval = malloc(sizeof *eval)) == NULL) {
		PMNO(errno);
		return NULL;
	}

	memset(eval, 0, sizeof *eval);
	if ((eval->toks = varray_new(sizeof(struct tok), NULL)) == NULL ||
			(eval->opstk = stack_new(4096, NULL)) == NULL ||
			(eval->stk = stack_new(4096, NULL)) == NULL ||
			(em = varray_get(eval->toks, eval->toki++)) == NULL) {
		AMSG("");
		eval_del(eval);
		return NULL;
	}
	eval->context = context;
	eval->symlook = symlook;
	em->type = TOK_TYPE_EMPTY;
	stack_push(eval->opstk, em);

	return eval;
}
Пример #2
0
Файл: Stack.c Проект: qyh/studio
int main(void)
{
    int value = 10, v = 11, i;
    Student s1 = {12, "student1", "addr1"},s2 = {13, "student2","a1"}, 
            s3 = {15, "stu3", "address3"}, *sptr;
    pStack ps = stack_new(sizeof(Student) + 20);
    pStack p = stack_new(sizeof(int));

    stack_push(p, &value);
    stack_push(p, &v);
    printf("%d\n", *((int*)stack_top(p)));
    stack_pop(p);
    printf("%d\n", *((int*)stack_top(p)));
    

    stack_push(ps, &s1);
    stack_push(ps, &s2);
    sptr = (pStudent)stack_top(ps);
    printf("no: %d, name: %s\n", sptr->no, sptr->addr);
    stack_pop(ps);
    stack_push(ps, &s3);
    sptr = (pStudent)stack_top(ps);
    printf("no: %d, name: %s\n", sptr->no, sptr->addr);
    stack_free(ps);

    for (i = 0; i < 100; i++) {
        stack_push(p, &i);
    }
    for (i = 0; i < 100; i++) {
        printf("%d ", *((int*)stack_top(p)));
        stack_pop(p);
    }
    stack_free(p);
    exit(EXIT_SUCCESS);
}
Пример #3
0
struct stack * type_constrain_ari(struct elt *e1, struct elt *e2)
{
	struct stack *types, *tmp1, *tmp2;

	// these will modify the type of e1 and e2 to match arithmetic operation.
	// if possible.
	if (type_vartype_constrain_ari(e1) == 0) { return NULL; }
	if (type_vartype_constrain_ari(e2) == 0) { return NULL; }

	if (e1->elttype == E_REG) {
		tmp1 = e1->reg->types;
	} else {
		tmp1 = stack_new();
		stack_push(tmp1, &possible_types[(int)e1->cst->type]);
	}

	if (e2->elttype == E_REG) {
		tmp2 = e2->reg->types;
	} else {
		tmp2 = stack_new();
		stack_push(tmp2, &possible_types[(int)e2->cst->type]);
	}

	types = type_inter(tmp1, tmp2);
	if (stack_size(types) == 0) { // float + int
		stack_push(types, &possible_types[FLO_T]);
	}

	if (e1->elttype == E_CST) { stack_free(&tmp1, NULL); }
	if (e2->elttype == E_CST) { stack_free(&tmp2, NULL); }

	return types;
}
Пример #4
0
void test_create_destroy_works(void)
{
	names_stack *stack;

	stack = stack_new();
	stack_destroy(stack);

	stack = stack_new();
	stack_enter(stack);
	stack_name_add(stack, "baz");
	stack_name_add(stack, "foo");
	stack_destroy(stack);

	stack = stack_new();
	stack_enter(stack);
	stack_enter(stack);
	stack_enter(stack);
	stack_name_add(stack, "baz");
	stack_name_add(stack, "foo");
	stack_enter(stack);
	stack_name_add(stack, "foo");
	stack_name_add(stack, "baz");
	stack_enter(stack);
	stack_destroy(stack);
}
Пример #5
0
struct context *context_new(bool state)
{
    struct context *context = (struct context*)malloc(sizeof(struct context));
    null_check(context);
    context->program_stack = stack_new();
    if (state)
        stack_push(context->program_stack, program_state_new(context, NULL));
    context->operand_stack = stack_new();
    context->vm_exception = NULL;
    context->runtime = true;
    context->num_vars = 0;
    context->indent = 0;

    return context;
}
Пример #6
0
struct bt_btr *bt_btr_create(struct bt_btr_cbs cbs, void *data)
{
	struct bt_btr *btr;

	BT_LOGD_STR("Creating binary type reader (BTR).");
	btr = g_new0(struct bt_btr, 1);
	if (!btr) {
		BT_LOGE_STR("Failed to allocate one binary type reader.");
		goto end;
	}

	btr->stack = stack_new();
	if (!btr->stack) {
		BT_LOGE_STR("Cannot create BTR's stack.");
		bt_btr_destroy(btr);
		btr = NULL;
		goto end;
	}

	btr->state = BTR_STATE_NEXT_FIELD;
	btr->user.cbs = cbs;
	btr->user.data = data;
	BT_LOGD("Created BTR: addr=%p", btr);

end:
	return btr;
}
Пример #7
0
void test_capacity_one_stack()
{
    Stack stk_instance = stack_new(1);
    Stack *stk = &stk_instance;

    StackResult result;

    assert(stack_empty(stk));
    assert(!stack_full(stk));

    stack_peek(stk, &result);
    assert(result.status == STACK_EMPTY);
    stack_pop(stk, &result);
    assert(result.status == STACK_EMPTY);

    stack_push(stk, 99, &result);
    assert(result.status == STACK_OK);
    assert(stack_full(stk));

    stack_push(stk, 222, &result);
    assert(result.status == STACK_FULL);
    stack_peek(stk, &result);
    assert(result.data == 99 && result.status == STACK_OK);

    stack_pop(stk, &result);
    assert(result.data == 99 && result.status == STACK_OK);
    assert(stack_empty(stk));
}
Пример #8
0
int test_stack_push(void)
{
    struct stack *s = stack_new();
    float *f1 = malloc_float(2.0);
    float *f2 = malloc_float(4.0);
    float *f3 = malloc_float(8.0);

    /* push float 1 */
    stack_push(s, f1);
    mu_check(fltcmp(s->end->value, f1) == 0);
    mu_check(s->size == 1);
    mu_check(s->root->value == f1);
    mu_check(s->end->prev == NULL);

    /* push float 2 */
    stack_push(s, f2);
    mu_check(fltcmp(s->end->value, f2) == 0);
    mu_check(s->size == 2);
    mu_check(s->root->value == f1);
    mu_check(s->end->prev->value == f1);
    mu_check(fltcmp(s->end->prev->value, f1) == 0);

    /* push float 3 */
    stack_push(s, f3);
    mu_check(fltcmp(s->end->value, f3) == 0);
    mu_check(s->size == 3);
    mu_check(s->root->value == f1);
    mu_check(s->end->prev->value == f2);
    mu_check(fltcmp(s->end->prev->value, f2) == 0);

    stack_destroy(s, free);
    return 0;
}
Пример #9
0
void test_stack_push()
{
    Stack *s = stack_new();

    int a = 1;
    int b = 2;
    int c = 3;

    stack_push(s, &a);

    cc_assert(stack_peek(s) == &a,
              cc_msg("stack_push: Top stack element not as expected"));

    stack_push(s, &b);

    cc_assert(stack_peek(s) == &b,
              cc_msg("stack_push: Top stack element not as expected"));

    stack_push(s, &c);

    cc_assert(stack_peek(s) == &c,
              cc_msg("stack_push: Top stack element not as expected"));

    stack_destroy(s);

}
Пример #10
0
int * inorderTraversal(struct TreeNode *root, int *returnSize)
{
    int idx, *list = (int *)malloc(sizeof(int) * 256); /* ~~, implement a list? */
    struct TreeNode *node;
    Stack *stack = stack_new(sizeof(struct TreeNode *));

    idx = 0;
    node = root;
    while (node != NULL) {
        stack_push(stack, &node);
        node = node->left;
    }
    while (!stack_is_empty(stack)) {
        stack_pop(stack, &node);
        list[idx++] = node->val;
        node = node->right;
        while (node != NULL) {
            stack_push(stack, &node);
            node = node->left;
        }
    }
    stack_free(stack);

    *returnSize = idx;
    return list;
}
Пример #11
0
static int type_vartype_constrain_ari(struct elt *e)
{
	int ret = 0;
	struct stack *tmp, *inter;

	// TODO: Init this only once at the begining
	tmp = stack_new();
	stack_push(tmp, &possible_types[FLO_T]);
	stack_push(tmp, &possible_types[INT_T]);

	if (e->elttype == E_REG) {
		inter = type_inter(tmp, e->reg->types);
		ret = stack_size(inter);

		if (ret != 0) {
			// replace old stack with the new one
			reg_settypes(e->reg, inter);
		}

		stack_free(&inter, NULL);
	} 
	
	else {
		if (e->cst->type != FLO_T && e->cst->type != INT_T) {
			ret = 0;
		} else {
			ret = 1;
		}
	}

	stack_free(&tmp, NULL);

	return ret;
}
Пример #12
0
int
main() {
  int retval;
  char cont;

  struct stack *stack;
  stack = stack_new(STACK_SIZE);

  cont = 1;
  while(cont) {
    retval = interact(stack);
    switch (retval) {
    case EXIT_OP:
      cont = 0;
      break;
    case INVALID_CHAR:
      printf("Invalid char!\n");
      break;
    case STACK_UNDERFLOW:
      printf("Stack underflow!\n");
      break;
    case STACK_OVERFLOW:
      printf("Stack overflow!\n");
      break;
    default:
      continue;
    }
  }

  stack_free(stack);

  return 0;
}
Пример #13
0
void TestStackCreation(CuTest* tc) 
{
	struct Stack* stack = stack_new(16);
	CuAssertIntEquals(tc, 16, stack->size);
	CuAssertTrue(tc, stack_is_empty(stack));
	stack_del(stack);
}
Пример #14
0
int main(int argc, char* argv[]) {
    struct Stack stack;
    struct Program program;
    int exit_code = 1, file_nr, size;
    FILE * code_file;
    
    stack_new(&stack, 100);
    debug(" * created stack of 100");
    
    if (argc > 1) {
        for (file_nr = 1; file_nr < argc; file_nr++) {
#ifdef DEBUG
            printf(" * open file '%s'\n", argv[file_nr]);
#endif
            code_file = fopen(argv[file_nr], "r");
            debug(" * start programm");
            size = program_size(code_file);
            fseek(code_file, 0, SEEK_SET);
            program_new(&program, size);
            debug(" * created programm, start reading...");
            read_program(code_file, &program);
            exit_code = interprete(&stack, &program);
            fclose(code_file);
        }
    } else {
        debug(" ! no programm specified, vm will end");
        printf("usage: %s {simple-language.slc}\n", argv[0]);
    }
    
    return exit_code;
}
Пример #15
0
Context *context_new(void)
{
	Context *context;
	context = (Context *) malloc(sizeof(Context));
	if (NULL == context) {
		goto err_malloc;
	}
	context->stack = stack_new((FreeFunc *) value_free_ignoring_symbols);
	if (NULL == context->stack) {
		goto err_malloc_stack;
	}
	context->map = map_new((FreeFunc *) value_free);
	if (NULL == context->map) {
		goto err_malloc_map;
	}
	context_builtin(context);
	context->defining_variable = FALSE;
	return context;
err_malloc_map:
	free(context->stack);
err_malloc_stack:
	free(context);
err_malloc:
	return NULL;
}
Пример #16
0
void test_arbitrary_stack()
{
    Stack stk_instance = stack_new(0);
    Stack *stk = &stk_instance;

    StackResult result = { 0, RESULT_INVALID };
    int i;

    for (i = 0; i < MAX_DEPTH; i++) {
        stack_push(stk, i, &result);
        assert(result.status == STACK_OK);
        result.status = RESULT_INVALID;
    }

    stack_push(stk, i, &result);
    assert(result.status == STACK_FULL);

    for (i = 0; i < MAX_DEPTH; i++) {
        result.status = 0;
        stack_peek(stk, &result);
        assert(result.status == STACK_OK);
        assert(result.data == MAX_DEPTH - i - 1);

        result.status = RESULT_INVALID;
        stack_pop(stk, &result);
        assert(result.status == STACK_OK);
    }
    assert(stack_empty(stk));
}
Пример #17
0
signal2_t *signal2_new()
{
	signal2_t * obj = (signal2_t *)calloc(1, sizeof(struct s_signal2));

	obj->slot2s_stack = stack_new(sizeof(slot2_t *));

	return obj;
}
Пример #18
0
main(){
	int no,i,j,k=0;
	int **info;
	info=malloc(MAX_FLOORS*sizeof(int*));
	person p;
	Stack *up,*down;  //up and down lists contain persons going up and down
	up=stack_new();
	down=stack_new();
	for(i=0;i<10;i++){
		scanf("%d",&no);  //number of persons in lift
		info[i]=malloc(2*no*sizeof(int));
		for(j=0;j<2*no;j++){
			scanf("%d",&info[i][j]);
			if(j%2==1&&info[i][j]>i){
				p.time=info[i][j-1];
				p.id=k; //k is the identity of person.
				k++;
				p.from_floor=i;
				p.to_floor=info[i][j];
				up=stack_push(up,p);
			}
			if(j%2==1&&info[i][j]<i){
				p.time=info[i][j-1];
				p.id=k;
				k++;
				p.from_floor=i;
				p.to_floor=info[i][j];
				down=stack_push(down,p);
			}	
		}
	}
	LIFT *lift1,*lift2;
	lift1=malloc(sizeof(LIFT));
	lift2=malloc(sizeof(LIFT));
	//initialising lifts
	lift1->list=stack_new();
	lift2->list=stack_new();
	lift1->size=0;
	lift2->size=0;
	lift2->position=0;
	lift1->position=0;
	lift1->status=0;
	lift2->status=0;
	//initialisations
	simulate2 (up,down,lift1,lift2);
}
void b_push(struct stack *balancer, struct node *n, int thresh) {
    struct stack *s = b_current_stack(balancer);
    if (s == NULL || s->n_items >= thresh) {
        s = stack_new();
        s_push(balancer,node_new(s));
    }

    s_push(s,n);
}
Пример #20
0
struct stack * type_constrain_boo(struct elt *e1, struct elt *e2)
{
	if (0 == type_vartype_constrain_boo(e1)) { return NULL; }
	if (0 == type_vartype_constrain_boo(e2)) { return NULL; }

	struct stack *types = stack_new();
	stack_push(types, &possible_types[BOO_T]);

	return types;
}
Пример #21
0
static void init_cache(void)
{
/*	int i;

	for (i=0;i < MAXSHIFT;++i) {
		mem_cache[i] = stack_new(10240);
	}*/
	iov_cache = stack_new(10240);
	atexit(destroy_cache);
}
Пример #22
0
void test_new()
{
	int capacity = 16;
	stack_int_t stack = stack_new(capacity);

	ok(
		stack.capacity == capacity &&
		stack.array != NULL,
		"stack_new."
	);
}
Пример #23
0
/* Se acontecer um erro, a função tenta mostrar a região do erro. */
void tentar_imprimir_pedaco_codigo(struct bstree *tree,	struct bstree *destino,
				const int char_errado)
{
	if (tree == NULL)
		return;
	coluna = destino->value->coluna;
	token_errada = char_errado;
	caminho = stack_new(MAX_CAMINHO);
	montar_caminho(tree, destino);
	caminho = stack_free(caminho);
}
Пример #24
0
int test_stack_new_and_destroy(void)
{
    struct stack *s = stack_new();

    mu_check(s->size == 0);
    mu_check(s->root == NULL);
    mu_check(s->end == NULL);

    stack_destroy(s, free);
    return 0;
}
int evaluate_postfix( char* expr )
{
    Stack* st = stack_new();
    char* pBuf;
    char* gen;
    int val; 
    gen = malloc(20*sizeof(char));
    pBuf = malloc(20*sizeof(char));
  //printf ("Splitting string \"%s\" into tokens:\n",expr);
    pBuf = strtok (expr," ");   
    while (pBuf != NULL)
    {
        strcpy(gen,pBuf);
        if(atoi(gen)!=0) 
        {         
            val = atoi(gen);
            stack_push(st,val);
        }
        else
        {
            if(strncmp(gen,"+",1)==0)
            {
                val = ((st->top)->data) + ((st->top->next)->data);
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
            if(strncmp(gen,"-",1)==0)
            {
                val = ((st->top->next)->data) - ((st->top)->data);
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
            if(strncmp(gen,"*",1)==0)
            {
                val = ((st->top)->data) * ((st->top->next)->data);
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
            if(strncmp(gen,"/",1)==0)
            {
                val = ((st->top->next)->data)/((st->top)->data) ;
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
       } 
       stack_print(st);   
       pBuf = strtok (NULL, " ");       
    }   
    return st->top->data;
}
Пример #26
0
void TestStackExpansion(CuTest* tc) 
{
	struct Stack* stack = stack_new(2);

	stack_push(stack, &t_data[0]);
	stack_push(stack, &t_data[1]);
	CuAssertIntEquals(tc, 2, stack->size);
	stack_push(stack, &t_data[1]);
	CuAssertIntEquals(tc, 4, stack->size);

	stack_del(stack);
}
Пример #27
0
int regression_evaluate(
    struct tree *t,
    float **func_input,
    struct data *d,
    char *resp
)
{
    int i;
    int res;
    int hits = 0;
    int col = data_field_index(d, resp);
    float value;
    float zero = 0.0;
    float err = 0.0;
    float sse = 0.0;
    struct node *result;
    struct stack *s = stack_new();

    /* evaluate chromosome */
    res = regression_traverse(0, t->size - 1, t->chromosome, s, func_input, d);
    silent_check(res != -1);

    /* check results */
    result = stack_pop(s);
    for (i = 0; i < d->rows; i++) {
        value = ((float *) result->values)[i];
        err = (float) fabs(value - *d->data[col][i]);
        err = (float) pow(err, 2);
        sse += err;

        if (fltcmp(&err, &zero) == 0) {
            hits++;
        }
    }

    /* record evaluation */
    if (t->score == NULL) {
        t->score = malloc_float(sse + t->size);
    } else {
        *t->score = (sse + t->size);
    }
    t->hits = hits;

    /* clean up */
    node_destroy(result);
    regression_clear_stack(s);
    return 0;
error:
    free(t->score);
    t->score = NULL;
    regression_clear_stack(s);
    return -1;
}
Пример #28
0
/* Function added to delete the memory allocated for AND nodes
   of a decorated syntax tree
   Decorated syntax tree is a conjuntion of simple selection predicates
*/
void delDecoratedSyntreeANDnodes(syntree *arg)
{
	stack *s;
	stack *and_stack;
	syntree *and_tree;

	ASSERT (arg);

    s = stack_new();
	and_stack = stack_new();
	
	// Store the nodes with type AND_TYPE
	push(s, arg);
	while(!is_stack_empty(s))
	{	
		arg = pop(s);
		// This node should not be traversed as it contains specific nodes different from syntree.
		if (arg->type == FT_PREDICATE_TYPE)
		{
			continue;
		}

		if (arg->type == AND_TYPE)
		{
			push(and_stack, arg);
		}
		if (arg->right_tree)
			push(s, arg->right_tree);
	
		if (arg->left_tree)
			push(s, arg->left_tree);
	}	
	// Free the memory for all the AND nodes
	while(!is_stack_empty(and_stack)) {		
		and_tree = pop(and_stack);
		syntree_delete_root_node(and_tree);	
	}
	stack_delete(s);
	stack_delete(and_stack);
}
Пример #29
0
int powaur_crawl(alpm_list_t *targets)
{
    int ret = 0;
    char cwd[PATH_MAX];
    if (!getcwd(cwd, PATH_MAX)) {
        return error(PW_ERR_GETCWD);
    }

    if (chdir(powaur_dir)) {
        return error(PW_ERR_CHDIR, powaur_dir);
    }

    struct pw_hashdb *hashdb = build_hashdb();
    if (!hashdb) {
        pw_fprintf(PW_LOG_ERROR, stderr, "Unable to build hash database!\n");
        ret = -1;
    }

    alpm_list_t *i, *target_pkgs;
    struct graph *graph;
    struct stack *topost = stack_new(sizeof(int));
    int have_cycles;
    for (i = targets; i; i = i->next) {
        stack_reset(topost);
        graph = NULL;
        target_pkgs = alpm_list_add(NULL, i->data);
        build_dep_graph(&graph, hashdb, target_pkgs, RESOLVE_THOROUGH);
        if (have_cycles) {
            printf("Cyclic dependencies for package \"%s\"\n", i->data);
        }

        graph_toposort(graph, topost);
        if (stack_empty(topost)) {
            printf("Package \"%s\" has no dependencies.\n", i->data);
        } else {
            printf("\n");
            pw_printf(PW_LOG_INFO, "\"%s\" topological order: ", i->data);
            print_topo_order(graph, topost);
        }

        graph_free(graph);
        alpm_list_free(target_pkgs);
    }

    stack_free(topost);
    hashdb_free(hashdb);

    if (chdir(cwd)) {
        return error(PW_ERR_RESTORECWD);
    }
    return ret;
}
Пример #30
0
void free_all_clients_events(struct event_base *base, events_filter_type filter, event_cb_arg_destruct_type destruct) {
	enum_clients_events_cb_arg_struct arg;
	stack_new(&arg.events_stack);
	arg.events_filter = filter;
	while (event_base_foreach_event(base, enum_clients_events_cb, &arg) > 0);
	while (!stack_is_empty(&arg.events_stack)) {
		struct event *event = stack_pop(&arg.events_stack);
		assert(event != NULL);
		if (event_del(event)) everror("event_del");
		destruct(event);
		event_free(event);
	}
}