void tv_static_type_query(Type* type, StaticTypeQuery* query) { Term* subject = query->subject; Type* subjectType = query->subjectType; // If the type doesn't have a specific size, then accept any list. if (!list_type_has_specific_size(&type->parameter)) { if (is_list_based_type(subjectType)) return query->succeed(); else return query->fail(); } caValue* expectedElementTypes = list_get_type_list_from_type(type); // Special case when looking at a call to list(); look at inputs instead of // looking at the result. if (subject && subject->function == FUNCS.list) { if (subject->numInputs() != circa_count(expectedElementTypes)) return query->fail(); for (int i=0; i < circa_count(expectedElementTypes); i++) if (!circa::term_output_always_satisfies_type( subject->input(i), as_type(circa_index(expectedElementTypes, i)))) return query->fail(); return query->succeed(); } // Look at the subject's type. if (!list_type_has_specific_size(&subjectType->parameter)) return query->fail(); caValue* subjectElementTypes = list_get_type_list_from_type(subjectType); bool anyUnableToDetermine = false; for (int i=0; i < circa_count(expectedElementTypes); i++) { if (i >= circa_count(subjectElementTypes)) return query->fail(); StaticTypeQuery::Result result = run_static_type_query( as_type(circa_index(expectedElementTypes,i)), as_type(circa_index(subjectElementTypes,i))); // If any of these fail, then fail. if (result == StaticTypeQuery::FAIL) return query->fail(); if (result == StaticTypeQuery::UNABLE_TO_DETERMINE) anyUnableToDetermine = true; } if (anyUnableToDetermine) return query->unableToDetermine(); else return query->succeed(); }
void Branch__get_static_errors_formatted(caStack* stack) { Branch* branch = as_branch(circa_input(stack, 0)); if (branch == NULL) return circa_output_error(stack, "NULL branch"); if (is_null(&branch->staticErrors)) set_list(circa_output(stack, 0), 0); caValue* errors = &branch->staticErrors; caValue* out = circa_output(stack, 0); set_list(out, circa_count(errors)); for (int i=0; i < circa_count(out); i++) format_static_error(circa_index(errors, i), circa_index(out, i)); }
void Block__get_static_errors_formatted(VM* vm) { Block* block = as_block(vm->input(0)); if (block == NULL) return vm->throw_str("NULL block"); Value* errors = block_get_static_errors(block); if (errors == NULL) { set_list(vm->output(), 0); return; } Value* out = vm->output(); set_list(out, circa_count(errors)); for (int i=0; i < circa_count(out); i++) format_static_error(circa_index(errors, i), circa_index(out, i)); }
int list_find_field_index_by_name(Type* listType, const char* name) { if (!is_list_based_type(listType)) return -1; caValue* names = list_get_name_list_from_type(listType); if (names == NULL) return -1; for (int i=0; i < circa_count(names); i++) if (string_eq(circa_index(names, i), name)) return i; // Not found return -1; }
void average(caStack* stack) { caValue* args = circa_input(stack, 0); int count = circa_count(args); caValue* out = circa_output(stack, 0); if (count == 0) { set_float(out, 0); return; } float sum = 0; for (int i=0; i < count; i++) sum += to_float(circa_index(args, i)); set_float(out, sum / count); }
// Pack a state value. Each input will correspond with a slot in the branch's state type. void pack_state(caStack* stack) { Term* caller = (Term*) circa_caller_term(stack); Branch* branch = caller->owningBranch; if (branch->stateType == NULL) return; caValue* args = circa_input(stack, 0); caValue* output = circa_output(stack, 0); create(branch->stateType, output); for (int i=0; i < circa_count(args); i++) { caValue* input = circa_index(args, i); caValue* dest = list_get(output, i); if (input == NULL) set_null(dest); else copy(input, dest); } }
int run_command_line(caWorld* world, caValue* args) { RawOutputPrefs rawOutputPrefs; bool printRaw = false; bool printState = false; bool dontRunScript = false; bool printTrace = false; // Prepended options while (true) { if (list_length(args) == 0) break; if (string_eq(list_get(args, 0), "-break-on")) { DEBUG_BREAK_ON_TERM = atoi(as_cstring(list_get(args, 1))); list_remove_index(args, 0); list_remove_index(args, 0); std::cout << "breaking on creation of term: " << DEBUG_BREAK_ON_TERM << std::endl; continue; } if (string_eq(list_get(args, 0), "-path")) { // Add a module path module_add_search_path(world, as_cstring(list_get(args, 1))); list_remove_index(args, 0); list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-p")) { printRaw = true; list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-pp")) { printRaw = true; rawOutputPrefs.showProperties = true; list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-b") || string_eq(list_get(args, 0), "-pb")) { printRaw = true; rawOutputPrefs.showBytecode = true; list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-n")) { dontRunScript = true; list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-print-state")) { printState = true; list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-t")) { printTrace = true; list_remove_index(args, 0); continue; } if (string_eq(list_get(args, 0), "-load")) { caValue* filename = list_get(args, 1); Value moduleName; module_get_default_name_from_filename(filename, &moduleName); list_remove_index(args, 0); list_remove_index(args, 0); continue; } break; } // No arguments remaining if (list_length(args) == 0) { print_usage(); return 0; } Block* mainBlock = fetch_module(world, "main"); // Check to handle args[0] as a dash-command. // Print help if (string_eq(list_get(args, 0), "-help")) { print_usage(); return 0; } // Eval mode if (string_eq(list_get(args, 0), "-e")) { list_remove_index(args, 0); Value command; set_string(&command, ""); bool firstArg = true; while (!list_empty(args)) { if (!firstArg) string_append(&command, " "); string_append(&command, list_get(args, 0)); list_remove_index(args, 0); firstArg = false; } caValue* result = term_value(mainBlock->eval(as_cstring(&command))); std::cout << to_string(result) << std::endl; return 0; } // Start repl if (string_eq(list_get(args, 0), "-repl")) { run_repl_stdin(world); return 0; } if (string_eq(list_get(args, 0), "-call")) { Symbol loadResult = load_script(mainBlock, as_cstring(list_get(args, 1))); if (loadResult == sym_Failure) { std::cout << "Failed to load file: " << as_cstring(list_get(args, 1)) << std::endl; return -1; } block_finish_changes(mainBlock); caStack* stack = circa_alloc_stack(world); // Push function caBlock* func = circa_find_function_local(mainBlock, as_cstring(list_get(args, 2))); circa_push_function(stack, func); // Push inputs for (int i=3, inputIndex = 0; i < circa_count(args); i++) { caValue* val = circa_input(stack, inputIndex++); circa_parse_string(as_cstring(list_get(args, i)), val); } circa_run(stack); if (circa_has_error(stack)) { circa_print_error_to_stdout(stack); } // Print outputs for (int i=0;; i++) { caValue* out = circa_output(stack, i); if (out == NULL) break; std::cout << to_string(circa_output(stack, i)) << std::endl; } circa_dealloc_stack(stack); } // Start debugger repl if (string_eq(list_get(args, 0), "-d")) return run_debugger_repl(as_cstring(list_get(args, 1))); // Generate cpp headers if (string_eq(list_get(args, 0), "-gh")) { load_script(mainBlock, as_cstring(list_get(args, 1))); std::cout << generate_cpp_headers(mainBlock); return 0; } // Run file checker if (string_eq(list_get(args, 0), "-check")) return run_file_checker(as_cstring(list_get(args, 1))); // Export parsed information if (string_eq(list_get(args, 0), "-export")) { const char* filename = ""; const char* format = ""; if (list_length(args) >= 2) format = as_cstring(list_get(args, 1)); if (list_length(args) >= 3) filename = as_cstring(list_get(args, 2)); return run_exporting_parser(format, filename); } // Build tool if (string_eq(list_get(args, 0), "-build")) { return run_build_tool(args); } // C++ gen if (string_eq(list_get(args, 0), "-cppgen")) { Value remainingArgs; list_slice(args, 1, -1, &remainingArgs); run_generate_cpp(&remainingArgs); return 0; } // Command reader (from stdin) if (string_eq(list_get(args, 0), "-run-stdin")) { run_commands_from_stdin(); return 0; } // Reproduce source text if (string_eq(list_get(args, 0), "-source-repro")) { load_script(mainBlock, as_cstring(list_get(args, 1))); std::cout << get_block_source_text(mainBlock); return 0; } // Rewrite source, this is useful for upgrading old source if (string_eq(list_get(args, 0), "-rewrite-source")) { load_script(mainBlock, as_cstring(list_get(args, 1))); std::string contents = get_block_source_text(mainBlock); write_text_file(as_cstring(list_get(args, 1)), contents.c_str()); return 0; } // Default behavior with no flags: run args[0] as a script filename. // Add the script's folder to module search paths. caValue* filename = list_get(args, 0); Value directory; get_directory_for_filename(filename, &directory); module_add_search_path(world, as_cstring(&directory)); load_script(mainBlock, as_cstring(filename)); block_finish_changes(mainBlock); refresh_bytecode(mainBlock); if (printRaw) print_block(mainBlock, &rawOutputPrefs, std::cout); if (dontRunScript) return 0; Stack* stack = alloc_stack(world); push_frame(stack, mainBlock); run_interpreter(stack); if (printState) std::cout << to_string(&stack->state) << std::endl; if (error_occurred(stack)) { std::cout << "Error occurred:\n"; print_error_stack(stack, std::cout); std::cout << std::endl; std::cout << "Stack:\n"; dump(stack); return 1; } return 0; }