void for_loop_remake_zero_branch(Branch* forContents) { Branch* zero = for_loop_get_zero_branch(forContents); clear_branch(zero); // Clone inputs for (int i=0;; i++) { Term* placeholder = get_input_placeholder(forContents, i); if (placeholder == NULL) break; Term* clone = append_input_placeholder(zero); rename(clone, placeholder->nameSymbol); } Term* loopOutput = create_list(zero); // Clone outputs for (int i=0;; i++) { Term* placeholder = get_output_placeholder(forContents, i); if (placeholder == NULL) break; // Find the appropriate connection Term* result = find_local_name(zero, placeholder->name.c_str()); if (i == 0) result = loopOutput; Term* clone = append_output_placeholder(zero, result); rename(clone, placeholder->nameSymbol); } branch_finish_changes(zero); }
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; }
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); }
int main(int argc, char **argv) { int i, j, wordnum = 0; FILE *fwords = NULL; printf("Welcome to Crossword Generator v0.1\n"); printf("===================================\n"); if (2 != argc) { printf("Usage: %s <file with a list of words>\n", argv[0]); printf("\nMax words: %d\nMax wordlen: %d\n", MAXWORDS, MAXWORDLEN); return 1; } // Read input words to the words[] array fwords = fopen(argv[1], "r"); for (i = 0; NULL != fgets(words[i].word, MAXWORDLEN, fwords) && i < MAXWORDS; i++) { // Remove newline symbol words[i].wordlen = strlen(words[i].word) - 1; words[i].word[words[i].wordlen] = '\0'; for (j = 0; j < words[i].wordlen; j++) words[i].word[j] = tolower(words[i].word[j]); printf("Word #%d: %s, %d\n", i, words[i].word, words[i].wordlen); } wordnum = i; // Build inital word pairs that we'll be using a lot later if (build_pairs(wordnum)) { fprintf(stderr, "Error building pairs between words\n"); return 1; } // Fill the tree with all possible pairs. Scan all the words. for (i = 0; i < wordnum; i++) build_branch(wordnum, words[i].firstchild); #ifdef DEBUG for (i = 0; i < wordnum; i++) { printf("\n--------------------------------\nword[%d]=%s\n--------------------------------\n", i, words[i].word); print_strie(words[i].firstchild); } #endif // Print the best branch if any print_branch(best_branch); // Free the memory for (i = 0; i < wordnum; i++) clear_branch(words[i].firstchild); return 0; }
static void load(Branch* branch, std::string const& filename) { if (filename == "") { clear_branch(branch); return; } load_script(branch, filename.c_str()); if (has_static_errors(branch)) print_static_errors_formatted(branch, std::cout); }
void test_that_stripping_state_is_recursive() { Branch branch; setup(branch); branch.compile("if true { state a = 1; state s; s = alloc_handle(s) }"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); clear_branch(&branch); branch.compile("if true { state a = 1 }"); strip_orphaned_state(&branch, &context.state); test_equals(&g_slots, "[false, false, false]"); }
void test_state_inside_if_block() { Branch branch; setup(branch); branch.compile("state s = null"); branch.compile("if is_null(s) { s = alloc_handle(s) }"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); clear_branch(&branch); strip_orphaned_state(&branch, &context.state); test_equals(&g_slots, "[false, false, false]"); }
void test_deleted_state() { Branch branch; setup(branch); branch.compile("state s"); branch.compile("alloc_handle(@s)"); branch.compile("state t"); branch.compile("alloc_handle(@t)"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, true, false]"); clear_branch(&branch); branch.compile("state t"); strip_orphaned_state(&branch, &context.state); test_equals(&g_slots, "[false, true, false]"); }