aboutsummaryrefslogtreecommitdiff
path: root/compiler/main.c
blob: 00d0b8cca5b89eb0dc66e7a9eaadb684d5e5becc (plain)
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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

/*

  main.c -- The entry point of the LRL compiler.

  Copyright © 2011-2016 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "backend.h"
#include "configfile.h"
#include "context_private.h"
#include "display.h"
#include "filesys.h"
#include "misc.h"
#include "platform.h"
#include "tokenizer.h"
#include "parser.h"
#include "verify.h"

static void cmdline_error(const char *format, ...)
{
    va_list args;
    
    va_start(args, format);
    fprintf(stderr, "lrlc: error: ");
    vfprintf(stderr, format, args);
    fprintf(stderr, "\n");
    va_end(args);
}

static int try_get_arg(char **dest, const char *description,
                       int *argc, char ***argv)
{
    char **av = *argv;
    int ac = *argc;
    
    if (av[-1][2]) {
        *dest = &av[-1][2];
        return 0;
    } else if (ac != 0) {
        *dest = av[0];
        ++*argv;
        --*argc;
        return 0;
    } else {
        cmdline_error("missing %s argument for -%c",
                      description, av[-1][1]);
        return 2;
    }
}

#define get_arg(destvar, description) do { \
    if (try_get_arg(destvar, description, &argc, &argv)) return 2; \
} while (0)

/**
 * Returns 1 if arg matches either --optname or --optname=xxx.
 * The optname parameter should contain the "--" but not the "=".
 */
static int is_longopt(const char *arg, const char *optname)
{
    size_t optlen = strlen(optname);
    if (strncmp(arg, optname, optlen) != 0) return 0;
    return arg[optlen] == '\0' || arg[optlen] == '=';
}

/**
 * Extracts the value of a long option. argv must point to a long option,
 * either as two strings "--longopt", "value" or as a single string
 * "--longopt=value".
 */
static char *get_longopt_value(int *argc, char ***argv)
{
    char *end = strchr((*argv)[-1], '=');
    if (end) {
        return end+1;
    } else if (*argc) {
        (*argc)--;
        (*argv)++;
        return (*argv)[-1];
    } else {
        return NULL;
    }
}

typedef struct InputEntry InputEntry;
struct InputEntry {
    InputEntry *next;
    
    /* TODO should support intermediate code also */
    char *filename;
};

typedef enum {
    CM_AllToOneOutput = 0, /* default */
    CM_CompileOnly = 1, /* -c */
    CM_ExpectErrors = 2 /* --expect-errors */
} CompileMode;

typedef enum {
    DT_None = 0,
    DT_Tokens,
    DT_AST,
    DT_Idents,
    DT_InteropAST,
    DT_InteropIdents,
    DT_IR
} DumpType;

static const LRLBackend *backends[LRL_BACKEND_COUNT];
static const LRLBackend *backend;
static LRLConfig *config;
static InputEntry *inputs;
static const LRLPath *includedirs;
static CompileMode mode = CM_AllToOneOutput;
static DumpType dump_type;
static char *output = NULL;
static char *outputdir = NULL;
static char *rootdir = ".";
static int quiet, testmode;

static LRLIdent *root_scope;


static NORETURN void show_help(const char *progname)
{
    /* List of backends */
    char blist[34]; /* amount that fits on the screen */
    char *listp = blist;
    size_t i;
    
    for (i = 0; i < LRL_BACKEND_COUNT; i++) {
        if (i > 0) {
            listp += sprintf(listp, i == LRL_BACKEND_COUNT-1 ?
                " or " : ", ");
        }
        listp += sprintf(listp, "%s", backends[i]->name);
    }
    
    printf(
"Usage: %s [options] file...\n"
"\n"
"Options:\n"
"  --backend NAME   Selects the backend to use: %s\n"
"  -c               Compile object files (.o), instead of an executable.\n"
"  --dump INFO      Dumps info and exits. Common values: ast, idents, ir\n"
"  -h, --help       Shows this help text.\n", progname, blist);
    printf(
"  -I PATH          Adds an include search path.\n"
"  -q, --quiet      Don't print any error messages.\n"
"  -r PATH          Sets the root path of the project being compiled.\n"
"  -o FILENAME      Sets the output file.\n"
"\n");
    
    exit(0);
}

static int set_mode(CompileMode newmode)
{
    if (mode) {
        cmdline_error("multiple modes specified (-c and --expect-errors)");
        return 2;
    }
    mode = newmode;
    return 0;
}

static int parse_args(int argc, char **argv)
{
    const char *progname = argv[0];
    InputEntry *last_input = NULL;
    int parse_opts = 1;
    size_t i;
    argc--;
    argv++;
    
    while (argc) {
        
        char *arg = *(argv++);
        argc--;
        
        if (parse_opts && arg[0] == '-' && arg[1]) {
            switch (arg[1]) {
                case '-':
                    if (!arg[2]) {
                        /* -- = stop parsing command line options */
                        parse_opts = 0;
                    } else if (is_longopt(arg, "--dump")) {
                        /* Dump type */
                        if ((arg = get_longopt_value(&argc, &argv)) == NULL)
                            goto invalid_dump_type;
                        
                        if (!strcmp(arg, "tokens")) dump_type = DT_Tokens;
                        else if (!strcmp(arg, "ast")) dump_type = DT_AST;
                        else if (!strcmp(arg, "idents")) dump_type = DT_Idents;
                        else if (!strcmp(arg, "interop_ast")) dump_type = DT_InteropAST;
                        else if (!strcmp(arg, "interop_idents")) dump_type = DT_InteropIdents;
                        else if (!strcmp(arg, "ir")) dump_type = DT_IR;
                        else if (!strcmp(arg, "none")) dump_type = DT_None;
                        else goto invalid_dump_type;
                        break;
                        
                      invalid_dump_type:
                        cmdline_error("invalid dump type. should be "
                                  "\"tokens\", \"ast\", \"idents\", "
                                  "\"interop_ast\", \"interop_idents\", "
                                  "\"ir\" or \"none\".");
                        return 2;
                    } else if (is_longopt(arg, "--backend")) {
                        /* Which backend to use */
                        if ((arg = get_longopt_value(&argc, &argv)) != NULL) {
                            for (i = 0; i < LRL_BACKEND_COUNT; i++) {
                                if (!strcmp(backends[i]->name, arg)) {
                                    backend = backends[i];
                                    goto valid_backend_name;
                                }
                            }
                        }
                        
                        cmdline_error("unknown backend name. "
                                      "Supported backends are:");
                        for (i = 0; i < LRL_BACKEND_COUNT; i++) {
                            fprintf(stderr, "    %s\n", backends[i]->name);
                        }
                        return 2;
                        
                      valid_backend_name:
                        break;
                    } else if (is_longopt(arg, "--internal--outdir")) {
                        /* Output path that is prefixed to the output files
                           (used internally for testing the compiler without
                            spawning a new process for each test file) */
                        if ((arg = get_longopt_value(&argc, &argv)) == NULL) {
                            cmdline_error("--output-dir requires an argument.");
                            return 2;
                        } else {
                            outputdir = arg;
                        }
                    } else if (!strcmp(arg, "--internal--testmode")) {
                        testmode = 1;
                    } else if (!strcmp(arg, "--expect-errors")) {
                        /* Used for running tests of the compiler */
                        if (set_mode(CM_ExpectErrors) != 0) return 2;
                    } else if (!strcmp(arg, "--quiet")) {
                        quiet = 1;
                    } else if (!strcmp(arg, "--help")) {
                        show_help(progname);
                    } else {
                        cmdline_error("invalid option: %s", arg);
                        return 2;
                    }
                    break;
                case 'c':
                    /* compile only */
                    if (arg[2]) goto invalid_option;
                    if (set_mode(CM_CompileOnly) != 0) return 2;
                    break;
                case 'h':
                    /* help */
                    show_help(progname);
                    break;
                case 'I': {
                    /* include path */
                    char *path;
                    LRLPath *entry;
                    get_arg(&path, "include path");
                    
                    entry = malloc(sizeof(LRLPath));
                    entry->next = includedirs;
                    entry->path = path;
                    includedirs = entry;
                    break; }
                case 'r':
                    /* root directory */
                    get_arg(&rootdir, "root directory");
                    break;
                case 'q':
                    /* quiet mode */
                    quiet = 1;
                    break;
                case 'o':
                    /* output */
                    get_arg(&output, "output file");
                    break;
                default:
                invalid_option:
                    cmdline_error("invalid option: %s", arg);
                    return 2;
            }
        } else {
            /* A filename argument (or "-") */
            InputEntry *entry = malloc(sizeof(InputEntry));
            entry->next = NULL;
            entry->filename = arg;
            
            if (last_input) last_input->next = entry;
            else inputs = entry;
            
            last_input = entry;
        }
    }
    
    if (!inputs) {
        cmdline_error("no input files specified.");
        return 2;
    }
    
    return 0;
}


typedef enum {
    LRL_FF_Implementation = 0x1,
    LRL_FF_Optional =       0x2
} LRLFileFlags;

static LRLASTNamespace *parse_file(LRLCtx *ctx, char *filename,
                                   LRLFileFlags flags);


/* TODO can this function be merged with lrl_filesys_ensure_loaded? */
static LRLASTNamespace *load_headers(LRLCtx *ctx, const char *filename)
{
    LRLASTNamespace *header_root;
    char *newname;
    
    /* Determine path without file extension */
    const char *nameend = strchr(filename, '.');
    size_t namelen = (nameend ? (size_t)(nameend - filename) : strlen(filename));
    
    if (nameend && strncmp(nameend, ".lh", 3) == 0) {
        /* This is a header. Don't load it twice! */
        /* XXX should .lh and .lc be changed to .lpub and .lpriv?
           .lh files are quite similar to C headers, but they are not
           duplicated in LRL. For example, they can be used to expose the
           implementation of functions, to allow for optional inlining or
           optimizations. */
        return NULL;
    }
    
    /* Load the .lh file */
    newname = malloc(namelen+4); /* name.lh\0 */
    memcpy(newname, filename, namelen);
    memcpy(newname+namelen, ".lh", 4);
    header_root = parse_file(ctx, newname, LRL_FF_Optional);
    
    /* newname is in use by this file now.
       it should not be modified or freed! */
    
    /* Load all .lh files in the directory */
    /* TODO */
    
    /*free(newname);*/
    return header_root;
}

static LRLASTNamespace *parse_file(LRLCtx *ctx, char *filename,
                                   LRLFileFlags flags)
{
    LRLIdent *scope;
    LRLASTNamespace *root, *header;
    
    /* Determine namespace from path */
    /* TODO resolve ../ to real directories and remove ./  */
    scope = lrl_ident_insert_path(ctx, root_scope, filename);
    scope->flags = (scope->flags & ~LRL_IdFl_NotLoaded) | LRL_IdFl_HasHere;
    
    if (flags & LRL_FF_Implementation) {
        /* Create a private scope for the implementation */
        scope = lrl_ident_create_priv_scope(scope);
        scope->flags |= LRL_IdFl_HasHere | LRL_IdFl_Implementation;
        scope->hash = scope->scope->hash;
        
        /* Load the interface (the header file) */
        header = load_headers(ctx, filename);
    }
    
    /* Load/parse the file into the scope */
    root = lrl_filesys_load_file(ctx, scope, filename,
                         !(flags & LRL_FF_Optional), /* must_exist */
                         dump_type == DT_Tokens); /* dump tokens only */
    
    if (!root) return NULL;
    
    lrl_ctx_bind_verify(ctx);
    
    if (flags & LRL_FF_Implementation) {
        /* Check compatibility of definitions */
        /* TODO */
    }
    
    if (dump_type == DT_AST) {
        lrl_display_deflist(root->list, 0);
        return NULL;
    } else if (dump_type == DT_InteropAST) {
        lrl_display_deflist_interops(root->list);
        return NULL;
    } else if (dump_type == DT_Idents) {
        lrl_display_scope(scope, 0);
        return NULL;
    } else if (dump_type == DT_InteropIdents) {
        /* interop identifiers are dumped after all files have been processed */
        return NULL;
    }
    
    return root;
}

/**
 * The "root scope" is simply an empty scope with the path set to the root
 * path, but it has to include any include directories and the builtin
 * definitions. This function creates the root scope.
 */
static void setup_root_scope(LRLCtx *ctx)
{
    LRLIdent *scope;
    const LRLPath *dir;
    
    /* Root of the project/whatever being compiled */
    root_scope = calloc(1, sizeof(LRLIdent));
    root_scope->path = rootdir;
    root_scope->flags |= LRL_IdFl_NotLoaded | LRL_IdFl_HasHere;
    
    /* Wrap the project root in include dir scopes */
    scope = root_scope;
    for (dir = includedirs; dir; dir = dir->next) {
        LRLIdent *include = calloc(1, sizeof(LRLIdent));
        scope->scope = include;
        include->path = (char*)dir->path;
        include->flags |= LRL_IdFl_NotLoaded;
        scope = include;
    }
    
    /* The outermost scope (the "real" root) is the builtins */
    scope->scope = ctx->builtins_scope;
}

/* Functions for testing of the compiler */
typedef struct {
    int line, optional, count;
} ExpectErrorLine;
static ExpectErrorLine *expect_lines;
static int num_expect_lines;
static const char *expect_filename;
static int unexpected_error;

static void add_expected_line(int line, int optional)
{
    expect_lines = try_realloc(expect_lines, ++num_expect_lines*sizeof(ExpectErrorLine));
    expect_lines[num_expect_lines-1].line = line;
    expect_lines[num_expect_lines-1].optional = optional;
    expect_lines[num_expect_lines-1].count = 0;
}

static void expect_handler(LRLCtx *ctx, const LRLError *err)
{
    const char *filename;
    int line, col;
    
    lrl_ctx_current_error_source(ctx, &filename, &line, &col);

    if (filename && !strcmp(filename, expect_filename)) {
        int i;
        for (i = 0; i < num_expect_lines; i++) {
            if (expect_lines[i].line == line) {
                expect_lines[i].count++;
                return;
            }
        }
        
        /* Display unexpected errors to stderr */
        lrl_err_default_handler(ctx, err);
        unexpected_error = 1;
    }
}

static void silent_handler(LRLCtx *ctx, const LRLError *err)
{
    /* Error message is supressed */
    (void)ctx;
    (void)err;
}


int main(int argc, char **argv)
{
    int error;
    LRLCtx *ctx;
    
    lrl_backend_get_list(backends);
    backend = backends[LRL_BACKEND_DEFAULT];
    
    lrl_platform_init_paths();
    includedirs = lrl_platform_includes;
    
    error = parse_args(argc, argv);
    if (error) return error;
    
    config = lrl_config_new();
    lrl_config_load_system(config);
    
    ctx = lrl_ctx_new(config);
    setup_root_scope(ctx);
    
    if (quiet) {
        lrl_err_set_handler(ctx, silent_handler);
    }
    
    switch (mode) {
    case CM_CompileOnly:
        /* Compile each file separately */
        if (output && (!inputs || inputs->next)) {
            cmdline_error("the -o option in -c mode requires exactly one "
                          "input file.");
            return 2;
        }
        
        for (; inputs; inputs = inputs->next) {
            LRLASTNamespace *file_ast;
            
            lrl_ctx_set_has_errors(ctx, 0);
            if (testmode) {
                fprintf(stderr, "-- %s --\n", inputs->filename);
            }
            file_ast = parse_file(ctx, inputs->filename, LRL_FF_Implementation);
            
            if (lrl_ctx_has_errors(ctx)) {
                error = 1;
                continue;
            }
            
            if (file_ast && (!dump_type || dump_type == DT_IR)) {
                char *defout = (output ?
                    NULL : get_output_filename(inputs->filename,
                                               outputdir, 0));
                LRLBackendOptions options;
                options.config = config;
                options.op = (dump_type == DT_IR ?
                              LRL_BO_DumpIR : LRL_BO_MakeObject);
                options.input = inputs->filename;
                options.output = (output ? output : defout);
                if (backend->process(ctx, file_ast, &options) != 0) {
                    error = 1;
                }
                free(defout);
            } else if (dump_type == DT_InteropIdents) {
                lrl_display_scope(ctx->external_scope, 0);
            }
        }
        lrl_ctx_free(ctx);
        break;
    case CM_AllToOneOutput: {
        /* Compile multiple files into one output */
        LRLBackendOptions options;
        
        LRLASTNamespace ast_root;
        ast_root.ident = NULL;
        ast_root.list = NULL;
        
        for (; inputs; inputs = inputs->next) {
            LRLASTNamespace *namespac = parse_file(ctx, inputs->filename, LRL_FF_Implementation);
            LRLASTDefList *entry;
            
            if (lrl_ctx_has_errors(ctx)) {
                error = 1;
                continue;
            }
            
            if (!namespac) continue;
            
            entry = malloc(sizeof(LRLASTDefList));
            entry->def.ast_type = LRL_AST_Namespace;
            entry->def.kind.namespac = namespac;
            
            entry->next = ast_root.list;
            ast_root.list = entry;
        }
        
        
        if (!error && (!dump_type || dump_type == DT_IR)) {
            options.config = config;
            options.op = (dump_type == DT_IR ?
                          LRL_BO_DumpIR : LRL_BO_MakeExec);
            options.input = NULL;
            options.output = (output ? output : "a.out");
            if (backend->process(ctx, &ast_root, &options) != 0) {
                error = 1;
            }
        } else if (dump_type == DT_InteropIdents) {
            lrl_display_scope(ctx->external_scope, 0);
        }
        
        lrl_ctx_free(ctx);
        break; }
    case CM_ExpectErrors:
        /* Used for running tests of the compiler */
        if (!inputs || output) {
            cmdline_error("the --expect-errors options requires at least "
                          "one input file and no outputs.");
            return 2;
        }
        
        lrl_err_set_handler(ctx, expect_handler);
        for (; inputs; inputs = inputs->next) {
            char *source, *s;
            int line = 1, i;
            int errorblock = 0;
            int eof_error = 0;
            int optional;
            
            if (testmode) {
                fprintf(stderr, "-- %s --\n", inputs->filename);
            }
            lrl_ctx_set_has_errors(ctx, 0);
            unexpected_error = 0;
            
            /* Scan source for lines marked with // ERROR */
            expect_filename = inputs->filename;
            source = read_input(inputs->filename);
            if (!source) {
                perror(inputs->filename);
                continue;
            }
            for (s = source; *s; s += strcspn(s, "/\n\r")) {
                char ch;
                if (!strncmp(s, "// ERROR", 8)) {
                    s += 8;
                    optional = !strncmp(s, " OPTIONAL", 9);
                    add_expected_line(line, optional);
                    continue;
                } else if (!strncmp(s, "// EOF ERROR", 12)) {
                    s += 12;
                    optional = !strncmp(s, " OPTIONAL", 9);
                    eof_error = 1;
                    continue;
                } else if (!strncmp(s, "/* ERRORS", 9)) {
                    s += 9;
                    optional = !strncmp(s, " OPTIONAL", 9);
                    errorblock = line;
                    continue;
                }
                ch = *(s++);
                
                if (errorblock && line > errorblock && (ch == '\n' || ch == '\r')) {
                    add_expected_line(line, optional);
                }
                
                if (ch == '\n') {
                    line++;
                    goto was_linebreak;
                } else if (ch == '\r') {
                    line++;
                    if (*s == '\n') { s++; }
                    goto was_linebreak;
                }
                
                if (0) {
                  was_linebreak:
                    if (errorblock && (strcspn(s, "\r\n") <= strspn(s, " \t};"))) {
                        errorblock = 0;
                    }
                }
            }
            free(source);
            
            if (eof_error || errorblock) {
                add_expected_line(line, optional);
            }
            
            /* Parse file */
            parse_file(ctx, inputs->filename, LRL_FF_Implementation);
            
            /* Check that all errors where reported correctly */
            for (i = 0; i < num_expect_lines; i++) {
                if (expect_lines[i].count == 0 && !expect_lines[i].optional) {
                    if (!quiet) {
                        fprintf(stderr, "%s: no error reported at line %d\n",
                                inputs->filename, expect_lines[i].line);
                    }
                    error = 1;
                }
            }
            error |= unexpected_error;
            
            free(expect_lines);
            expect_lines = NULL;
            num_expect_lines = 0;
            
        }
        lrl_ctx_free(ctx);
        break;
    }
    
    lrl_config_free(config);
    return error; /* 1 or 0 */
}