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

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