lisp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "lisp.h"
  18. struct file_list
  19. {
  20. struct file_list* next;
  21. FILE* file;
  22. };
  23. /* Prototypes */
  24. struct cell* eval(struct cell* exp, struct cell* env);
  25. void init_sl3();
  26. int Readline(FILE* source_file, char* temp);
  27. struct cell* parse(char* program, int size);
  28. void writeobj(FILE *ofp, struct cell* op);
  29. void garbage_init(int number_of_cells);
  30. void garbage_collect();
  31. /* Read Eval Print Loop*/
  32. int REPL(FILE* in, FILE *out)
  33. {
  34. int read;
  35. input = in;
  36. char* message = calloc(MAX_STRING + 2, sizeof(char));
  37. read = Readline(in, message);
  38. if(0 == read)
  39. {
  40. return TRUE;
  41. }
  42. struct cell* temp = parse(message, read);
  43. current = temp;
  44. temp = eval(temp, top_env);
  45. writeobj(out, temp);
  46. current = nil;
  47. if(echo) fputc('\n', out);
  48. return FALSE;
  49. }
  50. void recursively_evaluate(struct file_list* a)
  51. {
  52. if(NULL == a) return;
  53. recursively_evaluate(a->next);
  54. int Reached_EOF = FALSE;
  55. while(!Reached_EOF)
  56. {
  57. garbage_collect();
  58. Reached_EOF = REPL(a->file, console_output);
  59. }
  60. }
  61. /*** Main Driver ***/
  62. int main(int argc, char **argv)
  63. {
  64. int number_of_cells = 1000000;
  65. file_output = fopen("/dev/null", "w");
  66. console_output = stdout;
  67. struct file_list* essential = NULL;
  68. struct file_list* new;
  69. int i = 1;
  70. while(i <= argc)
  71. {
  72. if(NULL == argv[i])
  73. {
  74. i = i + 1;
  75. }
  76. else if(match(argv[i], "-c") || match(argv[i], "--console"))
  77. {
  78. console_output = fopen(argv[i + 1], "w");
  79. if(NULL == console_output)
  80. {
  81. file_print("The file: ", stderr);
  82. file_print(argv[i + 1], stderr);
  83. file_print(" does not appear writable\n", stderr);
  84. exit(EXIT_FAILURE);
  85. }
  86. i = i + 2;
  87. }
  88. else if(match(argv[i], "-f") || match(argv[i], "--file"))
  89. {
  90. new = calloc(1, sizeof(struct file_list));
  91. new->file = fopen(argv[i + 1], "r");
  92. if(NULL == new->file)
  93. {
  94. file_print("The file: ", stderr);
  95. file_print(argv[i + 1], stderr);
  96. file_print(" does not appear readable\n", stderr);
  97. exit(EXIT_FAILURE);
  98. }
  99. new->next = essential;
  100. essential = new;
  101. i = i + 2;
  102. }
  103. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  104. {
  105. file_print("Usage: ", stdout);
  106. file_print(argv[0], stdout);
  107. file_print(" -f FILENAME1 {-f FILENAME2}\n", stdout);
  108. exit(EXIT_SUCCESS);
  109. }
  110. else if(match(argv[i], "-m") || match(argv[i], "--memory"))
  111. {
  112. number_of_cells = numerate_string(argv[i + 1]);
  113. i = i + 2;
  114. }
  115. else if(match(argv[i], "-o") || match(argv[i], "--output"))
  116. {
  117. file_output = fopen(argv[i + 1], "w");
  118. if(NULL == file_output)
  119. {
  120. file_print("The file: ", stderr);
  121. file_print(argv[i + 1], stderr);
  122. file_print(" does not appear writable\n", stderr);
  123. exit(EXIT_FAILURE);
  124. }
  125. i = i + 2;
  126. }
  127. else if(match(argv[i], "-v") || match(argv[i], "--version"))
  128. {
  129. file_print("Slow_Lisp 0.1\n", stdout);
  130. exit(EXIT_SUCCESS);
  131. }
  132. else
  133. {
  134. file_print("Unknown option\n", stderr);
  135. exit(EXIT_FAILURE);
  136. }
  137. }
  138. /* Our most important initializations */
  139. garbage_init(number_of_cells);
  140. init_sl3();
  141. int Reached_EOF;
  142. echo = TRUE;
  143. recursively_evaluate(essential);
  144. Reached_EOF = FALSE;
  145. while(!Reached_EOF)
  146. {
  147. garbage_collect();
  148. Reached_EOF = REPL(stdin, stdout);
  149. }
  150. fclose(file_output);
  151. if (console_output != stdout)
  152. {
  153. fclose(console_output);
  154. }
  155. while(NULL != essential)
  156. {
  157. fclose(essential->file);
  158. essential = essential -> next;
  159. }
  160. return 0;
  161. }