diff options
| author | Mistivia <i@mistivia.com> | 2025-03-26 19:10:25 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-03-26 19:10:25 +0800 |
| commit | 312716a295626f2b60b41777728c7f220fee843d (patch) | |
| tree | 5747882b5a662c29a734dc66486035f93fe42990 /src/as_main.c | |
| parent | b2be728e0cf6f024ecc524b28806012c53ca5206 (diff) | |
finish fvm-as
Diffstat (limited to 'src/as_main.c')
| -rw-r--r-- | src/as_main.c | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/src/as_main.c b/src/as_main.c index 6c91a5f..cd9251b 100644 --- a/src/as_main.c +++ b/src/as_main.c @@ -1,18 +1,42 @@ #include <stdio.h> #include "as_tokenizer.h" +#include "as_analyzer.h" +#include "as_parser.h" +#include "as_codegen.h" #include "utils.h" -int main(int argc, char** argv) { - if (argc != 2) { - fprintf(stderr, "usage: fvm-as <inputfile>\n"); - return 1; +result main_impl(int argc, char** argv) { + if (argc != 3) { + return err("usage: fvm-as <input_file> <output_file>"); } struct allocator * alct = new_allocator(); FILE *fp = fopen(argv[1], "r"); - struct token_stream * ts = new_token_stream(alct, fp); + token_stream * ts = new_token_stream(alct, fp); + prog* prog = unwrap(parse_prog(alct, ts)); + sym_table symtbl = analyze_prog(alct, prog); + bytearray* output = unwrap(codegen(alct, prog, symtbl)); + fclose(fp); + + fp = fopen(argv[2], "wb"); + if (fp == NULL) { + return err("open output file failed."); + } + int ret =fwrite(output->buf, 1, output->len, fp); + if (ret != output->len) { + return err("write output file failed."); + } + fclose(fp); delete_allocator(alct); - return 0; + return ok(NULL); } +int main(int argc, char** argv) { + result result = main_impl(argc, argv); + if (result.errmsg != NULL) { + fprintf(stderr, "%s\n", result.errmsg); + return -1; + } + return 0; +} |
