aboutsummaryrefslogtreecommitdiff
path: root/src/as_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/as_main.c')
-rw-r--r--src/as_main.c36
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;
+}