diff --git a/Makefile b/Makefile index 57e91e5..bfbacfd 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,15 @@ $(BINARY): $(OBJ) Makefile tcc/libtcc1.a run: $(BINARY) ./$(BINARY) run example.lisp +run-debug: $(BINARY) + ./$(BINARY) run example.lisp -d yes + run-build: $(BINARY) - ./$(BINARY) build example.lisp example + ./$(BINARY) build example.lisp -o example + ./example + +run-build-debug: $(BINARY) + ./$(BINARY) build example.lisp -o example -d yes ./example tcc/libtcc1.a: diff --git a/README.md b/README.md index 85f15db..baa8695 100644 --- a/README.md +++ b/README.md @@ -15,5 +15,5 @@ to run the example run `make run`, or to compile another file run either ``` or ``` -./compiler build filename.lisp binaryname +./compiler build filename.lisp -o outputname ``` \ No newline at end of file diff --git a/example.lisp b/example.lisp index c1af896..d9c50b9 100755 --- a/example.lisp +++ b/example.lisp @@ -5,4 +5,3 @@ (printf "5 + (100 / 5) = %d\n" (add 5 (divide 100 5))) - diff --git a/include/args.h b/include/args.h new file mode 100644 index 0000000..ee18111 --- /dev/null +++ b/include/args.h @@ -0,0 +1,13 @@ +#ifndef __ARGS_H__ +#define __ARGS_H__ + +typedef struct Args { + const char* input; + const char* output; + int build; + int comp_debug; +} Args; + +Args parse_args(int argc, char** argv); + +#endif \ No newline at end of file diff --git a/include/binary.h b/include/binary.h index fca9211..3115cf7 100644 --- a/include/binary.h +++ b/include/binary.h @@ -1,4 +1,4 @@ -#include +#include #ifndef __BINARY_H__ #define __BINARY_H__ diff --git a/include/helpers.h b/include/helpers.h index 706246f..1c1073f 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -1,13 +1,6 @@ #ifndef __HELPERS_H__ #define __HELPERS_H__ -typedef struct Args { - const char* input; - const char* output; - int build; -} Args; - char* gen_ident(int indent); -Args parse_args(int argc, char* argv[]); #endif \ No newline at end of file diff --git a/include/slibs b/include/slibs index b0b09f6..9860064 160000 --- a/include/slibs +++ b/include/slibs @@ -1 +1 @@ -Subproject commit b0b09f6fd9efd5367dbac19629caf0d027e657e2 +Subproject commit 986006449d1586d9ae55e5696015aa67b0e33f4f diff --git a/src/args.c b/src/args.c new file mode 100644 index 0000000..f3dbaf8 --- /dev/null +++ b/src/args.c @@ -0,0 +1,48 @@ +#include +#include +#include + +char* arg_proceeding(int argc, char** argv, char* arg, char* default_return) { + for(int i = 0; i < argc - 1; i++) { + if(strcmp(argv[i], arg) == 0) { + return argv[i + 1]; + } + } + + return default_return; +} + +char* find_flagless_arg(int argc, char** argv, char* default_return) { + for(int i = 1; i < argc; i++) { + if(argv[i - 1][0] != '-' && argv[i][0] != '-') { + return argv[i]; + } + } + + return default_return; +} + +void skip_args(int* argc, char*** argv, int n) { + *argc -= n; + *argv += n; +} + +Args parse_args(int argc, char** argv) { + assert(argc > 1); + skip_args(&argc, &argv, 1); + Args args = { 0 }; + + if(argc == 1) { + args.input = argv[0]; + } else { + if(strcmp(argv[0], "build") == 0) { + args.build = 1; + args.output = arg_proceeding(argc, argv, "-o", "a.out"); + } + + args.input = find_flagless_arg(argc, argv, NULL); + args.comp_debug = arg_proceeding(argc, argv, "-d", NULL) != NULL; + } + + return args; +} \ No newline at end of file diff --git a/src/binary.c b/src/binary.c index 5969fc7..b2ebc77 100644 --- a/src/binary.c +++ b/src/binary.c @@ -18,7 +18,10 @@ int binary_produce(const char* code, Args args) { assert(tcc_compile_string(state, code) == 0); if(args.build) { - return tcc_output_file(state, args.output); + int ret = -1; + ret = tcc_output_file(state, args.output); + if(args.comp_debug) printf("Produced binary %s\n", args.output); + return ret; } else { return tcc_run(state, 0, NULL); } diff --git a/src/helpers.c b/src/helpers.c index 7fce2fe..1ffcae8 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -1,26 +1,10 @@ #include #include #include -#include char* gen_ident(int indent) { char* text = malloc(sizeof(char) * (indent + 1)); memset(text, ' ', indent); text[indent] = '\0'; return text; -} - -Args parse_args(int argc, char* argv[]) { - assert(argc > 2); - int build = 0; - if(strcmp(argv[1], "build") == 0) { - build = 1; - assert(argc > 3 && "Build needs an input and output filename."); - } else assert(strcmp(argv[1], "run") == 0 && "You must specify to either build or run."); - - return (Args){ - .input = argv[2], - .output = build ? argv[3] : NULL, - .build = build - }; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index d2efc98..14b61f5 100644 --- a/src/main.c +++ b/src/main.c @@ -5,28 +5,30 @@ #include #include #include -#include +#include int main(int argc, char* argv[]) { Args args = parse_args(argc, argv); - sl_string buffer = { 0 }; - sl_read_file(args.input, &buffer); + sl_string* input_str = sl_read_file(args.input); + if(!input_str) return -1; + char* input = sl_c_str(*input_str); - printf("Tokens:\n"); + int debug = args.comp_debug; + if(debug) printf("Tokens:\n"); TokenVec tokens = { 0 }; - tokenize(sl_c_str(buffer), &tokens); - tokens_print(tokens); - printf("\n"); + tokenize(input, &tokens); + if(debug) tokens_print(tokens); + if(debug) printf("\n"); - printf("AST:\n"); + if(debug) printf("AST:\n"); ASTNode* program = ast_parse(&tokens); - ast_print(program, 0); - printf("\n"); + if(debug) ast_print(program, 0); + if(debug) printf("\n"); - printf("Codegen:\n"); + if(debug) printf("Codegen:\n"); const char* code = codegen(program); - printf("%s\n\n", code); + if(debug) printf("%s\n\n", code); binary_produce(code, args);