Skip to content
Snippets Groups Projects
Commit 027342ba authored by Clemens Paumgarten's avatar Clemens Paumgarten
Browse files

Add semantic check and options to mcc file

parent a45ac5aa
No related branches found
No related tags found
No related merge requests found
...@@ -49,4 +49,4 @@ add_executable(mcc ...@@ -49,4 +49,4 @@ add_executable(mcc
src/utils/mcc_scope.c src/utils/mcc_scope.c
include/mcc/mcc_scope.h include/mcc/mcc_scope.h
src/utils/dynamic_array.c src/utils/dynamic_array.c
include/mcc/dynamic_array.h) include/mcc/dynamic_array.h include/mcc/mcc.h)
...@@ -12,11 +12,6 @@ ...@@ -12,11 +12,6 @@
#include "mcc/printer.h" #include "mcc/printer.h"
#include "mcc/mcc_scope.h" #include "mcc/mcc_scope.h"
void print_usage(const char *prg) {
printf("usage: %s <FILE>\n\n", prg);
printf(" <FILE> Input filepath or - for stdin\n");
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char* symbol_scope = "program"; char* symbol_scope = "program";
FILE* out = stdout; FILE* out = stdout;
...@@ -114,8 +109,8 @@ int main(int argc, char *argv[]) { ...@@ -114,8 +109,8 @@ int main(int argc, char *argv[]) {
} }
mcc_symbol_table_print(symbol_table,out); mcc_symbol_table_print(symbol_table,out);
// mcc_symbol_table_delete_table(symbol_table); mcc_symbol_table_delete_table(symbol_table);
mcc_symbol_table_delete_error_collector(ec); mcc_symbol_table_delete_error_collector(ec);
mcc_ast_delete(prog); mcc_ast_delete(prog);
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include <getopt.h> #include <getopt.h>
#include <ctype.h> #include <ctype.h>
#include "mcc/dynamic_array.h"
#include "mcc/ast.h" #include "mcc/ast.h"
#include "mcc/parser.h" #include "mcc/parser.h"
#include "mcc/symbol_table.h" #include "mcc/symbol_table.h"
...@@ -14,7 +13,6 @@ ...@@ -14,7 +13,6 @@
#include "mcc/mcc_scope.h" #include "mcc/mcc_scope.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char* symbol_scope = "program"; char* symbol_scope = "program";
FILE* out = stdout; FILE* out = stdout;
...@@ -54,7 +52,6 @@ int main(int argc, char *argv[]) { ...@@ -54,7 +52,6 @@ int main(int argc, char *argv[]) {
} }
} }
// get input src // get input src
char *input = NULL; char *input = NULL;
if (optind < argc) { if (optind < argc) {
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <getopt.h>
#include <ctype.h>
#include "mcc/mcc.h"
#include "mcc/ast.h" #include "mcc/ast.h"
#include "mcc/parser.h" #include "mcc/parser.h"
#include "mcc/symbol_table.h"
#include "mcc/symbol_table_parse.h"
#include "mcc/symbol_table_print.h"
#include "mcc/printer.h"
#include "mcc/mcc_scope.h"
void print_usage(const char *prg) int main(int argc, char *argv[]) {
{ FILE* out = stdout;
printf("\n\nusage: %s [OPTIONS] file...s\n\n", prg);
printf("The mC compiler. It takes mC input files and produces an executable.\n\n");
printf("<FILE> Input filepath or - for stdin\n\n");
}
int main(int argc, char *argv[])
{
if (argc < 2) { if (argc < 2) {
print_usage(argv[0]); mcc_print_usage(argv[0]);
return EXIT_FAILURE;
}
int c;
static struct option options[] = {
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "q", no_argument, 0, 'q' },
{ "output", required_argument, 0, 'o' },
{ 0, 0, 0, 0 }
};
while ((c = getopt_long(argc, argv, "hfvq:o:", options, NULL)) != -1) {
switch(c) {
case 'h':
mcc_symbol_table_print_usage(argv[0]);
return EXIT_SUCCESS;
case 'v':
printf("%f\n", VERSION);
return EXIT_SUCCESS;
case 'q':
printf("Not implemented yet \n");
return EXIT_SUCCESS;
case 'o':
out = fopen(optarg, "rw");
break;
case '?':
if(isprint(optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort();
}
}
// get input src
char *input = NULL;
if (optind < argc) {
input = argv[optind];
} else {
fprintf(stderr, "%s: Missing input!\n", argv[0]);
mcc_symbol_table_print_usage(argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// determine input source
FILE *in; FILE *in;
if (strcmp("-", argv[1]) == 0) { if (strcmp("-", input) == 0) {
in = stdin; in = stdin;
} else { } else {
in = fopen(argv[1], "r"); in = fopen(input, "r");
if (!in) { if (!in) {
perror("fopen"); perror("fopen");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
struct mcc_ast_program *prog = NULL;
// parsing phase // parsing phase
struct mcc_ast_program *prog = NULL;
{ {
struct mcc_parser_result result = mcc_parse_file(in); struct mcc_parser_result result = mcc_parse_file(in);
fclose(in);
if (result.status != MCC_PARSER_STATUS_OK) { if (result.status != MCC_PARSER_STATUS_OK) {
printf("NOT OK"); // print error message
printf("%s", result.parser_error -> error_msg);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
prog = result.program; prog = result.program;
} }
// TODO: // create symbol table
// - run semantic checks struct mcc_symbol_table_error_collector *ec = mcc_symbol_table_new_error_collector();
{ struct mcc_symbol_table *symbol_table;
// check if scope should be limited
symbol_table = mcc_symbol_table_build(prog, ec);
if (symbol_table == NULL) {
mcc_symbol_table_print_error(ec, out);
return EXIT_FAILURE;
} }
mcc_symbol_table_delete_table(symbol_table);
mcc_symbol_table_delete_error_collector(ec);
// - create three-address code // - create three-address code
// - output assembly code // - output assembly code
...@@ -59,5 +113,7 @@ int main(int argc, char *argv[]) ...@@ -59,5 +113,7 @@ int main(int argc, char *argv[])
// cleanup // cleanup
mcc_ast_delete_program(prog); mcc_ast_delete_program(prog);
printf("Compiled successfully \n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
//
// Created by Clemens Paumgarten on 07.06.19.
//
#ifndef MCC_MCC_H
#define MCC_MCC_H
#define VERSION 0.1
#endif //MCC_MCC_H
#ifndef MCC_PRINTER_H #ifndef MCC_PRINTER_H
#define MCC_PRINTER_H #define MCC_PRINTER_H
#include <stdio.h> void mcc_print_usage(const char *prg);
void mcc_ast_to_dot_print_usage(const char *prg); void mcc_ast_to_dot_print_usage(const char *prg);
......
#include <stdio.h>
#include "mcc/printer.h" #include "mcc/printer.h"
void mcc_print_usage(const char *prg) {
printf("\n\nusage: %s [OPTIONS] file...\n\n", prg);
printf("The mC compiler. It takes mC input files and produces an executable.\n");
printf("Use '-' as input file to read from stdin\n\n");
printf("OPTIONS:\n");
printf(" -h, --help displays this help message\n");
printf(" -v, --help displays the version number\n");
printf(" -q, --help suppress error output (not implemented yet)\n");
printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n");
printf("Environment variables: \n");
printf("MCC_BACKEND override the back-end compiler (defaults to 'gcc' in PATH) (not implemented yet)\n");
}
void mcc_ast_to_dot_print_usage(const char *prg) void mcc_ast_to_dot_print_usage(const char *prg)
{ {
printf("\n\nusage: %s [OPTIONS] file...\n\n", prg); printf("\n\nusage: %s [OPTIONS] file...\n\n", prg);
...@@ -7,7 +21,7 @@ void mcc_ast_to_dot_print_usage(const char *prg) ...@@ -7,7 +21,7 @@ void mcc_ast_to_dot_print_usage(const char *prg)
"can be visualised using graphviz. Errors are reported on invalid inputs.\n\n"); "can be visualised using graphviz. Errors are reported on invalid inputs.\n\n");
printf("Use '-' as input file to read from stdin\n\n"); printf("Use '-' as input file to read from stdin\n\n");
printf("OPTIONS:\n"); printf("OPTIONS:\n");
printf(" -h, --help displays this help message:\n"); printf(" -h, --help displays this help message\n");
printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n"); printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n");
printf(" -f, --function <name> limit scope to the given function\n"); printf(" -f, --function <name> limit scope to the given function\n");
} }
...@@ -19,7 +33,7 @@ void mcc_symbol_table_print_usage(const char *prg) ...@@ -19,7 +33,7 @@ void mcc_symbol_table_print_usage(const char *prg)
"invalid inputs.\n\n"); "invalid inputs.\n\n");
printf("Use '-' as input file to read from stdin.\n\n"); printf("Use '-' as input file to read from stdin.\n\n");
printf("OPTIONS:\n"); printf("OPTIONS:\n");
printf(" -h, --help displays this help message:\n"); printf(" -h, --help displays this help message\n");
printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n"); printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n");
printf(" -f, --function <name> limit scope to the given function\n"); printf(" -f, --function <name> limit scope to the given function\n");
} }
...@@ -31,7 +45,7 @@ void mcc_symbol_table_type_trace_usage(const char *prg) ...@@ -31,7 +45,7 @@ void mcc_symbol_table_type_trace_usage(const char *prg)
"invalid inputs.\n\n"); "invalid inputs.\n\n");
printf("Use '-' as input file to read from stdin.\n\n"); printf("Use '-' as input file to read from stdin.\n\n");
printf("OPTIONS:\n"); printf("OPTIONS:\n");
printf(" -h, --help displays this help message:\n"); printf(" -h, --help displays this help message\n");
printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n"); printf(" -o, --output <file> write the output to <file> (defaults to stdout)\n");
printf(" -f, --function <name> limit scope to the given function\n"); printf(" -f, --function <name> limit scope to the given function\n");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment