writer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "minilisp.h"
  2. #include "stream.h"
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #define TMP_BUF_SIZE 128
  6. #define INTFORMAT "%d"
  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->ar.value);
  36. } else if (cell->tag == TAG_CONS) {
  37. if (cell->ar.addr == 0 && cell->dr.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->ar.addr, tmpl, 0, TMP_BUF_SIZE);
  45. if (cell->dr.next && ((Cell*)cell->dr.next)->tag==TAG_CONS) {
  46. write_((Cell*)cell->dr.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->dr.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->ar.addr);
  65. } else if (cell->tag == TAG_STR) {
  66. snprintf(buffer, min(bufsize-1,cell->dr.size+3), "\"%s\"", (char*)cell->ar.addr);
  67. } else if (cell->tag == TAG_BIGNUM) {
  68. snprintf(buffer, bufsize, "%s", (char*)cell->ar.addr);
  69. } else if (cell->tag == TAG_LAMBDA) {
  70. char tmp_body[TMP_BUF_SIZE*2];
  71. Cell* args = car(cell->ar.addr);
  72. int ai = 0;
  73. /*while (args && args->addr && args->next) {
  74. ai += snprintf(tmp_args+ai, TMP_BUF_SIZE-ai, "%s ", (char*)(car(car(args)))->addr);
  75. args = cdr(args);
  76. }*/
  77. write_(cdr(cell->ar.addr), tmp_body, 0, TMP_BUF_SIZE);
  78. snprintf(buffer, bufsize, "(fn %s %s)", "", tmp_body);
  79. } else if (cell->tag == TAG_BUILTIN) {
  80. snprintf(buffer, bufsize, "(op "INTFORMAT")", cell->ar.value);
  81. } else if (cell->tag == TAG_ERROR) {
  82. switch (cell->ar.value) {
  83. case ERR_SYNTAX: snprintf(buffer, bufsize, "<e0:syntax error.>"); break;
  84. case ERR_MAX_EVAL_DEPTH: snprintf(buffer, bufsize, "<e1:deepest level of evaluation reached.>"); break;
  85. case ERR_UNKNOWN_OP: snprintf(buffer, bufsize, "<e2:unknown operation.>"); break;
  86. case ERR_APPLY_NIL: snprintf(buffer, bufsize, "<e3:cannot apply nil.>"); break;
  87. case ERR_INVALID_PARAM_TYPE: snprintf(buffer, bufsize, "<e4:invalid or no parameter given.>"); break;
  88. case ERR_OUT_OF_BOUNDS: snprintf(buffer, bufsize, "<e5:out of bounds.>"); break;
  89. default: snprintf(buffer, bufsize, "<e"INTFORMAT":unknown>", cell->ar.value); break;
  90. }
  91. } else if (cell->tag == TAG_BYTES) {
  92. int strsize = min(cell->dr.size*2+3, bufsize);
  93. int max_bytes = (strsize-3)/2;
  94. if (bufsize>5) {
  95. unsigned int i;
  96. buffer[0]='[';
  97. for (i=0; i<max_bytes; i++) {
  98. sprintf(buffer+1+i*2, "%02x", ((uint8_t*)cell->ar.addr)[i]);
  99. }
  100. buffer[i*2+1]=']';
  101. buffer[i*2+2]=0;
  102. }
  103. } else if (cell->tag == TAG_STREAM) {
  104. Stream* s = (Stream*)cell->ar.addr;
  105. snprintf(buffer, bufsize, "<stream:%d:%s:%s>", s->id, s->path->ar.addr, s->fs->mount_point->ar.addr);
  106. } else {
  107. snprintf(buffer, bufsize, "<tag:%i>", cell->tag);
  108. }
  109. return buffer;
  110. }
  111. char* lisp_write(Cell* cell, char* buffer, int bufsize) {
  112. return write_(cell, buffer, 0, bufsize);
  113. }
  114. Cell* lisp_write_to_cell(Cell* cell, Cell* buffer_cell) {
  115. if (buffer_cell->tag == TAG_STR || buffer_cell->tag == TAG_BYTES) {
  116. lisp_write(cell, buffer_cell->ar.addr, buffer_cell->dr.size);
  117. }
  118. return buffer_cell;
  119. }