@@ -34,25 +34,30 @@ extern int yyparse(void); // declare Bison parser function
3434extern int yy_scan_string (const char * str ); // lex the file for keywords
3535void parse_arguments (int argc , char * * argv ); // parse the arguments provided to BLang
3636char * read_file (const char * filename ); // Read an input file into a char*.
37+ void print_help ();
3738
3839CompilerContext ctx = (CompilerContext ){
3940 .emitAssembly = false,
4041 .emitLLVM = false,
42+ .dumpAST = false,
4143 .outputFilename = "a.out" ,
4244 .optimization = 0 ,
4345};
4446
4547int main (int argc , char * argv []) {
4648 parse_arguments (argc , argv );
47- initialize_llvm ();
4849
4950 yy_scan_string (ctx .sourceText ); // Feed input
5051 yyparse (); // Start parsing
5152
52- print_ast ();
53+ if (ctx .dumpAST ) print_ast ();
54+
55+ initialize_llvm ();
5356
5457 generate_llvm_ir ();
5558
59+ optimize ();
60+
5661 if (ctx .emitLLVM )
5762 export_ir ();
5863 else if (ctx .emitAssembly )
@@ -65,15 +70,15 @@ int main(int argc, char *argv[]) {
6570
6671
6772void parse_arguments (int argc , char * argv []) {
68- if (argc == 0 )
69- printf ( "--help function should print here. TODO." );
73+ if (argc == 1 )
74+ print_help ( );
7075
7176 for (int i = 1 ; i < argc ; ++ i ) {
72- // If "-S" is passed, the program will return assembly instead of machine code.
7377 if (strcmp (argv [i ], "-S" ) == 0 ) { ctx .emitAssembly = true; }
74-
75- // If "-emit-llvm" is passed, the program will return LLVM IR instead of machine code.
76- else if (strcmp (argv [i ], "--emit-llvm" ) == 0 ) { ctx .emitLLVM = true; }
78+ else if (strcmp (argv [i ], "-emit-llvm" ) == 0 ) { ctx .emitLLVM = true; }
79+ else if (strcmp (argv [i ], "-ast-dump" ) == 0 ) { ctx .dumpAST = true; }
80+
81+ else if (strcmp (argv [i ], "-h" ) == 0 || strcmp (argv [i ], "--help" ) == 0 ) { print_help (); }
7782
7883 else if (strcmp (argv [i ], "-o" ) == 0 ) {
7984 ctx .outputFilename = argv [i + 1 ];
@@ -94,7 +99,7 @@ void parse_arguments(int argc, char *argv[]) {
9499 ctx .optimization = 5 ;
95100
96101 else {
97- if (argv [i ][0 ] == '-' ) { fatal_error ("unknown argument: \'%s\'\n " , argv [i ]); }
102+ if (argv [i ][0 ] == '-' ) { fatal_error ("unknown argument: \'%s\'" , argv [i ]); }
98103 else {
99104 ctx .inputFile = argv [i ];
100105 ctx .sourceText = read_file (argv [i ]);
@@ -119,3 +124,27 @@ char* read_file(const char *filename) {
119124 fclose (f );
120125 return buf ;
121126}
127+
128+ void print_help () {
129+ printf (
130+ "Usage: blang [options] <source files>\n"
131+ "\n"
132+ "Blang Compiler " BLANG_VERSION_STRING "\n"
133+ "A simple compiler for the B programming language.\n"
134+ "\n"
135+ "Options:\n"
136+ " -h, --help Show this help message and exit\n"
137+ // " -v, --version Show compiler version\n"
138+ " -o <file> Specify output file name (default: a.out)\n"
139+ " -S Compile to assembly code only\n"
140+ " -emit-llvm Emit LLVM IR instead of machine code\n"
141+ " -dump-ast Output the abstract syntax tree (AST)\n"
142+ " -O0, -O1, -O2, -O3 Optimization level (default: -O0)\n"
143+ "\n"
144+ "Examples:\n"
145+ " blang main.b Compile and link main.b to a.out\n"
146+ " blang -S main.b Generate assembly code from main.b\n"
147+ " blang -O2 -o prog main.b Compile main.b with optimization level 2 to prog\n"
148+ );
149+ exit (0 );
150+ }
0 commit comments