void erase_term(Term* term) { pre_erase_term(term); set_null(term_value(term)); set_inputs(term, TermList()); change_function(term, NULL); term->type = NULL; remove_nested_contents(term); // for each user, clear that user's input list of this term remove_from_any_user_lists(term); clear_from_dependencies_of_users(term); if (term->owningBlock != NULL) { // remove name binding if necessary term->owningBlock->removeNameBinding(term); // index may be invalid if something bad has happened ca_assert(term->index < term->owningBlock->length()); term->owningBlock->_terms.setAt(term->index, NULL); term->owningBlock = NULL; term->index = -1; } dealloc_term(term); }
void Term::setDependency(int index, Term* term) { if (index == 0) change_function(this, term); else set_input(this, index - 1, term); }
void Term::setDependency(int index, Term* term) { if (index == 0) change_function(this, term); else if (index == 1) set_declared_type(this, unbox_type(term)); else set_input(this, index - 2, term); }
void test_change_function() { Branch branch; // simple test Term* a = branch.compile("add(3,3)"); evaluate_branch(&branch); test_assert(as_int(a) == 6); change_function(a, KERNEL->get("mult_i")); evaluate_branch(&branch); test_assert(as_int(a) == 9); }
void clear_block(Block* block) { block->names.clear(); block->inProgress = false; // Iterate through the block and tear down any term references, so that we // don't have to worry about stale pointers later. for (BlockIterator it(block); it; ++it) { if (*it == NULL) continue; pre_erase_term(*it); set_inputs(*it, TermList()); remove_from_any_user_lists(*it); change_function(*it, NULL); } for (int i= block->_terms.length() - 1; i >= 0; i--) { Term* term = block->get(i); if (term == NULL) continue; if (term->nestedContents) clear_block(term->nestedContents); } for (int i = block->_terms.length() - 1; i >= 0; i--) { Term* term = block->get(i); if (term == NULL) continue; // Delete any leftover users, mark them as repairable. for (int userIndex = 0; userIndex < term->users.length(); userIndex++) { Term* user = term->users[userIndex]; for (int depIndex = 0; depIndex < user->numDependencies(); depIndex++) { if (user->dependency(depIndex) == term) { // mark_repairable_link(user, term->name, depIndex); user->setDependency(depIndex, NULL); } } } erase_term(term); } block->_terms.clear(); }
bool change_vars(int vars) { if(vars < 1) { show_error(window, "You need to define at least 1 variable. Sorry."); return false; } bool changed1 = change_function(vars); bool changed2 = change_restrictions(vars); if(!changed1 || !changed2) { show_error(window, "Unable to allocated memory for this problem. Sorry."); return false; } return true; }
void block_link_missing_functions(Block* block, Block* source) { for (BlockIterator it(block); it; ++it) { Term* term = *it; if (term->function == NULL || term->function == FUNCS.unknown_function || term->function == FUNCS.unknown_function_prelude) { std::string funcName = term->stringProp(s_Syntax_FunctionName, ""); if (funcName == "") continue; // try to find this function Term* func = find_local_name(source, funcName.c_str(), s_LookupFunction); if (func != NULL) change_function(term, func); } } }
void refactor__change_function(caStack* stack) { change_function(as_term_ref(circa_input(stack, 0)), (Term*) circa_caller_input_term(stack, 1)); }