-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgen.c
146 lines (140 loc) · 3.6 KB
/
cgen.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
#include "cgen.h"
#include "list.h"
#include <string.h>
char nullStr;
void appendString(struct StringListContainer *sl, const char *s) {
if (sl->Last) {
sl->Last->L = get_mem(sizeof(struct StringList));
sl->Last->L->S = s;
sl->Last = sl->Last->L;
} else {
sl->Last = get_mem(sizeof(struct StringList));
sl->Last->S = s;
sl->First = sl->Last;
}
}
void appendStringList(struct StringListContainer *sl,
struct StringListContainer l) {
if (sl->First == l.First || !l.First) {
return;
}
if (sl->Last) {
sl->Last->L = l.First;
sl->Last = l.Last;
} else {
*sl = l;
}
}
void printAll(struct StringListContainer c) {
struct StringList *sl;
for (sl = c.First; sl; sl = sl->L) {
puts(sl->S);
}
}
char *toOneString(struct StringListContainer c) {
struct StringList *s;
char *r;
unsigned len, i, l;
len = 0;
for (s = c.First; s; s = s->L) {
len += strlen(s->S);
}
r = get_mem(len + 1);
r[len] = 0;
i = 0;
for (s = c.First; s; s = s->L) {
l = strlen(s->S);
memcpy(r + i, s->S, l);
i += l;
}
return r;
}
char *twoStringListsToOneString(struct StringListContainer c,
struct StringListContainer c2) {
struct StringList *s;
char *r;
unsigned len, i, l;
len = 0;
for (s = c.First; s; s = s->L) {
len += strlen(s->S);
}
for (s = c2.First; s; s = s->L) {
len += strlen(s->S);
}
r = get_mem(len + 1);
r[len] = 0;
i = 0;
for (s = c.First; s; s = s->L) {
l = strlen(s->S);
memcpy(r + i, s->S, l);
i += l;
}
for (s = c2.First; s; s = s->L) {
l = strlen(s->S);
memcpy(r + i, s->S, l);
i += l;
}
return r;
}
const char *sanitizeName(const char *name) {
unsigned long len;
char *r, *p;
len = strlen(name);
r = get_mem(len + 1);
memcpy(r, name, len + 1);
p = r;
while (*p) {
if (!((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') ||
(*p >= '0' && *p <= '9'))) {
*p = '_';
}
++p;
}
return r;
}
int uniqueIntAt;
int uniqueInt() { return ++uniqueIntAt; }
struct cexp *make_cexp(const char *a, const char *before, const char *global,
const char *header) {
struct cexp *r;
r = get_mem(sizeof(struct cexp));
r->E = a;
if (before) {
r->Context.Last = get_mem(sizeof(struct StringList));
r->Context.Last->S = before;
r->Context.First = r->Context.Last;
}
if (global) {
r->Global.Last = get_mem(sizeof(struct StringList));
r->Global.Last->S = global;
r->Global.First = r->Global.Last;
}
if (header) {
r->Header.Last = get_mem(sizeof(struct StringList));
r->Header.Last->S = header;
r->Header.First = r->Header.Last;
}
return r;
}
void add_cexp(struct cexp *a, struct cexp *b) {
appendStringList(&a->Context, b->Context);
appendStringList(&a->Global, b->Global);
appendStringList(&a->Header, b->Header);
}
void append_cexp(struct cexp *orig, const char *a, const char *before,
const char *global, const char *header) {
orig->E = a;
appendString(&orig->Context, before);
appendString(&orig->Global, global);
appendString(&orig->Header, header);
}
const char *get_expression(struct cexp *e) { return e->E; }
const char *get_context(struct cexp *e) { return toOneString(e->Context); }
const char *get_global(struct cexp *e) { return toOneString(e->Global); }
const char *get_header(struct cexp *e) { return toOneString(e->Header); }
void set_expression(struct cexp *e, const char *s) { e->E = s; }
void set_global(struct cexp *e, const char *s) {
e->Global.Last = e->Global.First;
e->Global.First->L = NULL;
e->Global.First->S = s;
}