Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

242 řádky
7.1 KiB

  1. #pragma once
  2. // #include <functional>
  3. #include "ftb/arraylist.hpp"
  4. #include "ftb/hashmap.hpp"
  5. namespace Slime {
  6. struct Lisp_Object;
  7. struct String;
  8. struct Environment;
  9. enum struct Thread_Type {
  10. Main,
  11. GarbageCollection
  12. };
  13. enum struct Lisp_Object_Type {
  14. Nil,
  15. T,
  16. Symbol,
  17. Keyword,
  18. Number,
  19. String,
  20. Pair,
  21. Vector,
  22. Continuation,
  23. Pointer,
  24. HashMap,
  25. // OwningPointer,
  26. Function,
  27. CFunction,
  28. };
  29. enum class Lisp_Object_Flags
  30. {
  31. // bits 1 to 5 (including) will be reserved for the type
  32. Already_Garbage_Collected = 1 << 5,
  33. Under_Construction = 1 << 6,
  34. };
  35. enum struct Function_Type {
  36. Lambda,
  37. Macro
  38. };
  39. enum struct Log_Level {
  40. None,
  41. Critical,
  42. Warning,
  43. Info,
  44. Debug,
  45. };
  46. struct Continuation {
  47. Array_List<Lisp_Object*> call_stack;
  48. Array_List<Environment*> envi_stack;
  49. };
  50. struct String {
  51. int length;
  52. char data;
  53. };
  54. struct Source_Code_Location {
  55. String* file;
  56. int line;
  57. int column;
  58. };
  59. struct Symbol {
  60. String* identifier;
  61. u64 hash;
  62. };
  63. struct Keyword {
  64. String* identifier;
  65. u64 hash;
  66. };
  67. struct Pair {
  68. Lisp_Object* first;
  69. Lisp_Object* rest;
  70. };
  71. struct Vector {
  72. int length;
  73. Lisp_Object* data;
  74. };
  75. struct Positional_Arguments {
  76. Array_List<Lisp_Object*> symbols;
  77. };
  78. struct Keyword_Arguments {
  79. // Array of Pointers to Lisp_Object<Keyword>
  80. Array_List<Lisp_Object*> keywords;
  81. // NOTE(Felix): values[i] will be nullptr if no defalut value was
  82. // declared for key identifiers[i]
  83. Array_List<Lisp_Object*> values;
  84. };
  85. struct Arguments {
  86. Positional_Arguments positional;
  87. Keyword_Arguments keyword;
  88. // NOTE(Felix): rest_argument will be nullptr if no rest argument
  89. // is declared otherwise its a symbol
  90. Lisp_Object* rest;
  91. };
  92. struct Environment {
  93. Array_List<Environment*> parents;
  94. Hash_Map<void*, Lisp_Object*> hm;
  95. };
  96. struct Function {
  97. Function_Type type;
  98. Arguments args;
  99. Lisp_Object* body; // maybe implicit begin
  100. Environment* parent_environment; // we are doing closures now!!
  101. };
  102. struct cFunction {
  103. std::function<Lisp_Object* ()> body;
  104. Arguments args;
  105. bool is_special_form;
  106. };
  107. struct Lisp_Object {
  108. Source_Code_Location* sourceCodeLocation;
  109. u64 flags;
  110. Lisp_Object* userType; // keyword
  111. String* docstring;
  112. union {
  113. Symbol symbol; // used for symbols and keywords
  114. double number;
  115. String* string;
  116. Pair pair;
  117. Vector vector;
  118. Function function;
  119. cFunction* cFunction;
  120. void* pointer;
  121. Continuation continuation;
  122. Hash_Map<Lisp_Object*, Lisp_Object*> hashMap;
  123. } value;
  124. };
  125. struct Error {
  126. Lisp_Object* position;
  127. // type has to be a keyword
  128. Lisp_Object* type;
  129. String* message;
  130. };
  131. const wchar_t* char_to_wchar(const char* c);
  132. char* read_entire_file(char* filename);
  133. void add_to_load_path(const char*);
  134. bool lisp_object_equal(Lisp_Object*,Lisp_Object*);
  135. Lisp_Object* built_in_load(String*);
  136. Lisp_Object* built_in_import(String*);
  137. void delete_error();
  138. void create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, const char* format, ...);
  139. void create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, String* message);
  140. void create_error(Lisp_Object* type, const char* message, const char* c_file_name, int c_file_line);
  141. Lisp_Object* eval_arguments(Lisp_Object*);
  142. Lisp_Object* eval_expr(Lisp_Object*);
  143. bool is_truthy (Lisp_Object*);
  144. int list_length(Lisp_Object*);
  145. void load_built_ins_into_environment();
  146. void create_arguments_from_lambda_list_and_inject(Lisp_Object* formal_arguments, Lisp_Object* function);
  147. Lisp_Object* lookup_symbol(Lisp_Object* symbol, Environment*);
  148. void define_symbol(Lisp_Object* symbol, Lisp_Object* value);
  149. void print(Lisp_Object* node, bool print_repr = false, FILE* file = stdout);
  150. void print_environment(Environment*);
  151. bool run_all_tests();
  152. inline Environment* get_root_environment();
  153. inline Environment* get_current_environment();
  154. inline void push_environment(Environment*);
  155. inline void pop_environment();
  156. const char* Lisp_Object_Type_to_string(Lisp_Object_Type type);
  157. void visualize_lisp_machine();
  158. void generate_docs(String* path);
  159. void log_error();
  160. namespace Memory {
  161. extern Lisp_Object* nil;
  162. extern Lisp_Object* t;
  163. Environment* create_child_environment(Environment* parent);
  164. Environment* create_built_ins_environment();
  165. Lisp_Object* create_lisp_object_cfunction(bool is_special);
  166. Lisp_Object* get_or_create_lisp_object_keyword(const char* identifier);
  167. inline Lisp_Object_Type get_type(Lisp_Object* node);
  168. void init(int);
  169. char* get_c_str(String*);
  170. void free_everything();
  171. String* create_string(const char*);
  172. Lisp_Object* create_lisp_object_number(double);
  173. Lisp_Object* create_lisp_object_pointer(void*);
  174. Lisp_Object* get_or_create_lisp_object_symbol(String* identifier);
  175. Lisp_Object* get_or_create_lisp_object_symbol(const char*);
  176. Lisp_Object* get_or_create_lisp_object_keyword(String* identifier);
  177. Lisp_Object* get_or_create_lisp_object_keyword(const char*);
  178. Lisp_Object* create_lisp_object_string(const char*);
  179. Lisp_Object* create_list(Lisp_Object*);
  180. Lisp_Object* create_list(Lisp_Object*,Lisp_Object*);
  181. Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*);
  182. Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*);
  183. Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*);
  184. Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*);
  185. }
  186. namespace Parser {
  187. // extern Environment* environment_for_macros;
  188. extern String* standard_in;
  189. extern String* parser_file;
  190. extern int parser_line;
  191. extern int parser_col;
  192. Lisp_Object* parse_single_expression(char* text);
  193. Lisp_Object* parse_single_expression(wchar_t* text);
  194. }
  195. namespace Globals {
  196. extern char* bin_path;
  197. extern Log_Level log_level;
  198. extern Array_List<void*> load_path;
  199. namespace Current_Execution {
  200. extern Array_List<Lisp_Object*> call_stack;
  201. extern Array_List<Environment*> envi_stack;
  202. }
  203. extern Error* error;
  204. extern bool breaking_on_errors;
  205. }
  206. }