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

659 строки
25 KiB

  1. Ast_Node* apply_arguments_to_function (Ast_Node* arguments, Ast_Node* function) {
  2. printf("calling function, yey\n");
  3. return create_ast_node_nil();
  4. }
  5. /**
  6. This parses the argument specification of funcitons into their
  7. Function struct. It dois this by allocating new
  8. positional_arguments, keyword_arguments and rest_argument and
  9. filling it in
  10. */
  11. void parse_argument_list(Ast_Node* arguments, Function* function) {
  12. // first init the fields
  13. function->positional_arguments = create_positional_argument_list(16);
  14. function->keyword_arguments = create_keyword_argument_list(16);
  15. function->rest_argument = nullptr;
  16. // okay let's try to read some positional arguments
  17. while (arguments->type == Ast_Node_Type_Pair) {
  18. if (arguments->value.pair->first->type == Ast_Node_Type_Keyword) {
  19. if (string_equal(arguments->value.pair->first->value.keyword->identifier, "keys") ||
  20. string_equal(arguments->value.pair->first->value.keyword->identifier, "rest"))
  21. break;
  22. else {
  23. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  24. return;
  25. }
  26. }
  27. if (arguments->value.pair->first->type != Ast_Node_Type_Symbol) {
  28. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  29. return;
  30. }
  31. // okay wow we found an actual symbol
  32. append_to_positional_argument_list(
  33. function->positional_arguments,
  34. arguments->value.pair->first->value.symbol->identifier);
  35. arguments = arguments->value.pair->rest;
  36. }
  37. // okay we are done with positional arguments, lets check for
  38. // keywords,
  39. if (arguments->type != Ast_Node_Type_Pair) {
  40. if (arguments->type != Ast_Node_Type_Nil)
  41. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  42. return;
  43. }
  44. if (arguments->value.pair->first->type == Ast_Node_Type_Keyword &&
  45. string_equal(arguments->value.pair->first->value.keyword->identifier, "keys"))
  46. {
  47. arguments = arguments->value.pair->rest;
  48. if (arguments->type != Ast_Node_Type_Pair ||
  49. arguments->value.pair->first->type != Ast_Node_Type_Symbol)
  50. {
  51. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  52. return;
  53. }
  54. while (arguments->type == Ast_Node_Type_Pair) {
  55. if (arguments->value.pair->first->type == Ast_Node_Type_Keyword) {
  56. if (string_equal(arguments->value.pair->first->value.keyword->identifier, "rest"))
  57. break;
  58. else {
  59. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  60. return;
  61. }
  62. }
  63. if (arguments->value.pair->first->type != Ast_Node_Type_Symbol) {
  64. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  65. return;
  66. }
  67. // we found a symbol (arguments->value.pair->first) for
  68. // the keyword args! Let's check if the next arguement is
  69. // :defaults-to
  70. Ast_Node* next = arguments->value.pair->rest;
  71. if (next->type == Ast_Node_Type_Pair &&
  72. next->value.pair->first->type == Ast_Node_Type_Keyword &&
  73. string_equal(next->value.pair->first->value.keyword->identifier,
  74. "defaults-to"))
  75. {
  76. // check if there is a next argument too, otherwise it
  77. // would be an error
  78. next = next->value.pair->rest;
  79. if (next->type == Ast_Node_Type_Pair) {
  80. append_to_keyword_argument_list(function->keyword_arguments,
  81. arguments->value.pair->first->value.symbol->identifier,
  82. next->value.pair->first);
  83. arguments = next->value.pair->rest;
  84. } else {
  85. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  86. return;
  87. }
  88. } else {
  89. // No :defaults-to, so just add it to the list
  90. append_to_keyword_argument_list(function->keyword_arguments,
  91. arguments->value.pair->first->value.symbol->identifier,
  92. nullptr);
  93. arguments = next;
  94. }
  95. }
  96. }
  97. // Now we are also done with keyword arguments, lets check for
  98. // if there is a rest argument
  99. if (arguments->type != Ast_Node_Type_Pair) {
  100. if (arguments->type != Ast_Node_Type_Nil)
  101. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  102. return;
  103. }
  104. if (arguments->value.pair->first->type == Ast_Node_Type_Keyword &&
  105. string_equal(arguments->value.pair->first->value.keyword->identifier, "rest"))
  106. {
  107. arguments = arguments->value.pair->rest;
  108. if (arguments->type != Ast_Node_Type_Pair ||
  109. arguments->value.pair->first->type != Ast_Node_Type_Symbol)
  110. {
  111. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  112. return;
  113. }
  114. function->rest_argument = arguments->value.pair->first->value.symbol->identifier;
  115. if (arguments->value.pair->rest->type != Ast_Node_Type_Nil) {
  116. create_error(Error_Type_Ill_Formed_Lambda_List, arguments);
  117. }
  118. } else {
  119. printf("this should not happen?");
  120. create_error(Error_Type_Unknown_Error, arguments);
  121. }
  122. }
  123. int list_length(Ast_Node* node) {
  124. if (node->type == Ast_Node_Type_Nil)
  125. return 0;
  126. if (node->type != Ast_Node_Type_Pair) {
  127. create_error(Error_Type_Type_Missmatch, node);
  128. return 0;
  129. }
  130. int len = 0;
  131. while (node->type == Ast_Node_Type_Pair) {
  132. ++len;
  133. node = node->value.pair->rest;
  134. if (node->type == Ast_Node_Type_Nil)
  135. return len;
  136. }
  137. create_error(Error_Type_Ill_Formed_List, node);
  138. return 0;
  139. }
  140. /**
  141. Copies a list, in that it creates a new list, however the items are
  142. the same as in the original (same pointers). This is needed to copy
  143. a list when evaluating a parameters list, but not wanting to change
  144. the parameters list. This happens if you have someting like:
  145. (define a 10)
  146. (define condition (quote (= a 10)))
  147. (eval condition)
  148. > 1.00000
  149. If we wouldn't copy the parameters list, after calling eval would
  150. be baked into the quoted list. So even after changing a, the result
  151. of (eval condition) would be 1.00000.
  152. **/
  153. Ast_Node* copy_list(Ast_Node* node) {
  154. // we don't copy immutables in here
  155. if (node->type != Ast_Node_Type_Pair) {
  156. return node;
  157. }
  158. Ast_Node* result = new(Ast_Node);
  159. result->type = Ast_Node_Type_Pair;
  160. result->value.pair = new(Pair);
  161. result->value.pair->first = copy_list(node->value.pair->first);
  162. result->value.pair->rest = copy_list(node->value.pair->rest);
  163. return result;
  164. }
  165. Ast_Node* eval_expr(Ast_Node* node, Environment* env);
  166. bool is_truthy (Ast_Node* expression, Environment* env);
  167. Parsed_Arguments* eval_and_parse_arguments(Ast_Node* arguments, Environment* env) {
  168. // TODO(Felix): if arguments is nil then maybe return a constant
  169. // Parsed_Arguments_nil?
  170. typedef enum {
  171. Expecting_Anything,
  172. Expecting_Keyword_Key,
  173. Expecting_Keyword_Value
  174. } Parse_State;
  175. Parse_State parse_state = Expecting_Anything;
  176. Parsed_Arguments* result = new(Parsed_Arguments);
  177. result->positional_arguments = create_Ast_Node_Array_List(16);
  178. result->keyword_keys = create_Ast_Node_Array_List(16);
  179. result->keyword_values = create_Ast_Node_Array_List(16);
  180. Ast_Node* current_head = copy_list(arguments);
  181. Ast_Node* current_evaluated_head;
  182. while (current_head->type == Ast_Node_Type_Pair) {
  183. try {
  184. current_evaluated_head = eval_expr(current_head->value.pair->first, env);
  185. }
  186. if (current_evaluated_head->type == Ast_Node_Type_Keyword) {
  187. if (parse_state == Expecting_Anything || Expecting_Keyword_Key) {
  188. append_to_Ast_Node_Array_List(result->keyword_keys, current_evaluated_head);
  189. parse_state = Expecting_Keyword_Value;
  190. } else {
  191. create_error(Error_Type_Ill_Formed_Arguments, arguments);
  192. return nullptr;
  193. }
  194. } else {
  195. if (parse_state == Expecting_Anything)
  196. append_to_Ast_Node_Array_List(result->positional_arguments, current_evaluated_head);
  197. else if (parse_state == Expecting_Keyword_Value) {
  198. append_to_Ast_Node_Array_List(result->keyword_values, current_evaluated_head);
  199. parse_state = Expecting_Keyword_Key;
  200. }
  201. else {
  202. create_error(Error_Type_Ill_Formed_Arguments, arguments);
  203. return nullptr;
  204. }
  205. }
  206. current_head = current_head->value.pair->rest;
  207. }
  208. return result;
  209. }
  210. Ast_Node* extract_keyword_value(char* keyword, Parsed_Arguments* args) {
  211. // NOTE(Felix): This will be a hashmap lookup later
  212. for (int i = 0; i < args->keyword_keys->next_index; ++i) {
  213. if (string_equal(args->keyword_keys->data[i]->value.keyword->identifier, keyword))
  214. return args->keyword_values->data[i];
  215. }
  216. return nullptr;
  217. }
  218. Ast_Node* eval_arguments(Ast_Node* arguments, Environment* env, int *out_arguments_length) {
  219. *out_arguments_length = 0;
  220. if (arguments->type == Ast_Node_Type_Nil) {
  221. return arguments;
  222. }
  223. // NOTE(Felix): Instead of copying and then mutating it, we could
  224. // just build the resulting list on the fly
  225. Ast_Node* evaluated_arguments = copy_list(arguments);
  226. Ast_Node* current_head = evaluated_arguments;
  227. while (current_head->type == Ast_Node_Type_Pair) {
  228. try {
  229. current_head->value.pair->first =
  230. eval_expr(current_head->value.pair->first, env);
  231. }
  232. current_head = current_head->value.pair->rest;
  233. ++(*out_arguments_length);
  234. }
  235. return evaluated_arguments;
  236. }
  237. Ast_Node* eval_expr(Ast_Node* node, Environment* env) {
  238. #define report_error(_type) { \
  239. create_error(_type, node); \
  240. return nullptr; \
  241. }
  242. if (error)
  243. return nullptr;
  244. Ast_Node* ret = new(Ast_Node);
  245. switch (node->type) {
  246. case Ast_Node_Type_Nil:
  247. ret->type = Ast_Node_Type_Nil;
  248. return ret;
  249. case Ast_Node_Type_Symbol:
  250. Ast_Node* symbol;
  251. try {
  252. symbol = lookup_symbol(node->value.symbol, env);
  253. }
  254. return symbol;
  255. case Ast_Node_Type_Number:
  256. case Ast_Node_Type_Keyword:
  257. case Ast_Node_Type_String:
  258. return node;
  259. case Ast_Node_Type_Pair: {
  260. Ast_Node* operator;
  261. try {
  262. operator = eval_expr(node->value.pair->first, env);
  263. }
  264. Ast_Node* arguments = node->value.pair->rest;
  265. int arguments_length;
  266. // check for special form
  267. if (operator->type == Ast_Node_Type_Built_In_Function) {
  268. switch (operator->value.built_in_function->type) {
  269. case Built_In_Lambda: {
  270. /*
  271. * (lambda ())
  272. * (lambda (x d) (+ 1 2) (- 1 2) (* 1 2))
  273. */
  274. try {
  275. arguments_length = list_length(arguments);
  276. }
  277. if (arguments_length == 0)
  278. report_error(Error_Type_Wrong_Number_Of_Arguments);
  279. Function* function = new(Function);
  280. // if parameters were specified
  281. if (arguments->value.pair->first->type != Ast_Node_Type_Nil) {
  282. try {
  283. assert_type(arguments->value.pair->first, Ast_Node_Type_Pair);
  284. }
  285. try {
  286. parse_argument_list(arguments->value.pair->first, function);
  287. }
  288. }
  289. arguments = arguments->value.pair->rest;
  290. // if there is a docstring, use it
  291. if (arguments->value.pair->first->type == Ast_Node_Type_String) {
  292. function->docstring = arguments->value.pair->first->value.string->value;
  293. arguments = arguments->value.pair->rest;
  294. }
  295. // we are now in the function body, just wrap it in an
  296. // implicit prog
  297. function->body = create_ast_node_pair(
  298. create_ast_node_built_in_function("prog"),
  299. arguments);
  300. Ast_Node* ret = new(Ast_Node);
  301. ret->type = Ast_Node_Type_Function;
  302. ret->value.function = function;
  303. return ret;
  304. }
  305. case Built_In_And: {
  306. bool result = true;
  307. while (arguments->type != Ast_Node_Type_Nil) {
  308. if (arguments->type != Ast_Node_Type_Pair) {
  309. report_error(Error_Type_Ill_Formed_List);
  310. }
  311. try {
  312. result &= is_truthy(arguments->value.pair->first, env);
  313. }
  314. arguments = arguments->value.pair->rest;
  315. if (!result) return create_ast_node_nil();
  316. }
  317. return create_ast_node_number(1);
  318. }
  319. case Built_In_Or: {
  320. bool result = false;
  321. while (arguments->type != Ast_Node_Type_Nil) {
  322. if (arguments->type != Ast_Node_Type_Pair) {
  323. report_error(Error_Type_Ill_Formed_List);
  324. }
  325. try {
  326. result |= is_truthy(arguments->value.pair->first, env);
  327. }
  328. arguments = arguments->value.pair->rest;
  329. if (result) return create_ast_node_number(1);;
  330. }
  331. return create_ast_node_nil();
  332. }
  333. case Built_In_Not: {
  334. try {
  335. arguments_length = list_length(arguments);
  336. }
  337. if (arguments_length != 1) {
  338. report_error(Error_Type_Wrong_Number_Of_Arguments);
  339. }
  340. bool truthy;
  341. try {
  342. truthy = is_truthy(arguments->value.pair->first, env);
  343. }
  344. if (truthy)
  345. return create_ast_node_nil();
  346. return create_ast_node_number(1);
  347. }
  348. case Built_In_If: {
  349. try {
  350. arguments_length = list_length(arguments);
  351. }
  352. if (arguments_length != 2 && arguments_length != 3) {
  353. report_error(Error_Type_Wrong_Number_Of_Arguments);
  354. }
  355. Ast_Node* condition = arguments->value.pair->first;
  356. Ast_Node* then_part = arguments->value.pair->rest;
  357. Ast_Node* else_part = then_part->value.pair->rest;
  358. bool truthy;
  359. try {
  360. truthy = is_truthy(condition, env);
  361. }
  362. Ast_Node* result;
  363. if (truthy)
  364. try{
  365. result = eval_expr(then_part->value.pair->first, env);
  366. }
  367. else if (arguments_length == 3)
  368. try {
  369. result = eval_expr(else_part->value.pair->first, env);
  370. }
  371. else return create_ast_node_nil();
  372. return result;
  373. }
  374. case Built_In_Quote: {
  375. arguments_length = list_length(arguments);
  376. if (arguments_length != 1) {
  377. report_error(Error_Type_Wrong_Number_Of_Arguments);
  378. }
  379. return arguments->value.pair->first;
  380. }
  381. case Built_In_Define: {
  382. try {
  383. arguments_length = list_length(arguments);
  384. }
  385. if (arguments_length != 2) {
  386. report_error(Error_Type_Wrong_Number_Of_Arguments);
  387. }
  388. Ast_Node* symbol = arguments->value.pair->first;
  389. if (symbol->type != Ast_Node_Type_Symbol)
  390. report_error(Error_Type_Type_Missmatch);
  391. Ast_Node* value = arguments->value.pair->rest->value.pair->first;
  392. try {
  393. value = eval_expr(value, env);
  394. }
  395. define_symbol(symbol, value, env);
  396. return value;
  397. }
  398. }
  399. // okay it is not a special form, so in any case we want
  400. // to evaluate the arguments; eval_arguments will also tell
  401. // us the arguments_length.
  402. Ast_Node* evaluated_arguments;
  403. Parsed_Arguments* evaluated_and_parsed_arguments;
  404. try {
  405. evaluated_arguments = eval_arguments(arguments, env, &arguments_length);
  406. evaluated_and_parsed_arguments = eval_and_parse_arguments(arguments, env);
  407. }
  408. switch (operator->value.built_in_function->type) {
  409. case Built_In_Addition: {
  410. return built_in_add(evaluated_and_parsed_arguments);
  411. }
  412. case Built_In_Subtraction: {
  413. return built_in_substract(evaluated_and_parsed_arguments);
  414. }
  415. case Built_In_Multiplication: {
  416. return built_in_multiply(evaluated_and_parsed_arguments);
  417. }
  418. case Built_In_Division: {
  419. return built_in_divide(evaluated_and_parsed_arguments);
  420. }
  421. case Built_In_Equal: {
  422. return built_in_equals(evaluated_and_parsed_arguments);
  423. }
  424. case Built_In_Pair: {
  425. if (arguments_length != 2) {
  426. report_error(Error_Type_Wrong_Number_Of_Arguments);
  427. }
  428. return create_ast_node_pair(evaluated_arguments->value.pair->first, evaluated_arguments->value.pair->rest->value.pair->first);
  429. }
  430. case Built_In_First: {
  431. if (arguments_length != 1) {
  432. report_error(Error_Type_Wrong_Number_Of_Arguments);
  433. }
  434. if (evaluated_arguments->value.pair->first->type == Ast_Node_Type_Nil)
  435. return create_ast_node_nil();
  436. if (evaluated_arguments->value.pair->first->type != Ast_Node_Type_Pair)
  437. report_error(Error_Type_Type_Missmatch);
  438. return evaluated_arguments->value.pair->first->value.pair->first;
  439. }
  440. case Built_In_Rest: {
  441. if (arguments_length != 1) {
  442. report_error(Error_Type_Wrong_Number_Of_Arguments);
  443. }
  444. if (evaluated_arguments->value.pair->first->type == Ast_Node_Type_Nil)
  445. return create_ast_node_nil();
  446. if (evaluated_arguments->value.pair->first->type != Ast_Node_Type_Pair)
  447. report_error(Error_Type_Type_Missmatch);
  448. return evaluated_arguments->value.pair->first->value.pair->rest;
  449. }
  450. case Built_In_Eval: {
  451. if (arguments_length != 1) {
  452. report_error(Error_Type_Wrong_Number_Of_Arguments);
  453. }
  454. Ast_Node* result;
  455. try {
  456. result = eval_expr(evaluated_arguments->value.pair->first, env);
  457. }
  458. return result;
  459. }
  460. case Built_In_Prog: {
  461. if (evaluated_arguments->type == Ast_Node_Type_Nil)
  462. return evaluated_arguments;
  463. // skip to the last evaluated operand and return it,
  464. // we use eval_arguments here instead of doing it
  465. // manually, because we want to increase code reuse,
  466. // but at the cost that we have to find the end of the
  467. // list again
  468. while (evaluated_arguments->value.pair->rest->type == Ast_Node_Type_Pair) {
  469. evaluated_arguments = evaluated_arguments->value.pair->rest;
  470. }
  471. return evaluated_arguments->value.pair->first;
  472. }
  473. case Built_In_List: {
  474. return evaluated_arguments;
  475. }
  476. case Built_In_Print: {
  477. /* if (arguments_length != 1) { */
  478. /* report_error(Error_Type_Wrong_Number_Of_Arguments); */
  479. /* } */
  480. /* print(evaluated_arguments->value.pair->first); */
  481. /* printf("\n"); */
  482. /* return arguments->value.pair->first; */
  483. if (evaluated_and_parsed_arguments->positional_arguments->next_index == 0) {
  484. report_error(Error_Type_Wrong_Number_Of_Arguments);
  485. }
  486. char* sep = " ";
  487. char* end = "\n";
  488. Ast_Node* temp = extract_keyword_value("sep", evaluated_and_parsed_arguments);
  489. if (temp) {
  490. try {
  491. assert_type(temp, Ast_Node_Type_String);
  492. }
  493. sep = temp->value.string->value;
  494. }
  495. temp = extract_keyword_value("end", evaluated_and_parsed_arguments);
  496. if (temp) {
  497. try {
  498. assert_type(temp, Ast_Node_Type_String);
  499. }
  500. end = temp->value.string->value;
  501. }
  502. print(*evaluated_and_parsed_arguments->positional_arguments->data);
  503. for (int i = 1; i < evaluated_and_parsed_arguments->positional_arguments->next_index; ++i) {
  504. printf("%s", sep);
  505. print(*(evaluated_and_parsed_arguments->positional_arguments->data+i));
  506. }
  507. printf("%s", end);
  508. // return last positional argument
  509. return *(evaluated_and_parsed_arguments->positional_arguments->data+
  510. (evaluated_and_parsed_arguments->positional_arguments->next_index-1));
  511. }
  512. case Built_In_Read: {
  513. if (arguments_length > 1) {
  514. report_error(Error_Type_Wrong_Number_Of_Arguments);
  515. }
  516. if (arguments_length == 1) {
  517. Ast_Node* prompt = evaluated_arguments->value.pair->first;
  518. if (prompt->type == Ast_Node_Type_String)
  519. printf("%s", prompt->value.string->value);
  520. else
  521. print(evaluated_arguments->value.pair->first);
  522. }
  523. char* line = read_line();
  524. return create_ast_node_string(line, (int)strlen(line));
  525. }
  526. case Built_In_Type: {
  527. if (arguments_length != 1) {
  528. report_error(Error_Type_Wrong_Number_Of_Arguments);
  529. }
  530. Ast_Node_Type type = evaluated_arguments->value.pair->first->type;
  531. switch (type) {
  532. case Ast_Node_Type_Built_In_Function: return create_ast_node_keyword("built-in-function");
  533. case Ast_Node_Type_Function: return create_ast_node_keyword("dynamic-function");
  534. case Ast_Node_Type_Keyword: return create_ast_node_keyword("keyword");
  535. case Ast_Node_Type_Nil: return create_ast_node_keyword("nil");
  536. case Ast_Node_Type_Number: return create_ast_node_keyword("number");
  537. case Ast_Node_Type_Pair: return create_ast_node_keyword("pair");
  538. case Ast_Node_Type_String: return create_ast_node_keyword("string");
  539. case Ast_Node_Type_Symbol: return create_ast_node_keyword("symbol");
  540. }
  541. }
  542. case Built_In_Exit: {
  543. if (arguments_length > 1) {
  544. report_error(Error_Type_Wrong_Number_Of_Arguments);
  545. }
  546. if (arguments_length == 1) {
  547. Ast_Node* error_code = evaluated_arguments->value.pair->first;
  548. if (error_code->type != Ast_Node_Type_Number)
  549. report_error(Error_Type_Type_Missmatch);
  550. exit((int)error_code->value.number->value);
  551. }
  552. exit(0);
  553. }
  554. default:
  555. report_error(Error_Type_Not_Yet_Implemented);
  556. }
  557. }
  558. // assume it's lambda function and evaluate the arguments
  559. arguments = eval_arguments(arguments, env, &arguments_length);
  560. if (operator->type == Ast_Node_Type_Function) {
  561. Ast_Node* result;
  562. try {
  563. result = apply_arguments_to_function(arguments, operator);
  564. }
  565. return result;
  566. }
  567. }
  568. default:
  569. report_error(Error_Type_Not_A_Function);
  570. }
  571. #undef report_error
  572. }
  573. bool is_truthy (Ast_Node* expression, Environment* env) {
  574. Ast_Node* result;
  575. try {
  576. result = eval_expr(expression, env);
  577. }
  578. if (result->type == Ast_Node_Type_Nil)
  579. return false;
  580. return true;
  581. }