sledge.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <sys/time.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include "minilisp.h"
  6. #include <stdlib.h>
  7. #include "alloc.h"
  8. #include "compiler_new.c"
  9. #define BUFSZ 2048
  10. #ifdef CPU_X64
  11. #include "compiler_x64_hosted.c"
  12. #endif
  13. #ifdef CPU_ARM
  14. #include "compiler_arm_hosted.c"
  15. #endif
  16. #ifdef CPU_X86
  17. #include "compiler_x86.c"
  18. #endif
  19. #ifdef __AMIGA
  20. #include "compiler_m68k.c"
  21. #endif
  22. #ifdef __APPLE__
  23. #include "../devices/macosx.c"
  24. #endif
  25. void terminal_writestring(const char* data);
  26. int main(int argc, char *argv[])
  27. {
  28. Cell* expr = NULL;
  29. char* in_line = malloc(BUFSZ);
  30. char* in_buffer = malloc(64*BUFSZ);
  31. char* out_buf = malloc(BUFSZ);
  32. char* res;
  33. int in_offset = 0;
  34. int parens = 0;
  35. size_t len = 0;
  36. int i;
  37. int in_fd = 0;
  38. FILE* in_f;
  39. init_compiler();
  40. filesystems_init();
  41. #ifdef DEV_SDL2
  42. void dev_sdl2_init();
  43. dev_sdl2_init();
  44. #endif
  45. #ifdef DEV_SDL
  46. void dev_sdl_init();
  47. dev_sdl_init();
  48. #endif
  49. #ifdef DEV_LINUXFB
  50. void mount_linux_fbfs();
  51. mount_linux_fbfs();
  52. #endif
  53. #ifdef DEV_POSIXFS
  54. void mount_posixfs();
  55. mount_posixfs();
  56. #endif
  57. #ifdef DEV_BIOS
  58. void mount_bios();
  59. mount_bios();
  60. #endif
  61. #ifdef DEV_CONSOLEKEYS
  62. void mount_consolekeys();
  63. mount_consolekeys();
  64. #endif
  65. #ifdef __AMIGA
  66. mount_amiga();
  67. #endif
  68. if (argc==2) {
  69. in_fd = open(argv[1],O_RDONLY);
  70. in_f = fdopen(in_fd,"r");
  71. if (!in_f) in_f = stdin;
  72. } else {
  73. in_f = stdin;
  74. }
  75. while (1) {
  76. if (in_f == stdin) printf("interim> ");
  77. expr = NULL;
  78. len = 0;
  79. res = fgets(in_line, BUFSZ, in_f);
  80. if (res) {
  81. len = strlen(in_line);
  82. }
  83. //printf("line: (%d) |%s|\r\n",len,in_line);
  84. if (len>0) {
  85. // recognize parens
  86. for (i=0; i<len; i++) {
  87. if (in_line[i] == ';') break;
  88. if (in_line[i] == '(') {
  89. parens++;
  90. } else if (in_line[i] == ')') {
  91. parens--;
  92. }
  93. }
  94. strncpy(in_buffer+in_offset, in_line, i);
  95. in_buffer[in_offset+i]='\n';
  96. in_buffer[in_offset+i+1]=0;
  97. if (parens>0) {
  98. if (in_f == stdin) printf("...\r\n");
  99. in_offset+=i;
  100. } else {
  101. in_offset=0;
  102. if (len>1) {
  103. expr = (Cell*)read_string(in_buffer);
  104. } else {
  105. //printf("\r\n");
  106. }
  107. }
  108. }
  109. if (feof(in_f) || len==0) {
  110. if (in_f!=stdin) close(in_fd);
  111. in_f = stdin;
  112. in_fd = 0;
  113. in_offset=0;
  114. if (feof(stdin)) {
  115. printf("\n");
  116. exit(0);
  117. }
  118. }
  119. if (expr) {
  120. Cell* res;
  121. int success = compile_for_platform(expr, &res);
  122. if (success) {
  123. if (in_f == stdin) {
  124. if (!res) {
  125. printf("invalid cell (%p)\r\n",res);
  126. } else {
  127. lisp_write(res, out_buf, 1024);
  128. printf("%s\r\n",out_buf);
  129. }
  130. }
  131. } else {
  132. printf("<compilation failed>\n");
  133. res = NULL;
  134. }
  135. }
  136. }
  137. return 0;
  138. }
  139. Cell* platform_eval(Cell* expr) {
  140. char* buf=malloc(BUFSZ);
  141. int i = 0;
  142. Cell* res = (Cell*)alloc_nil();
  143. Cell* c;
  144. int tag;
  145. if (!expr || expr->tag!=TAG_CONS) {
  146. printf("[platform_eval] error: no expr given.\r\n");
  147. return NULL;
  148. }
  149. while (expr && (c = car(expr))) {
  150. tag = compile_for_platform(c, &res);
  151. if (tag) {
  152. /*printf("~~ expr %d res: %p\r\n",i,res);
  153. lisp_write(res, buf, 512);
  154. printf("~> %s\r\n",buf);*/
  155. } else {
  156. lisp_write(c, buf, BUFSZ);
  157. printf("[platform_eval] stopped at expression %d: %s\r\n",i,buf);
  158. break;
  159. }
  160. // TOOD: when to free the code blocks? -> when no bound lambdas involved
  161. i++;
  162. expr = cdr(expr);
  163. }
  164. free(buf);
  165. return res;
  166. }