Beispiel #1
0
linked_list* merge_paths(linked_list* elist, linked_list* vlist)
{
   list_node* enode;
   list_node* vnode;
   char* vpath;

   if (elist && vlist)
   {
      for (vnode = list_tail(vlist) ; vnode ; vnode = previous(vnode))
      {
	 vpath = (char*) get_value(vnode);

	 for (enode = head(elist) ; enode ; enode = next(enode))
	    if (!strcmp(vpath, (char*) get_value(enode)))
	    {
	       remove_node(elist, enode, 0);
	       break;
	    }

	 add_to_head(elist, get_value(vnode));
      }
   }
   else if (vlist && head(vlist))
   {
      return(vlist);
   }

   return(elist);
}
Beispiel #2
0
void intercode_aly(node *p){		
	char name[20];
	strcpy(name,p->name);
	if (p == NULL){
		return ;
	}
	if(strcmp(name,"ExtDef")==0){
		InterCodes codes = translate_Extdef(p);
		add_to_head(codes);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;
	}
	else if(strcmp(name,"Exp")==0){
		Operand t1 = new_temp();
		InterCodes expe = translate_Exp(p,t1);
		add_to_head(expe);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;
	}
	else if(strcmp(name,"CompSt")==0){/*
		InterCodes codes =	translate_Compst(p);
		add_to_head(codes);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;*/
	}
	else if(strcmp(name,"Stmt")==0){
		InterCodes codes = translate_Stmt(p);
		add_to_head(codes);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;
	}

	if(p->child != NULL)
		intercode_aly(p->child);
	if(p->brother != NULL)
		intercode_aly(p->brother);
	return;
}
Beispiel #3
0
static int
decode_string(void* ctx, const unsigned char* data, unsigned int size)
{
    ErlNifBinary bin;
    if(!enif_alloc_binary_compat(ENV(ctx), size, &bin))
    {
        return CANCEL;
    }
    memcpy(bin.data, data, size);
    add_to_head(ctx, enif_make_binary(ENV(ctx), &bin));
    return CONTINUE;
}
Beispiel #4
0
static void game_setup()
{
    if (!game) {
        // Make a NEW game
        game = malloc(sizeof(game_state_t));
        
        // Make a head for a snake
        snake_section_t *head = malloc(sizeof(snake_section_t));
        head->x = SNAKE_BODY_WIDTH;
        head->y = SNAKE_BODY_WIDTH;
        head->next = NULL;
        
        // Make a snake with that head
        snake = malloc(sizeof(snake_t));
        snake->head = head;
        
        // Make an apple
        apple = malloc(sizeof(apple_t));
        
    } else if (game->is_resetting) {
        // Resets the snake
        snake_section_t *current_head = snake->head;
        snake_section_t *current_section = current_head->next;
        snake_section_t *next;
        // free all the snake sections
        while(current_section) {
            next = current_section;
            current_section = current_section->next;
            free(next);
        }
        current_head->x = SNAKE_BODY_WIDTH;
        current_head->y = SNAKE_BODY_WIDTH;
        current_head->next = NULL;
    }
    
    // Resets the game
    game->score = 0;
    game->queued_input = 0;
    game->alive = 1;
    snake->direction = 0;
    game->is_resetting = 0;
    game->bonus_points = 0;
    
    // Make the snake as long as the default size
    for (int i = 1; i < DEFAULT_SNAKE_SIZE; ++i) {
        add_to_head(snake);
    }
    snake->length = DEFAULT_SNAKE_SIZE;

    // Move apple to random location
    move_apple();
}
Beispiel #5
0
static void move_snake(snake_t *snake)
{
    // Check the bounds
    // If the snake has gone beyond the edge,
    // mkae the head appear on the other side
    snake_section_t *current_head = snake->head;
    if (current_head->x > container_width) {
        current_head->x = -SNAKE_BODY_WIDTH;
        current_head->y = current_head->y;
    } else if (current_head->x < 0) {
        current_head->x = container_width;
        current_head->y = current_head->y;
    }
    
    if (current_head->y > container_height) {
        current_head->y = -SNAKE_BODY_WIDTH;
        current_head->x = current_head->x;
    } else if (current_head->y < 0) {
        current_head->y = container_height;
        current_head->x = current_head->x;
    }
    
    // move snake by adding to the head and removing the last section
    // move the head
    add_to_head(snake);
    snake_section_t *current_section = snake->head;
    snake_section_t *next_section = current_section->next;
    
    // store the new head location to check collision
    unsigned collided = 0;
    int head_x = current_section->x;
    int head_y = current_section->y;
    
    // move the rest of the snake
    while (next_section->next != NULL) {
        current_section = next_section;
        next_section = next_section->next;
        // Check collision
        if (current_section->x == head_x && current_section->y == head_y) {
            collided = 1;
            break;
        }
    }
    // remove last section
    current_section->next = NULL;
    free(next_section);
    
    // Mark game as dead if the snake collided
    if (collided) {
        game->alive = 0;
    }
}
Beispiel #6
0
static int
decode_number(void * ctx, const char * numberVal, unsigned int numberLen)
{
    // scan in the input to see if it's a float or int

    int numberType = 0; // 0 means integer, 1 means float
    unsigned int i;
    ErlNifBinary bin;
    int missingDot = 1;
    unsigned int expPos;

    for(i=0; i<numberLen; i++) {
        switch (numberVal[i]) {
        case '.':
            missingDot = 0;
            numberType = 1; // it's  a float
            goto loopend;
        case 'E':
        case 'e':
            expPos = i;
            numberType = 1; // it's  a float
            goto loopend;
        }
    }
loopend:
    if ((numberType == 1) && missingDot)
    {
        if(!enif_alloc_binary_compat(ENV(ctx), numberLen + 2, &bin))
        {
            return CANCEL;
        }
        memcpy(bin.data, numberVal, expPos);
        bin.data[expPos] = '.';
        bin.data[expPos + 1] = '0';
        memcpy(bin.data + expPos + 2, numberVal + expPos, numberLen - expPos);
    }
    else
    {
        if(!enif_alloc_binary_compat(ENV(ctx), numberLen, &bin))
        {
            return CANCEL;
        }
        memcpy(bin.data, numberVal, numberLen);
    }
    add_to_head(ctx, enif_make_tuple(ENV(ctx), 2,
                        enif_make_int(ENV(ctx), numberType),
                        enif_make_binary(ENV(ctx), &bin)));
    return CONTINUE;
}
Beispiel #7
0
/* Links the node into the per-thread list of pending deletions.
 */
void free_node_later (void *q)
{
    uint8_t my_index = ltd.thread_index;

    mr_node_t* wrapper_node = ssalloc_alloc(1, sizeof(mr_node_t));
    wrapper_node->actual_node = q;
    // Create timestamp in mr node
    gettimeofday(&(wrapper_node->created), NULL);

    add_to_head(ltd.limbo_list[shtd[my_index].epoch], wrapper_node);
    ltd.rcount++;

    ltd.free_calls++;
    if (fallback.flag == 1 && ltd.free_calls >= R) {
        ltd.free_calls = 0;
        scan();
    }

}
Beispiel #8
0
linked_list* test_paths(linked_list* vlist)
{
   linked_list* elist = new_list();
   list_node* vnode;
   char* vpath;
   FILE* test;
   
   for (vnode = list_tail(vlist) ; vnode ; vnode = previous(vnode))
   {
      vpath = (char*) get_value(vnode);
      
      test = fopen(vpath, "r");
      if ( test )
      {
         add_to_head(elist, vpath);
         fclose(test);
      }
   }
   
   return(elist);
}
Beispiel #9
0
static int
decode_boolean(void* ctx, int val)
{
    add_to_head(ctx, enif_make_atom(ENV(ctx), val ? "true" : "false"));
    return CONTINUE;
}
Beispiel #10
0
static int
decode_null(void* ctx)
{
    add_to_head(ctx, enif_make_atom(ENV(ctx), "null"));
    return CONTINUE;
}
Beispiel #11
0
static int
decode_end_map(void* ctx)
{
    add_to_head(ctx, enif_make_int(ENV(ctx), 3));
    return CONTINUE;
}
Beispiel #12
0
static int
decode_start_array(void* ctx)
{
    add_to_head(ctx, enif_make_int(ENV(ctx), 0));
    return CONTINUE;
}
Beispiel #13
0
static void game_tick(void *data)
{
    if (game->alive) {
        if (!game->is_resetting) {
             game_timer = app_timer_register(GAME_TICK_INTERVAL, game_tick, NULL);
        }
    } else {
        game_end(1);
    }

    // If game is paused, has the user presed the resume button?
    if (game->is_paused && game->queued_input != 3) {
        return;
    }

    // Check User Input
    if (game->queued_input) {
        switch (game->queued_input) {
            case 1:
                // Up pressed, TURN COUNTERCLOCKWISE
                if (snake->direction > 0) {
                    snake->direction -= 1;
                } else {
                    snake->direction = 3;
                }
                break;
            case 2:
                // Down pressed, TURN CLOCKWISE
                if (snake->direction < 3) {
                    snake->direction  += 1;
                } else {
                    snake->direction = 0;
                }
                break;
            case 3:
                game->is_paused = !game->is_paused;
                break;
            default:
                break;
        }
        game->queued_input = 0; 
    }

    if (game->alive) {
        move_snake(snake);
    }

    // Check if snake ate apple
    if(snake_has_eaten_apple(snake, apple)) {
        add_to_head(snake);
        // Move apple
        move_apple();
        game->score += (1 + game->bonus_points);
        snake->length += 1;
        vibes_short_pulse();

        if (game->bonus_points == 0) {
            bonus_points_timer = app_timer_register(BONUS_TIMER_INVTERVAL, bonus_timer_callback, NULL);
        }

        game->bonus_points = snake->length / 2;
        
    }

    // Update the graphics
    // Note: this could be moved to a separate graphics update interval
    // But since the game tick here is the same as the graphics tick
    // I saw no need to do two timers

    layer_mark_dirty(game_layer);
    
    static char score_text[12];

    // Update score layer
    if (game->is_paused) {
        snprintf(score_text, sizeof(score_text), "Paused");  
    } else {
        snprintf(score_text, sizeof(score_text), "Score: %d ", game->score);   
    }

    text_layer_set_text(game_score_label, score_text);

    static char bonus_text[12];
    snprintf(bonus_text, sizeof(bonus_text), "Bonus: %d", game->bonus_points);
    text_layer_set_text(game_bonus_label, bonus_text);
}