static tag_t * eval_type(expr_t *expr) { tag_t *tag; symbol_t *symbol; record_t *record; switch (expr->token) { case tok_symbol: if ((symbol = get_symbol(expr->data._unary.cp)) == NULL) error(expr, "syntax error"); return (symbol->tag); case tok_pointer: tag = eval_type(expr->data._unary.expr); if (!pointer_type_p(tag->type)) error(expr, "not a pointer"); return (tag->tag); case tok_vector: tag = eval_type(expr->data._binary.lvalue); if (!pointer_type_p(tag->type)) error(expr, "not a vector"); return (tag->tag); case tok_dot: tag = eval_type(expr->data._binary.lvalue); if (pointer_type_p(tag->type)) error(expr, "not a struct or union"); goto record_tag; case tok_arrow: tag = eval_type(expr->data._binary.lvalue); if (!pointer_type_p(tag->type)) error(expr, "not a pointer"); tag = tag->tag; record_tag: if (type_mask(tag->type) != type_struct && type_mask(tag->type) != type_union) error(expr, "not a struct or union"); record = tag->name; expr = expr->data._binary.rvalue; if (expr->token != tok_symbol) error(expr, "syntax error"); symbol = (symbol_t *) get_hash((hash_t *)record, expr->data._unary.cp); if (symbol == NULL) error(expr, "no '%s' field in '%s'", expr->data._unary.cp, record->name ? record->name->name.string : "<anonymous>"); return (symbol->tag); default: error(expr, "syntax error"); } }
static void unary_sizeof(expr_t *expr) { tag_t *tag; int size; expr_t *uexp; eval(expr->data._unary.expr); uexp = expr->data._unary.expr; switch (uexp->token) { case tok_type: tag = uexp->data._unary.vp; size = tag->size; break; case tok_int: size = sizeof(int); break; case tok_float: size = sizeof(double); break; case tok_string: size = strlen(uexp->data._unary.cp) + 1; break; default: tag = eval_type(uexp); size = tag->size; break; } del_expr(uexp); expr->token = tok_int; expr->data._unary.i = size; }
/** * -xtype test. */ bool eval_xtype(const struct expr *expr, struct eval_state *state) { struct BFTW *ftwbuf = state->ftwbuf; bool is_root = ftwbuf->depth == 0; bool follow = state->cmdline->flags & (is_root ? BFTW_FOLLOW_ROOT : BFTW_FOLLOW_NONROOT); bool is_link = ftwbuf->typeflag == BFTW_LNK; if (follow == is_link) { return eval_type(expr, state); } // -xtype does the opposite of everything else int at_flags = follow ? AT_SYMLINK_NOFOLLOW : 0; struct stat sb; if (fstatat(ftwbuf->at_fd, ftwbuf->at_path, &sb, at_flags) != 0) { if (!follow && errno == ENOENT) { // Broken symlink return eval_type(expr, state); } else { eval_error(state); return false; } } switch (expr->idata) { case BFTW_BLK: return S_ISBLK(sb.st_mode); case BFTW_CHR: return S_ISCHR(sb.st_mode); case BFTW_DIR: return S_ISDIR(sb.st_mode); case BFTW_FIFO: return S_ISFIFO(sb.st_mode); case BFTW_LNK: return S_ISLNK(sb.st_mode); case BFTW_REG: return S_ISREG(sb.st_mode); case BFTW_SOCK: return S_ISSOCK(sb.st_mode); } return false; }
void Breakpoint::calc_support() { std::vector<short> SV; for (size_t i = 0; i < 5; i++) { SV.push_back(0); } //run over all supports and check the majority type: for (std::map<std::string, read_str>::iterator i = positions.support.begin(); i != positions.support.end(); i++) { summarize_type((*i).second.SV, SV); } //given the majority type get the stats: this->sv_type = eval_type(SV); }
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; }