stringie.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * stringie.c -- a brain-freezingly pedantic implementation of Underload in C
  3. * (with all the limitations that that implies)
  4. * Chris Pressey, September 2010
  5. * This work is in the public domain.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. struct stack {
  11. char *string;
  12. struct stack *next;
  13. } *root;
  14. void run(char *);
  15. char *pop(void)
  16. {
  17. char *r;
  18. struct stack *old;
  19. r = root->string;
  20. old = root;
  21. root = root->next;
  22. free(old);
  23. return r;
  24. }
  25. void push(char *string)
  26. {
  27. struct stack *s;
  28. s = malloc(sizeof(struct stack));
  29. s->next = root;
  30. s->string = string;
  31. root = s;
  32. }
  33. void dup(void)
  34. {
  35. char *e, *f;
  36. e = pop();
  37. f = strdup(e);
  38. push(e);
  39. push(f);
  40. }
  41. void swap(void)
  42. {
  43. char *e, *f;
  44. e = pop();
  45. f = pop();
  46. push(e);
  47. push(f);
  48. }
  49. void eval(void)
  50. {
  51. char *e;
  52. e = pop();
  53. run(e);
  54. }
  55. void concat(void)
  56. {
  57. char *e, *f, *g;
  58. e = pop();
  59. f = pop();
  60. g = malloc(strlen(e) + strlen(f) + 1);
  61. strcpy(g, f);
  62. strcat(g, e);
  63. push(g);
  64. free(f);
  65. free(e);
  66. }
  67. void enclose(void)
  68. {
  69. char *e, *g;
  70. e = pop();
  71. g = malloc(strlen(e) + 3);
  72. sprintf(g, "(%s)", e);
  73. push(g);
  74. free(e);
  75. }
  76. void output(void)
  77. {
  78. char *e;
  79. e = pop();
  80. printf("%s", e);
  81. free(e);
  82. }
  83. void dumpstack(void)
  84. {
  85. struct stack *s;
  86. fprintf(stderr, "STACK: ");
  87. for (s = root; s != NULL; s = s->next)
  88. {
  89. fprintf(stderr, "(%s)", s->string);
  90. }
  91. fprintf(stderr, "!\n");
  92. }
  93. void run(char *program)
  94. {
  95. int i = 0;
  96. int last_pos = strlen(program) - 1;
  97. for (i = 0; program[i] != '\0'; i++) {
  98. switch (program[i]) {
  99. case ':': dup(); break;
  100. case '!': pop(); break;
  101. case '^':
  102. {
  103. /* tail recursion */
  104. if (i == last_pos) {
  105. i = -1;
  106. free(program);
  107. program = pop();
  108. continue;
  109. } else {
  110. eval();
  111. }
  112. }
  113. break;
  114. case '~': swap(); break;
  115. case '*': concat(); break;
  116. case 'S': output(); break;
  117. case 'a': enclose(); break;
  118. case '(':
  119. {
  120. int level = 0;
  121. int j = 0;
  122. char *t = malloc(256);
  123. i++;
  124. level++;
  125. while (level > 0) {
  126. if (program[i] == '(')
  127. level++;
  128. else if (program[i] == ')')
  129. level--;
  130. if (level > 0) {
  131. t[j] = program[i];
  132. j++;
  133. }
  134. i++;
  135. }
  136. i--;
  137. t[j] = '\0';
  138. push(t);
  139. }
  140. break;
  141. }
  142. /*dumpstack();*/
  143. }
  144. free(program);
  145. }
  146. int main(int argc, char **argv)
  147. {
  148. char *program = strdup(argv[1]);
  149. root = NULL;
  150. run(program);
  151. exit(0);
  152. }