bool dict_query(Dict* d, void* key, void* value) { uint64 hash = d->hash_func(key); for (uint64 idx = 0; idx < d->alloc_size; idx++){ // Quadratic probing uint64 place = (hash + idx*idx) % d->alloc_size; Flag* p_flag = get_flag(d, d->data, place); if (*p_flag == BUCKET_EMPTY){ return false; } // Compare keys. void* current_key = get_key(d, d->data, place); if (d->equal_func(key, current_key)){ void* current_value = get_value(d, d->data, place); if (value != NULL) memcpy(value, current_value, d->value_size); return true; } } assert_never(); return false; }
static uint64 query(BucketType* buckets, Value* keys, uint64 size, Value key) { const uint64 h = op_hash(key); bool first_empty = false; uint64 first_empty_place = 100000000; // This should not get returned. // If it does, it will hopefully // crash :) for (uint64 idx = 0; idx < size; idx++){ // Quadratic probing uint64 place = (h + idx*idx) % size; switch(buckets[place]){ case BUCKET_EMPTY: if (first_empty) return first_empty_place; return place; case BUCKET_WAS_FULL: if (not first_empty){ first_empty = true; first_empty_place = place; } break; case BUCKET_FULL: if (op_equal2(key, keys[place])) return place; break; } } if (first_empty) return first_empty_place; assert_never(); return 1000000000; }
// Get prime that is 2^power-(small number), and is bigger than n. int64 map_prime(int64 n) { // Size of the sizes[] list. #define MAP_PRIMES 27 // This list was generated by a script that is in closet. const int64 sizes[] = {13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139, 524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859, 134217689, 268435399, 536870909, 1073741789}; int i; for (i = 0; i < MAP_PRIMES - 1; i++) { if (n < sizes[i]) return sizes[i]; } // Otherwise, return the largest possible hash table size. assert_never(); return sizes[MAP_PRIMES - 1]; }
static void set(Dict* d, void* key, void* value, void* buckets, uint64 num_buckets) { uint64 hash = d->hash_func(key); for (uint64 idx = 0; idx < num_buckets; idx++){ // Quadratic probing uint64 place = (hash + idx*idx) % num_buckets; Flag* p_flag = get_flag(d, buckets, place); void* dict_key = get_key(d, buckets, place); void* dict_value = get_value(d, buckets, place); if (*p_flag == BUCKET_EMPTY){ *p_flag = BUCKET_FULL; memcpy(dict_key, key, d->key_size); memcpy(dict_value, value, d->value_size); return; } if (d->equal_func(key, dict_key)){ memcpy(dict_value, value, d->value_size); } } assert_never(); }
EE* eval_expr(Node* expr) { if (is_unary_op(expr)) return ee_new(UNTYPED, mem_asprintf("%s(%s)", unary_op_map(expr->type), eval_Value(node_get_child(expr, 0)))); if (is_binary_op(expr)) return ee_new (UNTYPED, mem_asprintf("%s(%s, %s)", binary_op_map(expr->type), eval_Value(node_get_child(expr, 0)), eval_Value(node_get_child(expr, 1)))); switch(expr->type){ case K_TRUE: return ee_new("Bool", "VALUE_TRUE"); case K_FALSE: return ee_new("Bool", "VALUE_FALSE"); case K_NIL: return ee_new("Nil", "VALUE_NIL"); case K_EOF: return ee_new("Eof", "VALUE_EOF"); case ID: if (context_block != NULL){ // If it's a block param, then OK. if (var_query_kind(expr->text) == VAR_BLOCK_PARAM) return ee_new(var_query_type(expr->text), var_query_c_name(expr->text)); else return ee_new(var_query_type(expr->text), closure_add(expr->text, var_query_c_name(expr->text))); } else return ee_new(var_query_type(expr->text), var_query_c_name(expr->text)); case SYMBOL: return ee_new("Integer", cache_dsym(expr->text + 1)); case INT: return ee_new("Integer", mem_asprintf("int64_to_val(%s)", expr->text)); case DOUBLE: return ee_new("Double", mem_asprintf("double_to_val(%s)", expr->text)); case STRING: { const char* str = expr->text; return ee_new("String", mem_asprintf("string_to_val(\"%s\")", str)); } break; case CHARACTER: { const char* str = expr->text; return ee_new("Integer", mem_asprintf("int64_to_val(%d)", (int) str[1])); } break; case EXPR_ARRAY: { Node* expr_list = node_get_child(expr, 0); return ee_new("Array", mem_asprintf("array1_to_val2(%u %s)", expr_list->children.size, eval_expr_list(expr_list, true))); } case EXPR_MAP: { Node* m_list = node_get_child(expr, 0); bool is_map = false, is_set = false; const char* args = ""; const int num_ms = node_num_children(m_list); assert(num_ms > 0); for (int i = 0; i < num_ms; i++){ Node* m = node_get_child(m_list, i); switch (node_num_children(m)){ case 2: is_map = true; args = mem_asprintf("%s, %s, %s", args, eval_Value(node_get_child(m, 0)), eval_Value(node_get_child(m, 1))); break; case 1: is_set = true; args = mem_asprintf("%s, %s", args, eval_Value(node_get_child(m, 0))); break; default: assert_never(); break; } } if (is_map and is_set){ fatal_node(expr, "invalid curly braces: is this a set or a map?"); } if (is_map) return ee_new("Map", mem_asprintf("ht_new_map(%d%s)", num_ms, args)); if (is_set) return ee_new("Set", mem_asprintf("ht_new_set(%d%s)", num_ms, args)); assert_never(); } break; case EXPR_INDEX: return ee_new(UNTYPED, eval_index(node_get_child(expr, 0), node_get_child(expr, 1), NULL)); case EXPR_CALL: { Node* callee = node_get_node(expr, "callee"); Node* args = node_get_node(expr, "args"); // If callee is a field, then we must check if its a method call. if (callee->type == EXPR_FIELD){ const char* field_name = node_get_string(callee, "name"); Node* left = node_get_child(callee, 0); const char* s = eval_expr_as_id(left); if (s == NULL or var_query(s)) return ee_new(UNTYPED, eval_obj_call(left, field_name, args)); } const char* s = eval_expr_as_id(callee); if (s != NULL){ // Could be a tuple constructor. if (strequal(s, "tuple")){ return ee_new("Tuple", mem_asprintf("tuple_to_val(%u %s)", node_num_children(args), eval_expr_list(args, true))); } // If all of callee can be evaluated as an id, then it must be a static // call. return ee_new(UNTYPED, eval_static_call(s, args)); } return ee_new(UNTYPED, mem_asprintf("func_call%d(%s %s)", node_num_children(args), eval_Value(callee), eval_expr_list(args, true))); } case EXPR_RANGE_BOUNDED: { Node* left = node_get_child(expr, 0); Node* right = node_get_child(expr, 1); return ee_new("Range", mem_asprintf("range_to_val(RANGE_BOUNDED, " "val_to_int64(%s), val_to_int64(%s))", eval_Value(left), eval_Value(right))); } case EXPR_RANGE_BOUNDED_LEFT: return ee_new("Range", mem_asprintf("range_to_val(RANGE_BOUNDED_LEFT, " "val_to_int64(%s), 0)", eval_Value(node_get_child(expr, 0)))); case EXPR_RANGE_BOUNDED_RIGHT: return ee_new("Range", mem_asprintf("range_to_val(RANGE_BOUNDED_RIGHT, " "0, val_to_int64(%s))", eval_Value(node_get_child(expr, 0)))); case EXPR_RANGE_UNBOUNDED: return ee_new("Range", "range_to_val(RANGE_UNBOUNDED, 0, 0)"); case EXPR_FIELD: { // Attempt to evaluate this field as a static symbol. Node* left = node_get_child(expr, 0); const char* s = eval_expr_as_id(left); if (s == NULL or var_query(s)){ // Dynamic field. const char* field = node_get_string(expr, "name"); return ee_new(UNTYPED, mem_asprintf("field_get(%s, %s)", eval_Value(left), cache_dsym(field))); } else { // Could be a global variable. s = eval_expr_as_id(expr); if (context_block != NULL){ return ee_new(var_query_type(s), closure_add(s, var_query_c_name(s))); } else return ee_new(var_query_type(s), var_query_c_name(s)); } } break; case EXPR_AT_VAR: { const char* name = node_get_string(expr, "name"); if (context_ci == NULL){ fatal_node(expr, "'@%s' in something that's not a class", name); } if (context_ci->type != CLASS_FIELD){ fatal_node(expr, "'@%s' in a class that's not a field class", name); } return ee_new(UNTYPED, mem_asprintf("field_get(__self, %s)", cache_dsym(name))); } case C_CODE: { const char* str = util_trim_ends(expr->text); if (strstr(expr->text, "return")){ fatal_warn("careless return in C code may disrupt the stack (use RRETURN)"); } if (context_fi != NULL){ if (context_fi->type == METHOD or context_fi->type == CONSTRUCTOR or context_fi->type == VIRTUAL_GET or context_fi->type == VIRTUAL_SET){ if (strchr(str, '@') != NULL and context_ci->type != CLASS_CDATA) fatal_node(expr, "@ in C code in a class that is not cdata"); str = util_replace(str, '@', "_c_data->"); } } return ee_new(UNTYPED, str); } case EXPR_IS_TYPE: { const char* type = eval_type(node_get_child(expr, 1)); if (var_query(type)){ fatal_node(expr, "type '%s' in 'is' expression is a variable", type); } return ee_new("Bool", mem_asprintf("pack_bool(obj_klass(%s) == %s)", eval_Value(node_get_child(expr, 0)), cache_type(type))); } case EXPR_BLOCK: { fatal_push("in anonymous block"); if (context_block != NULL){ fatal_node(expr, "nested blocks are not implemented yet"); } // Initialize context_block context_block = mem_new(BlockContext); sbuf_init(&(context_block->sbuf_code), ""); sarray_init(&(context_block->closure_names)); sarray_init(&(context_block->closure_exprs)); static int counter = 0; counter++; context_block->func_name = mem_asprintf("ripe_blk%d", counter); Node* param_list = node_get_node(expr, "param_list"); Node* stmt_list = node_get_node(expr, "stmt_list"); var_push(); // Print out the header of the anonymous function sbuf_printf(&(context_block->sbuf_code), "static Value %s(Value __block", context_block->func_name); for (int i = 0; i < node_num_children(param_list); i++){ Node* param = node_get_child(param_list, i); const char* name = node_get_string(param, "name"); const char* c_name = util_c_name(name); if (node_has_string(param, "array")) fatal_node(expr, "array parameters for blocks are not implemented yet"); const char* type = "?"; // TODO: Deal with type. var_add_local2(name, c_name, type, VAR_BLOCK_PARAM); sbuf_printf(&(context_block->sbuf_code), ", Value %s", c_name); } sbuf_printf(&(context_block->sbuf_code), ")\n"); // Generate block code sbuf_printf(&(context_block->sbuf_code), "{\n"); sbuf_printf(&(context_block->sbuf_code), " Func* _c_data = obj_c_data(__block);\n"); sbuf_printf(&(context_block->sbuf_code), " stack_annot_push(\"anonymous function\");\n"); sbuf_printf(&(context_block->sbuf_code), "%s", gen_block(stmt_list)); sbuf_printf(&(context_block->sbuf_code), "}\n"); // Now, print out the block function to WR_HEADER wr_print(WR_HEADER, "%s", context_block->sbuf_code.str); const char* result = mem_asprintf("block_to_val(%s, %d, %d", context_block->func_name, node_num_children(param_list), context_block->closure_names.size); for (uint i = 0; i < context_block->closure_names.size; i++){ const char* evaluated = sarray_get_ptr(&(context_block->closure_exprs), i); result = mem_asprintf("%s, %s", result, evaluated); } result = mem_asprintf("%s)", result); // End EXPR_BLOCK var_pop(); context_block = NULL; fatal_pop(); return ee_new("Function", result); } default: assert_never(); } return NULL; }