void AudioSelector::enableAutoSelect(AudioSource *source, int prio)
{
  assert(branch_map.find(source) != branch_map.end());
  Branch *branch = branch_map[source];
  branch->setSelectionPrio(prio);
  branch->enableAutoSelect();
} /* AudioSelector::enableAutoSelect */
void test_snippet(std::string const& source)
{
    Branch branch;
    branch.compile(source);

    if (test_fail_on_static_error(&branch))
        return;

    Stack context;
    evaluate_branch(&context, &branch);

    if (test_fail_on_runtime_error(context))
        return;

    // Try stripping orphaned state, this should not have an effect.
    caValue trash;
    strip_orphaned_state(&branch, &context.state, &trash);

    if (!is_null(&trash)) {
        std::cout << "Falsely orphaned state in " << get_current_test_name() << std::endl;
        std::cout << "Code = " << source << std::endl;
        std::cout << "Trash = " << trash.toString() << std::endl;
        declare_current_test_failed();
        return;
    }
}
void test_trimmed_state(std::string const& source, std::string const& dest,
    std::string const& expectedTrash)
{
    Branch sourceBranch;
    sourceBranch.compile(source);

    if (test_fail_on_static_error(&sourceBranch))
        return;

    Stack context;
    evaluate_branch(&context, &sourceBranch);

    if (test_fail_on_runtime_error(context))
        return;

    Branch destBranch;
    destBranch.compile(dest);

    if (test_fail_on_static_error(&destBranch))
        return;

    caValue trash;
    strip_orphaned_state(&destBranch, &context.state, &trash);

    if (expectedTrash != trash.toString()) {
        declare_current_test_failed();
        std::cout << "In test " << get_current_test_name() << std::endl;
        std::cout << expectedTrash << " != " << trash.toString() << std::endl;
    }
}
Exemple #4
0
bool BranchList::add(docstring const & s)
{
	bool added = false;
	size_t i = 0;
	while (true) {
		size_t const j = s.find_first_of(separator_, i);
		docstring name;
		if (j == docstring::npos)
			name = s.substr(i);
		else
			name = s.substr(i, j - i);
		// Is this name already in the list?
		bool const already =
			find_if(list.begin(), list.end(),
				     BranchNamesEqual(name)) != list.end();
		if (!already) {
			added = true;
			Branch br;
			br.setBranch(name);
			br.setSelected(false);
			br.setFileNameSuffix(false);
			list.push_back(br);
		}
		if (j == docstring::npos)
			break;
		i = j + 1;
	}
	return added;
}
void test_state_is_reset_when_if_fails()
{
    Branch branch;
    Stack context;

    Term* c = branch.compile("c = true");
    branch.compile("if c { state i = 0; i += 1 } else { 'hi' }");

    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{i: 1}]}");

    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{i: 2}]}");

    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{i: 3}]}");

    set_bool(c, false);

    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [null, null]}");

    set_bool(c, true);

    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{i: 1}]}");
}
Tree::Tree(const std::string& name, double initialLength, double initialThickness,
           int initialBranches, int leavesPerBranch, int leafStartLevel, 
           double thicknessReduction, double lengthReduction, int recursiveDepth, int seed)
  : SceneNode(name)
{
  totalBranches = 0;
  totalLeaves = 0;

  ::initialLength = initialLength;
  ::initialThickness = initialThickness;
  ::initialBranches = initialBranches;

  ::leavesPerBranch = leavesPerBranch;
  ::leafStartLevel = leafStartLevel;

  ::thicknessReduction = thicknessReduction;
  ::lengthReduction = lengthReduction;

  ::recursiveDepth = recursiveDepth;

  wood.bump("woodbump.png");

  srand( seed );

  Branch* trunk = new Branch("Trunk", 0, initialThickness, initialLength);
  trunk->rotate('x', -90);

  add_child(trunk);

  //std::cerr << "Total Branches: " <<  totalBranches 
    //        << " Total Leaves: " << totalLeaves << std::endl;
}
Exemple #7
0
Component* Branch::CreatBranch()
{
	Branch *pBranch = new Branch;
	pBranch->Init();
	pBranch->SetSpecial(-1);
	return pBranch;
}
Exemple #8
0
bool RemoteBranchModel::refreshBranches(const QString &workingDirectory, bool remoteBranches,
                                        int *currentBranch, QString *errorMessage)
{
    // Run branch command with verbose.
    QStringList branchArgs;
    branchArgs << QLatin1String(GitClient::noColorOption) << QLatin1String("-v");
    QString output;
    *currentBranch = -1;
    if (remoteBranches)
        branchArgs.push_back(QLatin1String("-r"));
    if (!runGitBranchCommand(workingDirectory, branchArgs, &output, errorMessage))
        return false;
    if (debug)
        qDebug() << Q_FUNC_INFO << workingDirectory << output;
    // Parse output
    m_workingDirectory = workingDirectory;
    m_branches.clear();
    const QStringList branches = output.split(QLatin1Char('\n'));
    const int branchCount = branches.size();
    bool isCurrent;
    for (int b = 0; b < branchCount; b++) {
        Branch newBranch;
        if (newBranch.parse(branches.at(b), &isCurrent)) {
            m_branches.push_back(newBranch);
            if (isCurrent)
                *currentBranch = b;
        }
    }
    reset();
    return true;
}
void test_state_is_reset_when_if_fails2()
{
    // Similar to test_state_is_reset_when_if_fails, but this one doesn't
    // have an 'else' block and it uses test_oracle.
    
    internal_debug_function::oracle_clear();

    Branch branch;
    Term* a = branch.compile("a = true");
    
    branch.compile("if a { state s = test_oracle() }");

    internal_debug_function::oracle_send(1);
    internal_debug_function::oracle_send(2);
    internal_debug_function::oracle_send(3);

    Stack context;
    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{s: 1}]}");

    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{s: 1}]}");

    set_bool(a, false);
    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [null, null]}");

    set_bool(a, true);
    evaluate_branch(&context, &branch);
    test_equals(&context.state, "{_if_block: [{s: 2}]}");
}
Exemple #10
0
void test_type_not_prematurely_used()
{
    // Verify that a circa-defined type is not used until interpreter time. Modifying
    // a type's release() handler after there are already instances of it, is not good.
    
    Branch branch;
    branch.compile(
        "type MyType; \n"
        "def f() -> MyType\n"
        "def g(MyType t)\n"
        "s = f()\n"
        "g(s)\n"
        "l = [s s s]\n"
        "type MyCompoundType {\n"
        "  MyType t\n"
        "}\n"
        "state MyType st\n"
        "state MyType st2 = create(MyType)\n"
        );

    Type* myType = (Type*) circa_find_type(&branch, "MyType");
    Type* myCompoundType = (Type*) circa_find_type(&branch, "MyCompoundType");
    test_assert(!myType->inUse);
    test_assert(!myCompoundType->inUse);

    circa::Value value1, value2;
    create(myType, &value1);
    create(myCompoundType, &value2);

    test_assert(myType->inUse);
    test_assert(myCompoundType->inUse);
}
void repro_source_after_rename()
{
    Branch branch;

    // Simple test
    Term* a = branch.compile("a = 1");

    rename(a, "apple");

    test_equals(get_term_source_text(a), "apple = 1");

    // Rename a function argument
    Term* b = branch.compile("b = 5");
    Term* add = branch.compile("add( b , 10)");

    rename(b, "banana");

    test_equals(get_term_source_text(add), "add( banana , 10)");

    // Rename a function
    Term* myfunc = import_function(&branch, NULL, "def myfunc(int)");
    Term* myfunc_call = branch.compile("myfunc(555)");

    rename(myfunc, "my_renamed_func");

    test_equals(get_term_source_text(myfunc_call), "my_renamed_func(555)");
}
void test_lazy()
{
#ifdef DEFERRED_CALLS_FIRST_DRAFT
    Branch branch;

    Term* a = branch.compile("a = add(1 2)");
    set_lazy_call(a, true);

    Stack context;
    push_frame(&context, &branch);
    run_interpreter(&context);

    test_equals(get_register(&context, a), ":Unevaluated");

    Frame* frame = push_frame(&context, &branch);
    frame->pc = a->index;
    frame->startPc = frame->pc;
    frame->endPc = frame->pc + 1;
    frame->strategy = ByDemand;
    run_interpreter(&context);

    test_equals(get_register(&context, a), "3");

    reset_stack(&context);
    Term* b = branch.compile("b = add(a a)");
    push_frame(&context, &branch);
    run_interpreter(&context);

    test_equals(get_register(&context, a), "3");
    test_equals(get_register(&context, b), "6");
#endif
}
Exemple #13
0
void test_custom_object()
{
    g_currentlyAllocated = 0;
    g_totalAllocated = 0;

    Branch branch;
    branch.compile(
        "type MyType; \n"
        "def create_object() -> MyType\n"
        "def check_object(MyType t)\n"
        "s = create_object()\n"
        "check_object(s)\n"
            );

    circa_install_function(&branch, "create_object", create_object);
    circa_install_function(&branch, "check_object", check_object);

    circa_setup_object_type(circa_find_type(&branch, "MyType"),
            sizeof(CustomObject), CustomObjectRelease);

    // Shouldn't allocate any objects before running.
    test_equals(g_currentlyAllocated, 0);
    test_equals(g_totalAllocated, 0);

    Stack stack;
    push_frame(&stack, &branch);
    run_interpreter(&stack);
    test_assert(&stack);
    circa_clear_stack(&stack);

    // Running the script should only cause 1 object allocation.
    test_equals(g_currentlyAllocated, 0);
    test_equals(g_totalAllocated, 1);
}
void bug_reproducing_list_after_eval()
{
    // There once was a bug where source repro would fail when using a list
    // in a vectorized function, but only after that piece of code had been
    // evaluated. This was because vectorize_vv was calling apply() which
    // was changing the 'statement' property of its inputs.
    Branch branch;

    Term* sum = branch.compile("[1 1] + [1 1]");
    Term* in0 = sum->input(0);
    Term* in1 = sum->input(0);

    test_equals(get_term_source_text(in0), "[1 1]");
    test_equals(get_term_source_text(in1), "[1 1]");
    test_equals(get_input_source_text(sum, 0), "[1 1] ");
    test_equals(get_input_source_text(sum, 1), " [1 1]");
    test_equals(get_branch_source_text(&branch), "[1 1] + [1 1]");

    evaluate_branch(&branch);

    test_equals(get_term_source_text(in0), "[1 1]");
    test_equals(get_term_source_text(in1), "[1 1]");
    test_equals(get_input_source_text(sum, 0), "[1 1] ");
    test_equals(get_input_source_text(sum, 1), " [1 1]");

    test_equals(get_branch_source_text(&branch), "[1 1] + [1 1]");
}
Exemple #15
0
std::string cpp_accessor_for_type(Type* type)
{
#if 0
    std::stringstream out;
    std::string indent = "    ";

    out << "struct " << get_cpp_type_name(type) << "\n";
    out << "{\n";
    out << indent << "Term* _term\n";
    out << "\n";
    out << indent << get_cpp_type_name(type) << "(Term* term) : _term(term) {}\n";
    out << "\n";

    Branch* prototype = type->prototype;
    for (int i=0; i < prototype.length(); i++) {
        Term* field = prototype[i];
        out << indent << get_cpp_type_name(field->type) << "& " << field->name << "() ";
        out << "{ return " << get_cpp_type_accessor(field->type) << "(_term->field(" <<
            i << ")); }\n";
    }

    out << "};\n";
    return out.str();
#endif
    return "";
}
void assign_output_type()
{
    Branch branch;
    branch.compile("a = [1]");
    Term* assign = branch.compile("a[0] = 2");
    test_equals(assign->type->name, "List");
}
Exemple #17
0
Term* if_block_append_case(Term* ifBlock, Term* input)
{
    Branch* contents = nested_contents(ifBlock);
    int insertPos = 0;
    for (int i=0; i < contents->length(); i++) {
        Term* term = contents->get(i);

        if (term->function == FUNCS.input)
            insertPos = term->index + 1;

        // Insert position is right after the last non-default case.
        if (term->function == FUNCS.case_func && term->input(0) != NULL)
            insertPos = term->index + 1;
    }

    Term* newCase = apply(contents, FUNCS.case_func, TermList(input));
    contents->move(newCase, insertPos);

    // Add existing input placeholders to this case
    for (int i=0;; i++) {
        Term* placeholder = get_input_placeholder(contents, i);
        if (placeholder == NULL) break;
        Term* localPlaceholder = append_input_placeholder(nested_contents(newCase));
        change_declared_type(localPlaceholder, placeholder->type);
    }

    return newCase;
}
static vector<Segment> segmentsFromBranch(const Branch& branch) {
  vector<Segment> segments = branch.body();
  for (const auto& childBranch : branch.branches()) {
    auto branchSegments = segmentsFromBranch(childBranch);
    move(begin(branchSegments), end(branchSegments), back_inserter(segments));
  }
  return segments;
}
void infer_type_of_append()
{
    Branch branch;
    branch.compile("a = []");
    branch.compile("a.append(1)");
    Term* b = branch.compile("a[0]");
    test_equals(b->type->name, "int");
}
void for_loop_output_type()
{
    Branch branch;
    branch.compile("a = 1");
    branch.compile("for i in [1] { a = 2 }");

    test_equals(branch["a"]->type->name, "int");
}
Exemple #21
0
int if_block_count_cases(Term* term)
{
    Branch* contents = nested_contents(term);
    int result = 0;
    for (int i=0; i < contents->length(); i++)
        if (contents->get(i) != NULL && contents->get(i)->function == FUNCS.case_func)
            result++;
    return result;
}
Exemple #22
0
void AudioSplitter::addSink(AudioSink *sink, bool managed)
{
  Branch *branch = new Branch(this, sink, managed);
  branches.push_back(branch);
  if (do_flush)
  {
    branch->sinkFlushSamples();
  }
} /* AudioSplitter::addSink */
void Branch__get_term(caStack* stack)
{
    Branch* branch = as_branch(circa_input(stack, 0));
    if (branch == NULL)
        return circa_output_error(stack, "NULL branch");

    int index = circa_int_input(stack, 1);
    set_term_ref(circa_output(stack, 0), branch->get(index));
}
Exemple #24
0
/**
 * Calculates contributions to partial derivatives of LL function wrt
 * each of the model parameters for current data bin. Contributions are
 * added to totals in provided deriv argument.
 */
static void calc_deriv(BkgdEvoMdlParam *ll_deriv,
		       BkgdEvoMdlConfig *conf, 
		       const BkgdEvoMdlParam *param, 
		       const BkgdBin *bin) {
  Branch *br;
  ColType *cltype;
  int i;
  double coef;

  /* calculate branch length and branch substitution probability 
   * partial derivatives
   */
  for(i = 0; i < conf->n_branch; i++) {
    br = &conf->branches[i];
    br->set_dlen(br, bin, param);
    branch_set_dprob(br, bin, param, conf);
  }

  /* calculate column probability partial derivatives */
  for(i = 0; i < conf->n_cltype; i++) {
    cltype = conf->cltypes[i];

    if(cltype->subst_type == SUBST_TYPE_CONSERVED) {
      /* only do columns with substitutions in first step */
      continue;
    }

    cltype_set_dprob(cltype, param, conf);
    
    if(cltype->n > 0) {
      /* add contribution to LL partial derivs */
      coef = cltype->n / cltype->prob;

      if(isnan(coef)) {
	fprintf(stderr, "nan coef, cltype->name=%s, cltype->n=%ld, "
		"cltype->prob=%g\n",cltype->name, cltype->n, cltype->prob);
      }

      param_add(&cltype->dprob, coef, ll_deriv);
    }
  }

  /* now conserved column */
  if(conf->cons_cltype != NULL) {
    cltype = conf->cons_cltype;

    cltype_cons_set_dprob(cltype, param, conf);

    if(cltype->n) {
      /* add contribution to LL partial derivs */
      coef = cltype->n / cltype->prob;
      param_add(&cltype->dprob, coef, ll_deriv);
    }
  }

  return;
}
Exemple #25
0
int run_repl()
{
    Stack context;
    Branch branch;
    bool displayRaw = false;

    push_frame(&context, &branch);

    while (true) {
        std::cout << "> ";

        std::string input;

        if (!std::getline(std::cin, input))
            break;

        if (input == "exit" || input == "/exit")
            break;

        if (input == "")
            continue;

        if (input == "/raw") {
            displayRaw = !displayRaw;
            if (displayRaw) std::cout << "Displaying raw output" << std::endl;
            else std::cout << "Not displaying raw output" << std::endl;
            continue;
        }
        if (input == "/clear") {
            clear_branch(&branch);
            std::cout << "Cleared working area" << std::endl;
            continue;
        }
        if (input == "/dump") {
            dump(&branch);
            continue;
        }

        if (input == "/help") {
            std::cout << "Special commands: /raw, /help, /clear, /dump, /exit" << std::endl;
            continue;
        }

        int previousHead = branch.length();
        repl_evaluate_line(&context, input, std::cout);

        if (displayRaw) {
            for (int i=previousHead; i < branch.length(); i++) {
                std::cout << get_term_to_string_extended(branch[i]) << std::endl;
                if (nested_contents(branch[i])->length() > 0)
                    print_branch(std::cout, nested_contents(branch[i]));
            }
        }
    }

    return 0;
}
Exemple #26
0
void test_refs_are_destruction_safe()
{
    Branch branch;
    Term* a = branch.compile("a = 1");
    caValue ref;
    set_ref(&ref, a);
    test_assert(as_ref(&ref) == a);
    clear_branch(&branch);
    test_assert(as_ref(&ref) == NULL);
}
void Branch__find_term(caStack* stack)
{
    Branch* branch = as_branch(circa_input(stack, 0));
    if (branch == NULL)
        return circa_output_error(stack, "NULL branch");

    Term* term = branch->get(circa_string_input(stack, 1));

    set_term_ref(circa_output(stack, 0), term);
}
void test_dont_always_rebind_inner_names()
{
    Branch branch;
    branch.compile("if false { b = 1 } elif false { c = 1 } elif false { d = 1 } else { e = 1 }");
    evaluate_branch(&branch);
    test_assert(!branch.contains("b"));
    test_assert(!branch.contains("c"));
    test_assert(!branch.contains("d"));
    test_assert(!branch.contains("e"));
}
void compare_builtin_types()
{
    Branch branch;
    Term* aFloat = branch.compile("1.2");
    test_assert(term_output_always_satisfies_type(aFloat, unbox_type(FLOAT_TYPE)));
    test_assert(term_output_never_satisfies_type(aFloat, unbox_type(INT_TYPE)));

    Term* anInt = branch.compile("1");
    test_assert(term_output_always_satisfies_type(anInt, unbox_type(FLOAT_TYPE)));
    test_assert(term_output_always_satisfies_type(anInt, unbox_type(INT_TYPE)));
}
Exemple #30
0
    const char* switch_getOutputName(Term* term, int outputIndex)
    {
        Branch* contents = nested_contents(term);

        // check if term is still being initialized:
        if (contents->length() == 0)
            return "";

        Branch* outerRebinds = contents->getFromEnd(0)->contents();
        return outerRebinds->get(outputIndex - 1)->name.c_str();
    }