git @ Cat's Eye Technologies Maentwrog / master src / maentw.c
master

Tree @master (Download .tar.gz)

maentw.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
/*
 * maentw.c
 *
 * Maentwrog (RPN calculator & simple interpreted language)
 * - derived from rpn, Aug 1993 Chris Pressey
 * - updated Jul 1997 Chris Pressey, fixed minor bugs
 * - updated Jul 1998 Chris Pressey, fixed more minor bugs
 * -         and ANSI C-ized: now case sensitive
 * - updated Jul 2010 Chris Pressey, buildability w/gcc & pcc
 * - updated Sep 2014 Chris Pressey, buildability and warnings avoidance
 * Usage : maentw maentw-expressions   executes and exits
 *         maentw                      goes into interactive mode
 *         maentw <maentwrog-file      runs file through maentw
 *
 * This work is in the public domain.  See the file UNLICENSE for more info.
 */

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

#define DEFSIZE 1024

struct stack                    /* stack structure, for values on stack */
{
  signed long val;
  struct stack *next;
} *head;                        /* head of stack */

struct word                     /* word structure, for words (pre- and user- */
{                               /* defined) */
  char name[80];                /* name of word */
  char *macro;                  /* macro (user-defined; only executed if
                                 * fcn==0) */
  int fcn;                      /* built-in fcn or 0 for user-defined fcn */
  struct word *next;
} *whead;                       /* head of words */

struct vari                     /* variable structure, for variables */
{
  char name[80];                /* name of variable */
  signed long value;                    /* values of variable (signed longs only) */
  struct vari *next;
} *vhead;                       /* head of variables */

int debug=0;

/* prototypes */

/* word-handling */
struct word *addword(const char *, const char *, int);
struct word *lookup(const char *);
void initwords(void);
void makeword(void);

/* variable-handling */
struct vari *addvari(char *);
struct vari *getvari(char *);
void setvari(char *, signed long);

/* stack-handling */
void push(signed long);
signed long pop(void);

/* functions */
void dofunc(struct word *);
void words(void);
void vars(void);
signed long sizestack(void);

/* parsing and interpreting */
char *strdupe(const char *);
void process(char *);
void procstr(char *);

/* entry point */
int main(int, char **);

int main(argc, argv)
  int argc;
  char **argv;
{
  char s[80];
  int i;

  head = NULL;                  /* init */
  whead = NULL;
  vhead = NULL;
  initwords();
  if (argc != 1)
  {
    for(i=2;i<=argc;i++) procstr(argv[i-1]);
    exit(0);
  }
  scanf("%s", s);               /* process commands/values from stdin */
  while (!feof(stdin))
  {
    process(s);
    scanf("%s", s);
  }
  return 0;
}

/*
 * processes the word in s according to parsing rules.
 *
 * if word starts with a digit or a -digit, it is converted to a float and put
 * on the stack.
 *
 * if word starts with a = then the variable following equals is assigned the
 * last value popped off the stack.
 *
 * if word starts with a * a variable is defined using that word.
 *
 * if word starts with a @, the stack is popped, is checked for the boolean
 * value, and if true, executes it. (if.)
 *
 * if word starts with a $, the stack is popped, and command is repeated
 * that number of times.
 *
 * if word starts with a [, the stack is popped, is checked for the boolean
 * value, and if true, executes it.  Then goes back and pops the stack
 * again (while.)
 *
 * if word is in words list, that function is executed.
 *
 * if word is in variables list, the value of it is pushed onto the stack.
 *
 * otherwise generates an error.
 */
void process(char *s)
{
  struct word *w;
  struct vari *v;
  int i;

  if(debug) printf("%s ", s);

  if (isdigit((int)s[0]) || (s[0] == '-' && isdigit((int)s[1])))
    push(atoi(s));
  else if (s[0] == '=')
    setvari(s + 1, pop());
  else if ((s[0] == '*') && (isalpha((int)s[1])))
    addvari(s + 1);
  else if (s[0] == '@')
  {
    if (pop())
      if ((w = lookup(s + 1)))
        dofunc(w);
  }
  else if (s[0] == '$')
  {
    for(i=pop();i;i--)
      if ((w = lookup(s + 1)))
        dofunc(w);
  }
  else if (s[0] == '[')
  {
    for(;;)
    {
      if (pop())
      {
        if ((w = lookup(s + 1)))
          dofunc(w);
      } else break;
    }
  }
  else if ((w = lookup(s)))
    dofunc(w);
  else if ((v = getvari(s)))
    push(v->value);
  else
    printf("unknown command '%s'\n", s);
}

char *strdupe(const char *s)
{
  char *t = malloc(strlen(s) + 1);
  strcpy(t, s);
  return t;
}

/*
 * processes each word in the string s.
 * strtok doesn't work with recursion :-(
 */
void procstr(char *s)
{
  char *h=strdupe(s);
  char *g, *gg;
  g = h;

  for (;;)
  {
    gg = g;
    while (!isspace((int)gg[0])&&gg[0])
      gg++;
    if (!gg[0])
      break;
    gg[0] = 0;

    process(g);

    gg[0] = ' ';
    g = gg;
    while (isspace((int)g[0])&&g[0])
      g++;
  }

  free(h);                      /* called with strdupe(), so we must free */
}

/*
 * adds a unique word to the list of words.
 */
struct word *addword(const char *name, const char *macro, int fcn)
{
  struct word *new;
  for (new = whead; new; new = new->next)
    if (!strcmp(new->name, name))
    {
      printf("already exists\n");
      return NULL;
    }
  new = (struct word *) malloc(sizeof(struct word));
  strcpy(new->name, name);
  new->macro = strdupe(macro);
  new->fcn = fcn;

  new->next = whead;
  whead = new;
  return new;
}

/*
 * attempts to find the word 'name' in the words list.  returns NULL if it
 * could not be found.
 */
struct word *lookup(const char *name)
{
  struct word *l = whead;
  struct word *k = NULL;

  while (l)
  {
    if (!strcmp(name, l->name))
    {
      k = l;
      l = NULL;
    } else
      l = l->next;
  }
  return (k);
}

/*
 * initialize the words list with all the built-in words.
 */
void initwords()
{
  addword("bye", "", 200);
  addword("rem", "", 199);
  addword("debug", "", 198);
  addword("vars", "", 101);
  addword("words", "", 100);

  addword("free", "", 91);
  addword("alloc", "", 90);

  addword(";", "", 81);
  addword(":", "", 80);

  addword("size", "", 50);
  addword("dup", "", 51);
  addword("swap", "", 52);
  addword("pop", "", 53);

  addword("get", "", 45);
  addword("put", "", 44);

  addword("rnd", "", 40);

  addword(">", "", 22);
  addword("<", "", 21);
  addword("==", "", 20);

  addword(".", "", 1);
  addword("..", "", 6);

  addword("mod", "", 30);

  addword("/", "", 5);
  addword("*", "", 4);
  addword("-", "", 3);
  addword("+", "", 2);
}

/*
 * makes a word, reading between the : and ;, defining the new word's macro,
 * and adds it.
 */
void makeword()
{
  char s[80];
  char t[80];
  char *y;
  unsigned int size = DEFSIZE;

  y = (char *)malloc(size);
  scanf("%s", s);
  strcpy(y, "");
  scanf("%s", t);
  while (strcmp(t, ";"))
  {
    if ((strlen(y)+strlen(t))>size)
    {
      printf("out of memory\n");
      exit(0);
    }
    strcat(y, t);
    strcat(y, " ");
    scanf("%s", t);
  }
  {
    char *n = (char *)malloc(strlen(y)+1);
/*  printf("(usage : %d bytes)\n", strlen(y)+1); */
    strcpy(n, y);
    free(y);
    y = n;
  }
  addword(s, y, 0);
}

/*
 * pushes a value onto the stack.
 */
void push(signed long val)
{
  struct stack *s;
  s = (struct stack *) malloc(sizeof(struct stack));
  s->val = val;
  s->next = head;
  head = s;
}

/*
 * pops a value off the stack. generates error and returns 0.0 in case of
 * underflow.
 */
signed long pop()
{
  signed long v;
  struct stack *s = head;
  if (s)
  {
    v = head->val;
    head = head->next;
    free(s);
    return v;
  } else
  {
    printf("stack underflow\n");
    return 0;
  }
}

/*
 * adds a unique variable to the vari list.
 */
struct vari *addvari(char *name)
{
  struct vari *v;
  for (v = vhead; v; v = v->next)
    if (!strcmp(v->name, name))
    {
      printf("already exists\n");
      return NULL;
    }
  v = (struct vari *) malloc(sizeof(struct vari));
  strcpy(v->name, name);
  v->next = vhead;
  vhead = v;
  return v;
}

/*
 * gets the value of a variable off the variable list.
 */
struct vari *getvari(char *name)
{
  struct vari *l = vhead;
  struct vari *k = NULL;

  while (l)
  {
    if (!strcmp(name, l->name))
    {
      k = l;
      l = NULL;
    } else
      l = l->next;
  }
  return (k);
}

/*
 * sets the value of a variable
 */
void setvari(char *name, signed long value)
{
  struct vari *l = vhead;
  while (l)
  {
    if (!strcmp(name, l->name))
    {
      l->value = value;
      l = NULL;
    } else
      l = l->next;
  }
}

/*
 * lists all words in mw's words list
 */
void words()
{
  struct word *w;
  for (w = whead; w; w = w->next)
    printf("%s ", w->name);
  printf("\n");
}

signed long sizestack()
{
  signed long total = 0;
  struct stack *s = head;

  while(s)
  {
    total++;
    s=s->next;
  }
  return total;
}

/*
 * list all variables in mw's vari list
 */
void vars()
{
  struct vari *v;
  for (v = vhead; v; v = v->next)
    printf("%-16s %ld\n", v->name, v->value);
}

/*
 * execute function of a word.  if w->fcn is 0, it will run processtr on the
 * word.  otherwise, a built-in function will be called. see the rpn
 * documentation on built-in functions.
 */
void dofunc(struct word * w)
{
  signed long a, b;
  signed long *ax;
  switch (w->fcn)
  {
    case 1:                     /* output (.) */
      printf("%ld\n", pop());
      break;
    case 2:                     /* add (+) */
      push(pop() + pop());
      break;
    case 3:                     /* subtract (-) */
      a = pop();
      b = pop();
      push(b - a);
      break;
    case 4:                     /* multiply (*) */
      push(pop() * pop());
      break;
    case 5:                     /* divide (/) */
      a = pop();
      b = pop();
      push(b / a);
      break;
    case 6:                     /* output ASCII (..) */
      printf("%c", (char)pop());
      break;
    case 20:
      push(!pop() == !pop());
      break;
    case 21:
      push(pop()>pop());
      break;
    case 22:
      push(pop()<pop());
      break;
    case 30:
      a = pop();
      b = pop();
      push(b % a);
      break;
    case 40:
      push(a);                  /* something random */
      break;
    case 44:                    /* put */
      a = pop();
      b = pop();
      if (b % 4)
        printf("must be longword boundary\n"); else
        {
          ax = (signed long *)b;
          *ax = a;
        }
      break;
    case 45:
      a = pop();
      if (a % 4)
        printf("must be longword boundary\n"); else
        {
          ax = (signed long *)a;
          push(*ax);
        }
      break;
    case 50:
      push(sizestack());        /* size of stack (size) */
      break;
    case 51:                    /* duplicate top element (dup) */
      a = pop();
      push(a); push (a); break;
    case 52:                    /* swap top elements (swap) */
      a = pop();
      b = pop();
      push(a); push (b);
      break;
    case 53:                    /* pop element (pop) */
      pop();
      break;
    case 80:                    /* define word (: ... ;) */
      makeword();
      break;
    case 81:                    /* null (;) */
      break;
    case 90:
      a = pop();
      push((signed long)malloc(a*sizeof(signed long)));
      break;
    case 91:
      free((void *)pop());
      break;
    case 100:                   /* list known words (words) */
      words();
      break;
    case 101:                   /* list variables (vars) */
      vars();
      break;
    case 198:
      debug = 1;
      break;
    case 199:                   /* rem */
    {
      char t[80];
      scanf("%s", t);
      while (strcmp(t, ";"))
        scanf("%s", t);
    }
      break;
    case 200:                   /* exit mw (bye) */
      exit(0);
      break;
    default:                    /* user-defined word */
      procstr(w->macro);
  }
}