git @ Cat's Eye Technologies Sally / master src / sally.c
master

Tree @master (Download .tar.gz)

sally.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
/*
  sally.c: Sally language subroutines v2003.1104
  (c)2003 Cat's Eye Technologies.  All rights reserved.
*/

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

#include "sally.h"

/******************************  TYPE SYSTEM  ******************************/

type * typestack[256];
int tsp = 0;

type * debit_type(void) /* pop type from type stack */
{
  if (tsp > 0) return typestack[tsp--];
  error("Type underflow");
  return NULL;
}

void credit_type(type * t) /* push type onto type stack */
{
  if (tsp < 255) typestack[++tsp] = t; else error("Type Overflow");
}

type * construct_type(int id, type * of, type * to)
{
  type * t = (type *)malloc(sizeof(type));
  if (t != NULL)
  {
    t->id = id;
    t->of = of;
    t->to = to;
    t->name = NULL;
  }
  return t;
}

type * construct_pair(type * a, type * b)
{
  if (a == NULL) return b;
  if (b == NULL) return a;
  return construct_type(TYPEID_pair, a, b);
}

type * construct_function(type * a, type * b)
{
  return construct_type(TYPEID_func, a, b);
}

type * unname_type(type * a)
{
  if (a == NULL) return NULL;
  if (a->id >= 128)
    return construct_type(a->id, unname_type(a->of), unname_type(a->to));
  if (a->name == NULL) return a;
  return a->of;
}

int count_type(type * x)  /* returns number of primitives in tuple */
{
  if (x == NULL) return 0;
  if (x->id != TYPEID_pair) return 1;
  return count_type(x->of) + count_type(x->to);
}

type * nth_type(type * x, int n) /* returns one primitive from a tuple */
{
  int l = count_type(x);
  while (n < l && x != NULL && x->id == TYPEID_pair)
  {
    l--;
    x = x->of;
  }
  if (x->id == TYPEID_pair) x = x->to;
  return x;
}

type * domain_type(type * x) /* returns type of arguments to function */
{
  if (x == NULL) return NULL;
  if (x->id == TYPEID_func) return x->of;
  return x;
}

type * range_type(type * x) /* returns type of result of function */
{
  if (x == NULL) return NULL;
  if (x->id == TYPEID_func) return x->to;
  return x;
}

int equivalent_types(type * a, type * b)
{
  if (a == b) return 1;
  if (a == NULL || b == NULL) return 0;
  if (a->name != NULL && b->name != NULL) return (a->name == b->name);
  if (a->id == b->id)
  {
    if (a->id >= 128) return equivalent_types(a->of, b->of) &&
			     equivalent_types(a->to, b->to);
		 else return 1;
  }
  return 0;
}

void print_type(FILE * f, type * t)
{
  if (t==NULL)
  {
    fprintf(f, "void");
    return;
  } else
  if (t->name != NULL)
  {
    fprintf(f, "%s", t->name->id);
  } else
  switch(t->id)
  {
    case TYPEID_type: fprintf(f, "type"); break;
    case TYPEID_int:  fprintf(f, "int"); break;
    case TYPEID_char: fprintf(f, "char"); break;
    case TYPEID_pair: print_type(f, t->of);
		      fprintf(f, ", ");
		      print_type(f, t->to); break;
    case TYPEID_func: fprintf(f, "{");
		      print_type(f, t->of);
		      fprintf(f, " -> ");
		      print_type(f, t->to);
		      fprintf(f, "}"); break;
    case TYPEID_many: fprintf(f, "many ");
		      print_type(f, t->of); break;
    default: fprintf(f, "type error (%d)", t->id);
  }
}

/****************************** SYMBOL TABLE ******************************/

symbol * head = NULL;

/* assumed: preceding call to sym_lookup failed. */
symbol * sym_defn(char * s, type * t)
{
  symbol * n = (symbol *)malloc(sizeof(symbol));
  if (n == NULL)
  {
    fprintf(stderr, "Could not allocate symbol table record\n");
  } else
  {
    n->id = (char *)malloc(strlen(s)+1);
    if (n->id == NULL)
    {
      fprintf(stderr, "Could not allocate lexeme\n");
    } else
    {
      strcpy(n->id, s);
      n->t = t;
      n->next = head;
      head = n;
      return n;
    }
  }
  return NULL;
}

symbol * sym_lookup(char * s)
{
  symbol * h = head;
  while(h != NULL)
  {
    if(!strcmp(s, h->id)) return h;
    h = h->next;
  }
  return NULL;
}

/******************************    SCANNER    ******************************/

FILE * infile;
FILE * outfile;
char   token[256];
int    lino = 1;
int    column = 0;

int isatype(void)
{
  symbol * s = sym_lookup(token);
  if (s != NULL && s->t->id == TYPEID_type) return 1;
  if (tokeq("void") || tokeq("int") || tokeq("char") || tokeq("like")) return 1;
  return 0;
}

void error(char * s)
{
  fprintf(stderr, "Error (line %d, column %d, token '%s'): %s.\n",
	  lino, column, token, s);
}

void scan(void)
{
  char x;
  int i = 0;

  chkeof;
  x = (char)getc(infile); column++;
  chkeof;
  while (x <= ' ')
  {
    if (x == '\n') { lino++; column = 0; }
    x = (char)getc(infile); column++;
    chkeof;
  }

  if (x == '"')
  {
    token[i++] = x;
    x = (char)getc(infile); column++;
    chkeof;
    while (x != '"')
    {
      token[i++] = x;
      x = (char)getc(infile); column++;
      chkeof;
    }
    token[i] = 0;
    return;
  } else
  if (!isspace((int)x) && !feof(infile))
  {
    while (!isspace((int)x) && !feof(infile))
    {
      token[i++] = x;
      x = (char)getc(infile); column++;
      chkeof;
    }
    ungetc(x, infile); column--;
    token[i] = 0;
    return;
  } else
  {
    token[0] = 0;
    return;
  }
}

/******************************    PARSER    ******************************/

type * application(type * func, type * avail)
{
  type * args = NULL;
  int i;

  while(count_type(args) < count_type(domain_type(func))
	&& token[0] != 0)
    args = construct_pair(args, instruction(avail));

  for(i=count_type(domain_type(func));i>=1;i--)
    if(!equivalent_types(debit_type(), nth_type(domain_type(func),i)))
      error("Type mismatch");

  for(i=1;i<=count_type(range_type(func));i++)
    credit_type(nth_type(range_type(func),i));

  return range_type(func);
}

type * instruction(type * avail)
{
  type * mytype = NULL;  /* the type of this instruction */
  symbol * s = sym_lookup(token);
  if (tokeq("do"))
  {
    int argnum;
    scan();
    argnum = atoi(token);
    if (argnum > count_type(avail))
      error("Maximum argument count exceeded");
    scan();
    mytype = application(nth_type(avail, argnum), avail);
    fprintf(outfile, "  (*arg%d)();\n", argnum);
  } else
  if (tokeq("the"))
  {
    scan();
    s = sym_lookup(token);
    scan();
    mytype = s->t;
    fprintf(outfile, "  push((int)&apply_%s);\n", s->id);
    credit_type(mytype);
  } else
  if (tokeq("as"))
  {
    int i = 0;
    scan();
    while(isatype())
      mytype = construct_pair(mytype, parse_type());
    while(i < count_type(unname_type(mytype)))
    {
      i += count_type(unname_type(instruction(avail)));
      debit_type();
    }
    for(i=1;i<=count_type(mytype);i++)
      credit_type(nth_type(mytype,i));
  } else
  if (tokeq("if"))
  {
    type * b = NULL;
    type * c = NULL;
    scan();

    (void)instruction(avail);
    fprintf(outfile, "  if(pop() != 0) {\n");
    (void)debit_type();

    b = instruction(avail);
    fprintf(outfile, "  } else {\n");
    (void)debit_type();

    c = instruction(avail);
    fprintf(outfile, "  }\n");
    (void)debit_type();

    if (!equivalent_types(b, c)) error("Need equivalent types in 'if'");
    mytype = b;
    credit_type(mytype);
  } else
  if (isdigit((int)token[0]))
  {
    int litnum = atoi(token);
    fprintf(outfile, "  push(%d);\n", litnum);
    scan();
    mytype = construct_type(TYPEID_int, NULL, NULL);
    credit_type(mytype);
  } else
  if (token[0] == '\'')
  {
    int litnum = (int)token[1];
    if (litnum==0) litnum = 32;
    fprintf(outfile, "  push(%d);\n", litnum);
    scan();
    mytype = construct_type(TYPEID_char, NULL, NULL);
    credit_type(mytype);
  } else
  if (token[0] == '$')
  {
    int argnum = atoi(token+1);
    if (argnum <= count_type(avail))
    {
      int j;
      type * x = nth_type(avail, argnum);
      if (x->name != NULL)
      {
	for(j = 1; j <= count_type(unname_type(x)); j++)
	  fprintf(outfile, "  push(arg%d_%d);\n", argnum, j);
      } else fprintf(outfile, "  push(arg%d);\n", argnum);
    } else error("Maximum argument count exceeded");
    scan();
    mytype = nth_type(avail, argnum);
    credit_type(mytype);
  } else
  if (s != NULL)
  {
    char name[80];
    strcpy(name, token);
    scan();
    mytype = application(s->t, avail);
    fprintf(outfile, "  apply_%s();\n", name);
  } else
  {
    error("Undefined symbol");
    scan();
  }
  return mytype;
}

type * parse_type(void)
{
  type * t = NULL;
  symbol * s = sym_lookup(token);

  if (s != NULL)
  {
    if (s->t->id == TYPEID_type)
    {
      t = s->t;
      scan();
    }
  } else
  if (tokeq("void")) scan(); else
  if (tokeq("int"))
  {
    t = construct_type(TYPEID_int, NULL, NULL);
    scan();
  } else
  if (tokeq("char"))
  {
    t = construct_type(TYPEID_char, NULL, NULL);
    scan();
  } else
  if (tokeq("like"))
  {
    scan();
    s = sym_lookup(token);
    if (s == NULL) error("Undefined function named in 'like'"); else
    if (s->t->id == TYPEID_type) error("'like' needs function name, not type name");
    t = s->t;
    scan();
  } else
  {
    error("Invalid type");
    scan();
  }
  return t;
}

void definition(void)
{
  type * t = NULL;
  type * u = NULL;
  symbol * s = NULL;

  while (!isatype() && token[0] != 0)
  {
    FILE * f;
    FILE * g = infile;
    int l = lino;
    char fn[256];
    sprintf(fn, "%s.sal", token);
    f = fopen(fn, "r");
    if (f != NULL)
    {
      infile = f;
      scan();
      while(token[0] != 0) definition();
      infile = g;
      fclose(f);
    }
    lino = l;
    scan();
  }

  while(isatype()) t = construct_pair(t, parse_type());

  if(sym_lookup(token) == NULL)
  {
    type * y = NULL;
    char name[80];
    strcpy(name, token);
    scan();
    if(tokeq("type"))
    {
      y = construct_type(TYPEID_type, t, NULL);
      s = sym_defn(name, y);
      y->name = s;
      scan();
    } else
    {
      while(isatype())
        u = construct_pair(u, parse_type());
      y = construct_function(t, u);
      s = sym_defn(name, y);
      fprintf(outfile, "/* ");
      print_type(outfile, y);
      fprintf(outfile, ": */ ");
      if(tokeq("proto"))
      {
        fprintf(outfile, "void apply_%s(void);\n", name);
        scan();
      } else
      if(s != NULL)
      {
	int i = 0, j = 0;
	int is_main = 0;
	int inputs = count_type(t);
	type * v = NULL;

	if(!strcmp(name, "main"))
	{
	  is_main = 1;
	  fprintf(outfile, "int main(int argc, char ** argv)\n{\n");
	  for(i=1; i <= inputs; i++)
	    fprintf(outfile, "  int arg%d = atoi(argv[%d]);\n", i, i);
  	fprintf(outfile, "  if(argc <= %d) { fprintf(stderr, \"%d values needed\"); exit(1); }\n", inputs, inputs);
        } else
	{
	  fprintf(outfile, "void apply_%s(void)\n{\n", name);
	  for(i = inputs; i >= 1; i--)
	  {
	    type * x = nth_type(t, i);
	    if(x->name != NULL)
	    {
	      for(j = count_type(unname_type(x)); j >= 1; j--)
		fprintf(outfile, "  int arg%d_%d = pop();\n", i, j);
	      fprintf(outfile, "    /* ");
	    } else
	    switch(x->id)
	    {
	      case TYPEID_int:  fprintf(outfile, "  int arg%d = pop(); /* ", i); break;
	      case TYPEID_char: fprintf(outfile, "  char arg%d = (char)pop(); /* ", i); break;
	      case TYPEID_func: fprintf(outfile, "  void (*arg%d)(void) = (void (*)())pop(); /* ", i); break;
	      default: fprintf(outfile, "  int arg%d = pop(); /* ", i);
	    }
	    print_type(outfile, x);
	    fprintf(outfile, " */\n");
          }
        }

        while(!isatype() && token[0] != 0)
	  v = construct_pair(v, instruction(t));

        for(i=count_type(range_type(s->t)); i >= 1; i--)
	  if (!equivalent_types(debit_type(), nth_type(range_type(s->t), i)))
	    error("Type mismatch");
        if(tsp != 0) error("Type mismatch");

        if(is_main)
        {
	  fprintf(outfile, "  {\n");
	  for(i=count_type(range_type(s->t)); i >= 1; i--)
	    fprintf(outfile, "    int result%d = pop();\n", i);
	  for(i=1; i <= count_type(range_type(s->t)); i++)
	    fprintf(outfile, "    printf(\"Result #%d: %cd\\n\", result%d);\n", i, '%', i);
	  fprintf(outfile, "  }\n  argv = argv;\n  return 0;\n");
        }

        fprintf(outfile, "}\n\n");
      } else error("Could not allocate symbol");
    }
  } else error("Symbol already declared");
}

/* End of sally.c */