summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/loader.c
blob: 0d2f40661441f8f1731ade48734b4a7383401674 (plain)
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
#include "../pgf.h"
#include "data.h"
#include "panic.h"
#include <stdio.h>
#include <stdlib.h>

static int readTag(FILE *f) {
  return getc(f);
}

static int readInt16(FILE *f) {
  int x = getc(f);
  int y = getc(f);
  return ((x << 8) | y);
}

static int readInt(FILE *f) {
  unsigned int x = (unsigned int) getc(f);
  if (x <= 0x7f)
    return (int) x;
  else {
    unsigned int y = (unsigned int) readInt(f);
    return (int) ((y << 7) | (x & 0x7f)) ;
  }
}

static double readFloat(FILE *f) {
  double d;
  fread(&d, sizeof(d), 1, f);
  return d;
}

static String readString(FILE *f) {
  int len = readInt(f);
  String str = (String) malloc(sizeof(struct _CId)+len*sizeof(unsigned int));
  str->len = len;
  
  int i;
  for (i = 0; i < len; i++) {
    int c = fgetc(f);
    str->chars[i] = c;
  }
  
  return str;
}

static CId readCId(FILE *f) {
  int len = readInt(f);
  CId cid = (CId) malloc(sizeof(struct _CId)+len*sizeof(char));
  cid->len = len;
  fread(&cid->chars, sizeof(char), len, f);
  return cid;
}

static CIdList readCIdList(FILE *f) {
  int i;
  int count = readInt(f);
  CIdList list = (CIdList) malloc(sizeof(struct _CIdList)+count*sizeof(CId));
  
  list->count = count;
  for (i = 0; i < count; i++) {
    list->names[i]  = readCId(f);
  }

  return list;
}

static Literal readLiteral(FILE *f) {
  int tag = readTag(f);
  switch (tag) {
    case LIT_STR:
      { 
	LiteralStr lit = (LiteralStr) malloc(sizeof(struct _LiteralStr));
        lit->_.tag = tag;
        lit->val   = readString(f);
        return ((Literal) lit);
      }
    case LIT_INT:
      { 
	LiteralInt lit = (LiteralInt) malloc(sizeof(struct _LiteralInt));
        lit->_.tag = tag;
        lit->val   = readInt(f);
        return ((Literal) lit);
      }
    case LIT_FLOAT:
      {
        LiteralFloat lit = (LiteralFloat) malloc(sizeof(struct _LiteralFloat));
        lit->_.tag = tag;
        lit->val   = readFloat(f);
        return ((Literal) lit);
      }
    default:
      __pgf_panic("Unknown literal tag");
  }
}

static Flags readFlags(FILE *f) {
  int i;
  int count = readInt(f);
  Flags flags = (Flags) malloc(sizeof(struct _Flags)+count*sizeof(struct _Flag));
  
  flags->count = count;
  for (i = 0; i < count; i++) {
    flags->values[i].name  = readCId(f);
    flags->values[i].value = readLiteral(f);
  }

  return flags;
}

static Context readContext(FILE *f);
static Type readType(FILE *f);

static Expr readExpr(FILE *f) {
  int tag = readTag(f);
  
  switch (tag) {
  case TAG_ABS:
    {
        ExprAbs e = (ExprAbs) malloc(sizeof(struct _ExprAbs));
        e->_.tag = tag;
        e->bt    = readTag(f);
        e->var   = readCId(f);
        e->body  = readExpr(f);
        return ((Expr) e);
    }
  case TAG_APP:
    {
        ExprApp e = (ExprApp) malloc(sizeof(struct _ExprApp));
        e->_.tag = tag;
        e->left  = readExpr(f);
        e->right = readExpr(f);
        return ((Expr) e);
    }
  case TAG_LIT:
    {
        ExprLit e = (ExprLit) malloc(sizeof(struct _ExprLit));
        e->_.tag = tag;
        e->lit   = readLiteral(f);
        return ((Expr) e);
    }
  case TAG_MET:
    {
        ExprMeta e = (ExprMeta) malloc(sizeof(struct _ExprMeta));
        e->_.tag = tag;
        e->id    = readInt(f);
        return ((Expr) e);
    }
  case TAG_FUN:
    {
        ExprFun e = (ExprFun) malloc(sizeof(struct _ExprFun));
        e->_.tag = tag;
        e->fun   = readCId(f);
        return ((Expr) e);
    }
  case TAG_VAR:
    {
        ExprVar e = (ExprVar) malloc(sizeof(struct _ExprVar));
        e->_.tag = tag;
        e->index = readInt(f);
        return ((Expr) e);
    }
  case TAG_TYP:
    {
        ExprTyped e = (ExprTyped) malloc(sizeof(struct _ExprTyped));
        e->_.tag = tag;
        e->e     = readExpr(f);
        e->ty    = readType(f);
        return ((Expr) e);
    }
  case TAG_IMP:
    {
        ExprImplArg e = (ExprImplArg) malloc(sizeof(struct _ExprImplArg));
        e->_.tag = tag;
        e->e     = readExpr(f);
        return ((Expr) e);
    }
  default:
    __pgf_panic("Unknown expression tag");
  }
}
	
static Type readType(FILE *f) {
  Context hypos = readContext(f);
  CId     cat   = readCId(f);
  
  int i;
  int count = readInt(f);
  Type ty = (Type) malloc(sizeof(struct _Type)+count*sizeof(Expr));
  
  ty->hypos = hypos;
  ty->cat   = cat;
  ty->nArgs = count;
  for (i = 0; i < count; i++) {
    ty->args[i]  = readExpr(f);
  }
  
  return ty;
}

static void readHypo(FILE *f, Hypo h) {
  h->bt  = readTag(f);
  h->var = readCId(f);
  h->ty  = readType(f);
}

static Context readContext(FILE *f) {
  int i;
  int size = readInt(f);
  Context ctxt = (Context) malloc(sizeof(struct _Context)+size*sizeof(struct _Hypo));
  
  ctxt->size = size;
  for (i = 0; i < size; i++) {
    readHypo(f, &ctxt->hypos[i]);
  }
  
  return ctxt;
}

static Patt readPatt(FILE *f) {
  int tag = readTag(f);

  switch (tag) {
  case TAG_PAPP:
    {
       CId fun = readCId(f);
       
       int i;
       int count = readInt(f);
       PattApp p = (PattApp) malloc(sizeof(struct _PattApp)+count*sizeof(Patt));
  
       p->_.tag      = tag;
       p->fun        = fun;
       p->args.count = count;
       for (i = 0; i < count; i++) {
         p->args.pats[i] = readPatt(f);
       }
       
       return ((Patt) p);
    }
  case TAG_PVAR:
    {
        PattVar p = (PattVar) malloc(sizeof(struct _PattVar));
        p->_.tag = tag;
        p->var   = readCId(f);
        return ((Patt) p);
    }
  case TAG_PAT:
    {
        PattAt p = (PattAt) malloc(sizeof(struct _PattAt));
        p->_.tag = tag;
        p->var   = readCId(f);
        p->pat   = readPatt(f);
        return ((Patt) p);
    }
  case TAG_PWILD:
    {
        PattWild p = (PattWild) malloc(sizeof(struct _PattWild));
        p->_.tag = tag;
        return ((Patt) p);
    }
  case TAG_PLIT:
    {
        PattLit p = (PattLit) malloc(sizeof(struct _PattLit));
        p->_.tag = tag;
        p->lit   = readLiteral(f);
        return ((Patt) p);
    }
  case TAG_PIMP:
    {
        PattImplArg p = (PattImplArg) malloc(sizeof(struct _PattImplArg));
        p->_.tag = tag;
        p->pat   = readPatt(f);
        return ((Patt) p);
    }
  case TAG_PTILDE:
    {
        PattTilde p = (PattTilde) malloc(sizeof(struct _PattTilde));
        p->_.tag = tag;
        p->e     = readExpr(f);
        return ((Patt) p);
    }
  default:
    __pgf_panic("Unknown pattern tag");
  }  
}

static Patts readPatts(FILE *f) {
  int i;
  int count = readInt(f);
  Patts pats = (Patts) malloc(sizeof(struct _Patts)+count*sizeof(Patt));
  
  pats->count = count;
  for (i = 0; i < count; i++) {
    pats->pats[i] = readPatt(f);
  }

  return pats;
}

static Equations readEquations(FILE *f) {
  int i;
  int count = readInt(f);
  Equations equs = (Equations) malloc(sizeof(struct _Equations)+count*sizeof(struct _Equation));
  
  equs->count = count;
  for (i = 0; i < count; i++) {
    equs->equs[i].lhs = readPatts(f);
    equs->equs[i].rhs = readExpr(f);
  }

  return equs;
}

static void readAbsFun(FILE *f, AbsFun fun) {
  fun->name   = readCId(f);
  fun->ty     = readType(f);
  fun->arrity = readInt(f);
  if (readTag(f) != 0)
    fun->equs   = readEquations(f);
  else
    fun->equs   = NULL;
}

static AbsFuns readAbsFuns(FILE *f) {
  int i;
  int count = readInt(f);
  AbsFuns funs = (AbsFuns) malloc(sizeof(struct _AbsFuns)+count*sizeof(struct _AbsFun));
  
  funs->count = count;
  for (i = 0; i < count; i++) {
    readAbsFun(f, &funs->lst[i]);
  }
  
  return funs;
}

static void readAbsCat(FILE *f, AbsCat cat) {
  cat->name  = readCId(f);
  cat->hypos = readContext(f);
  cat->funs  = readCIdList(f);
}

static AbsCats readAbsCats(FILE *f) {
  int i;
  int count = readInt(f);
  AbsCats cats = (AbsCats) malloc(sizeof(struct _AbsCats)+count*sizeof(struct _AbsCat));
  
  cats->count = count;
  for (i = 0; i < count; i++) {
    readAbsCat(f, &cats->lst[i]);
  }
  
  return cats;
}

static void readAbstr(FILE *f, Abstract abstr) {
  abstr->name  = readCId(f);
  abstr->flags = readFlags(f);
  abstr->funs  = readAbsFuns(f);
  abstr->cats  = readAbsCats(f);
}

static void readConcr(FILE *f, Concrete concr) {
  concr->name  = readCId(f);
  concr->flags = readFlags(f);
}

PGF readPGF(char *filename) {
  FILE *f = fopen(filename, "rb");
  if (f == NULL)
    return NULL;
    
  int maj_ver = readInt16(f);
  int min_ver = readInt16(f);
  
  Flags flags = readFlags(f);
  
  struct _Abstract abstr;
  readAbstr(f, &abstr);
  
  int nConcr = readInt(f);
  PGF pgf = (PGF) malloc(sizeof(struct _PGF)+sizeof(Concrete)*nConcr);
  
  pgf->flags    = flags;
  pgf->abstract = abstr;
  pgf->nConcr   = nConcr;
  
  int i;
//  for (i = 0; i < nConcr; i++) {
//    readConcr(f, &pgf->concretes[i]);
//  }

  fclose(f);
  return pgf;
}