C feature expansion preprocessor

root / ceature.c

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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "token.h"
#include "utils.h"

/* State of the parser/modifier. */
FILE *fout_c = NULL;
FILE *fout_h = NULL;
FILE *fdefer[128] = {NULL};
int fdefer_count = 0;
int defer_nonpop = 0;
int lambda_count = 0;

int parse_tok(struct Tok *tok, int n, FILE *fout);

int parse_lambda(struct Tok *tok, int n, FILE *fout) {
    int lambda_idx = lambda_count++;

    fprintf(fout, "lambda_%d", lambda_idx);

    FILE *ftmp = tmpfile();

    int bbr = 0;
    int ebr = 0;
    int i = 1;

    fprintf(ftmp, "static");

    while (!((ebr == bbr) && ebr)) {
        if (ARROW_LEFT == tok[i].type) {
            fprintf(ftmp, "lambda_%d", lambda_idx);
            ++i;
            continue;
        } else {
            if (CURLY_BEGIN == tok[i].type) {
                ++bbr;
            }
            if (CURLY_END == tok[i].type) {
                ++ebr;
            }
            i += parse_tok(&tok[i], n - i, ftmp);
        }
    }
    fprintf(fout_h, "\n");

    tmpflush(fout_h, ftmp);
    fclose(ftmp);

    return i;
}

int parse_defer(struct Tok *tok, int n, FILE *fout) {
    int i = 1;
    while (i < n) {
        if (SEMICOLON == tok[i].type) {
            fprintf(fdefer[fdefer_count-1], ";");
            ++i;
            break;
        }
        i += parse_tok(&tok[i], n-i, fdefer[fdefer_count-1]);
    }
    return i;
}

void defer_push() {
    fdefer[fdefer_count++] = tmpfile();
}

void defer_pop(FILE *fout) {
    if (defer_nonpop) {
        defer_nonpop = 0;
        return;
    }

    tmpflush(fout, fdefer[fdefer_count - 1]);
    fclose(fdefer[--fdefer_count]);
}

void defer_flush(FILE *fout) {
    for (int i = fdefer_count - 1; i >= 0; --i) {
        tmpflush(fout, fdefer[i]);
    }
    fclose(fdefer[--fdefer_count]);
    defer_nonpop = 1;
}

int parse_tok(struct Tok *tok, int n, FILE *fout) {
    if (n) {
        if (LAMBDA == tok[0].type) {
            return parse_lambda(tok, n, fout);
        }

        if (DEFER == tok[0].type) {
            return parse_defer(tok, n, fout);
        }

        if (CURLY_BEGIN == tok[0].type) {
            defer_push();
        }

        if (CURLY_END == tok[0].type) {
            defer_pop(fout);
        }

        if (RETURN == tok[0].type) {
            defer_flush(fout);
        }

        fprintf(fout, "%.*s", (int)(tok->end - tok->begin), tok->begin);
        return 1;
    }

    return 0;
}

void parse(struct Tok *tok, long nt) {
    if (tok) {
        int it = 0;
        while (it < nt) {
            int progress;
            it += progress = parse_tok(&tok[it], nt - it, fout_c);

            if (0 == progress) {
                fprintf(stderr, "Break.\n");
                break;
            }
        }
    }
}

int main(int argc, char **argv)
{
    FILE *fin = NULL;

    if (argc < 4) {
        printf ("Usage: cea FILE_C_IN FILE_C_OUT FILE_H_OUT\n");
        return 1;
    }

    fin = fopen(argv[1], "r");
    fout_c = fopen(argv[2], "w");
    fout_h = fopen(argv[3], "w");

    long n = 0;
    char *a = readf(fin, &n);

    long nt = 0;

    struct Tok *tok = tokenize(a, n, &nt);

    parse(tok, nt);

    free(tok);
    free(a);

    fclose(fin);
    fclose(fout_c);
    fclose(fout_h);

    return 0;
}