aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorTeddy <ted.sybil@gmail.com>2014-03-25 04:11:54 +0800
committerTeddy <ted.sybil@gmail.com>2014-03-25 04:11:54 +0800
commit4f90efb9a08b697b7ef270a4d2ee067dc853f251 (patch)
tree229eb348ca132a3969b1906dec90e29edd8cd06a /main.c
parentda3bf74ba8946f88b892907361f86f567301be83 (diff)
move out the main function to a seperate file
Diffstat (limited to 'main.c')
-rw-r--r--main.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..181dde2
--- /dev/null
+++ b/main.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <getopt.h>
+#include "cibic.tab.h"
+#include "ast.h"
+
+extern int yyparse();
+extern FILE *yyin;
+
+int yywrap() {
+ return 1;
+}
+
+int yyerror(char *s) {
+}
+
+void print_ast() {
+ yyparse();
+ if (ast_root)
+ cnode_debug_print(ast_root, 1);
+ else
+ fprintf(stderr, "Syntax Error\n");
+}
+
+void print_help() {
+}
+
+static struct option lopts[] = {
+ { "ast", no_argument, 0, 'a'},
+ { "help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}
+};
+
+enum {
+ PRINT_AST,
+ PRINT_HELP
+} mode = PRINT_HELP;
+
+int main(int argc, char **argv) {
+ int option_index = 0, i;
+ while (1)
+ {
+ int c = getopt_long(argc, argv, "ah", lopts, &option_index);
+ if (c == -1)
+ break;
+ switch (c)
+ {
+ case 0: break;
+ case 'a': mode = PRINT_AST; break;
+ case 'h': mode = PRINT_HELP; break;
+ }
+ }
+ if (optind == argc - 1)
+ {
+ yyin = fopen(argv[argc - 1], "r");
+ if (!yyin)
+ {
+ fprintf(stderr, "Error while opening file.");
+ return 1;
+ }
+ }
+ else if (optind == argc)
+ yyin = stdin;
+ else
+ {
+ fprintf(stderr, "Only one source file is exepected.\n");
+ return 1;
+ }
+ switch (mode)
+ {
+ case PRINT_AST: print_ast(); break;
+ case PRINT_HELP: print_help(); break;
+ }
+ return 0;
+}