mes.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /* globals used in REPL */
  23. char* message;
  24. int DISABLE_MACRO_EXPANSION;
  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);
  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();
  35. void garbage_init();
  36. void init_sl3();
  37. void push_cell(struct cell* a);
  38. void reset_block(char* a);
  39. void writeobj(struct cell* output_file, struct cell* op, int write_p);
  40. /* Deal with common errors */
  41. void require(int bool, char* error)
  42. {
  43. if(!bool)
  44. {
  45. file_print(error, stderr);
  46. exit(EXIT_FAILURE);
  47. }
  48. }
  49. /* Read Eval Print Loop*/
  50. int REPL()
  51. {
  52. int read;
  53. /* Read S-Expression block */
  54. reset_block(message);
  55. read = Readline(__c_stdin->file, message);
  56. if(0 == read) return TRUE;
  57. /* Process S-expression */
  58. R0 = parse(message, read);
  59. /* perform macro processing here */
  60. if(!DISABLE_MACRO_EXPANSION) R0 = expand_macros(R0);
  61. /* now to eval what results */
  62. eval();
  63. /* Print */
  64. if(match("/dev/stdin", __c_stdin->string) && (NULL != R1) && (cell_unspecified != R1))
  65. {
  66. file_print("$R0 = ", __c_stdout->file);
  67. writeobj(__c_stdout, R1, TRUE);
  68. fputc('\n', __c_stdout->file);
  69. }
  70. /* Display user friendly prompt */
  71. if(match("/dev/stdin", __c_stdin->string))
  72. {
  73. file_print("REPL: ", __c_stdout->file);
  74. }
  75. return FALSE;
  76. }
  77. /* The foundational loader */
  78. struct cell* load_file(char* s)
  79. {
  80. int Reached_EOF = FALSE;
  81. FILE* f = fopen(s, "r");
  82. /* Punt on bad inputs */
  83. if(NULL == f) return cell_unspecified;
  84. push_cell(__c_stdin);
  85. __c_stdin = make_file(f, s);
  86. while(!Reached_EOF)
  87. {
  88. garbage_collect();
  89. Reached_EOF = REPL();
  90. }
  91. __c_stdin = pop_cell();
  92. return cell_t;
  93. }
  94. int main(int argc, char **argv, char** envp)
  95. {
  96. FUZZING = FALSE;
  97. DISABLE_MACRO_EXPANSION = FALSE;
  98. __envp = envp;
  99. __argv = argv;
  100. __argc = argc;
  101. stack_pointer = 0;
  102. arena = numerate_string(env_lookup("MES_ARENA", envp));
  103. if(0 == arena) arena = 1;
  104. mes_debug_level = numerate_string(env_lookup("MES_DEBUG", envp));
  105. max_arena = numerate_string(env_lookup("MES_MAX_ARENA", envp));
  106. if(0 == max_arena) max_arena = 50000000;
  107. MAX_STRING = numerate_string(env_lookup("MES_MAX_STRING", envp));
  108. if(0 == MAX_STRING) MAX_STRING = 4096;
  109. MAX_TOKEN = numerate_string(env_lookup("MES_MAX_TOKEN", envp));
  110. if(0 == MAX_TOKEN) MAX_TOKEN = 1024;
  111. GC_SAFETY = numerate_string(env_lookup("MES_SAFETY", envp));
  112. MAX_STACK = numerate_string(env_lookup("MES_STACK", envp));
  113. if(0 == MAX_STACK) MAX_STACK = 100000;
  114. /* Our most important initializations */
  115. memory_block = calloc(MAX_TOKEN + 8, sizeof(char));
  116. message = calloc(MAX_STRING + 8, sizeof(char));
  117. garbage_init();
  118. init_sl3();
  119. g_stack = calloc(MAX_STACK, sizeof(struct cell*));
  120. /* Initialization: stdin, stdout and stderr */
  121. __c_stdin = make_file(stdin, "/dev/stdin");
  122. __c_stdout = make_file(stdout, "/dev/stdout");
  123. __c_stderr = make_file(stderr, "/dev/stderr");
  124. char* testing = env_lookup("MES_CORE", envp);
  125. if(NULL != testing)
  126. {
  127. int i = 1;
  128. while(i <= argc)
  129. {
  130. if(NULL == argv[i])
  131. {
  132. i = i + 1;
  133. }
  134. else if(match(argv[i], "--boot"))
  135. {
  136. load_file(argv[i + 1]);
  137. i = i + 2;
  138. }
  139. else if(match(argv[i], "-f") || match(argv[i], "--file"))
  140. {
  141. load_file(argv[i + 1]);
  142. i = i + 2;
  143. }
  144. else if(match(argv[i], "-h") || match(argv[i], "--help"))
  145. {
  146. file_print("Usage: ", stdout);
  147. file_print(argv[0], stdout);
  148. file_print(" [--boot boot.scm] [-f|--file file.scm] [-h|--help]\n", stdout);
  149. i = i + 1;
  150. exit(EXIT_SUCCESS);
  151. }
  152. else if(match(argv[i], "--fuzzing"))
  153. {
  154. FUZZING = TRUE;
  155. i = i + 1;
  156. }
  157. else if(match(argv[i], "--disable-macro-expansion-phase"))
  158. {
  159. DISABLE_MACRO_EXPANSION = TRUE;
  160. i = i + 1;
  161. }
  162. else
  163. {
  164. file_print("Received unknown option: ", stderr);
  165. file_print(argv[i], stderr);
  166. file_print("\nUsage: ", stderr);
  167. file_print(argv[0], stderr);
  168. file_print(" [--boot boot.scm] [-f|--file file.scm] [-h|--help]\nAborting\n", stderr);
  169. exit(EXIT_FAILURE);
  170. }
  171. }
  172. file_print("REPL: ", __c_stdout->file);
  173. load_file("/dev/stdin");
  174. file_print("\nexiting, have a nice day!\n", __c_stdout->file);
  175. exit(EXIT_SUCCESS);
  176. }
  177. else
  178. {
  179. char* mes_boot = env_lookup("MES_BOOT", envp);
  180. if(NULL == mes_boot)
  181. {
  182. mes_boot = "boot-0.scm";
  183. }
  184. char* mes_path = env_lookup("MES_PREFIX", envp);
  185. if(NULL == mes_path) mes_path = ".";
  186. mes_path = string_append(mes_path, "/module/mes/");
  187. char* boot = string_append(mes_path, mes_boot);
  188. load_file(boot);
  189. file_print("mes: boot failed: no such file: ", stderr);
  190. file_print(boot, stderr);
  191. file_print("\nIf you prefer not to load a bootfile\nrun: export MES_CORE=0\n", stderr);
  192. exit(EXIT_FAILURE);
  193. }
  194. }