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

762 строки
28 KiB

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