aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/sym.c
blob: 69d3bcac00f5c8869a4281376d65a43c4d0cc01a (plain) (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
#include "sym.h"
#include "errloc.h"
#include "fn.h"
#include "lex.h"
#include "parse.h"
#include "prv.h"
#include "storage.h"
#include "type.h"
#include <stdio.h>
#include <stdlib.h>

static int addr(const struct lex *, struct prv *);
static int cnt(const struct lex *, struct prv *);
static int off(const struct lex *, struct prv *);
static int szof(const struct lex *, struct prv *);
static int idof(const struct lex *, struct prv *);
static int numof(const struct lex *, struct prv *);
static int cast(const struct lex *, struct prv *);
static int to(const struct lex *, struct prv *);
static int lit(const struct lex *, struct prv *);
static int num(const struct lex *, struct prv *);
static int id(const struct lex *, struct prv *);
static int push_to(struct prv *);

static const struct seq seqs[] =
{
    {(struct step[]){{ID, "address"}, {ID, "of"}, {0}}, .fn = addr},
    {(struct step[]){{ID, "contents"}, {ID, "of"}, {0}}, .fn = cnt},
    {(struct step[]){{ID, "offset"}, {ID, "of"}, {0}}, .fn = off},
    {(struct step[]){{ID, "size"}, {ID, "of"}, {0}}, .fn = szof},
    {(struct step[]){{ID, "cast"}, {0}}, .fn = cast},
    {(struct step[]){{ID}, {ID, "of"}, {0}}, .fn = idof},
    {(struct step[]){{NUM}, {ID, "of"}, {0}}, .fn = numof},
    {(struct step[]){{LIT}, {0}}, .fn = lit},
    {(struct step[]){{NUM}, {0}}, .fn = num},
    {(struct step[]){{ID}, {0}}, .fn = id},
    {0}
};

static const struct seq cast_seqs[] =
{
    {(struct step[]){{ID, "to"}, {0}}, .fn = to},
    {0}
};

static void queue(struct prv *p, struct sym *s)
{
    struct sym *ss = p->s;

    if (!ss)
        p->s = s;
    else
    {
        while (ss->child)
            ss = ss->child;

        ss->child = s;
        s->parent = ss;
    }

    reset(p);
}

static int pushsym(struct prv *p, enum symtype t)
{
    struct sym *s = malloc(sizeof *s);

    if (!s)
    {
        perror("malloc(3)");
        return -1;
    }

    *s = (struct sym){.type = t, .tk = p->stk};
    queue(p, s);
    return 0;
}

static int addr(const struct lex *l, struct prv *p)
{
    return pushsym(p, SYM_ADDR);
}

static int cnt(const struct lex *l, struct prv *p)
{
    return pushsym(p, SYM_CNT);
}

static int off(const struct lex *l, struct prv *p)
{
    return pushsym(p, SYM_OFF);
}

static int szof(const struct lex *l, struct prv *p)
{
    return pushsym(p, SYM_SZ);
}

static void insert(struct fn *fn, struct sym *s)
{
    struct sym *ss = fn->syms;

    while (ss && ss->next)
        ss = ss->next;

    if (!ss)
        fn->syms = s;
    else
        ss->next = s;
}

static int symbol(const struct sym *s)
{
    switch (s->type)
    {
    case SYM_ID:
    case SYM_TMP:
    case SYM_LIT:
    case SYM_NUM:
        return 1;

    case SYM_CAST:
    case SYM_ADDR:
    case SYM_SZ:
    case SYM_CNT:
    case SYM_OFF:
        break;
    }

    return 0;
}

static struct sym *getsym(struct sym *s)
{
    for (struct sym *ss = s; ss; ss = ss->child)
        if (symbol(ss))
            return ss;

    return NULL;
}

static int idof(const struct lex *l, struct prv *p)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    reset(p);
    return -1;
}

static int numof(const struct lex *l, struct prv *p)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    reset(p);
    return -1;
}

static struct sym *inner(struct sym *s)
{
    struct sym *ret = NULL;

    for (; s; s = s->child)
        if (s->type == SYM_CAST && !s->u.t)
            ret = s;

    return ret;
}

static struct sym *resolve(const struct sym *s)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    return NULL;
}

static int run(const struct lex *l, struct prv *p)
{
    struct fn *fn = fn_cur(p);
    struct sym *s = p->s;

    insert(fn, s);
    p->s = NULL;

    if (getsym(s))
        return p->sym(l, p, s);

    errloc(p->stk - 1, "expected identifier, literal or number");
    return -1;
}

static int totype(const struct lex *l, struct prv *p, const struct type *t)
{
    struct sym *s = inner(p->s);

    if (!s || s->u.t)
    {
        errloc(p->tk - 1, "incomplete cast");
        return -1;
    }

    s->u.t = t;

    if (pop(l, p))
        return -1;

    return !inner(p->s) && getsym(p->s) ? run(l, p) : 0;
}

static int to(const struct lex *l, struct prv *p)
{
    return type(l, p, totype);
}

static int endcast(const struct lex *l, struct prv *p)
{
    struct sym *s = inner(p->s);

    if (!p->cast--)
    {
        errloc(p->tk - 1, "incomplete cast");
        return -1;
    }
    else if (s)
    {
        if (lex_eof(l, p->tk))
        {
            errloc(p->tk - 1, "incomplete cast");
            return -1;
        }

        return push_to(p);
    }

    return 0;
}

static int cast(const struct lex *l, struct prv *p)
{
    p->cast++;
    return pushsym(p, SYM_CAST);
}

static int push_to(struct prv *p)
{
    struct pos init =
    {
        .name = __func__,
        .seq = cast_seqs,
        .end = endcast
    };

    return push(&init, p);
}

static int lit(const struct lex *l, struct prv *p)
{
    struct sym *cs = inner(p->s);

    if (pushsym(p, SYM_LIT))
        return -1;

    return cs ? push_to(p) : run(l, p);
}

static int num(const struct lex *l, struct prv *p)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    reset(p);
    return -1;
}

static int id(const struct lex *l, struct prv *p)
{
    struct sym *cs = inner(p->s);

    if (pushsym(p, SYM_ID))
        return -1;
    else if (cs)
        return push_to(p);

    p->stk = p->tk;
    return run(l, p);
}

void sym_free(struct sym *s)
{
    struct sym *ss = s;

    while (ss)
    {
        struct sym *child = ss->child;

        free(ss);
        ss = child;
    }
}

int sym_lval(const struct sym *s)
{
    int ret = 1;

    while (s)
    {
        switch (s->type)
        {
        case SYM_OFF:
        case SYM_SZ:
        case SYM_LIT:
        case SYM_NUM:
        case SYM_TMP:
            ret = 0;
            break;

        case SYM_ADDR:
            if (!s->parent || s->parent->type != SYM_CNT)
                ret = 0;
        case SYM_ID:
        case SYM_CNT:
        case SYM_CAST:
            break;
        }

        s = s->child;
    }

    return ret;
}

static int endsym(const struct lex *l, struct prv *p)
{
    return p->s ? run(l, p) : 0;
}

static int invalid(const struct tk *tk, const struct type *a,
    const struct type *b)
{
    static int t[BUILTIN + 1][BUILTIN + 1] =
    {
        /*              U  S  C  P  T PTR ARY BUILTIN */
        /* U        */ {1, 0, 0, 0, 0, 0,  0,  0},
        /* S        */ {0, 1, 0, 0, 0, 0,  0,  0},
        /* C        */ {0, 0, 1, 0, 0, 0,  0,  0},
        /* P        */ {0, 0, 0, 1, 0, 1,  0,  0},
        /* T        */ {0, 0, 0, 0, 1, 0,  0,  0},
        /* PTR      */ {0, 0, 0, 0, 0, 1,  1,  0},
        /* ARY      */ {0, 0, 0, 0, 0, 0,  1,  0},
        /* BUILTIN  */ {0, 0, 0, 0, 0, 0,  0,  1}
    };

    int valid = t[a->type][b->type];

    if (!valid)
    {
        char *s1 = type_name(b), *s2 = type_name(a);

        if (s1 && s2)
            errloc(tk, "cannot convert \"%s\" to \"%s\"", s1, s2);

        free(s1);
        free(s2);
    }

    return !valid;
}

static const struct type *genlittype(struct prv *p)
{
    const struct fn *fn = fn_cur(p);
    const char *tr = triplet(p);
    const struct type *byte = type_find(fn, tr, "char");
    struct type *bytedup = NULL, *t = NULL;

    if (!byte)
        return NULL;
    else if (!(bytedup = malloc(sizeof *bytedup))
        || !(t = malloc(sizeof *t)))
    {
        perror("malloc(3)");
        goto failure;
    }

    *bytedup = *byte;
    *t = (struct type)
    {
        .type = ARY,
        .u.ary.etc = 1,
        .child = bytedup
    };

    return p->ast->littype = type_insert(p, t);

failure:
    free(t);
    free(bytedup);
    return NULL;
}

static const struct type *littype(struct prv *p)
{
    const struct type *t = p->ast->littype;

    return t ? t : genlittype(p);
}

const struct type *sym_type(struct prv *p, const struct sym *s)
{
    const struct fn *fn = fn_cur(p);
    const struct type *ret = NULL;

    for (; s; s = s->child)
    {
        const struct tk *tk = s->tk;

        switch (s->type)
        {
        case SYM_CAST:
            if (!ret)
                ret = s->u.t;
            else if (invalid(tk, ret, s->u.t))
                return NULL;

            break;

        case SYM_ID:
        {
            const struct stentry *e = fn_var(fn, tk);
            const char *tr = triplet(p);
            const struct type *t;

            if (!e && !(t = type_ufind(fn, tr, tk->s)))
                return NULL;
            else if (e)
                t = e->t;

            if (!ret)
                ret = t;
            else if (invalid(tk, ret, t))
                return NULL;
        }
            break;

        case SYM_LIT:
        {
            const struct type *lt = littype(p);

            if (!ret)
                ret = lt;
            else if (invalid(tk, ret, lt))
                return NULL;
        }
            break;

        case SYM_ADDR:
        case SYM_TMP:
        case SYM_NUM:
        case SYM_SZ:
        case SYM_CNT:
        case SYM_OFF:
            break;
        }
    }

    return ret;
}

int sym_rval(const struct sym *s)
{
    return !sym_lval(s);
}

int sym(struct prv *p, sym_fn fn)
{
    struct pos init =
    {
        .name = __func__,
        .seq = seqs,
        .end = endsym
    };

    p->stk = p->tk;
    p->sym = fn;
    return push(&init, p);
}