aboutsummaryrefslogtreecommitdiff
path: root/compiler/main.cup
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-05 08:23:14 -0500
committerMustafa Quraish <[email protected]>2022-02-05 08:56:15 -0500
commitaeaf92127d1c090f9281616e49ad10dda414bd45 (patch)
treef85127c08b0caa13b95b3fb80e2996d3b5186434 /compiler/main.cup
parentRemove old test which disallowed initializing globals (diff)
downloadcup-aeaf92127d1c090f9281616e49ad10dda414bd45.tar.xz
cup-aeaf92127d1c090f9281616e49ad10dda414bd45.zip
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
Diffstat (limited to 'compiler/main.cup')
-rw-r--r--compiler/main.cup34
1 files changed, 34 insertions, 0 deletions
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 <input_file>");
+
+ 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