Пример #1
0
Файл: vm.c Проект: txus/terrorvm
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);
}
Пример #2
0
static List *neighbours_list(World *world, Point *point, Point *destination, Hashmap *nodes)
{
  List *neighbours = List_create();
  int nx, ny;

  for(nx = point->x - 1; nx <= point->x + 1; nx++) {
    if(nx < 0 || nx >= world->width) continue;
    for(ny = point->y - 1; ny <= point->y + 1; ny++) {
      if(ny < 0 || ny >= world->height ||
	 (ny == point->y && nx == point->x) ||
	 (!World_can_enter(world, nx, ny, point->z) &&
	  !(nx == destination->x && ny == destination->y))) continue;

      Point *p  = Point_create(nx, ny, point->z);
      Node *node = Node_create(p, 0, 0, NULL);

      Node *old_node = Hashmap_get(nodes, node);
      if(old_node) {
	Node_destroy(node);
	node = old_node;
      } else {
	Hashmap_set(nodes, node, node);
      }

      List_push(neighbours, node);
    }
  }

  return neighbours;
}
Пример #3
0
char *test_get_set()
{
    int rc = Hashmap_set(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set &test1");
    bstring result = Hashmap_get(map, &test1);
    mu_assert(result == &expect1, "Wrong value for test1.");

    rc = Hashmap_set(map, &test2, &expect2);
    mu_assert(rc == 0, "Failed to set test2");
    result = Hashmap_get(map, &test2);
    mu_assert(result == &expect2, "Wrong value for test2.");

    rc = Hashmap_set(map, &test3, &expect3);
    mu_assert(rc == 0, "Failed to set test3");
    result = Hashmap_get(map, &test3);
    mu_assert(result == &expect3, "Wrong value for test3.");

    return NULL;
}
Пример #4
0
Object* Object_lookup_slot(Object *receiver, bstring slot_name) {
  Object *obj    = receiver;
  Object *result = NULL;

  do {
    result = (Object*)Hashmap_get(obj->slots, slot_name);
    if(result) return result;
  } while((obj = obj->parent));

  return NULL;
}
Пример #5
0
void add_feature(FeatureGroup group, char* value, Hashmap *featuremap, DArray *list) {
    struct FeatureKey ro;
    ro.grp = group;
    ro.value = value;

    FeatureValue fv;
    fv = (FeatureValue) Hashmap_get(featuremap, &ro);

    if (fv != NULL)
        DArray_push(list, &(fv->feature_id));
}
Пример #6
0
List *Path(World *world, Point *source, Point *destination)
{
  int tentative_gscore;
  List *result = NULL;
  int tries = 0;

  Hashmap *nodes = Hashmap_create(cmp, hash);
  Hashmap *closedset = Hashmap_create(cmp, hash);
  PQueue *openset = PQueue_create(cmp, hash);

  Node *current;
  Node *start = Node_create(source, 0, 0, NULL);
  Hashmap_set(nodes, start, start);

  start->fscore = start->gscore + heuristic_cost_estimate(start->point, destination);
  PQueue_push(openset, start, start->fscore);

  while(!PQueue_empty(openset) &&
	tries < 300) {
    tries++;


    current = PQueue_pop(openset);
    Hashmap_set(closedset, current, current);

    if(POINT_EQ(current->point, destination)) {
      result = reconstruct_path(current);
      break;

    } else {

      List *neighbours = neighbours_list(world, current->point, destination, nodes);

      LIST_FOREACH(neighbours, first, next, cur) {
	Node *neighbour = cur->value;

	if(Hashmap_get(closedset, neighbour) == NULL) {
	  tentative_gscore =  current->gscore + 1;

	  if(!PQueue_contains(openset, neighbour) ||
	     tentative_gscore > neighbour->gscore) {
	    if(!PQueue_contains(openset, neighbour)) {
	      neighbour->came_from = current;
	      neighbour->gscore = tentative_gscore;
	      neighbour->fscore = neighbour->gscore + heuristic_cost_estimate(neighbour->point, destination);
	      PQueue_push(openset, neighbour, neighbour->fscore);
	    }
	  }
	}
      }

      List_destroy(neighbours);
    }
  }
Пример #7
0
char *test_delete()
{
    bstring deleted = (bstring)Hashmap_delete(map, &test1);
    mu_assert(deleted != NULL, "Got NULL on delete.");
    mu_assert(deleted == &expect1, "Should get test1");
    bstring result = Hashmap_get(map, &test1);
    mu_assert(result == NULL, "Should delete.");

    deleted = (bstring)Hashmap_delete(map, &test2);
    mu_assert(deleted != NULL, "Got NULL on delete.");
    mu_assert(deleted == &expect2, "Should get test2");
    result = Hashmap_get(map, &test2);
    mu_assert(result == NULL, "Should delete.");

    deleted = (bstring)Hashmap_delete(map, &test3);
    mu_assert(deleted != NULL, "Got NULL on delete.");
    mu_assert(deleted == &expect3, "Should get test3");
    result = Hashmap_get(map, &test3);
    mu_assert(result == NULL, "Should delete.");

    return NULL;
}
Пример #8
0
char *test_get_set()
{
	/*be attention that bsStatic return value only
 * 	can be used as tagstring type  ,the point to which is the bstring
 */
	int rc =Hashmap_set(map , &test1 , &expect1);
	mu_assert(rc == 0 , "failed to set &test1");
	bstring result = Hashmap_get(map , &test1);
	mu_assert(result == &expect1, "wrong value for test1");
	
	rc = Hashmap_set(map , &test2 , &expect2);
	mu_assert(rc == 0 , "failed to set test2");
	result = Hashmap_get(map , &test2);
	mu_assert(result == &expect2 , "wrong values for test2 ");

	rc = Hashmap_set(map , &test3 , &expect3);
	mu_assert(rc == 0 , "failed to set test2");
	result = Hashmap_get(map , &test3);
	mu_assert(result == &expect3 , "wrong values for test2 ");
	
	return NULL;
}
Пример #9
0
char *test_hashes()
{
    Object **contents = calloc(4, sizeof(Object*));
    contents[0] = String_new(bfromcstr("foo"));
    contents[1] = Integer_new(1);
    contents[2] = String_new(bfromcstr("bar"));
    contents[3] = Integer_new(2);

    Object *array = Array_new(contents, 4);
    Object *object = Hash_new(array);

    mu_assert(object->type == tHash, "Hash has the wrong type");
    Hashmap *map = (Hashmap*)object->value.other;
    Object *foo = (Object*)Hashmap_get(map, contents[0]);
    mu_assert(foo->value.integer == 1, "Hash element 'foo' is wrong.");
    Object *bar = (Object*)Hashmap_get(map, contents[2]);
    mu_assert(bar->value.integer == 2, "Hash element 'bar' is wrong.");

    Object_destroy(object);
    Object_destroy(array);
    free(contents);
    return NULL;
}
Пример #10
0
char *Server_dump_stats(Hashmap *store, char *name)
{
  bstring b_name = bfromcstr(name);
  Stats *stat = Hashmap_get(store, b_name);
  check(stat != NULL, "Could not find stat");

  free(b_name);

  return Stats_dump(stat);

error:
  if (b_name) { free(b_name); }
  return NULL;
}
Пример #11
0
double Server_return_stddev(Hashmap *store, char *name)
{
  bstring b_name = bfromcstr(name);
  Stats *stat = Hashmap_get(store, b_name);
  check(stat != NULL, "Could not find stat");

  free(b_name);

  return Stats_stddev(stat);

error:
  if (b_name) { free(b_name); }
  return -1;
}
Пример #12
0
int Server_add_sample(Hashmap *store, char *name, double sample)
{
  bstring b_name = bfromcstr(name);
  Stats *stat = Hashmap_get(store, b_name);
  check(stat != NULL, "Could not find stat");
  Stats_sample(stat, sample);
  free(b_name);

  return 0;

error:
  if (b_name) { free(b_name); }
  return -1;
}
Пример #13
0
char *test_load()
{
  BytecodeFile *file = BytecodeFile_new(state, bfromcstr("tests/testfile.tvm"));

  bstring main_name = bfromcstr("0_main");
  Function *main = Hashmap_get(file->functions, main_name);
  bstring add_name = bfromcstr("4_add");
  Function *add = Hashmap_get(file->functions, add_name);

  mu_assert(*main->code == PUSH, "error parsing main");

  VALUE first_lit = DArray_first(main->literals);
  mu_assert(strcmp(VAL2STR(first_lit), "add") == 0, "error parsing literal in main");

  VALUE first_num = DArray_at(main->literals, 1);
  mu_assert(VAL2NUM(first_num) == 1.2, "error parsing float literal in main");

  mu_assert(*add->code == PUSHSELF, "error parsing add");

  bdestroy(main_name);
  bdestroy(add_name);

  return NULL;
}
Пример #14
0
int main(int argc, char const *argv[])
{
	Hashmap *map = Hashmap_create(NULL, Hashmap_fnv1a_hash);
	char * test1 = "test1";
	char * test2 = "test2";
	char * test3 = "test3";
	char *expect1 = "expect1";
	char *expect2 = "expect2";
	char *expect3 = "expect3";

	int rc1 = Hashmap_set(map, &test1, &expect1);
	int rc2 = Hashmap_set(map, &test2, &expect2);
	int rc3 = Hashmap_set(map, &test3, &expect3);

	int *res = Hashmap_get(map, &test1);

	printf("res = %s\n", (char *)*res);

	return 0;
}
Пример #15
0
Object* Object_get_slot(Object *receiver, bstring slot_name) {
  return Hashmap_get(receiver->slots, slot_name);
}
Пример #16
0
int PQueue_contains(PQueue *p_queue, void *item)
{
  PQueueNode *node = Hashmap_get(p_queue->map, item);
  return node != NULL ? 1 : 0;
}