SHARE
TWEET

Arthur's one-page interpreter fragment

a guest Feb 6th, 2016 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef char C;
  2. typedef long I;
  3. typedef struct a { I t, r, d[3], p[2]; } * A;
  4. #define P printf
  5. #define R return
  6. #define V1(f) A f(w) A w;
  7. #define V2(f) A f(a, w) A a, w;
  8. #define DO(n, x)                                                               \
  9.   {                                                                            \
  10.     I i = 0, _n = (n);                                                         \
  11.     for (; i < _n; ++i) {                                                      \
  12.       x;                                                                       \
  13.     }                                                                          \
  14.   }
  15. I *ma(n) { R(I *) malloc(n * 4); }
  16. mv(d, s, n) I *d, *s;
  17. { DO(n, d[i] = s[i]); }
  18. tr(r, d) I *d;
  19. {
  20.   I z = 1;
  21.   DO(r, z = z * d[i]);
  22.   R z;
  23. }
  24. A ga(t, r, d) I *d;
  25. {
  26.   A z = (A)ma(5 + tr(r, d));
  27.   z->t = t, z->r = r, mv(z->d, d, r);
  28.   R z;
  29. }
  30. V1(iota) {
  31.   I n = *w->p;
  32.   A z = ga(0, 1, &n);
  33.   DO(n, z->p[i] = i);
  34.   R z;
  35. }
  36. V2(plus) {
  37.   I r = w->r, *d = w->d, n = tr(r, d);
  38.   A z = ga(0, r, d);
  39.   DO(n, z->p[i] = a->p[i] + w->p[i]);
  40.   R z;
  41. }
  42. V2(from) {
  43.   I r = w->r - 1, *d = w->d + 1, n = tr(r, d);
  44.   A z = ga(w->t, r, d);
  45.   mv(z->p, w->p + (n * *a->p), n);
  46.   R z;
  47. }
  48. V1(box) {
  49.   A z = ga(1, 0, 0);
  50.   *z->p = (I)w;
  51.   R z;
  52. }
  53. V2(cat) {
  54.   I an = tr(a->r, a->d), wn = tr(w->r, w->d), n = an + wn;
  55.   A z = ga(w->t, 1, &n);
  56.   mv(z->p, a->p, an);
  57.   mv(z->p + an, w->p, wn);
  58.   R z;
  59. }
  60. V2(find) {}
  61. V2(rsh) {
  62.   I r = a->r ? *a->d : 1, n = tr(r, a->p), wn = tr(w->r, w->d);
  63.   A z = ga(w->t, r, a->p);
  64.   mv(z->p, w->p, wn = n > wn ? wn : n);
  65.   if (n -= wn)
  66.     mv(z->p + wn, z->p, n);
  67.   R z;
  68. }
  69. V1(sha) {
  70.   A z = ga(0, 1, &w->r);
  71.   mv(z->p, w->d, w->r);
  72.   R z;
  73. }
  74. V1(id) { R w; }
  75. V1(size) {
  76.   A z = ga(0, 0, 0);
  77.   *z->p = w->r ? *w->d : 1;
  78.   R z;
  79. }
  80. pi(i) { P("%d ", i); }
  81. nl() { P("\n"); }
  82. pr(w) A w;
  83. {
  84.   I r = w->r, *d = w->d, n = tr(r, d);
  85.   DO(r, pi(d[i]));
  86.   nl();
  87.   if (w->t)
  88.     DO(n, P("< "); pr(w->p[i])) else DO(n, pi(w->p[i]));
  89.   nl();
  90. }
  91.  
  92. C vt[] = "+{~<#,";
  93. A(*vd[])()={0,plus,from,find,0,rsh,cat},
  94. (*vm[])()={0,id,size,iota,box,sha,0};
  95. I st[26];
  96. qp(a) { R a >= 'a' && a <= 'z'; }
  97. qv(a) { R a < 'a'; }
  98. A ex(e) I *e;
  99. {
  100.   I a = *e;
  101.   if (qp(a)) {
  102.     if (e[1] == '=')
  103.       R st[a - 'a'] = ex(e + 2);
  104.     a = st[a - 'a'];
  105.   }
  106.   R qv(a) ? (*vm[a])(ex(e + 1)) : e[1] ? (*vd[e[1]])(a, ex(e + 2)) : (A)a;
  107. }
  108. noun(c) {
  109.   A z;
  110.   if (c < '0' || c > '9')
  111.     R 0;
  112.   z = ga(0, 0, 0);
  113.   *z->p = c - '0';
  114.   R z;
  115. }
  116. verb(c) {
  117.   I i = 0;
  118.   for (; vt[i];)
  119.     if (vt[i++] == c)
  120.       R i;
  121.   R 0;
  122. }
  123. I *wd(s) C *s;
  124. {
  125.   I a, n = strlen(s), *e = ma(n + 1);
  126.   C c;
  127.   DO(n, e[i] = (a = noun(c = s[i])) ? a : (a = verb(c)) ? a : c);
  128.   e[n] = 0;
  129.   R e;
  130. }
  131.  
  132. main() {
  133.   C s[99];
  134.   while (gets(s))
  135.     pr(ex(wd(s)));
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top