writer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "minilisp.h"
  2. #include "stream.h"
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #define TMP_BUF_SIZE 512
  6. #define INTFORMAT "%ld"
  7. char* tag_to_str(int tag) {
  8. switch (tag) {
  9. case TAG_FREED: return "freed";
  10. case TAG_INT: return "int";
  11. case TAG_CONS: return "cons";
  12. case TAG_SYM: return "sym";
  13. case TAG_LAMBDA: return "lambda";
  14. case TAG_BUILTIN: return "builtin";
  15. case TAG_BIGNUM: return "bignum";
  16. case TAG_STR: return "string";
  17. case TAG_BYTES: return "bytes";
  18. case TAG_VEC: return "vector";
  19. case TAG_ERROR: return "error";
  20. case TAG_ANY: return "any";
  21. case TAG_VOID: return "void";
  22. case TAG_STREAM: return "stream";
  23. case TAG_FS: return "filesystem";
  24. case TAG_MARK: return "gc_mark";
  25. default: return "unknown";
  26. }
  27. }
  28. char* write_(Cell* cell, char* buffer, int in_list, int bufsize) {
  29. //printf("writing %p (%d) to %p, size: %d\n",cell,cell->tag,buffer,bufsize);
  30. bufsize--;
  31. buffer[0]=0;
  32. if (cell == NULL) {
  33. snprintf(buffer, bufsize, "null");
  34. } else if (cell->tag == TAG_INT) {
  35. snprintf(buffer, bufsize, INTFORMAT, cell->value);
  36. } else if (cell->tag == TAG_CONS) {
  37. if (cell->addr == 0 && cell->next == 0) {
  38. if (!in_list) {
  39. snprintf(buffer, bufsize, "nil");
  40. }
  41. } else {
  42. char tmpl[TMP_BUF_SIZE];
  43. char tmpr[TMP_BUF_SIZE];
  44. write_((Cell*)cell->addr, tmpl, 0, TMP_BUF_SIZE);
  45. if (cell->next && ((Cell*)cell->next)->tag==TAG_CONS) {
  46. write_((Cell*)cell->next, tmpr, 1, TMP_BUF_SIZE);
  47. if (in_list) {
  48. if (tmpr[0]) {
  49. snprintf(buffer, bufsize, "%s %s", tmpl, tmpr);
  50. } else {
  51. snprintf(buffer, bufsize, "%s", tmpl); // skip nil at end of list
  52. }
  53. } else {
  54. // we're head of a list
  55. snprintf(buffer, bufsize, "(%s %s)", tmpl, tmpr);
  56. }
  57. } else {
  58. write_((Cell*)cell->next, tmpr, 0, TMP_BUF_SIZE);
  59. // improper list
  60. snprintf(buffer, bufsize, "(%s.%s)", tmpl, tmpr);
  61. }
  62. }
  63. } else if (cell->tag == TAG_SYM) {
  64. snprintf(buffer, bufsize, "%s", (char*)cell->addr);
  65. } else if (cell->tag == TAG_STR) {
  66. snprintf(buffer, min(bufsize-1,cell->size+3), "\"%s\"", (char*)cell->addr);
  67. } else if (cell->tag == TAG_BIGNUM) {
  68. snprintf(buffer, bufsize, "%s", (char*)cell->addr);
  69. } else if (cell->tag == TAG_LAMBDA) {
  70. //char tmp_args[TMP_BUF_SIZE];
  71. char tmp_body[TMP_BUF_SIZE*2];
  72. Cell* args = car(cell->addr);
  73. int ai = 0;
  74. /*while (args && args->addr && args->next) {
  75. ai += snprintf(tmp_args+ai, TMP_BUF_SIZE-ai, "%s ", (char*)(car(car(args)))->addr);
  76. args = cdr(args);
  77. }*/
  78. write_(cdr(cell->addr), tmp_body, 0, TMP_BUF_SIZE);
  79. snprintf(buffer, bufsize, "(fn %s %s)", "", tmp_body);
  80. } else if (cell->tag == TAG_BUILTIN) {
  81. snprintf(buffer, bufsize, "(op "INTFORMAT")", cell->value);
  82. } else if (cell->tag == TAG_ERROR) {
  83. switch (cell->value) {
  84. case ERR_SYNTAX: snprintf(buffer, bufsize, "<e0:syntax error.>"); break;
  85. case ERR_MAX_EVAL_DEPTH: snprintf(buffer, bufsize, "<e1:deepest level of evaluation reached.>"); break;
  86. case ERR_UNKNOWN_OP: snprintf(buffer, bufsize, "<e2:unknown operation.>"); break;
  87. case ERR_APPLY_NIL: snprintf(buffer, bufsize, "<e3:cannot apply nil.>"); break;
  88. case ERR_INVALID_PARAM_TYPE: snprintf(buffer, bufsize, "<e4:invalid or no parameter given.>"); break;
  89. case ERR_OUT_OF_BOUNDS: snprintf(buffer, bufsize, "<e5:out of bounds.>"); break;
  90. default: snprintf(buffer, bufsize, "<e"INTFORMAT":unknown>", cell->value); break;
  91. }
  92. } else if (cell->tag == TAG_BYTES) {
  93. int strsize = min(cell->size*2+3, bufsize);
  94. int max_bytes = (strsize-3)/2;
  95. if (bufsize>5) {
  96. buffer[0]='[';
  97. unsigned int i;
  98. for (i=0; i<max_bytes; i++) {
  99. sprintf(buffer+1+i*2, "%02x", ((uint8_t*)cell->addr)[i]);
  100. }
  101. buffer[i*2+1]=']';
  102. buffer[i*2+2]=0;
  103. }
  104. } else if (cell->tag == TAG_STREAM) {
  105. Stream* s = (Stream*)cell->addr;
  106. snprintf(buffer, bufsize, "<stream:%d:%s:%s>", s->id, s->path->addr, s->fs->mount_point->addr);
  107. } else {
  108. snprintf(buffer, bufsize, "<tag:%i>", cell->tag);
  109. }
  110. return buffer;
  111. }
  112. char* lisp_write(Cell* cell, char* buffer, int bufsize) {
  113. return write_(cell, buffer, 0, bufsize);
  114. }
  115. Cell* lisp_write_to_cell(Cell* cell, Cell* buffer_cell) {
  116. if (buffer_cell->tag == TAG_STR || buffer_cell->tag == TAG_BYTES) {
  117. lisp_write(cell, buffer_cell->addr, buffer_cell->size);
  118. }
  119. return buffer_cell;
  120. }