#include <getopt.h>
#include "ast.h"
+#include "gen.h"
#include "parse.h"
#include "scan.h"
+#include "type.h"
extern int yylex_destroy(void);
void usage(FILE *out, char *arg0)
"Compile an spl file. If FILE is not specified stdin is used.\n"
"\n"
"Options:\n"
- "\t-p\tJust parse and pretty print\n"
- "\t-t\tJust parse and typecheck\n"
- "\t-c\tparse, typecheck and compile (default)\n"
+ "\t-p\tPretty print the parsed abstract syntax tree\n"
+ "\t-t\tPretty print the typed abstract syntax tree\n"
"\t-h\tShow this help\n"
, arg0);
}
-enum mode {parse,type,compile};
-
int main(int argc, char *argv[])
{
int opt;
- enum mode mode = compile;
+ bool pparse = false, ptype = false;
- while ((opt = getopt(argc, argv, "hptc")) != -1) {
+ while ((opt = getopt(argc, argv, "hpt")) != -1) {
switch (opt) {
case 'p':
- mode = parse;
+ pparse = true;
break;
case 't':
- mode = type;
- break;
- case 'c':
- mode = compile;
+ ptype = true;
break;
case 'h':
usage(stdout, argv[0]);
if (r != 0)
return 1;
yylex_destroy();
- if (mode == parse) {
+ if (pparse)
ast_print(result, stdout);
+ if ((result = type(result)) == NULL) {
+ r = 1;
+ goto end;
+ }
+ if (ptype)
+ ast_print(result, stdout);
+ if (!gen(result)) {
+ r = 1;
goto end;
}
-end:
ast_free(result);
+end:
if (yyin == stdin)
if (fclose(yyin) == -1)
perror("fclose");
- return 0;
+ return r;
}