From aeaf92127d1c090f9281616e49ad10dda414bd45 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sat, 5 Feb 2022 08:23:14 -0500 Subject: Add implementation of self-hosted compiler so far There's also a `run.sh2` script which does the following: - Compiles the C compiler `build/cupcc` - Compiles the self-hosted compiler `build/cup.out` (with `cupcc`) - Compiles the specified file on CLI with `build/cup.out` - Runs this exectuable and shows the output --- compiler/main.cup | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 compiler/main.cup (limited to 'compiler/main.cup') diff --git a/compiler/main.cup b/compiler/main.cup new file mode 100644 index 0000000..a0a3476 --- /dev/null +++ b/compiler/main.cup @@ -0,0 +1,34 @@ +import "std/file.cup" +import "compiler/lexer.cup" +import "compiler/parser.cup" +import "compiler/codegen.cup" + +fn main(argc: int, argv: char **): int { + if (argc != 2) + die("Usage: cupcc "); + + let input_file = fopen(argv[1], 'r'); + defer fclose(input_file); + + // using `fmap` here doesn't work on linux, for some reason. + let file_size = fsize(input_file); + let src: char* = malloc(file_size+1); + fread(input_file, src, file_size); + src[file_size] = '\0'; + + let lexer = lexer_new(argv[1], src, file_size); + let ast = parse_program(lexer); + + dump_ast(ast, 0); + + let out_file = fopen("build/host.nasm", 'w'); + defer fclose(out_file); + + generate_program(ast, out_file); + + puts("---------------------------\n"); + + puts("Total amount of memory used by malloc: "); + putu(__malloc_buf_pos); + putsln("\nDone."); +} \ No newline at end of file -- cgit v1.2.3