void VM_start(bstring binary, bstring filename) { STATE = State_new(); Runtime_init(state); VALUE lobby = Lobby_new(state); // toplevel object state->lobby = lobby; state->binary = binary; BytecodeFile *file = BytecodeFile_new(state, filename); int fn_count = DArray_count(file->function_names); for(int j=0; j < fn_count; j++) { bstring fn_name = (bstring)DArray_at(file->function_names, j); Function *fn = (Function*)Hashmap_get(file->functions, fn_name); Hashmap_set(state->functions, fn_name, fn); } bstring main_fn = bfromcstr("0_main"); CallFrame *top_frame = CallFrame_new(lobby, STATE_FN(main_fn), NULL); bdestroy(main_fn); top_frame->name = "main"; Stack_push(FRAMES, top_frame); // now we're ready to bootstrap State_bootstrap(state); // and run the codes! VM_run(state); State_destroy(state); }
char *all_tests() { mu_suite_start(); state = State_new(); mu_run_test(test_load); return NULL; }
static State *DFA_findState( DFA *d, Ins **kernel, uint kCount ) { Ins **cP, **iP, *i; State *s; kernel[kCount] = NULL; cP = kernel; for( iP = kernel; (i = *iP) != NULL; ++iP ) { if( i->i.tag == CHAR || i->i.tag == TERM ) { *cP++ = i; } else { i->i.marked = FALSE; } } kCount = cP - kernel; kernel[kCount] = NULL; for( s = d->head; s != NULL; s = s->next ) { if( s->kCount == kCount ) { for( iP = s->kernel; (i = *iP) != NULL; ++iP ) { if( !i->i.marked ) { break; } } if( i == NULL ) { break; } } } if( s == NULL ) { s = State_new(); DFA_addState( d, d->tail, s ); s->kCount = kCount; s->kernel = malloc( ( kCount + 1 ) * sizeof( Ins * ) ); memcpy( s->kernel, kernel, ( kCount + 1 ) * sizeof( Ins * ) ); s->link = d->toDo; d->toDo = s; } for( iP = kernel; (i = *iP) != NULL; ++iP ) { i->i.marked = FALSE; } return s; }
List doPathfinding(Map map, Car cars[3]) { Heap openSet = Heap_new(State_compare); List closedSet = List_new(); List finalPath = List_new(); List neighbors; Position neighbor; State state, newState; Vector newSpeed, acceleration; int end = 0, i, j, useBoost, positionTaken, distance; float cost; LOGINFO("A* doin' da werk!"); state = State_new(Car_getPosition(cars[0]), Car_getSpeed(cars[0]), Car_getBoosts(cars[0]), map); Heap_insert(openSet, state); while(!Heap_isEmpty(openSet) && !end) { state = Heap_extractMin(openSet); if(Map_getTile(map, State_getPosition(state)->x, State_getPosition(state)->y) == ARRIVAL) { end = 1; break; } distance = Map_getDistance(map, State_getPosition(state)->x, State_getPosition(state)->y); neighbors = Map_getReachablePositions(map, State_getPosition(state), State_getSpeed(state), State_getBoosts(state)); List_foreach(neighbors, neighbor, i) { if(Map_getDistance(map, neighbor->x, neighbor->y) > distance) { Position_delete(neighbor); continue; } cost = State_getRealCost(state) + 1; newSpeed = Position_findOffset(State_getPosition(state), neighbor); acceleration = Vector_copy(newSpeed); Vector_substract(acceleration, State_getSpeed(state)); useBoost = 0; positionTaken = 0; if(Vector_squaredLength(acceleration) > 2) { useBoost = 1; } for(j = 1; j < 3; j++) { if(Position_equal(neighbor, Car_getPosition(cars[j]))) { positionTaken = 1; } } if(!positionTaken) { newState = State_new(neighbor, newSpeed, State_getBoosts(state) - useBoost, map); State_setRealCost(newState, cost); State_setParent(newState, state); Heap_insert(openSet, newState); } Vector_delete(newSpeed); Vector_delete(acceleration); Position_delete(neighbor); } List_insert(closedSet, state); List_empty(neighbors); List_delete(neighbors); } while(state != NULL) { List_insert(finalPath, Position_copy(State_getPosition(state))); state = State_getParent(state); } List_head(closedSet); while(!List_isEmpty(closedSet)) { state = List_getCurrent(closedSet); List_remove(closedSet); State_delete(state); } List_delete(closedSet); while((state = Heap_extractMin(openSet)) != NULL) { State_delete(state); } Heap_delete(openSet); LOGINFO("A* is done mate"); return finalPath; }
//Function to simulate the twin lifts. //VARIABLE EXPLANATION:- // qf: array of array of queues // mq: main queue which contains people int their order of ariival. // num_floors: total number of floors. void Simulate(pqueue ** qf, pqueue * mq, int num_floors) { State * lift1 = State_new(0, num_floors, 0, 1, 1); State * lift2 = State_new(num_floors, num_floors, 0, -1, 2); person * p; for(p=mq->head;p!=NULL;p=p->mainnext) { if(p->time!=-1) continue; int reserved = CalculateNumPersons(qf[p->floor_arrival]); int a1 = lift1->floor-p->floor_arrival; int a2 = lift2->floor-p->floor_arrival; if(a1<0)a1*=-1; if(a2<0)a2*=-1; a1*=FLOOR_TIME; a2*=FLOOR_TIME; if(a1+lift1->time<=a2+lift2->time) { GoTo(lift1->floor, p->floor_arrival, lift1, qf, num_floors, reserved); Pickup(p->floor_arrival, lift1, qf); Drop(lift1->floor, p->floor_dest, lift1, qf, num_floors); } else { GoTo(lift2->floor, p->floor_arrival, lift2, qf, num_floors, reserved); Pickup(p->floor_arrival, lift2, qf); Drop(lift2->floor, p->floor_dest, lift2, qf, num_floors); } printf("Lift 1: %d %d\n",lift1->time,lift1->floor); printf("Lift 2: %d %d\n",lift2->time,lift2->floor); } lift1->num_people-=lift1->dst[lift1->floor]; lift2->num_people-=lift2->dst[lift2->floor]; if(lift1->num_people==0 && lift2->num_people==0) printf("\nSuccessfuly simulated!!\n\n"); int j; lift1->place_time[lift1->time+1] = lift1->floor; lift2->place_time[lift2->time+1] = lift2->floor; findfloors(lift1->place_time,lift1->time+1); findfloors(lift2->place_time,lift2->time+1); /*printf("\nLift1 : \n"); for(j=0;j<=lift1->time;++j) { printf("%d %.1f\n",j,lift1->place_time[j]); } printf("\nLift2 : \n"); for(j=0;j<=lift2->time;++j) { printf("%d %.1f\n",j,lift2->place_time[j]); }*/ state_print(qf, num_floors, lift1, lift2, lift1->time>lift2->time?lift1->time:lift2->time); }