sexp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Public Domain.
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "sexp.h"
  6. #include "fs.h"
  7. static char decode(char** s);
  8. static sexp* parse_literal(char** s) {
  9. sexp* x;
  10. char* e;
  11. char q;
  12. int len, i;
  13. x = calloc(1, sizeof(*x));
  14. x->type = 1;
  15. //check if it's not quoted
  16. if(**s != '"' && **s != '\'') {
  17. e = strpbrk(*s, " \r\n\t)");
  18. if(!e) {
  19. fprintf(stderr, "sexp: unexpected end of input parsing literal\n");
  20. return x;
  21. }
  22. x->str = strndup(*s, e - *s);
  23. *s = e;
  24. return x;
  25. }
  26. // handled quoted strings
  27. q = **s;
  28. (*s)++;
  29. // find max length
  30. for(len = 0; (*s)[len] && (*s)[len] != q && (*s)[len - 1] != '\\'; len++);
  31. x->str = malloc(len + 1);
  32. i = 0;
  33. while(**s && **s != q && *(*s - 1) != '\\') {
  34. x->str[i] = decode(s);
  35. (*s)++;
  36. i++;
  37. }
  38. x->str[i] = '\0';
  39. return x;
  40. }
  41. static sexp* parse(char** s) {
  42. sexp* x, *y;
  43. x = calloc(1, sizeof(*x));
  44. x->type = 0;
  45. while(**s) {
  46. char c = **s;
  47. switch(c) {
  48. case '(': /* fall through */ // sub expression
  49. case '{': /* fall through */ // sub expression
  50. case '[': /* fall through */ // sub expression
  51. case '<': /* fall through */ // sub expression
  52. (*s)++;
  53. // TODO: check for (* *) and skip as comment
  54. y = parse(s);
  55. y->brace = c;
  56. VEC_PUSH(&x->args, y);
  57. break;
  58. case ')': /* fall through */ // end of expression
  59. case '}': /* fall through */ // end of expression
  60. case ']': /* fall through */ // end of expression
  61. case '>': /* fall through */ // end of expression
  62. (*s)++;
  63. return x;
  64. case '\r': /* fall through */ // skip whitespace
  65. case '\n': /* fall through */
  66. case '\t': /* fall through */
  67. case '\v': /* fall through */
  68. case ' ':
  69. (*s)++;
  70. break;
  71. default: // some literal of some sort
  72. y = parse_literal(s);
  73. VEC_PUSH(&x->args, y);
  74. break;
  75. }
  76. }
  77. fprintf(stderr, "sexp: unexpected end of input parsing expression.\n");
  78. return x;
  79. }
  80. sexp* sexp_parse(char* source) {
  81. char* s = strpbrk(source, "({[<") + 1;
  82. return parse(&s);
  83. }
  84. sexp* sexp_parse_file(char* path) {
  85. char* s;
  86. s = readWholeFile(path, NULL);
  87. return parse(&s);
  88. }
  89. // returns 0 on all failed conversions
  90. int64_t sexp_asInt(sexp* x) {
  91. int64_t n;
  92. int base = 10;
  93. if(!x->str) return 0;
  94. if(x->type == 0) return 0;
  95. // HACK. does not allow negative hex/octal/binary
  96. if(x->str[0] == '0') {
  97. if(x->str[1] == 'x') { // safe, implied by [0] being not null above
  98. base = 16;
  99. }
  100. else if(x->str[1] == 'b') {
  101. base = 2;
  102. }
  103. else {
  104. base = 8;
  105. }
  106. }
  107. n = strtol(x->str, NULL, base);
  108. return n;
  109. }
  110. double sexp_asDouble(sexp* x) {
  111. if(!x->str) return 0.0;
  112. if(x->type == 0) return 0.0;
  113. return strtod(x->str, NULL);
  114. }
  115. int64_t sexp_argAsInt(sexp* x, size_t argn) {
  116. if(x->type != 0) return 0;
  117. if(VEC_LEN(&x->args) < argn) return 0;
  118. return sexp_asInt(VEC_ITEM(&x->args, argn));
  119. }
  120. double sexp_argAsDouble(sexp* x, size_t argn) {
  121. if(x->type != 0) return 0.0;
  122. if(VEC_LEN(&x->args) < argn) return 0.0;
  123. return sexp_asDouble(VEC_ITEM(&x->args, argn));
  124. }
  125. // returns internally managed string, user must dup
  126. char* sexp_argAsStr(sexp* x, size_t argn) {
  127. if(x->type != 0) return "";
  128. if(VEC_LEN(&x->args) < argn) return "";
  129. return VEC_ITEM(&x->args, argn)->str;
  130. }
  131. sexp* sexp_argAsSexp(sexp* x, size_t argn) {
  132. if(x->type != 0) return NULL;
  133. if(VEC_LEN(&x->args) < argn) return NULL;
  134. return VEC_ITEM(&x->args, argn);
  135. }
  136. void sexp_free(sexp* x) {
  137. size_t i;
  138. if(!x) return;
  139. for(i = 0; i < VEC_LEN(&x->args); i++) {
  140. sexp_free(VEC_ITEM(&x->args, i));
  141. }
  142. if(x->str) free(x->str);
  143. free(x);
  144. }
  145. // out must be big enough, at least as big as in+1 just to be safe
  146. // appends a null to out, but is also null-safe
  147. static char decode(char** s) {
  148. char c = **s;
  149. if(c == '\\') {
  150. (*s)++;
  151. switch(**s) {
  152. case '\'': return '\'';
  153. case '"': return '"';
  154. case '`': return '`';
  155. case '?': return '?';
  156. case '0': return '\0';
  157. case 'r': return '\r';
  158. case 'n': return '\n';
  159. case 'f': return '\f';
  160. case 'a': return '\a';
  161. case 'b': return '\b';
  162. case 'v': return '\v';
  163. case 't': return '\t';
  164. case 'x':
  165. // TODO: parse hex code
  166. return '?';
  167. case 'U':
  168. // TODO parse longer unicode
  169. case 'u':
  170. // TODO: parse unicode
  171. return '?';
  172. // TODO: parse octal
  173. default:
  174. return '?';
  175. }
  176. }
  177. return c;
  178. }