Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

158 строки
7.1 KiB

  1. proc generate_docs(String* path) -> void {
  2. FILE *f = fopen(Memory::get_c_str(path), "w");
  3. if (!f) {
  4. create_generic_error("The file for writing the documentation (%s) "
  5. "could not be opened for writing.", Memory::get_c_str(path));
  6. return;
  7. }
  8. defer {
  9. fclose(f);
  10. };
  11. Environment_Array_List visited = create_Environment_array_list();
  12. // recursive inner funciton
  13. std::function<void(Environment*, char* prefix)> print_this_env;
  14. print_this_env = [&](Environment* env, char* prefix) -> void {
  15. bool we_already_printed = false;
  16. // TODO(Felix): Make a generic array_list_contains function
  17. for_array_list(visited) {
  18. if (it == env) {
  19. we_already_printed = true;
  20. break;
  21. }
  22. }
  23. if (!we_already_printed) {
  24. printf("Working ion env::::");
  25. print_environment(env);
  26. printf("\n--------------------------------\n");
  27. append_to_array_list(&visited, env);
  28. push_environment(env);
  29. defer {
  30. pop_environment();
  31. };
  32. for (int i = 0; i < env->next_index; ++i) {
  33. fprintf(f, "\\hrule\n* =%s%s= \n"
  34. // " :PROPERTIES:\n"
  35. // " :UNNUMBERED: t\n"
  36. // " :END:"
  37. ,prefix, env->keys[i]);
  38. /*
  39. * sourcecodeLocation
  40. */
  41. if (env->values[i]->sourceCodeLocation) {
  42. try_void fprintf(f, "\n - defined in :: =%s:%d:%d=",
  43. Memory::get_c_str(env->values[i]->sourceCodeLocation->file),
  44. env->values[i]->sourceCodeLocation->line,
  45. env->values[i]->sourceCodeLocation->column);
  46. }
  47. /*
  48. * type
  49. */
  50. Lisp_Object_Type type = Memory::get_type(env->values[i]);
  51. Lisp_Object* LOtype;
  52. try_void LOtype = eval_expr(Memory::create_list(
  53. Memory::get_or_create_lisp_object_symbol("type"),
  54. env->values[i]));
  55. fprintf(f, "\n - type :: =");
  56. print(LOtype, true, f);
  57. fprintf(f, "=");
  58. /*
  59. * if printable value -> print it
  60. */
  61. switch (type) {
  62. case(Lisp_Object_Type::Nil):
  63. case(Lisp_Object_Type::T):
  64. case(Lisp_Object_Type::Number):
  65. case(Lisp_Object_Type::String):
  66. case(Lisp_Object_Type::Pair):
  67. case(Lisp_Object_Type::Symbol):
  68. case(Lisp_Object_Type::Keyword): {
  69. fprintf(f, "\n - value :: =");
  70. print(env->values[i], true, f);
  71. fprintf(f, "=");
  72. } break;
  73. default: break;
  74. }
  75. /*
  76. * if function then print arguments
  77. */
  78. if (type == Lisp_Object_Type::Function) {
  79. Lisp_Object* fun = env->values[i];
  80. bool printed_at_least_some_args = false;
  81. fprintf(f, "\n - arguments :: ");
  82. if (fun->value.function.args.positional.symbols.next_index != 0) {
  83. if (!printed_at_least_some_args)
  84. fprintf(f, ":");
  85. fprintf(f, "\n - postitional :: ");
  86. try_void fprintf(f, "=%s=", Memory::get_c_str(fun->value.function.args.positional.symbols.data[0]->value.symbol.identifier));
  87. for (int i = 1; i < fun->value.function.args.positional.symbols.next_index; ++i) {
  88. fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.args.positional.symbols.data[i]->value.symbol.identifier));
  89. }
  90. }
  91. if (fun->value.function.args.keyword.values.next_index != 0) {
  92. if (!printed_at_least_some_args)
  93. fprintf(f, ":");
  94. fprintf(f, "\n - keyword :: ");
  95. fprintf(f, "=%s=", Memory::get_c_str(fun->value.function.args.keyword.keywords.data[0]->value.symbol.identifier));
  96. if (fun->value.function.args.keyword.values.data[0]) {
  97. fprintf(f, " =(");
  98. print(fun->value.function.args.keyword.values.data[0], true, f);
  99. fprintf(f, ")=");
  100. }
  101. for (int i = 1; i < fun->value.function.args.keyword.values.next_index; ++i) {
  102. fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.args.keyword.keywords.data[i]->value.symbol.identifier));
  103. if (fun->value.function.args.keyword.values.data[i]) {
  104. fprintf(f, " =(");
  105. print(fun->value.function.args.keyword.values.data[i], true, f);
  106. fprintf(f, ")=");
  107. }
  108. }
  109. }
  110. if (fun->value.function.args.rest) {
  111. if (!printed_at_least_some_args)
  112. fprintf(f, ":");
  113. fprintf(f, "\n - rest :: =%s=", Memory::get_c_str(fun->value.function.args.rest->value.symbol.identifier));
  114. }
  115. // if no args at all
  116. if (fun->value.function.args.positional.symbols.next_index == 0 &&
  117. fun->value.function.args.keyword.values.next_index == 0 &&
  118. !fun->value.function.args.rest)
  119. {
  120. fprintf(f, "none.");
  121. }
  122. }
  123. fprintf(f, "\n - docu :: ");
  124. if (env->values[i]->docstring)
  125. fprintf(f, "\n #+BEGIN:\n%s\n #+END:\n",
  126. Memory::get_c_str(env->values[i]->docstring));
  127. else
  128. fprintf(f, "none\n");
  129. // if (Memory::get_type(env->values[i]) == Lisp_Object_Type::Function &&
  130. // env->values[i]->userType &&
  131. // (string_equal(env->values[i]->userType->value.symbol.identifier, "package") ||
  132. // string_equal(env->values[i]->userType->value.symbol.identifier, "constructor")))
  133. // {
  134. // char new_prefix[200];
  135. // strcpy(new_prefix, prefix);
  136. // strcat(new_prefix, env->keys[i]);
  137. // strcat(new_prefix, " ");
  138. // print_this_env(env->values[i]->value.function.parent_environment, new_prefix);
  139. // }
  140. }
  141. }
  142. for (int i = 0; i < env->parents.next_index; ++i) {
  143. print_this_env(env->parents.data[i], prefix);
  144. }
  145. };
  146. print_this_env(get_current_environment(), (char*)"");
  147. }