parse.c 13.5 KB
Newer Older
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
1
2
3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
4
#include <stdbool.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
5
6
7

#include "parse.h"
#include "mem.h"
8
#include "code.h"
9
10
#include "desc.h"

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
11
12
13
14
15
16
17
18
19
20
int parseInt(char** ptr, int* result) {
    char *end;
    *result = strtol(*ptr, &end, 10);

    if (*ptr == end) {
        return 0;
    } else {
        *ptr = end + 1;
        return 1;
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
21
22
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
24
25
26
27
28
29
30
31
32
int parseReal(char** ptr, double* result) {
    char *end;
    *result = strtod(*ptr, &end);

    if (*ptr == end) {
        return 0;
    } else {
        *ptr = end + 1;
        return 1;
    }
33
34
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
35
36
37
38
void copyStringAndForward(char* dest, char** source, int length) {
    memcpy(dest, *source, length);
    dest[length] = '\0';
    *source += length;
39
40
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Desc* parseFunName(char** ptr) {
    // This is used to temporarily copy (converts clean string to C style) the function name for lookup
    char name[MAX_IDENTIFIER_LENGTH];

    int nameLength;
    if (!parseInt(ptr, &nameLength)) return 0;

    copyStringAndForward(name, ptr, nameLength);
    Desc* f = (Desc*) find_desc(name);

    if (f == 0) {
        printf("%s not found\n", name);
        return 0;
    }

    return f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
57
58
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
int parseDef1(char** ptr) {
    int defSize;
    if (!parseInt(ptr, &defSize)) return -1;

    char* nextDef = *ptr + defSize;

    // 2. Type char
    char type = *(*ptr)++;
    switch (type) {

        case 'F': // Normal function
        {
            // example.g_45 !a_0 !b_1 c_2 = add a_0 b_1
            // 37 F12 example.g_453 6 AF3 add2 VA0 VA1

            // 1. field: length of the name
            int nameLength;
            if (!parseInt(ptr, &nameLength)) return 0;

            // 2. field: characters of the name (postpone copying)
            char* namePtr = *ptr;
            *ptr += nameLength;

            // 3. field: arity
            int arity;
            if (!parseInt(ptr, &arity)) return 0;

            // before the FunEntry there are "arity" number of SliceEntries
            SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof (SliceEntry) * arity + sizeof (FunEntry) + nameLength + 1);

            FunEntry* entry = (FunEntry*) (entry_base + arity);
            entry->base.type = FT_FUN;
            entry->base.arity = arity;
92
            entry->base.thunk_size = thunk_size_f(arity);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
93
94
95
            entry->base.unboxable = false;
            entry->base.hnf = false;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
            // now the name can be copied into the FunEntry
            memcpy(entry->name, namePtr, nameLength);
            entry->name[nameLength] = '\0';

            // 4. field: strictness bits
            if (!parseInt(ptr, &entry->strictness)) return 0;

            // set the continuation for stage 2
            entry->parseCont = *ptr;

            // generate slices. avoid function call if arity is zero
            if (arity > 0) gen_slices(entry_base, (Desc*) entry, arity);

            add_desc(entry->name, (Desc*) entry);
            break;
        }

        case 'C': // CAF
        {
            // example.f =: example.g_48 1 2
            // 39 C9 example.fAF12 example.g_482 LI1 LI2 

            int nameLength;
            if (!parseInt(ptr, &nameLength)) return 0;

            CAFEntry* entry = (CAFEntry*) alloc_desc(sizeof (CAFEntry) + nameLength + 1);
            entry->base.type = FT_CAF;
            entry->base.arity = 0;
124
            entry->base.thunk_size = thunk_size_f(0);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
125
126
127
            entry->base.unboxable = false;
            entry->base.hnf = false;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
            copyStringAndForward(entry->name, ptr, nameLength);

            // set the continuation for stage 2
            entry->parseCont = *ptr;

            add_desc(entry->name, (Desc*) entry);
            break;
        }

        case 'A': // ADT
        {
            // :: example.A = example.A !a1 a2 | example.B 
            // 33 A2 9 example.A2 2 9 example.B0 0 

            // 1. field: the number of constructors
            int conNum;
            if (!parseInt(ptr, &conNum)) return 0;

            for (int i = 0; i < conNum; i++) {
                // 1. field: length of the name
                int nameLength;
                if (!parseInt(ptr, &nameLength)) return 0;

                // 2. field: characters of the name (postpone copying)
                char* namePtr = *ptr;
                *ptr += nameLength;

                // 3. field: arity
                int arity;
                if (!parseInt(ptr, &arity)) return 0;

                // before the FunEntry there are "arity" number of SliceEntries
                SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof (SliceEntry) * arity + sizeof (ADTEntry) + nameLength + 1);

                ADTEntry* entry = (ADTEntry*) (entry_base + arity);
                entry->base.type = FT_ADT;
                entry->base.arity = arity;
165
                entry->base.thunk_size = thunk_size_f(arity);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
166
167
                entry->base.unboxable = false;
                entry->base.hnf = true;
168
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
                // now the name can be copied into the ADTEntry
                memcpy(entry->name, namePtr, nameLength);
                entry->name[nameLength] = '\0';

                // 4. field: strictness bits
                if (!parseInt(ptr, &entry->strictness)) return 0;

                // generate slices. avoid function call if arity is zero
                if (arity > 0) gen_slices(entry_base, (Desc*) entry, arity);

                add_desc(entry->name, (Desc*) entry);
            }

            break;
        }

        case 'R': // Record
        {
            // :: example._R = {example.a, !example.b}
            // 40 R10 example._R2 1 9 example.a9 example.b

            int nameLength;
            if (!parseInt(ptr, &nameLength)) return 0;

            RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof (RecordEntry) + nameLength + 1);
            entry->base.type = FT_RECORD;

            copyStringAndForward(entry->name, ptr, nameLength);

            int arity;
            if (!parseInt(ptr, &arity)) return 0;
            entry->base.arity = arity;
            if (!parseInt(ptr, &entry->strictness)) return 0;

203
            entry->base.thunk_size = thunk_size_f(arity);            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
204
205
            entry->base.unboxable = false;
            entry->base.hnf = true;
206
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
            entry->fields = (char**) alloc_desc(sizeof (char*) * arity);
            for (int i = 0; i < arity; i++) {
                int fieldNameLength;
                if (!parseInt(ptr, &fieldNameLength)) return 0;

                entry->fields[i] = (char*) alloc_desc(fieldNameLength);
                copyStringAndForward(entry->fields[i], ptr, fieldNameLength);
            }

            add_desc(entry->name, (Desc*) entry);
            break;
        }

        default:
            return 0;
    }

    *ptr = nextDef;
    return 1;
226
227
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
228
ThunkEntry* parseLit(char **ptr) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
229
230
231
232
233
234
235
236
    // 1. Type char
    char type = *(*ptr)++;

    int strlen = 0;
    if (type == 'S') {
        if (!parseInt(ptr, &strlen)) return 0;
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
237
238
    struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry) + strlen);
    entry->base.type = CT_THUNK;
239
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
240
241
242
    switch (type) {
        case 'I': // Int
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
243
            entry->thunk.desc = (Desc*) __INT_SHARED__;
244
            if (!parseInt(ptr, &entry->thunk._int)) return 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
245
246
247
248
249
            break;
        }

        case 'C': // Char
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
250
            entry->thunk.desc = (Desc*) __CHAR_SHARED__;
251
            entry->thunk._char = *(*ptr)++;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
252
253
254
255
256
            break;
        }

        case 'R': // Real
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
257
            entry->thunk.desc = (Desc*) __REAL_SHARED__;
258
            if (!parseReal(ptr, &entry->thunk._real)) return 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
259
260
261
262
263
264
            break;
        }

        case '0': // Bool
        case '1':
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
265
            entry->thunk.desc = (Desc*) __BOOL_SHARED__;
266
            entry->thunk._bool = type == '1';
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
267
268
269
270
271
            break;
        }

        case 'S': // String
        {
272
273
            // TODO
            return 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
274
275
276
277
        }
    }

    return entry;
278
279
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
280
VarEntry* parseVar(char **ptr, VarEntry* target) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
281
282
283
    // 1. Type char
    char type = *(*ptr)++;

284
285
    struct VarEntry* entry = target == NULL ? (VarEntry*) alloc_code(sizeof (VarEntry)) : target;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
286
287
    switch (type) {
        case 'L': // Local var
288
289
290
            entry->base.type = CT_VAR;            
            entry->base.strict = false;
            break;
291
        case 'S': // Strict local var    
292
293
            entry->base.type = CT_VAR_STRICT;            
            entry->base.strict = true;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
294
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
295
296
        default:
            return 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
297
    }
298
299
300
    
    if (!parseInt(ptr, &entry->index)) return 0;
    return entry;    
301
302
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
303
Code* parseTerm(char **ptr);
304

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
305
306
307
308
309
310
311
/* 
 * It is very messy, because it handles 3 cases:
 * 1. Dynamic app: the function part is a local variable or argument  
 * 2. Static app, where the function is a zero arg data constructor, or a non-zero parameter function with zero arguments
 * 3. Other static cases
 */
Code* parseApp(char **ptr, bool dynamic) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
312
313
314
    int nrArgs;
    if (!parseInt(ptr, &nrArgs)) return 0;

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
315
316
317
318
319
320
321
322
    struct AppEntry* entry = NULL;
    
    if(!dynamic && nrArgs == 0)
    {
        (*ptr)++; // Skip 'F' for the static var

        Desc* desc = parseFunName(ptr); // can fail 
        if(desc != NULL) desc = get_slice(desc, nrArgs);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
323

324
        if(desc != NULL && (desc->type == FT_ADT || desc->type == FT_SLICE))
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
325
326
327
328
329
330
331
332
333
        {
            struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry));
            entry->base.type = CT_THUNK;
            entry->thunk.desc = desc;
            return (Code*) entry;
        }
        
        entry = (AppEntry*) alloc_code(sizeof (AppEntry));        
        entry->f = desc;
334
        entry->base.type = CT_APP;            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
335
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
336
337
338
339
340
341
342
343
    else
    {
        entry = (AppEntry*) alloc_code(sizeof (AppEntry) + sizeof (void*) * nrArgs);

        for (int i = 0; i < nrArgs; i++) {
            entry->args[i] = parseTerm(ptr);
            if (entry->args[i] == 0) return 0;
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
344

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
345
346
347
        if(dynamic)
        {
            parseVar(ptr, &entry->var);        
348
            entry->base.type = CT_APP_DYN;                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
349
350
351
352
353
354
355
356
        }
        else
        {
            (*ptr)++; // Skip 'F' for the static var            
            
            Desc* desc = parseFunName(ptr); // can fail 
            if(desc != NULL) desc = get_slice(desc, nrArgs);
            entry->f = desc;
357
            entry->base.type = CT_APP;            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
358
359
        }
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
360
    
361
    entry->base.nr_args = nrArgs;        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
362
    return (Code*) entry;
363
364
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
SelectEntry* parseSelect(char **ptr) {
    Code* expr = parseTerm(ptr);

    int nrCases;
    if (!parseInt(ptr, &nrCases)) return 0;

    struct SelectEntry* entry = (SelectEntry*) alloc_code(sizeof (SelectEntry) + sizeof (SelectCaseEntry) * nrCases);
    entry->base.type = CT_SELECT;
    entry->base.nr_cases = nrCases;
    entry->expr = expr;

    for (int i = 0; i < nrCases; i++) {
        char type = *(*ptr)++;

        switch (type) {
            case 'C':
                entry->cases[i].type = SC_CONS;
                entry->cases[i].cons = (ADTEntry*) parseFunName(ptr);
                break;
            case 'L':
                entry->cases[i].type = SC_LIT;
                entry->cases[i].lit = parseLit(ptr);
                break;
            case 'D':
                entry->cases[i].type = SC_DEFAULT;
                break;
            default:
                return 0; // unknown case
        }

        entry->cases[i].body = (Code*) parseTerm(ptr);
    }

    return entry;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
399
400
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
401
402
403
404
405
406
407
408
IfEntry* parseIf(char **ptr) {
    struct IfEntry* entry = (IfEntry*) alloc_code(sizeof (IfEntry));
    entry->base.type = CT_IF;
    entry->cond = parseTerm(ptr);
    entry->texpr = parseTerm(ptr);
    entry->fexpr = parseTerm(ptr);

    return entry;
409
410
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
411
412
413
414
415
416
417
418
Code* parseTerm(char **ptr) {
    // 1. Type char
    char type = *(*ptr)++;

    switch (type) {
        case 'L': // Literal
            return (Code*) parseLit(ptr);
        case 'V': // Variable
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
419
            return (Code*) parseVar(ptr, NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
420
421
422
423
        case 'A': // Static application
            return (Code*) parseApp(ptr, false);
        case 'D': // Dynamic application
            return (Code*) parseApp(ptr, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
424
425
426
427
428
429
430
431
        case 'S': // Select
            return (Code*) parseSelect(ptr);
        case 'I': // If
            return (Code*) parseIf(ptr);
        default:
            printf("parseTerm: unhandled term type\n");
            exit(-1);
    }
432
433
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
int parseDef2(char** ptr) {
    int defSize;
    if (!parseInt(ptr, &defSize)) return -1;

    char* nextDef = *ptr + defSize;

    // 2. Type char
    char type = *(*ptr)++;

    switch (type) {
        case 'F': // Normal function
        {
            FunEntry* entry = (FunEntry*) parseFunName(ptr); // should not fail, just added to the map in the 1. phase

            // read continuation
            *ptr = entry->parseCont;

            // parse body
            entry->body = parseTerm(ptr);

            break;
        }

        case 'C': // CAF
        {
            CAFEntry* entry = (CAFEntry*) parseFunName(ptr); // should not fail, just added to the map in the 1. phase

            // read continuation
            *ptr = entry->parseCont;

            // parse body
            entry->body = parseTerm(ptr);

            break;
        }

        case 'A':
        case 'R':
            break; // skip it, already fully parsed

        default:
            return -1;
    }

    *ptr = nextDef;
    return 1;
480
481
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
int parse(char** ptr, int length) {
    char* origptr = *ptr;

    char* endptr = *ptr + length;
    int numDefs = 0;

    while (*ptr < endptr) {
        numDefs++;
        if (parseDef1(ptr) < 0) return -1;
    }

    *ptr = origptr;

    while (*ptr < endptr) {
        if (parseDef2(ptr) < 0) return -1;
    }

    return numDefs;
500
}
501
502