|
- proc generate_docs(String* path) -> void {
- FILE *f = fopen(Memory::get_c_str(path), "w");
- if (!f) {
- create_generic_error("The file for writing the documentation (%s) "
- "could not be opened for writing.", Memory::get_c_str(path));
- return;
- }
- defer {
- fclose(f);
- };
-
- Environment_Array_List visited = create_Environment_array_list();
-
- // recursive inner funciton
- std::function<void(Environment*, char* prefix)> print_this_env;
- print_this_env = [&](Environment* env, char* prefix) -> void {
- bool we_already_printed = false;
- // TODO(Felix): Make a generic array_list_contains function
- for_array_list(visited) {
- if (it == env) {
- we_already_printed = true;
- break;
- }
- }
- if (!we_already_printed) {
- printf("Working ion env::::");
- print_environment(env);
- printf("\n--------------------------------\n");
- append_to_array_list(&visited, env);
-
- push_environment(env);
- defer {
- pop_environment();
- };
-
- for (int i = 0; i < env->next_index; ++i) {
- fprintf(f, "\\hrule\n* =%s%s= \n"
- // " :PROPERTIES:\n"
- // " :UNNUMBERED: t\n"
- // " :END:"
- ,prefix, env->keys[i]);
- /*
- * sourcecodeLocation
- */
- if (env->values[i]->sourceCodeLocation) {
- try_void fprintf(f, "\n - defined in :: =%s:%d:%d=",
- Memory::get_c_str(env->values[i]->sourceCodeLocation->file),
- env->values[i]->sourceCodeLocation->line,
- env->values[i]->sourceCodeLocation->column);
- }
- /*
- * type
- */
- Lisp_Object_Type type = Memory::get_type(env->values[i]);
- Lisp_Object* LOtype;
- try_void LOtype = eval_expr(Memory::create_list(
- Memory::get_or_create_lisp_object_symbol("type"),
- env->values[i]));
-
- fprintf(f, "\n - type :: =");
- print(LOtype, true, f);
- fprintf(f, "=");
-
-
- /*
- * if printable value -> print it
- */
- switch (type) {
- case(Lisp_Object_Type::Nil):
- case(Lisp_Object_Type::T):
- case(Lisp_Object_Type::Number):
- case(Lisp_Object_Type::String):
- case(Lisp_Object_Type::Pair):
- case(Lisp_Object_Type::Symbol):
- case(Lisp_Object_Type::Keyword): {
- fprintf(f, "\n - value :: =");
- print(env->values[i], true, f);
- fprintf(f, "=");
- } break;
- default: break;
- }
- /*
- * if function then print arguments
- */
- if (type == Lisp_Object_Type::Function) {
- Lisp_Object* fun = env->values[i];
- bool printed_at_least_some_args = false;
- fprintf(f, "\n - arguments :: ");
- if (fun->value.function.args.positional.symbols.next_index != 0) {
- if (!printed_at_least_some_args)
- fprintf(f, ":");
- fprintf(f, "\n - postitional :: ");
- try_void fprintf(f, "=%s=", Memory::get_c_str(fun->value.function.args.positional.symbols.data[0]->value.symbol.identifier));
- for (int i = 1; i < fun->value.function.args.positional.symbols.next_index; ++i) {
- fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.args.positional.symbols.data[i]->value.symbol.identifier));
- }
- }
- if (fun->value.function.args.keyword.values.next_index != 0) {
- if (!printed_at_least_some_args)
- fprintf(f, ":");
- fprintf(f, "\n - keyword :: ");
- fprintf(f, "=%s=", Memory::get_c_str(fun->value.function.args.keyword.keywords.data[0]->value.symbol.identifier));
- if (fun->value.function.args.keyword.values.data[0]) {
- fprintf(f, " =(");
- print(fun->value.function.args.keyword.values.data[0], true, f);
- fprintf(f, ")=");
- }
- for (int i = 1; i < fun->value.function.args.keyword.values.next_index; ++i) {
- fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.args.keyword.keywords.data[i]->value.symbol.identifier));
- if (fun->value.function.args.keyword.values.data[i]) {
- fprintf(f, " =(");
- print(fun->value.function.args.keyword.values.data[i], true, f);
- fprintf(f, ")=");
- }
- }
- }
- if (fun->value.function.args.rest) {
- if (!printed_at_least_some_args)
- fprintf(f, ":");
- fprintf(f, "\n - rest :: =%s=", Memory::get_c_str(fun->value.function.args.rest->value.symbol.identifier));
- }
- // if no args at all
- if (fun->value.function.args.positional.symbols.next_index == 0 &&
- fun->value.function.args.keyword.values.next_index == 0 &&
- !fun->value.function.args.rest)
- {
- fprintf(f, "none.");
- }
- }
- fprintf(f, "\n - docu :: ");
- if (env->values[i]->docstring)
- fprintf(f, "\n #+BEGIN:\n%s\n #+END:\n",
- Memory::get_c_str(env->values[i]->docstring));
- else
- fprintf(f, "none\n");
-
- // if (Memory::get_type(env->values[i]) == Lisp_Object_Type::Function &&
- // env->values[i]->userType &&
- // (string_equal(env->values[i]->userType->value.symbol.identifier, "package") ||
- // string_equal(env->values[i]->userType->value.symbol.identifier, "constructor")))
- // {
- // char new_prefix[200];
- // strcpy(new_prefix, prefix);
- // strcat(new_prefix, env->keys[i]);
- // strcat(new_prefix, " ");
- // print_this_env(env->values[i]->value.function.parent_environment, new_prefix);
- // }
- }
- }
-
- for (int i = 0; i < env->parents.next_index; ++i) {
- print_this_env(env->parents.data[i], prefix);
- }
- };
-
- print_this_env(get_current_environment(), (char*)"");
- }
|