forked from rc0/jbofihe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm_gather.c
290 lines (239 loc) · 5.27 KB
/
cm_gather.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
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
/***************************************
$Header$
Functions for gathering text into a list and preprocessing the list.
***************************************/
/* COPYRIGHT */
#include "cm.h"
/* ================================================== */
typedef enum {
TT_CMAVO,
TT_BRIVLA,
TT_CMENE,
TT_NEWLINE,
TT_PAREN
} toktype;
typedef struct node {
struct node *next;
struct node *prev;
toktype type;
char *lojban;
char *selmao;
char *trans;
} node;
/* ================================================== */
static node head = {&head, &head};
/* ================================================== */
static void
add_node(toktype type, const char *text)
{
node *nn = new(node);
nn->type = type;
nn->lojban = new_string(text);
nn->prev = head.prev;
nn->next = &head;
head.prev->next = nn;
head.prev = nn;
}
/* ================================================== */
void
gather_cmavo (const char *x)
{
add_node(TT_CMAVO, x);
}
/* ================================================== */
void
gather_brivla (const char *x)
{
add_node(TT_BRIVLA, x);
}
/* ================================================== */
void
gather_cmene (const char *x)
{
add_node(TT_CMENE, x);
}
/* ================================================== */
void
gather_newline(void)
{
add_node(TT_NEWLINE, "");
}
/* ================================================== */
void
gather_fallthru(const char *x)
{
fprintf(stderr, "This fell through [%s]\n", x);
}
/* ================================================== */
void
gather_paren(const char *x)
{
add_node(TT_PAREN, x);
}
/* ================================================== */
#if 0
static void
try_cmavo_fragment(node *p)
{
char buf_in[32];
char buf[2048];
char buf_out[256];
char *q, *r, *t;
buf[0] = 0;
q = p->lojban;
if (*q == '.') q++;
while (*q) {
r = buf_in;
do {
*r++ = *q++;
} while (*q && strchr("aeiou'", *q));
*r = 0;
t = translate(buf_in);
if (t) {
strcat(buf, t);
strcat(buf, " ");
} else {
strcat(buf, "? ");
}
}
p->trans = new_string(buf);
}
#endif
/* ================================================== */
static char *
translate_selmao(char *x) {
char buffer[64];
strcpy(buffer, "+");
strcat(buffer, x);
return translate(buffer);
}
/* ================================================== */
static void
try_cmavo_fragment(char *lojban, char **transbuf, char* (*transfn)(char *))
{
char buf[256], buf2[256];
char *pbuf;
char *t, *e;
char bufout[1024];
int len;
bufout[0] = 0;
strcpy(buf, lojban);
pbuf = buf;
if (*pbuf == '.') {
pbuf++;
}
#if DIAG
fprintf(stderr, "Start with %s\n", pbuf);
#endif
while (*pbuf) {
e = pbuf;
while (*e) e++;
while (e > pbuf) {
len = e - pbuf;
#if DIAG
fprintf(stderr, "Length = %d\n", len);
#endif
strncpy(buf2, pbuf, len);
buf2[len] = 0;
t = (*transfn)(buf2);
if (t) {
#if DIAG
fprintf(stderr, "Got trans %s for %s\n", t, buf2);
#endif
if (bufout[0]) {
strcat(bufout, " ");
}
strcat(bufout, t);
pbuf += len;
#if DIAG
fprintf(stderr, "Now looking at %s\n", pbuf);
#endif
goto next_outer;
} else {
#if DIAG
fprintf(stderr,"Failed to get a translation for %s\n", buf2);
#endif
do {
e--;
} while (strchr("aeiuo'", *e));
}
}
if (bufout[0]) {
strcat(bufout, " ");
}
strcat(bufout, "?");
do {
pbuf++;
} while (*pbuf && strchr("aeiou'", *pbuf));
if (*pbuf == '.') ++pbuf;
next_outer:
;
}
(*transbuf) = new_string(bufout);
}
/* ================================================== */
void
do_trans(void)
{
node *p;
char *t, *x;
char buf[32];
for (p=head.next; p!=&head; p=p->next) {
switch (p->type) {
case TT_CMAVO:
x = p->lojban;
if (*x == '.') x++;
t = translate(x);
if (t) {
p->trans = new_string(t);
strcpy(buf, "+");
strcat(buf, x);
t = translate(buf);
if (t) {
p->selmao = new_string(t);
} else {
p->selmao = new_string("");
}
} else {
try_cmavo_fragment(p->lojban, &p->trans, translate);
try_cmavo_fragment(p->lojban, &p->selmao, translate_selmao);
}
break;
case TT_BRIVLA:
t = translate(p->lojban);
if (t) {
p->trans = new_string(t);
} else {
t = translate_unknown(p->lojban);
if (t) {
p->trans = new_string(t);
} else {
p->trans = new_string("?");
}
}
break;
default:
p->trans = new_string("");
}
}
}
/* ================================================== */
void
do_output(void)
{
node *p;
for (p=head.next; p!=&head; p=p->next) {
if (p->type == TT_NEWLINE) {
output_newline();
} else if (p->type == TT_PAREN) {
output_paren(p->lojban);
} else if (p->type == TT_CMAVO) {
output(p->lojban, p->trans, p->selmao);
} else if (p->type == TT_BRIVLA) {
output(p->lojban, p->trans, "BRIVLA");
} else if (p->type == TT_CMENE) {
output(p->lojban, p->trans, "CMENE");
}
}
}
/* ================================================== */