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

254 строки
7.9 KiB

  1. struct Ast_Node;
  2. // #define define_array_list(type, name) \
  3. // struct name##_Array_List { \
  4. // type* data; \
  5. // int length; \
  6. // int next_index; \
  7. // }; \
  8. // \
  9. // \
  10. // void append_to_##name##_array_list(name##_Array_List* arraylist, type element) { \
  11. // if (arraylist->next_index == arraylist->length) { \
  12. // arraylist->length *= 2; \
  13. // arraylist->data = \
  14. // (type*)realloc(arraylist->data, arraylist->length * sizeof(type)); \
  15. // } \
  16. // arraylist->data[arraylist->next_index++] = element; \
  17. // } \
  18. // \
  19. // \
  20. // name##_Array_List* create_##name##_array_list(int initial_capacity) { \
  21. // name##_Array_List* ret = new(name##_Array_List); \
  22. // ret->data = (type*)malloc(initial_capacity * sizeof(type)); \
  23. // ret->next_index = 0; \
  24. // ret->length = initial_capacity; \
  25. // return ret; \
  26. // }
  27. define_array_list(Ast_Node*, Ast_Node);
  28. enum struct Ast_Node_Type {
  29. Nil,
  30. T,
  31. Symbol,
  32. Keyword,
  33. Number,
  34. String,
  35. Pair,
  36. Function,
  37. CFunction,
  38. };
  39. char* Ast_Node_Type_to_string(Ast_Node_Type type) {
  40. switch (type) {
  41. case(Ast_Node_Type::Nil): return "nil";
  42. case(Ast_Node_Type::T): return "t";
  43. case(Ast_Node_Type::Number): return "number";
  44. case(Ast_Node_Type::String): return "string";
  45. case(Ast_Node_Type::Symbol): return "symbol";
  46. case(Ast_Node_Type::Keyword): return "keyword";
  47. case(Ast_Node_Type::Function): return "function";
  48. case(Ast_Node_Type::CFunction): return "C-function";
  49. case(Ast_Node_Type::Pair): return "pair";
  50. }
  51. return "unknown";
  52. }
  53. struct Symbol {
  54. char* identifier;
  55. };
  56. struct Keyword {
  57. char* identifier;
  58. };
  59. struct Number {
  60. double value;
  61. };
  62. struct String {
  63. char* value;
  64. int length;
  65. };
  66. struct Pair {
  67. struct Ast_Node* first;
  68. struct Ast_Node* rest;
  69. };
  70. struct Positional_Arguments {
  71. // TODO(Felix) use Ast_Node_symbols here instead, so we don't have
  72. // to convert them to strings and back to symbols
  73. char** identifiers;
  74. int next_index;
  75. int length;
  76. };
  77. struct Keyword_Arguments {
  78. char** identifiers;
  79. // values[i] will be nullptr if no defalut value was declared for
  80. // key identifiers[i]
  81. Ast_Node_Array_List* values;
  82. int next_index;
  83. int length;
  84. };
  85. /* Ast_Node_Array_List* create_Ast_Node_Array_List(int initial_length); */
  86. /* void append_to_Ast_Node_Array_List(Ast_Node_Array_List* list, struct Ast_Node* node); */
  87. Positional_Arguments* create_positional_argument_list(int initial_capacity) {
  88. Positional_Arguments* ret = new(Positional_Arguments);
  89. ret->identifiers = (char**)malloc(initial_capacity * sizeof(char*));
  90. ret->next_index = 0;
  91. ret->length = initial_capacity;
  92. return ret;
  93. }
  94. void append_to_positional_argument_list(Positional_Arguments* args, char* identifier) {
  95. if (args->next_index == args->length) {
  96. args->length *= 2;
  97. args->identifiers = (char**)realloc(args->identifiers, args->length * sizeof(char*));
  98. }
  99. args->identifiers[args->next_index++] = identifier;
  100. }
  101. Keyword_Arguments* create_keyword_argument_list(int initial_capacity) {
  102. Keyword_Arguments* ret = new(Keyword_Arguments);
  103. ret->identifiers = (char**)malloc(initial_capacity * sizeof(char*));
  104. ret->values = create_Ast_Node_array_list(initial_capacity);
  105. ret->next_index = 0;
  106. ret->length = initial_capacity;
  107. return ret;
  108. }
  109. void append_to_keyword_argument_list(Keyword_Arguments* args,
  110. char* identifier,
  111. struct Ast_Node* default_value)
  112. {
  113. if (args->next_index == args->length) {
  114. args->length *= 2;
  115. args->identifiers = (char**)realloc(args->identifiers, args->length * sizeof(char*));
  116. }
  117. args->identifiers[args->next_index++] = identifier;
  118. append_to_Ast_Node_array_list(args->values, default_value);
  119. }
  120. struct Environment;
  121. struct Function {
  122. bool is_special_form;
  123. char* docstring;
  124. Positional_Arguments* positional_arguments;
  125. Keyword_Arguments* keyword_arguments;
  126. // rest_argument will be nullptr if no rest argument is declared
  127. char* rest_argument;
  128. Ast_Node* body; // implicit prog
  129. Environment* parent_environment; // we are doing closures now!!
  130. };
  131. struct CFunction {
  132. std::function<Ast_Node*(Ast_Node*, Environment*)> function;
  133. };
  134. struct Ast_Node {
  135. Source_Code_Location* sourceCodeLocation;
  136. Ast_Node_Type type;
  137. union {
  138. Symbol* symbol;
  139. Keyword* keyword;
  140. Number* number;
  141. String* string;
  142. Pair* pair;
  143. Function* function;
  144. CFunction* cfunction;
  145. } value;
  146. };
  147. struct Parsed_Arguments {
  148. Ast_Node_Array_List* positional_arguments;
  149. // TODO(Felix): Really use hashmap (keyword[sting] ->
  150. // value[Ast_Node*]) here
  151. Ast_Node_Array_List* keyword_keys;
  152. Ast_Node_Array_List* keyword_values;
  153. };
  154. Ast_Node* create_ast_node() {
  155. Ast_Node* node = new(Ast_Node);
  156. node->sourceCodeLocation = nullptr;
  157. return node;
  158. }
  159. Ast_Node* create_ast_node_nil() {
  160. Ast_Node* node = create_ast_node();
  161. node->type = Ast_Node_Type::Nil;
  162. node->value.pair = nullptr;
  163. return node;
  164. }
  165. Ast_Node* create_ast_node_t() {
  166. Ast_Node* node = create_ast_node();
  167. node->type = Ast_Node_Type::T;
  168. node->value.pair = nullptr;
  169. return node;
  170. }
  171. Ast_Node* create_ast_node_number(double number) {
  172. Ast_Node* node = create_ast_node();
  173. node->type = Ast_Node_Type::Number;
  174. node->value.number = new(Number);
  175. node->value.number->value = number;
  176. return node;
  177. }
  178. Ast_Node* create_ast_node_string(char* str, int length) {
  179. Ast_Node* node = create_ast_node();
  180. node->type = Ast_Node_Type::String;
  181. node->value.string = new(String);
  182. node->value.string->value = str;
  183. node->value.string->length = length;
  184. return node;
  185. }
  186. Ast_Node* create_ast_node_symbol(char* identifier) {
  187. Ast_Node* node = create_ast_node();
  188. node->type = Ast_Node_Type::Symbol;
  189. node->value.symbol = new(Symbol);
  190. node->value.symbol->identifier = identifier;
  191. return node;
  192. }
  193. Ast_Node* create_ast_node_keyword(char* keyword) {
  194. Ast_Node* node = create_ast_node();
  195. node->type = Ast_Node_Type::Keyword;
  196. node->value.keyword = new(Keyword);
  197. node->value.keyword->identifier = keyword;
  198. return node;
  199. }
  200. Ast_Node* create_ast_node_cfunction(std::function<Ast_Node*(Ast_Node*, Environment*)> function) {
  201. Ast_Node* node = create_ast_node();
  202. node->type = Ast_Node_Type::CFunction;
  203. node->value.cfunction = new(CFunction);
  204. node->value.cfunction->function = function;
  205. return node;
  206. }
  207. Ast_Node* create_ast_node_pair(Ast_Node* first, Ast_Node* rest) {
  208. Ast_Node* node = create_ast_node();
  209. node->type = Ast_Node_Type::Pair;
  210. node->value.pair = new(Pair);
  211. node->value.pair->first = first;
  212. node->value.pair->rest = rest;
  213. return node;
  214. }
  215. Ast_Node* copy_ast_node(Ast_Node* n) {
  216. Ast_Node* target = create_ast_node();
  217. *target = *n;
  218. return target;
  219. }