~snowball-yiddish-dev/snowball-yiddish/trunk

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
#include <stdio.h>   /* stderr etc */
#include <stdlib.h>  /* malloc free */
#include <string.h>  /* strlen */
#include <ctype.h>   /* isalpha etc */
#include "header.h"

struct system_word
{   int s_size;   /* size of system word */
    byte * s;     /* pointer to the system word */
    int code;     /* it's internal code */
};


/* ASCII collating assumed in syswords.c */

#include "syswords"

static int smaller(int a, int b) { return a < b ? a : b; }

extern symbol * get_input(symbol * p)
{
    char * s = b_to_s(p);
    {   FILE * input = fopen(s, "r");
        free(s);
        if (input == 0) return 0;
        {   symbol * u = create_b(STARTSIZE);
            int size = 0;
            repeat
            {   int ch = getc(input);
                if (ch == EOF) break;
                if (size >= CAPACITY(u)) u = increase_capacity(u, size/2);
                u[size++] = ch;
            }
            fclose(input);
            SIZE(u) = size; return u;
        }
    }
}

static void error(struct tokeniser * t, char * s1, int n, symbol * p, char * s2)
{
    if (t->error_count == 20) { fprintf(stderr, "... etc\n"); exit(1); }
    fprintf(stderr, "Line %d", t->line_number);
    if (t->get_depth > 0) fprintf(stderr, " (of included file)");
    fprintf(stderr, ": ");
    unless (s1 == 0) fprintf(stderr, "%s", s1);
    unless (p == 0)
    {   int i;
        for (i = 0; i < n; i++) fprintf(stderr, "%c", p[i]);
    }
    unless (s2 == 0) fprintf(stderr, "%s", s2);
    fprintf(stderr, "\n");
    t->error_count++;
}

static void error1(struct tokeniser * t, char * s)
   { error(t, s, 0,0, 0); }

static void error2(struct tokeniser * t, char * s)
   { error(t, "unexpected end of text after ", 0,0, s); }

static int compare_words(int m, symbol * p, int n, byte * q)
{   unless (m == n) return m - n;
    {   int i; for (i = 0; i < n; i++)
        {   int diff = p[i] - q[i];
            unless (diff == 0) return diff;
        }
    }
    return 0;
}

static int find_word(int n, symbol * p)
{   int i = 0; int j = vocab->code;
    repeat
    {   int k = i + (j - i)/2;
        struct system_word * w = vocab + k;
        int diff = compare_words(n, p, w->s_size, w->s);
        if (diff == 0) return w->code;
        if (diff < 0) j = k; else i = k;
        if (j - i == 1) break;
    }
    return -1;
}

static int get_number(int n, symbol * p)
{   int x = 0;
    int i; for (i = 0; i < n; i++) x = 10*x + p[i] - '0';
    return x;
}

static int eq_s(struct tokeniser * t, char * s)
{   int l = strlen(s);
    if (SIZE(t->p) - t->c < l) return false;
    {   int i;
        for (i = 0; i < l; i++) if (t->p[t->c + i] != s[i]) return false;
    }
    t->c += l; return true;
}

static int white_space(struct tokeniser * t, int ch)
{   switch (ch)
    {   case '\n': t->line_number++;
        case '\r':
        case '\t':
        case ' ': return true;
    }
    return false;
}

static symbol * find_in_m(struct tokeniser * t, int n, symbol * p)
{   struct m_pair * q = t->m_pairs;
    repeat
    {   if (q == 0) return 0;
        {   symbol * name = q->name;
            if (n == SIZE(name) && memcmp(name, p, n * sizeof(symbol)) == 0) return q->value;
        }
        q = q->next;
    }
}

static int read_literal_string(struct tokeniser * t, int c)
{   symbol * p = t->p;
    int ch;
    SIZE(t->b) = 0;
    repeat
    {   if (c >= SIZE(p)) { error2(t, "'"); return c; }
        ch = p[c];
        if (ch == '\n') { error1(t, "string not terminated"); return c; }
        c++;
        if (ch == t->m_start)
        {   int c0 = c;
            int newlines = false; /* no newlines as yet */
            int black_found = false; /* no printing chars as yet */
            repeat
            {   if (c >= SIZE(p)) { error2(t, "'"); return c; }
                ch = p[c]; c++;
                if (ch == t->m_end) break;
                unless (white_space(t, ch)) black_found = true;
                if (ch == '\n') newlines = true;
                {   if (newlines && black_found)
                    {   error1(t, "string not terminated");
                        return c;
                    }
                }
            }
            unless (newlines)
            {
                int n = c - c0 - 1;    /* macro size */
                int firstch = p[c0];
                symbol * q = find_in_m(t, n, p + c0);
                if (q == 0)
                {   if (n == 1 && (firstch == '\'' || firstch == t->m_start))
                        t->b = add_to_b(t->b, 1, p + c0);
                    else
                        error(t, "string macro '", n, p + c0, "' undeclared");
                }
                else
                    t->b = add_to_b(t->b, SIZE(q), q);
            }
        }  else
        {   if (ch == '\'') return c;
            t->b = add_to_b(t->b, 1, p + c - 1);
        }
    }
}

static int next_token(struct tokeniser * t)
{   symbol * p = t->p;
    int c = t->c;
    int ch;
    int code = -1;
    repeat
    {   if (c >= SIZE(p)) { t->c = c; return -1; }
        ch = p[c];
        if (white_space(t, ch)) { c++; continue; }
        if (isalpha(ch))
        {   int c0 = c;
            while (c < SIZE(p) && (isalnum(p[c]) || p[c] == '_')) c++;
            code = find_word(c - c0, p + c0);
            if (code < 0)
            {   t->b = move_to_b(t->b, c - c0, p + c0);
                code = c_name;
            }
        } else
        if (isdigit(ch))
        {   int c0 = c;
            while (c < SIZE(p) && isdigit(p[c])) c++;
            t->number = get_number(c - c0, p + c0);
            code = c_number;
        } else
        if (ch == '\'')
        {   c = read_literal_string(t, c + 1);
            code = c_literalstring;
        } else
        {   int lim = smaller(2, SIZE(p) - c);
            int i; for (i = lim; i > 0; i--)
            {   code = find_word(i, p + c);
                if (code >= 0) { c += i; break; }
            }
        }
        if (code >= 0)
        {   t->c = c;
            return code;
        }
        error(t, "'", 1, p + c, "' unknown");
        c++;
        continue;
    }
}

static int next_char(struct tokeniser * t)
{   if (t->c >= SIZE(t->p)) return -1;
    return t->p[t->c++];
}

static int next_real_char(struct tokeniser * t)
{   repeat
    {   int ch = next_char(t);
        if (white_space(t, ch)) continue;
        return ch;
    }
}

static void read_chars(struct tokeniser * t)
{   int ch = next_real_char(t);
    if (ch < 0)
        { error2(t, "stringdef"); return; }
    {   int c0 = t->c-1;
        repeat
        {   ch = next_char(t);
            if (white_space(t, ch) || ch < 0) break;
        }
        t->b2 = move_to_b(t->b2, t->c - c0 - 1, t->p + c0);
    }
}

static int decimal_to_num(int ch)
{   if ('0' <= ch && ch <= '9') return ch - '0';
    return -1;
}

static int hex_to_num(int ch)
{   if ('0' <= ch && ch <= '9') return ch - '0';
    if ('a' <= ch && ch <= 'f') return ch - 'a' + 10;
    return -1;
}

static void convert_numeric_string(struct tokeniser * t, symbol * p, int base)
{
    int c = 0; int d = 0;
    repeat
    {   while (c < SIZE(p) && p[c] == ' ') c++;
        if (c == SIZE(p)) break;
        {   int number = 0;
            repeat
            {   int ch = p[c];
                if (c == SIZE(p) || ch == ' ') break;
                if (base == 10)
                {   ch = decimal_to_num(ch);
                    if (ch < 0)
                    {   error1(t, "decimal string contains non-digits");
                        return;
                    }
                } else
                {   ch = hex_to_num(tolower(ch));
                    if (ch < 0)
                    {   error1(t, "hex string contains non-hex characters");
                        return;
                    }
                }
                number = base * number + ch;
                c++;
            }
            if (t->widechars) unless (0 <= number && number <= 0xffff)
            {   error1(t, "character values exceed 64K");
                return;
            }
            unless (t->widechars) unless (0 <= number && number <= 0xff)
            {   error1(t, "character values exceed 256");
                return;
            }
            p[d++] = number;
        }
    }
    SIZE(p) = d;
}

extern int read_token(struct tokeniser * t)
{   symbol * p = t->p;
    int held = t->token_held;
    t->token_held = false;
    if (held) return t->token;
    repeat
    {   int code = next_token(t);
        switch (code)
        {   case c_comment1: /*  slash-slash comment */
               while (t->c < SIZE(p) && p[t->c] != '\n') t->c++;
               continue;
            case c_comment2: /* slash-star comment */
               repeat
               {   if (t->c >= SIZE(p))
                   {   error1(t, "/* comment not terminated");
                       t->token = -1;
                       return -1;
                   }
                   if (p[t->c] == '\n') t->line_number++;
                   if (eq_s(t, "*/")) break;
                   t->c++;
               }
               continue;
            case c_stringescapes:
               {   int ch1 = next_real_char(t);
                   int ch2 = next_real_char(t);
                   if (ch2 < 0)
                       { error2(t, "stringescapes"); continue; }
                   if (ch1 == '\'')
                       { error1(t, "first stringescape cannot be '"); continue; }
                   t->m_start = ch1;
                   t->m_end = ch2;
               }
               continue;
            case c_stringdef:
               {   int base = 0;
                   read_chars(t);
                   code = read_token(t);
                   if (code == c_hex) { base = 16; code = read_token(t); } else
                   if (code == c_decimal) { base = 10; code = read_token(t); }
                   unless (code == c_literalstring)
                       { error1(t, "string omitted after stringdef"); continue; }
                   if (base > 0) convert_numeric_string(t, t->b, base);
                   {   NEW(m_pair, q);
                       q->next = t->m_pairs;
                       q->name = copy_b(t->b2);
                       q->value = copy_b(t->b);
                       t->m_pairs = q;
                   }
               }
               continue;
            case c_get:
               {   code = read_token(t);
                   unless (code == c_literalstring)
                       { error1(t, "string omitted after get"); continue; }
                   t->get_depth++;
                   if (t->get_depth > 10)
                   {   fprintf(stderr, "get directives go 10 deep. Looping?\n");
                       exit(1);
                   }
                   {   NEW(input, q);
                       symbol * u = get_input(t->b);
                       if (u == 0)
                       {   struct include * r = t->includes;
                           until (r == 0)
                           {   symbol * b = copy_b(r->b);
                               b = add_to_b(b, SIZE(t->b), t->b);
                               u = get_input(b);
                               lose_b(b);
                               unless (u == 0) break;
                               r = r->next;
                           }
                       }
                       if (u == 0)
                       {   error(t, "Can't get '", SIZE(t->b), t->b, "'");
                           exit(1);
                       }
                       memmove(q, t, sizeof(struct input));
                       t->next = q;
                       t->p = u;
                       t->c = 0;
                       t->line_number = 1;
                   }
                   p = t->p;
                   continue;
               }
            case -1:
               unless (t->next == 0)
               {   lose_b(p);
                   {   struct input * q = t->next;
                       memmove(t, q, sizeof(struct input)); p = t->p;
                       FREE(q);
                   }
                   t->get_depth--;
                   continue;
               }
               /* drop through */
            default:
                t->previous_token = t->token;
                t->token = code;
                return code;
        }
    }
}

extern byte * name_of_token(int code)
{   int i;
    for (i = 1; i < vocab->code; i++)
        if ((vocab + i)->code == code) return (vocab + i)->s;
    switch (code)
    {
        case c_mathassign:   return (byte *) "=";
        case c_name:         return (byte *) "name";
        case c_number:       return (byte *) "number";
        case c_literalstring:return (byte *) "literal";
        case c_neg:          return (byte *) "neg";
        case c_grouping:     return (byte *) "grouping";
        case c_call:         return (byte *) "call";
        case c_booltest:     return (byte *) "Boolean test";
        case -2:             return (byte *) "start of text";
        case -1:             return (byte *) "end of text";
        default:             return (byte *) "?";
    }
}

extern struct tokeniser * create_tokeniser(symbol * p)
{   NEW(tokeniser, t);
    t->next = 0;
    t->p = p;
    t->c = 0;
    t->line_number = 1;
    t->b = create_b(0);
    t->b2 = create_b(0);
    t->m_start = -1;
    t->m_pairs = 0;
    t->get_depth = 0;
    t->error_count = 0;
    t->token_held = false;
    t->token = -2;
    t->previous_token = -2;
    return t;
}

extern void close_tokeniser(struct tokeniser * t)
{   lose_b(t->b);
    lose_b(t->b2);
    {   struct m_pair * q = t->m_pairs;
        until (q == 0)
        {   struct m_pair * q_next = q->next;
            lose_b(q->name);
            lose_b(q->value);
            FREE(q);
            q = q_next;
        }
    }
    {   struct input * q = t->next;
        until (q == 0)
        {   struct input * q_next = q->next;
            FREE(q);
            q = q_next;
        }
    }
    FREE(t);
}