sledge.c 3.4 KB

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