git @ Cat's Eye Technologies RUBE / master src / rube.c
master

Tree @master (Download .tar.gz)

rube.c @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 * rube.c v1.6, Oct 2014, Chris Pressey
 * Interpreter/Debugger for the RUBE programming language
 *
 * (c)1997-2014 Chris Pressey, Cat's Eye Technologies.  All rights reserved.
 * Covered under a BSD-style license; see LICENSE for more information.
 *
 * Usage :
 *
 * rube [-d] [-q] [-i] [-y delay] [-f frame-skip] [-o offset] <rube-source>
 *
 *  -d: disable debugging output
 *  -q: produce no output but program output
 *  -i: run interactively (single step with lines from stdin)
 *  -y: specify debugging delay in milliseconds (default 0)
 *  -f: specify debugging frame skip in frames (default 1)
 *
 * Compilation :
 *
 * MS-DOS: tested with Turbo C++ v3.0, Borland C++ v3.1, DGJPP gcc v4.7.3.
 *         Load ANSI.SYS or compatible ANSI driver before using.
 * Windows: tested with GCC 4.8.3 under Cygwin.
 * Linux: tested with GCC 4.8.2 under Ubuntu 14.04 LTS.
 * NetBSD: tested with GCC 4.5.3 under NetBSD 6.1.4
 * AmigaOS: tested with DICE C 3.16 under AmigaOS 1.3.
 *
 * History :
 *
 * v1.00: May/Jun 97 original, minimal implementation
 * v1.01: Jun 97 added K gate, AV swinches, + packer, - unpacker
 * v1.02: Jul 97 fixed bug in WM winches, - unpacker, added . and C
 *        doubled height of playfield and improved debugger.
 *        added Ben Olmstead's cursor-turner-offer-thingy.
 *        added -q option.
 * v1.3: Feb 110 made compilable in POSIX and strict ANSI C89.
 *        screen is cleared before drawing initial playfield.
 * v1.4: Apr 110 really made compilable in strict ANSI C89.
 *        made command-line options case-senstive.
 *        added GNU Makefile.
 * v1.5: Jan 111 simplified and cleaned up code, allowing it to
 *        build on AmigaOS 1.3 using DICE C.  Added -i option.
 * v1.6: Oct 114 cleaned up functioning under DOS, Turbo C, DJGPP
 *        use kbhit() instead of (less reliable?) setcbrk()
 *        use _setcursortype() instead of inline assembly
 *        properly clear ANSI terminal screen (esp. Amiga console)
 *        reorganized build system and converted tabs to spaces
 */

/********************************************************* #INCLUDE'S */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#if defined(__TURBOC__) || defined(__BORLANDC__) || defined(__DJGPP__)
  #define MSDOS 1
#endif
#ifdef MSDOS
  #include <dos.h>
  #include <conio.h>  /* for _setcursortype() and kbhit() */
#endif
#ifdef __GNUC__
  #include <unistd.h>
#endif
#ifdef _POSIX_C_SOURCE
  #include <sys/time.h>
#endif

/********************************************************** #DEFINE'S */

#define LINEWIDTH    80
#define PAGEHEIGHT   50

#define SCREENWIDTH  79
#define SCREENHEIGHT 22

#define nex          pg2[y * LINEWIDTH + x].c
#define nexd(dx,dy)  pg2[(y+dy) * LINEWIDTH + (x+dx)].c

#define shrink(s) s[strlen(s)-1]=0

typedef struct cellstruct
{
  char c;
} cell;

/*************************************************** GLOBAL VARIABLES */

static cell pg[LINEWIDTH * PAGEHEIGHT]; /* playfield */
static cell pg2[LINEWIDTH * PAGEHEIGHT];/* playfield' */

static int x = 0, y = 0;      /* x and y looping */
static int debug = 1;         /* flag: display ANSI debugging? */
static int interactive = 0;   /* flag: ask for input each frame? */
static int deldur = 0;        /* debugging delay in milliseconds */
static int debskip = 1;       /* frame skip in debug view */
static int debopos = 1;       /* output column in debugger */
static int frame = 1;
static int quiet = 0;

/********************************************************* PROTOTYPES */

int curd(int, int);
int isramp(char);
int isblock(char);
int issupport(char);
int iscrate(char);
int ctoh(char);
char htoc(int);
void rube_delay(int);

/******************************************************* MAIN PROGRAM */

int main (int argc, char **argv)
{
  FILE *f;
  int i;
  int done=0;
  int maxy=0; int maxx=0;

  srand (time (0));

  if (argc < 2)
  {
    printf ("USAGE: rube [-d] [-q] [-i] [-y delay] [-f skip] foo.rub\n");
    exit (0);
  }
  for (i = 1; i < argc; i++)
  {
    if (!strcmp(argv[i], "-d")) { debug = 0; }
    if (!strcmp(argv[i], "-q")) { quiet = 1; debug = 0; }
    if (!strcmp(argv[i], "-i")) { interactive = 1; debug = 1; }
    if (!strcmp(argv[i], "-y")) { deldur = atoi(argv[i + 1]); }
    if (!strcmp(argv[i], "-f")) { debskip = atoi(argv[i + 1]); }
  }
  if (!quiet) printf ("Cat's Eye Technologies' RUBE Interpreter v1.5\n");
  f = fopen (argv[argc - 1], "r");
  if (f == NULL) {
    printf ("Error : couldn't open '%s' for input.\n", argv[argc - 1]);
    exit (0);
  }
  while (!feof (f))
  {
    int cur = fgetc(f);
    pg[y * LINEWIDTH + x].c = cur;
    if (cur == '\n')
    {
      pg[y * LINEWIDTH + x].c = ' ';
      x = 0;
      y++;
      if (y >= PAGEHEIGHT) break;
    } else
    {
      x++;
      if (x > maxx) maxx = x;
      if (x >= LINEWIDTH)
      {
        x = 0;
        y++;
        if (y >= PAGEHEIGHT) break;
      }
    }
  }
  fclose (f);
  maxy = y;

#if MSDOS
  _setcursortype(_NOCURSOR);
#endif

  if (debug)
  {
    printf ("%c[1;1H%c[2J", 27, 27);
  }
  while (!done)          /*** Intepreting Phase */
  {
    if ((debug) && (!(frame++ % debskip) || (!frame)))
    {
      printf ("%c[1;1H", 27);
      for(y = 0; (y <= maxy) && (y <= SCREENHEIGHT); y++)
      {
        for(x = 0; (x <= maxx) && (x <= SCREENWIDTH); x++)
        {
          int cur = pg[y * LINEWIDTH + x].c;
          putc(isprint(cur) ? cur : ' ', stdout);
        }
        printf("\n");
      }
    } else
    {
      /* putc('.', stdout); */
    }
    fflush (stdout);
    fflush (stdin);
    for (x=0; x<=(maxx); x++)
    {
      for (y=0; y<=(maxy); y++)
      {
        int cur = pg[y * LINEWIDTH + x].c;
        if (cur <= 32) {
            if (iscrate(curd(0,-1))) nex = curd(0,-1);    /* falling in from above */
            if (curd(0,-1) == '(') nex = '(';
            if (curd(0,-1) == ')') nex = ')';

            if (curd(1,1) == 'W') nex = curd(2,2);
            if (curd(-1,1) == 'W') nex = curd(-2,2);

            if ((curd(1,1) == 'V') && iscrate(curd(2,1))) nex = curd(2,1);
            if ((curd(-1,1) == 'V') && iscrate(curd(-2,1))) nex = curd(-2,1);

            if (curd(1,-1) == 'M') nex = curd(2,-2);
            if (curd(-1,-1) == 'M') nex = curd(-2,-2);

            if ((curd(1,-1) == 'A') && iscrate(curd(2,-1))) nex = curd(2,-1);
            if ((curd(-1,-1) == 'A') && iscrate(curd(-2,-1))) nex = curd(-2,-1);

            if (curd(0,-1) == '~') nex = '~';
            if ((curd(-1,0) == '~') && (issupport(curd(-1,1)))) nex = '~';
            if ((curd(1,0) == '~') && (issupport(curd(1,1)))) nex = '~';

            if (curd(1,-1) == '+')
            {
              if (iscrate(curd(1,0)) && iscrate(curd(2,0)))
              {
                nex = htoc((ctoh(curd(1,0))+ctoh(curd(2,0))) % 16);
              }
            }

            if (curd(-1,-1) == '+')
            {
              if (iscrate(curd(-1,0)) && iscrate(curd(-2,0)))
              {
                nex = htoc((ctoh(curd(-1,0))+ctoh(curd(-2,0))) % 16);
              }
            }

            if (curd(1,-1) == '-')
            {
              if (iscrate(curd(1,0)) && iscrate(curd(2,0)))
              {
                int z;
                z = ctoh(curd(2,0)) - ctoh(curd(1,0));
                while (z < 0) z += 16;
                nex = htoc(z);
              }
            }

            if (curd(-1,-1) == '-')
            {
              if (iscrate(curd(-1,0)) && iscrate(curd(-2,0)))
              {
                int z;
                z = ctoh(curd(-2,0)) - ctoh(curd(-1,0));
                while (z < 0) z += 16;
                nex = htoc(z);
              }
            }

            if ((curd(1,-1) == 'K') && (iscrate(curd(1,-2))))
            {
              if(ctoh(curd(1,-2)) < ctoh(curd(1,0))) nex = curd(1,-2);
            }

            if ((curd(-1,-1) == 'K') && (iscrate(curd(-1,-2))))
            {
              if(ctoh(curd(-1,-2)) >= ctoh(curd(-1,0))) nex = curd(-1,-2);
            }

            if ((iscrate(curd(-1,0))) && (curd(-1,1) == '>')) nex = curd(-1,0);
            if ((iscrate(curd(1,0))) && (curd(1,1) == '<')) nex = curd(1,0);
            if (curd(0,-1) == ':') nex = curd(0,-2);
            if ((curd(0,-1) == ';') && (iscrate(curd(0,-2)))) nex = curd(0,-2);
            if ((curd(0,1) == '.') && (iscrate(curd(0,2)))) nex = curd(0,2);
            if ((curd(-1,0) == '(') && (curd(1,0) == ')')) /* collision */
            {
              nex = ' ';
            } else
            {
              if ((curd(-1,0) == '(') && (issupport(curd(-1,1)))) nex = '(';
              if ((curd(1,0) == ')') && (issupport(curd(1,1)))) nex = ')';
              if ((curd(0,1) == '/') || (curd(0,1) == '\\'))
              {
                if ((curd(-1,1) == '(') && (issupport(curd(-1,2)))) nex = '(';
                if ((curd(1,1) == ')') && (issupport(curd(1,2)))) nex = ')';
              }
            }
            if (iscrate(curd(-1,0)))
            { /* shift crates */
              int bx=-1;
              while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
              {
                if (curd(bx-1,0) == '(')
                {
                  nex = curd(-1,0);
                }
                bx--;
              }
            }
            if (iscrate(curd(1,0)))
            {
              int bx=1;
              while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
              {
                if (curd(bx+1,0) == ')')
                {
                  nex = curd(1,0);
                }
                bx++;
              }
            }
        } else switch (cur)
        {
          case '(':
            if (((curd(1,0) == '(') ||
                 (curd(1,0) <= ' ') ||
                 (curd(0,1) <= ' ') ||
                 (curd(0,1) == '('))) nex = ' ';
            if (isramp(curd(0,1))) nex = ' ';
            if (isramp(curd(1,0))) nex = ' ';
            if (isramp(curd(-1,0))) nex = ' ';
            if ((isblock(curd(1,0))) ||
                (curd(1,-1) == ',') ||
                (curd(1,0) == '*')) nex = ')';
            if (iscrate(curd(1,0)))
            {
              int bx=1;
              while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
              {
                if (isblock(curd(bx+1,0)))
                {
                  nex = ')';
                }
                bx++;
              }
            }
            break;
          case ')':
            if (((curd(-1,0) == ')') ||
                 (curd(-1,0) <= ' ') ||
                 (curd(0,1) <= ' ') ||
                 (curd(0,1) == ')'))) nex = ' ';
            if (isramp(curd(0,1))) nex = ' ';
            if (isramp(curd(1,0))) nex = ' ';
            if (isramp(curd(-1,0))) nex = ' ';
            if ((isblock(curd(-1,0))) ||
                (curd(-1,-1) == ',') ||
                (curd(-1,0) == '*')) nex = '(';
            if (iscrate(curd(-1,0)))
            {
              int bx=-1;
              while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
              {
                if (isblock(curd(bx-1,0)))
                {
                  nex = '(';
                }
                bx--;
              }
            }
            break;
          case 'O':
            if ((iscrate(curd(0,-1))) && (iscrate(curd(0,-2))))
            {
              int d;

              d = ctoh(curd(0,-1)) + ctoh(curd(0,-2)) * 16;
              if (curd(0, 1) == 'b')
              {
                if (debug)
                {
                  char s[80];
                  printf ("%c[%d;%dH", 27, 25, debopos);
                  sprintf(s, "%d ", (int)d);
                  debopos += strlen(s);
                  if (debopos > SCREENWIDTH)
                  {
                    debopos = 1;
                    printf ("%c[%d;%dH%c[K", 27, 25, 1, 27);
                    debopos += strlen(s);
                  }
                  printf("%s", s);
                } else
                {
                  printf("%d ", (int)d);
                }
              }
              if (curd(0, 1) == 'c')
              {
                if (debug)
                {
                  printf ("%c[%d;%dH", 27, 25, debopos++);
                  if (debopos > SCREENWIDTH)
                  {
                    debopos = 1;
                    printf ("%c[%d;%dH%c[K", 27, 25, 1, 27);
                    debopos++;
                  }
                  printf ("%c", (char)d);
                } else
                {
                  putc((char)d, stdout);
                }
              }
            }
          case 'A':
            if (iscrate(curd(-1,0))  || iscrate(curd(1,0))) nex = 'V'; else nex = cur;
            break;
          case 'V':
            if (iscrate(curd(-1,0))  || iscrate(curd(1,0))) nex = 'A'; else nex = cur;
            break;
          default: nex = cur;
        }
        if (iscrate(cur))
        {
          if (issupport(curd(0,1))) nex = cur; else nex = ' ';
          if ((curd(1,0) <= ' ') && (curd(0,1) == '>')) nex = ' ';
          if ((curd(-1,0) <= ' ') && (curd(0,1) == '<')) nex = ' ';
          if ((curd(1,-1) == 'W') && (curd(2,-2) == cur)) nex = ' ';
          if ((curd(-1,-1) == 'W') && (curd(2,-2) == cur)) nex = ' ';
          if ((curd(1,1) == 'M') && (curd(2,2) == cur)) nex = ' ';
          if ((curd(-1,1) == 'M') && (curd(-2,2) == cur)) nex = ' ';
          if (curd(1,0) == 'V') nex = ' ';
          if (curd(-1,0) == 'V') nex = ' ';
          if (curd(1,0) == 'A') nex = ' ';
          if (curd(-1,0) == 'A') nex = ' ';
          if (iscrate(curd(-1,0)) && ((curd(-1,-1) == '+')
              || (curd(-1,-1) == '-'))) nex = ' ';
          if (iscrate(curd(1,0)) && ((curd(1,-1) == '+')
              || (curd(1,-1) == '-'))) nex = ' ';
          if ((iscrate(curd(-1,0)) || iscrate(curd(1,0))) && ((curd(0,-1) == '+')
              || (curd(0,-1) == '-'))) nex = ' ';
        }
      }
    }
    /* fix nex array */
    for (x=0; x<=(maxx); x++)
    {
      for (y=0; y<=(maxy); y++)
      {
        int cur = pg[y * LINEWIDTH + x].c;
        switch (cur)
        {
          case '*':
            if (curd(-1,0) == ')') nex = ' ';
            if (curd(1,0) == '(') nex = ' ';
            break;
          case 'O':
            if ((iscrate(curd(0,-1))) && (iscrate(curd(0,-2))))
            {
              nexd(0,-1)=' ';
              nexd(0,-2)=' ';
            }
            break;
        }
        if (iscrate(cur))
        {
          if (curd(1,0) == ')')
          {
            int bx=0; int flag=0;
            while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
            {
              if (curd(bx-1,0) <= ' ')
              {
                flag = 1;
              }
              bx--;
            }
            if (flag)
            {
              bx=0;
              while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
              {
                nexd(bx-1,0) = curd(bx,0);
                bx--;
              }
              nex = ')'; nexd(1,0) = ' ';
            }
          }
          if (curd(-1,0) == '(')
          {
            int bx=0; int flag=0;
            while (iscrate(curd(bx,0)) && (issupport(curd(bx,1))))
            {
              if (curd(bx+1,0) <= ' ')
              {
                flag=1;
              }
              bx++;
            }
            if (flag)
            {
              bx=0;
              while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1))))
              {
                nexd(bx+1,0) = curd(bx,0);
                bx++;
              }
              nex = '('; nexd(-1,0)= ' ';
            }
          }
          if ((curd(-1,0)=='C') ||
              (curd(1,0)=='C') ||
              (curd(0,-1)=='C') ||
              (curd(0,1)=='C')) nex = ' ';
        }
        if ((curd(-1,0)=='F') ||
            (curd(1,0)=='F') ||
            (curd(0,-1)=='F') ||
            (curd(0,1)=='F')) nex = ' ';
      }
    }
    if (interactive) {
      char s[80];
      fgets(s, 79, stdin);
      if (s[0] == 'q') done = 1;
    } else {
      if (deldur > 0) {
        rube_delay (deldur);
      }
#ifdef MSDOS
      if (kbhit()) {
        char c;
        c = getch();
        if (c == 27) done = 1;
      }
#endif
    }
    memcpy(pg, pg2, LINEWIDTH * PAGEHEIGHT * sizeof(cell));
  }
  if (debug) printf ("%c[22;1H", 27);
#if MSDOS
  _setcursortype(_NORMALCURSOR);
#endif
  exit (0);
}

int curd(int zdx, int zdy)
{
  int r = (y+zdy) * LINEWIDTH + (x+zdx);
  if (r < 0 || r >= LINEWIDTH * PAGEHEIGHT)
    return 0;
  return pg[r].c;
}

int isramp(char c)
{
  return ((c=='/')||(c=='\\'));
}

int isblock(char c)
{
  return ((c=='='));
}

int issupport(char c)
{
  return ((c=='=')||iscrate(c)||(c=='(')||(c==')')||(c==';')||
          (c=='/')||(c=='\\')||(c==':')||(c=='*')||(c==',')||
          (c=='>')||(c=='<')||(c=='O')||(c=='W')||(c=='M')||
          (c=='A')||(c=='V')||(c=='~')||(c=='.'));
}

int iscrate(char c)
{
  return ((c=='0')||(c=='1')||(c=='2')||(c=='3')||
          (c=='4')||(c=='5')||(c=='6')||(c=='7')||
          (c=='8')||(c=='9')||(c=='a')||(c=='b')||
          (c=='c')||(c=='d')||(c=='e')||(c=='f'));
}

int ctoh(char c)
{
  if((c>='0') && (c<='9')) return (c-'0'); else return ((c-'a')+10);
}

char htoc(int i)
{
  if((i>=0) && (i<=9)) return ((char)(i+'0')); else return ((char)(i+'a')-10);
}

void rube_delay(int msec)
{
#if MSDOS
  delay (msec);
#elif _POSIX_C_SOURCE
  struct timespec d;

  d.tv_sec = msec / 1000;
  msec %= 1000;
  d.tv_nsec = msec * 1000000;
  nanosleep(&d, NULL);
#else
  sleep(msec / 1000);
#endif
}