You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

232 rivejä
6.8 KiB

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