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

147 строки
6.0 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. Array_List<Environment*> visited;
  12. const auto print_this_env = [&](const auto& rec, Environment* env, char* prefix) -> void {
  13. bool we_already_printed = false;
  14. // TODO(Felix): Make a generic array_list_contains function
  15. for(auto it : visited) {
  16. if (it == env) {
  17. we_already_printed = true;
  18. break;
  19. }
  20. }
  21. if (!we_already_printed) {
  22. // printf("Working on env::::");
  23. // print_environment(env);
  24. // printf("\n--------------------------------\n");
  25. visited.append(env);
  26. push_environment(env);
  27. defer {
  28. pop_environment();
  29. };
  30. for_hash_map(env->hm) {
  31. try_void fprintf(f,
  32. "#+latex: \\hrule\n"
  33. "#+html: <hr/>\n"
  34. "* =%s%s= \n"
  35. " :PROPERTIES:\n"
  36. " :UNNUMBERED: t\n"
  37. " :END:"
  38. ,prefix, Memory::get_c_str(((Lisp_Object*)key)->value.symbol));
  39. /*
  40. * sourcecodeLocation
  41. */
  42. if (value->sourceCodeLocation) {
  43. try_void fprintf(f, "\n - defined in :: =%s:%d:%d=",
  44. Memory::get_c_str(value->sourceCodeLocation->file),
  45. value->sourceCodeLocation->line,
  46. value->sourceCodeLocation->column);
  47. }
  48. /*
  49. * type
  50. */
  51. Lisp_Object_Type type = Memory::get_type(value);
  52. Lisp_Object* LOtype;
  53. Lisp_Object* type_expr = Memory::create_list(
  54. Memory::get_or_create_lisp_object_symbol("type"),
  55. value);
  56. try_void LOtype = eval_expr(type_expr);
  57. fprintf(f, "\n - type :: =");
  58. print(LOtype, true, f);
  59. fprintf(f, "=");
  60. /*
  61. * if printable value -> print it
  62. */
  63. switch (type) {
  64. case(Lisp_Object_Type::Nil):
  65. case(Lisp_Object_Type::T):
  66. case(Lisp_Object_Type::Number):
  67. case(Lisp_Object_Type::String):
  68. case(Lisp_Object_Type::Pair):
  69. case(Lisp_Object_Type::Symbol):
  70. case(Lisp_Object_Type::Keyword): {
  71. fprintf(f, "\n - value :: =");
  72. print(value, true, f);
  73. fprintf(f, "=");
  74. } break;
  75. default: break;
  76. }
  77. /*
  78. * if function then print arguments
  79. */
  80. if (type == Lisp_Object_Type::Function ||
  81. type == Lisp_Object_Type::CFunction)
  82. {
  83. Arguments* args =
  84. (type == Lisp_Object_Type::Function)
  85. ? &value->value.function->args
  86. : &value->value.cFunction->args;
  87. fprintf(f, "\n - arguments :: ");
  88. // if no args at all
  89. if (args->positional.symbols.next_index == 0 &&
  90. args->keyword.values.next_index == 0 &&
  91. !args->rest)
  92. {
  93. fprintf(f, "none.");
  94. } else {
  95. if (args->positional.symbols.next_index != 0) {
  96. fprintf(f, "\n - postitional :: ");
  97. fprintf(f, "=%s=", Memory::get_c_str(args->positional.symbols.data[0]->value.symbol));
  98. for (int i = 1; i < args->positional.symbols.next_index; ++i) {
  99. fprintf(f, ", =%s=", Memory::get_c_str(args->positional.symbols.data[i]->value.symbol));
  100. }
  101. }
  102. if (args->keyword.values.next_index != 0) {
  103. fprintf(f, "\n - keyword :: ");
  104. fprintf(f, "=%s=", Memory::get_c_str(args->keyword.keywords.data[0]->value.symbol));
  105. if (args->keyword.values.data[0]) {
  106. fprintf(f, " =(");
  107. print(args->keyword.values.data[0], true, f);
  108. fprintf(f, ")=");
  109. }
  110. for (int i = 1; i < args->keyword.values.next_index; ++i) {
  111. fprintf(f, ", =%s=", Memory::get_c_str(args->keyword.keywords.data[i]->value.symbol));
  112. if (args->keyword.values.data[i]) {
  113. fprintf(f, " =(");
  114. print(args->keyword.values.data[i], true, f);
  115. fprintf(f, ")=");
  116. }
  117. }
  118. }
  119. if (args->rest) {
  120. fprintf(f, "\n - rest :: =%s=", Memory::get_c_str(args->rest->value.symbol));
  121. }
  122. }
  123. }
  124. fprintf(f, "\n - docu :: ");
  125. if (value->docstring)
  126. fprintf(f, "\n #+BEGIN:\n%s\n #+END:\n",
  127. Memory::get_c_str(value->docstring));
  128. else
  129. fprintf(f, "none\n");
  130. }
  131. }
  132. for (int i = 0; i < env->parents.next_index; ++i) {
  133. try_void rec(rec, env->parents.data[i], prefix);
  134. }
  135. };
  136. print_this_env(print_this_env, get_current_environment(), (char*)"");
  137. }