git @ Cat's Eye Technologies FBBI / master src / f98i.c
master

Tree @master (Download .tar.gz)

f98i.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
/*
   f98i.c for fbbi.c v1.0 May 2011 Chris Pressey
   Funge-98 Instructions - source.

   Copyright (c)1998-2011 Cat's Eye Technologies.  All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notices, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notices, this list of conditions, and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.
   3. Neither the names of the copyright holders nor the names of their
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission. 

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.

*/

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

#include "fbbi.h"
#include "bf98spc.h"
#include "f98ip.h"
#include "f98i.h"
#include "f98fp.h"

extern char ** environ;
extern char ** global_argv;
extern int global_argc;
extern int fungeprog_arg;

/*-- INSTRUCTIONS */

void fi_space  (ip * i) { i = i; /* THIS NEVER HAPPENS */ }
void fi_nop    (ip * i) { fi_space(i); }
void fi_reverse(ip * i) { NEG(i->dx); NEG(i->dy); }

void fi_die    (ip * i) { i->hm = 1; i->ec = 0; }
void fi_quit   (ip * i) { i->hm = 1; i->ec = ip_pop(i); }

void fi_digit  (ip * i) { ip_push(i, i->ir - '0'); }
void fi_hexdig (ip * i) { ip_push(i, i->ir - 'a' + 10); }

void fi_north  (ip * i) { DELTA(i,0,-1); }
void fi_south  (ip * i) { DELTA(i,0,1); }
void fi_east   (ip * i) { DELTA(i,1,0); }
void fi_west   (ip * i) { DELTA(i,-1,0); }

void fi_vertif (ip * i)
{
  i->dx = 0;
  if (ip_pop(i) != 0)
    i->dy = -1; else
    i->dy = 1;
}
void fi_horzif (ip * i)
{
  i->dy = 0;
  if (ip_pop(i) != 0)
    i->dx = -1; else
    i->dx = 1;
}
void fi_add    (ip * i) { ip_push(i, ip_pop(i) + ip_pop(i)); }
void fi_sub    (ip * i) { GET_ab(i); ip_push(i, i->b - i->a); }
void fi_mul    (ip * i) { ip_push(i, ip_pop(i) * ip_pop(i)); }
void fi_div    (ip * i) { GET_ab(i); if (i->a != 0) ip_push(i, i->b / i->a); else ip_push(i, 0); }
void fi_mod    (ip * i) { GET_ab(i); if (i->a != 0) ip_push(i, i->b % i->a); else ip_push(i, 0); }
void fi_swap   (ip * i) { GET_ab(i); ip_push(i, i->a); ip_push(i, i->b); }
void fi_string (ip * i) { i->sm = !i->sm; }
void fi_odec   (ip * i)
{
  i->b = 0;
  #ifndef FBBI_MINIMAL
  if (trace)
    i->b = printf ("***** Decimal: %ld\n", ip_pop(i)); else
  #endif
    i->b = printf ("%ld ", ip_pop(i));
  fflush (stdout);
  if (i->b == EOF) ip_reverse(i);
}

void fi_ochar  (ip * i)
{
  GET_a(i);
  i->b = 0;
  #ifndef FBBI_MINIMAL
  if (trace)
    i->b = printf ("***** Character: 0x%08lx ('%c')\n", i->a, PRINT(i->a)); else
  #endif
    i->b = printf ("%c", (char)i->a);
  fflush (stdout);
  if (i->b == EOF) ip_reverse(i);
}

void fi_dup    (ip * i) { GET_a(i); ip_push(i, i->a); ip_push(i, i->a);  }
void fi_not    (ip * i) { if (ip_pop(i) != 0) ip_push(i, 0); else ip_push(i, 1); }
void fi_greater(ip * i) { GET_a(i); if (ip_pop(i) > i->a) ip_push(i, 1); else ip_push(i, 0); }
void fi_ramp   (ip * i) { ip_march(i); }
void fi_char   (ip * i) { ip_march(i); ip_push(i, bfspace_fetch(i->bs, i->x, i->y)); }
void fi_store  (ip * i) { ip_march(i); bfspace_store(i->bs, i->x, i->y, ip_pop(i)); }
void fi_pop    (ip * i) { GET_a(i); }
void fi_away   (ip * i)
{
  DELTA(i,0,1);
  switch ((rand() / 32) % 4)
  {
    case 0: DELTA(i,1,0); break;
    case 1: DELTA(i,-1,0); break;
    case 2: i->dy=-1;
  }
}

void fi_idec   (ip * i)
{
  fflush(stdin);
  do
  {
    i->a = (long)0|fgetc(stdin);
    if ((i->a >= '0') && (i->a <= '9')) ungetc(i->a, stdin);
  } while ((i->a < '0') || (i->a > '9'));

  if(scanf ("%ld", &i->a) != EOF)
  {
    ip_push(i, i->a);
  } else
  {
    ip_reverse(i);
  }
}

void fi_ichar  (ip * i)
{
  fflush(stdin);
  i->a = (long)0 | fgetc(stdin);
  if (i->a != ((long)0 | EOF))
  {
    ip_push(i, i->a);
  } else
  {
    ip_reverse(i);
  }
}

void fi_get    (ip * i) { GET_a(i); ip_push(i, bfspace_fetch(i->bs, ip_pop(i)+i->sx, i->a+i->sy)); }
void fi_put    (ip * i) { GET_ab(i); bfspace_store(i->bs, i->b+i->sx, i->a+i->sy, ip_pop(i)); }

void fi_skip   (ip * i) { i = i; /* THIS NEVER HAPPENS */ }

void fi_rleft  (ip * i) { i->a = i->dy; i->dy = -1 * i->dx; i->dx = i->a; }
void fi_rright (ip * i) { i->a = i->dx; i->dx = -1 * i->dy; i->dy = i->a; }
void fi_jump   (ip * i) { GET_a(i); i->x += i->dx * i->a; i->y += i->dy * i->a; }
void fi_clear  (ip * i) { ip_clearstack(i); }
void fi_compare(ip * i)
{
  GET_ab(i);
  if (i->a > i->b)
     fi_rleft(i); else
    if (i->a < i->b)
      fi_rright(i);
}
void fi_delta  (ip * i) { i->dy = ip_pop(i); i->dx = ip_pop(i); }
void fi_under  (ip * i)
{
  cell f;
  GET_a(i);
  if (ip_stacks_measure(i) <= 1)
  {
    ip_reverse(i);
  } else if (i->a > 0)
  {
    for (i->b = 0; i-> b < i->a; i->b++)
    {
      f = ip_pop_soss(i);
      ip_push(i, f);
    }
  } else if (i->a < 0)
  {
    for (i->b=0;i->b > i->a;i->b--)
    {
      ip_push_soss(i, ip_pop(i));
    }
  }
}
void fi_begin (ip * i)
{
  GET_a(i);
  /* Regardless of sign of popped value, push new stack on SS */
  if (!ip_allocstack(i))
  {
    ip_reverse(i);
    return;
  }
  if (i->a<0)
  {
    /* Negative argument to { pushes |n| 0s onto the SOSS */
    while(i->a < 0)
    {
      ip_push_soss(i, 0);
      i->a++;
    }
  } else if (i->a > 0) ip_transfer(i, i->a);
  ip_push_soss(i, i->sx);
  ip_push_soss(i, i->sy);
  i->sx = i->x + i->dx;
  i->sy = i->y + i->dy;
  /*printf("$$$ EXECUTING 'BEGIN': assigned storage offset %ld, %ld\n", i->sx, i->sy);*/
}

void fi_end   (ip * i)
{
  /* check if there *is* a SOSS very first */
  if (ip_stacks_measure(i) <= 1)
  {
    ip_reverse(i);
    return;
  }
  GET_a(i);
  if (i->a<0)
  {
    /* Negative argument to } destroys top |n| values on the SOSS. */
    if(ip_stacks_measure(i) > 1)
    {
      while(i->a < 0)
      {
        ip_pop_soss(i);
        i->a++;
      }
    }
  }
  /* bugfix 2003.0722: } should still attempt to pop the TOSS */
  i->sy = ip_pop_soss(i);
  i->sx = ip_pop_soss(i);
  if (i->a > 0) ip_transfer(i, -1 * i->a);
  ip_freestack(i);
  /*printf("$$$ EXECUTING 'END': restored storage offset %ld, %ld\n", i->sx, i->sy);*/
}

void fi_in    (ip * i)
{
  cell c, d, flags;
  FILE *f; char s[256];

  ip_pop_string(i, s);
  flags = ip_pop(i);
  GET_ab(i);

  if((f=fopen(s, "r"))!=NULL)
  {
    bfspace_fread(i->bs, f, i->b+i->sx, i->a+i->sy, &c, &d, flags);
    fclose(f);
    ip_push(i, c);
    ip_push(i, d);
    ip_push(i, i->b);
    ip_push(i, i->a);
  } else ip_reverse(i);
}

void fi_out   (ip * i)
{
  FILE *f; char s[256];
  cell zx, zy, flags;
  ip_pop_string(i, s);
  flags = ip_pop(i);
  GET_ab(i);

  zy = ip_pop(i);
  zx = ip_pop(i);

  if((f=fopen(s, "w"))!=NULL)
  {
    bfspace_fwrite(i->bs, f, i->b+i->sx, i->a+i->sy,
                   zx, zy, flags);
    fclose(f);
  } else ip_reverse(i);
}
void fi_exec  (ip * i)
{
  char s[256];
  ip_pop_string(i, s);
  ip_push(i, system(s));
}
void fi_unimp (ip * i)
{
  #ifndef FBBI_MINIMAL
  if(warn)
    fprintf(stderr, "fbbi warning: unimplemented 0x%08lx at (%ld,%ld)\n",
                    i->ir, i->x, i->y);
  #endif
  fi_reverse(i);
}
void fi_nonb93 (ip * i)
{
  #ifndef FBBI_MINIMAL
  if(warn)
    fprintf(stderr, "fbbi warning: non-Befunge-93 instruction 0x%08lx at (%ld,%ld)\n",
                    i->ir, i->x, i->y);
  #endif
  /* fi_reverse(i); not in b93 compat mode! */
}


void fi_iterate(ip * i)
{
  cell c, sx, sy;
  c = ip_pop(i);
  sx = i->x;
  sy = i->y;
  ip_move(i);
  i->ir = bfspace_fetch(i->bs, i->x, i->y);
  if (c <= 0)
    return;
  i->x = sx;
  i->y = sy;
  if ((i->ir < 32) || (i->ir > 126))
  {
    fi_unimp(i);
    return;
  }
  for(;c;c--)
    instable[(char)i->ir-32](i);
}

void fi_pushsem(ip * i)
{
  i->a = get_fingerprint(i);
#ifndef FBBI_MINIMAL
  if (fingerprint_semantics(i, i->a, 1))
  {
    ip_push(i, i->a);
    ip_push(i, 1);
  } else
#endif
  fi_reverse(i);
}

void fi_popsem(ip * i)
{
  i->a = get_fingerprint(i);
#ifndef FBBI_MINIMAL
  if (!fingerprint_semantics(i, i->a, 0))
#endif
    fi_reverse(i);
}

void fi_sysinfo(ip * i)
{
  time_t t;
  struct tm * lt;
  cell sscs;

  t = time(NULL);
  lt = localtime(&t);
  GET_a(i);
  sscs = ip_stack_measure(i);

  ip_push(i, 0);              /* environment double */
  ip_push(i, 0);              /* terminator */
  /*
  for(i->b=0;environ[i->b]!=NULL&&strlen(environ[i->b])>0;i->b++)
    ip_push_string(i, environ[i->b]);
  */

  ip_push(i, 0);              /* arguments double */
  ip_push(i, 0);              /* terminator */
  for(i->b=global_argc-1;i->b>=fungeprog_arg;i->b--)
    ip_push_string(i, global_argv[i->b]);

  for(i->b=-1 * ip_stacks_measure(i) + 1;i->b < 0;i->b++)        /* size of each stack under current */
     ip_push(i, ip_stack_measure_offset(i, i->b));
  ip_push(i, sscs);
  ip_push(i, ip_stacks_measure(i));    /* size of stack stack */
  ip_push(i,
        ((long)lt->tm_hour << 16)
      | ((long)lt->tm_min << 8)
      | ((long)lt->tm_sec)); /* hour/min/sec */
  ip_push(i,
        ((long)lt->tm_year << 16)
      | ((long)lt->tm_mon << 8)
      | ((long)lt->tm_mday)); /* year/month/day */

  ip_push(i, i->bs->max_x - i->bs->min_x);  /* largest */
  ip_push(i, i->bs->max_y - i->bs->min_y);

  ip_push(i, i->bs->min_x);                 /* smallest */
  ip_push(i, i->bs->min_y);

  ip_push(i, i->sx);            /* offset */
  ip_push(i, i->sy);
  ip_push(i, i->dx);            /* delta */
  ip_push(i, i->dy);
  ip_push(i, i->x);             /* position */
  ip_push(i, i->y);
  ip_push(i, 42);            /* unique Team ID */
  ip_push(i, 42);            /* unique IP ID */
  ip_push(i, 2);             /* scalars per vector */
#ifdef __MSDOS__
  ip_push(i, (long)0|'\\');  /* MS-DOS paradigm */
#else
  ip_push(i, (long)0|'/');   /* probably.  Mac users will have 2 suffer 4 now :-> */
#endif
  ip_push(i, 1);             /* C system() paradigm */
  ip_push(i, FBBI_VERSION_MAJOR * 100 + FBBI_VERSION_MINOR);
  ip_push(i, 0x46424249);    /* handprint */
  ip_push(i, sizeof(cell));  /* bytes per cell */
  ip_push(i, 0x0000000e);    /* flags */

  if (i->a > 0)
  {
    i->a = ip_stack_peek(i, i->a-1);
    while (ip_stack_measure(i) > sscs) ip_pop(i);
    ip_push(i, i->a);
  }
}

/*-- INSTRUCTION TABLE */

void (*instable[96])(ip *) =
{
  /* 32-63 */
  fi_space,  fi_not,    fi_string, fi_ramp,   fi_pop,    fi_mod,    fi_idec,   fi_char,
  fi_pushsem,fi_popsem, fi_mul,    fi_add,    fi_ochar,  fi_sub,    fi_odec,   fi_div,
  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,
  fi_digit,  fi_digit,  fi_dup,    fi_skip,   fi_west,   fi_exec,   fi_east,   fi_away,

  /* 64-95 */
  fi_die,    fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,
  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,
  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,  fi_unimp,
  fi_unimp,  fi_unimp,  fi_unimp,  fi_rleft,  fi_swap,   fi_rright, fi_north,  fi_horzif,

  /* 96-127 */
  fi_greater,fi_hexdig, fi_hexdig, fi_hexdig, fi_hexdig, fi_hexdig, fi_hexdig, fi_get,
  fi_unimp,  fi_in,     fi_jump,   fi_iterate,fi_unimp,  fi_unimp,  fi_clear,  fi_out,
  fi_put,    fi_quit,   fi_reverse,fi_store,  fi_unimp,  fi_under,  fi_south,  fi_compare,
  fi_delta,  fi_sysinfo,fi_nop,    fi_begin,  fi_vertif, fi_end,    fi_ichar,  fi_unimp
};

void (*b93instable[96])(ip *) =
{
  /* 32-63 */
  fi_space,  fi_not,    fi_string, fi_ramp,   fi_pop,    fi_mod,    fi_idec,   fi_nonb93,
  fi_nonb93, fi_nonb93, fi_mul,    fi_add,    fi_ochar,  fi_sub,    fi_odec,   fi_div,
  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,  fi_digit,
  fi_digit,  fi_digit,  fi_dup,    fi_nonb93, fi_west,   fi_nonb93, fi_east,   fi_away,

  /* 64-95 */
  fi_die,    fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93,
  fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93,
  fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93,
  fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_swap,   fi_nonb93, fi_north,  fi_horzif,

  /* 96-127 */
  fi_greater,fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_get,
  fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93,
  fi_put,    fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_south,  fi_nonb93,
  fi_nonb93, fi_nonb93, fi_nonb93, fi_nonb93, fi_vertif, fi_nonb93, fi_ichar,  fi_nonb93
};