mes.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019 Jeremiah Orians
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "mes.h"
  22. // CONSTANT MAX_STRING 4096
  23. #define MAX_STRING 4096
  24. char* message;
  25. /* Prototypes */
  26. FILE* open_file(char* name, char* mode);
  27. char* env_lookup(char* token, char** envp);
  28. char* string_append(char* a, char* b);
  29. int Readline(FILE* source_file, char* temp, unsigned max_string);
  30. struct cell* expand_macros(struct cell* exps);
  31. struct cell* make_file(FILE* a, char* name);
  32. struct cell* parse(char* program, int size);
  33. struct cell* pop_cell();
  34. void eval(struct cell* exp, struct cell* env);
  35. void garbage_collect();
  36. void garbage_init(int number_of_cells);
  37. void init_sl3();
  38. void push_cell(struct cell* a);
  39. void reset_block(char* a);
  40. void writeobj(struct cell* output_file, struct cell* op, int write_p);
  41. /* Deal with common errors */
  42. void require(int bool, char* error)
  43. {
  44. if(!bool)
  45. {
  46. file_print(error, stderr);
  47. exit(EXIT_FAILURE);
  48. }
  49. }
  50. /* Read Eval Print Loop*/
  51. int REPL()
  52. {
  53. int read;
  54. /* Read S-Expression block */
  55. reset_block(message);
  56. read = Readline(__stdin->file, message, MAX_STRING);
  57. if(0 == read) return TRUE;
  58. /* Process S-expression */
  59. R0 = parse(message, read);
  60. g_env = top_env;
  61. /* perform macro processing here */
  62. R0 = expand_macros(R0);
  63. /* now to eval what results */
  64. eval(R0, g_env);
  65. /* Print */
  66. if(match("/dev/stdin", __stdin->string) && (NULL != R0) && (cell_unspecified != R0))
  67. {
  68. file_print("$R0 = ", __stdout->file);
  69. writeobj(__stdout, R0, TRUE);
  70. fputc('\n', __stdout->file);
  71. }
  72. /* Display user friendly prompt */
  73. if(match("/dev/stdin", __stdin->string))
  74. {
  75. file_print("REPL: ", __stdout->file);
  76. }
  77. return FALSE;
  78. }
  79. /* The foundational loader */
  80. struct cell* load_file(char* s)
  81. {
  82. int Reached_EOF = FALSE;
  83. push_cell(__stdin);
  84. /* Punt on bad inputs */
  85. FILE* f = fopen(s, "r");
  86. if(NULL == f) return cell_unspecified;
  87. __stdin = make_file(f, s);
  88. while(!Reached_EOF)
  89. {
  90. garbage_collect();
  91. Reached_EOF = REPL();
  92. }
  93. __stdin = pop_cell();
  94. return cell_t;
  95. }
  96. int main(int argc, char **argv, char** envp)
  97. {
  98. __envp = envp;
  99. __argv = argv;
  100. __argc = argc;
  101. stack_pointer = 0;
  102. int arena = numerate_string(env_lookup("MES_ARENA", envp));
  103. if(0 == arena) arena = 1000000;
  104. int stack = numerate_string(env_lookup("MES_STACK", envp));
  105. if(0 == stack) stack = 100000;
  106. /* Our most important initializations */
  107. memory_block = calloc(MAX_STRING, sizeof(char));
  108. message = calloc(MAX_STRING + 2, sizeof(char));
  109. garbage_init(arena);
  110. init_sl3();
  111. g_stack = calloc(stack, sizeof(struct cell*));
  112. /* Initialization: stdin, stdout and stderr */
  113. __stdin = make_file(stdin, "/dev/stdin");
  114. __stdout = make_file(stdout, "/dev/stdout");
  115. __stderr = make_file(stderr, "/dev/stderr");
  116. char* testing = env_lookup("MES_CORE", envp);
  117. if(NULL != testing)
  118. {
  119. int i = 1;
  120. while(i <= argc)
  121. {
  122. if(NULL == argv[i])
  123. {
  124. i = i + 1;
  125. }
  126. else if(match(argv[i], "--boot"))
  127. {
  128. load_file(argv[i + 1]);
  129. i = i + 2;
  130. }
  131. else if(match(argv[i], "-f") || match(argv[i], "--file"))
  132. {
  133. load_file(argv[i + 1]);
  134. i = i + 2;
  135. }
  136. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  137. {
  138. file_print("Usage: ", stdout);
  139. file_print(argv[0], stdout);
  140. file_print(" [--boot boot.scm] [-f|--file file.scm] [-h|--help]\n", stdout);
  141. i = i + 1;
  142. exit(EXIT_SUCCESS);
  143. }
  144. else
  145. {
  146. file_print("Received unknown option: ", stderr);
  147. file_print(argv[i], stderr);
  148. file_print("\nUsage: ", stderr);
  149. file_print(argv[0], stderr);
  150. file_print(" [--boot boot.scm] [-f|--file file.scm] [-h|--help]\nAborting\n", stderr);
  151. exit(EXIT_FAILURE);
  152. }
  153. }
  154. file_print("REPL: ", __stdout->file);
  155. load_file("/dev/stdin");
  156. file_print("\nexiting, have a nice day!\n", __stdout->file);
  157. exit(EXIT_SUCCESS);
  158. }
  159. else
  160. {
  161. char* mes_boot = env_lookup("MES_BOOT", envp);
  162. if(NULL == mes_boot)
  163. {
  164. mes_boot = "boot-0.scm";
  165. }
  166. char* mes_path = env_lookup("MES_PREFIX", envp);
  167. if(NULL == mes_path) mes_path = ".";
  168. mes_path = string_append(mes_path, "/module/mes/");
  169. char* boot = string_append(mes_path, mes_boot);
  170. load_file(boot);
  171. file_print("mes: boot failed: no such file: ", stderr);
  172. file_print(boot, stderr);
  173. file_print("\nIf you prefer not to load a bootfile\nrun: export MES_CORE=0\n", stderr);
  174. exit(EXIT_FAILURE);
  175. }
  176. }