Term* start_building_for_loop(Term* forTerm, const char* iteratorName, Type* iteratorType) { Branch* contents = nested_contents(forTerm); // Add input placeholder for the list input Term* listInput = apply(contents, FUNCS.input, TermList()); // Add loop_index() Term* index = apply(contents, FUNCS.loop_index, TermList(listInput)); hide_from_source(index); // Add get_index to fetch the list's current element. Term* iterator = apply(contents, FUNCS.get_index, TermList(listInput, index), name_from_string(iteratorName)); if (iteratorType == NULL) iteratorType = infer_type_of_get_index(forTerm->input(0)); change_declared_type(iterator, iteratorType); hide_from_source(iterator); // Add the zero branch create_branch_unevaluated(contents, "#zero"); // Add an loop output index Term* loopOutputIndex = apply(contents, FUNCS.loop_output_index, TermList()); return iterator; }
Term* start_building_for_loop(Term* forTerm, const char* iteratorName) { Branch* contents = nested_contents(forTerm); // Add input placeholder for the list input Term* listInput = apply(contents, FUNCS.input, TermList()); // Add loop_index() Term* index = apply(contents, FUNCS.loop_index, TermList(listInput)); hide_from_source(index); // Add loop_iterator() Term* iterator = apply(contents, FUNCS.get_index, TermList(listInput, index), iteratorName); change_declared_type(iterator, infer_type_of_get_index(forTerm->input(0))); hide_from_source(iterator); return iterator; }
void test_find_type_of_get_index() { Branch branch; Term* range = branch.compile("range(4,5)"); test_equals(infer_type_of_get_index(range)->name, "int"); Term* list1 = branch.compile("['hello' 'hi' 'bye']"); test_equals(infer_type_of_get_index(list1)->name, "string"); Term* list2 = branch.compile("[0 -1 11]"); test_equals(infer_type_of_get_index(list2)->name, "int"); Term* list3 = branch.compile("[]"); test_equals(infer_type_of_get_index(list3)->name, "any"); Term* list4 = branch.compile("[0.1 4]"); test_equals(infer_type_of_get_index(list4)->name, "number"); branch.compile("type T { number x, number y }"); Term* list5 = branch.compile("[.1 .1] -> T"); test_equals(infer_type_of_get_index(list5)->name, "number"); }
Type* specializeType(Term* term) { return infer_type_of_get_index(term->input(0)); }