summaryrefslogtreecommitdiff
path: root/util/muse.c
blob: b3f203e3eccc1a5c2434030d67b446f5b5aa709d (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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "parse.h"

/* FIXME: move into one place...? */
Node *file;
char *outfile;
int debug;
char debugopt[128];
char **incpaths;
size_t nincpaths;

static void usage(char *prog)
{
    printf("%s [-h] [-o outfile] inputs\n", prog);
    printf("\t-h\tPrint this help\n");
    printf("\t-I path\tAdd 'path' to use search path\n");
    printf("\t-d\tPrint debug dumps\n");
    printf("\t-o\tOutput to outfile\n");
    printf("\t-s\tShow the contents of usefiles `inputs`\n");
}


int main(int argc, char **argv)
{
    int opt;
    int i;
    Stab *globls;
    FILE *f;

    while ((opt = getopt(argc, argv, "d::ho:I:")) != -1) {
        switch (opt) {
            case 'o':
                outfile = optarg;
                break;
            case 'h':
                usage(argv[0]);
                exit(0);
                break;
            case 'd':
                debug = 1;
                while (optarg && *optarg)
                    debugopt[*optarg++ & 0x7f] = 1;
                break;
	    case 'I':
		lappend(&incpaths, &nincpaths, optarg);
		break;
            default:
                usage(argv[0]);
                exit(0);
                break;
        }
    }

    if (debugopt['s']) {
        for (i = optind; i < argc; i++) {
            globls = mkstab();
            f = fopen(argv[i], "r");
            readuse(file, globls);
            dumpstab(globls, stdout);
        }
        exit(0);
    }

    for (i = optind; i < argc; i++) {
        globls = mkstab();
        tyinit(globls);
        tokinit(argv[i]);
        file = mkfile(argv[i]);
        file->file.exports = mkstab();
        file->file.globls = globls;
        yyparse();

        infer(file);
	if (!outfile)
	    die("need output file name right now. FIX THIS.");
	f = fopen(outfile, "w");
	writeuse(file, f);
	fclose(f);
    }

    return 0;
}