void Term__type(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); set_type(circa_output(stack, 0), t->type); }
void Term__tweak(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); int steps = tweak_round(to_float(circa_input(stack, 1))); caValue* val = term_value(t); if (steps == 0) return; if (is_float(val)) { float step = get_step(t); // Do the math like this so that rounding errors are not accumulated float new_value = (tweak_round(as_float(val) / step) + steps) * step; set_float(val, new_value); } else if (is_int(val)) set_int(val, as_int(val) + steps); else circa_output_error(stack, "Ref is not an int or number"); }
void Term__function(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); set_branch(circa_output(stack, 0), function_contents(as_function(t->function))); }
void Term__to_string(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); circa::to_string(term_value(t), vm->output()); }
void Term__to_string(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); set_string(circa_output(stack, 0), circa::to_string(term_value(t))); }
void Term__unique_name(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_value(vm->output(), unique_name(t)); }
void Term__function(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_block(vm->output(), term_function(t)); }
void Term__parent(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_block(vm->output(), t->owningBlock); }
void Term__type(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_type(vm->output(), t->type); }
void Term__is_value(VM* vm) { Term* t = as_term_ref(vm->input(0)); bool consideredValue = is_value(t) || t->function == FUNCS.require; set_bool(vm->output(), consideredValue); }
void update_all_code_references_in_value(caValue* value, Branch* oldBranch, Branch* newBranch) { for (ValueIterator it(value); it.unfinished(); it.advance()) { caValue* val = *it; if (is_ref(val)) { set_term_ref(val, translate_term_across_branches(as_term_ref(val), oldBranch, newBranch)); } else if (is_branch(val)) { // If this is just a reference to 'oldBranch' then simply update it to 'newBranch'. if (as_branch(val) == oldBranch) set_branch(val, newBranch); // Noop on null branch. if (as_branch(val) == NULL) continue; // Noop if branch has no owner. Term* oldTerm = as_branch(val)->owningTerm; if (oldTerm == NULL) continue; Term* newTerm = translate_term_across_branches(oldTerm, oldBranch, newBranch); if (newTerm == NULL) { set_branch(val, NULL); continue; } set_branch(val, newTerm->nestedContents); } } }
void Term__properties(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL reference"); copy(&t->properties, vm->output()); }
void Term__to_source_string(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); set_string(circa_output(stack, 0), get_term_source_text(t)); }
void migrate_value(Value* value, Migration* migration) { if (is_ref(value)) { set_term_ref(value, migrate_term_pointer(as_term_ref(value), migration)); } else if (is_block(value)) { set_block(value, migrate_block_pointer(as_block(value), migration)); } else if (is_list_based(value)) { migrate_list_value(value, migration); } else if (is_hashtable(value)) { Value oldHashtable; move(value, &oldHashtable); set_hashtable(value); Value* hashtable = value; for (int i=0; i < hashtable_slot_count(&oldHashtable); i++) { Value* oldKey = hashtable_key_by_index(&oldHashtable, i); if (oldKey == NULL || is_null(oldKey)) continue; Value* oldValue = hashtable_value_by_index(&oldHashtable, i); Value key; copy(oldKey, &key); migrate_value(&key, migration); Value* newValue = hashtable_insert(hashtable, &key); copy(oldValue, newValue); migrate_value(newValue, migration); } } }
int term_hashFunc(caValue* val) { Term* term = as_term_ref(val); if (term == NULL) return 0; return term->id; }
void Term__properties(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); circa::copy(&t->properties, circa_output(stack, 0)); }
void Term__num_inputs(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_int(vm->output(), t->numInputs()); }
void Term__contents(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_block(vm->output(), t->nestedContents); }
void Term__asfloat(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_float(vm->output(), to_float(term_value(t))); }
void Term__set_value(VM* vm) { Term* target = as_term_ref(vm->input(0)); if (target == NULL) return vm->throw_str("NULL term"); copy(vm->input(1), term_value(target)); }
void Term__global_id(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); set_int(circa_output(stack, 0), t->id); }
void Term__location_string(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); set_string(circa_output(stack, 0), get_short_location(t).c_str()); }
void Term__global_id(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL reference"); set_int(vm->output(), t->id); }
void Term__location_string(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL reference"); get_short_location(t, vm->output()); }
void Term__asint(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); if (!is_int(term_value(t))) return vm->throw_str("Not an int"); set_int(vm->output(), as_int(term_value(t))); }
void Term__parent(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) { circa_output_error(stack, "NULL reference"); return; } set_branch(circa_output(stack, 0), t->owningBranch); }
void Term__contents(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) { circa_output_error(stack, "NULL reference"); return; } set_branch(circa_output(stack, 0), t->nestedContents); }
void Term__has_property(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL reference"); Symbol key = as_symbol(vm->input(1)); Value* value = term_get_property(t, key); set_bool(vm->output(), value != NULL); }
void Term__num_inputs(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) { circa_output_error(stack, "NULL reference"); return; } set_int(circa_output(stack, 0), t->numInputs()); }
void Term__name(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); if (has_empty_name(t)) set_string(vm->output(), ""); else copy(term_name(t), vm->output()); }