aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/print.c
blob: 604c2a3981e184ce2752602d050ff07f74ca266c (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
#include "print.h"
#include "errloc.h"
#include "fn.h"
#include "lit.h"
#include "parse.h"
#include "prv.h"
#include "stmt.h"
#include "sym.h"
#include "type.h"
#include <stdlib.h>

static int etc(const struct lex *, struct prv *);

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

static int invalid(const struct sym *s, const struct type *t)
{
    char *tn = type_name(t);

    if (tn)
        errloc(s->tk, "cannot print type \"%s\"", tn);

    free(tn);
    return -1;
}

static const struct sym *inner(const struct sym *s)
{
    for (; s; s = s->child)
        if (!s->child)
            break;

    return s;
}

static int setptr(struct prv *p, const struct sym *s, struct prisym *ps,
    const struct type *t)
{
    const struct fn *fn = fn_cur(p);
    const struct stentry *e = fn_var(fn, s->tk);

    if (!e)
    {
        errloc(s->tk, "undefined reference to \"%s\"", s->tk->s);
        return -1;
    }

    ps->type = PRS_PTR;
    ps->u.e = e;
    return 0;
}

static int settype(struct prv *p, const struct sym *s, struct prisym *ps)
{
    const struct type *t = sym_type(p, s);
    const struct tk *tk = s->tk;

    if (!t)
    {
        errloc(tk, "undefined reference to \"%s\"", tk->s);
        return -1;
    }
    else if (inner(s)->type == SYM_LIT)
    {
        const struct lit *l = lit_push(tk, p);

        if (!l)
            return -1;
        else if (t == p->ast->littype)
        {
            ps->type = PRS_LIT;
            ps->u.lit = l;
        }
        else
        {
            ps->type = PRS_NAM;
            ps->u.name = l->name;
        }
    }
    else
        switch (t->type)
        {
            case U:
            case S:
            case T:
            case ARY:
                return invalid(s, t);
            case C:
            case BUILTIN:
                fprintf(stderr, "%s: TODO\n", __func__);
                return -1;
            case P:
                ps->type = PRS_NAM;
                ps->u.name = s->tk->s;
                break;
            case PTR:
                return setptr(p, s, ps, t);
        }

    return 0;
}

static int onsym(const struct lex *l, struct prv *p, const struct sym *s)
{
    struct stmt *stmt = stmt_cur(p);
    struct print *pr = &stmt->u.print;
    size_t n = pr->nsyms + 1;
    struct prisym *syms, *nsym;

    if (!(syms = realloc(pr->syms, n * sizeof *syms)))
    {
        perror("realloc(3)");
        return -1;
    }

    pr->syms = syms;
    nsym = &syms[pr->nsyms++];
    *nsym = (struct prisym){.s = s};
    return settype(p, s, nsym);
}

static int etc(const struct lex *l, struct prv *p)
{
    struct stmt *stmt = stmt_cur(p);
    struct print *pr = &stmt->u.print;

    pr->skipln = 1;
    reset(p);
    return 0;
}

void print_free(struct print *pr)
{
    free(pr->syms);
}

int print(const struct lex *l, struct prv *p)
{
    struct pos init =
    {
        .name = __func__,
        .seq = seqs
    };

    if (push(&init, p))
        return -1;

    return sym(p, onsym);
}