-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasdl.c
118 lines (101 loc) · 2.79 KB
/
asdl.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
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "asdl.h"
extern FILE *yyin;
extern int yyparse(void);
static inline void print_help_and_exit() {
printf(
"Usage: asdl [-o output] [-s symfile] [-d def_suffix] [-f fn_suffix] [-k "
"kind_suffix] [-a arg_suffix] [-p fn_prefix] FILE\n");
printf("\n");
printf("Options:\n");
printf(" -o output Specify the output file name\n");
printf(" -d header file Specify the symbol declaration file header name\n");
printf(" -D def_suffix Specify the suffix for typedefs\n");
printf(" -f fn_suffix Specify the suffix for functions\n");
printf(" -k kind_suffix Specify the suffix for kind types\n");
printf(" -a arg_suffix Specify the suffix for arguments\n");
printf(" -p fn_prefix Specify the prefix for function signatures\n");
printf("\n");
exit(EX_USAGE);
}
void parse_arguments(int argc, char **argv) {
int c = 0;
char *def_suffix = "def", *fn_suffix = "fn", *arg_suffix = "arg",
*kind_suffix = "kind";
char *fn_prefix = NULL;
char *outpath = NULL;
char *sympath = NULL;
yyin = stdin;
while ((c = getopt(argc, argv, "o:d:f:a:k:p:h:D")) != -1) {
switch (c) {
case 'o':
outpath = optarg;
break;
case 'd':
sympath = optarg;
break;
case 'D':
def_suffix = optarg;
break;
case 'a':
arg_suffix = optarg;
break;
case 'f':
fn_suffix = optarg;
break;
case 'k':
kind_suffix = optarg;
break;
case 'p':
fn_prefix = optarg;
break;
case 'h':
print_help_and_exit();
break;
default:
fprintf(stderr, "Error: wrong argument, pass -h to view help\n");
exit(EXIT_FAILURE);
}
}
if (optind >= argc && isatty(STDIN_FILENO)) {
fprintf(stderr,
"Error: no input file given, neither via arguments nor STDIN\n");
exit(EXIT_FAILURE);
} else if (optind < argc && isatty(STDIN_FILENO)) {
if (access(argv[optind], F_OK | R_OK) == 0)
yyin = fopen(argv[optind], "r");
else {
fprintf(stderr, "Error: input file is not readable, or does not exist");
exit(EXIT_FAILURE);
}
} else if (optind < argc && !isatty(STDIN_FILENO)) {
yyin = stdin;
}
assign_prefixes(fn_prefix);
assign_suffixes(def_suffix, fn_suffix, arg_suffix, kind_suffix);
init_translator(outpath, sympath);
init_absyn();
}
void do_at_exit(void) {
dump_absyn();
dump_translator();
}
int main(int argc, char **argv) {
parse_arguments(argc, argv);
atexit(do_at_exit);
symtable_init();
yyparse();
finalize_absyn();
finalize_translator();
if (yyin != stdin)
fclose(yyin);
fprintf(stderr, "Successfully tanslated\n");
return 0;
}