feat: implement file writer

This commit is contained in:
samsonium
2026-10-10 03:56:00 +03:00
parent e979a22739
commit 96115b347e
4 changed files with 138 additions and 1 deletions
+40 -1
View File
@@ -2,6 +2,7 @@
#include <compiler/fzl_parser.h>
#include <compiler/fzl_reader.h>
#include <compiler/fzl_walker.h>
#include <compiler/fzl_writer.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -11,6 +12,7 @@
typedef struct
{
char *filepath;
char *outpath;
bool verbose;
bool help;
} build_args_t;
@@ -32,6 +34,7 @@ build_args_t *getargs(int argc, char **argv)
return NULL;
args->filepath = NULL;
args->outpath = NULL;
args->verbose = false;
args->help = false;
@@ -53,6 +56,15 @@ build_args_t *getargs(int argc, char **argv)
{
args->verbose = true;
}
else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--out") == 0)
{
if (!argv[i + 1] || argv[i + 1][0] == '-')
{
printf("Error: file path is not specified.\n");
return NULL;
}
args->outpath = argv[++i];
}
else
{
printf("Error: unknown argument '%s'\n", argv[i]);
@@ -156,7 +168,8 @@ int main(int argc, char **argv)
{
printf("ok!\n");
fzl_debug_print_ast(parser->root);
printf("-> walking AST...");
printf("> building\n");
printf("-> walking AST...\n");
}
fzl_walker_t *walker = fzl_mkwalker(parser->root);
@@ -178,13 +191,39 @@ int main(int argc, char **argv)
printf("--> Ops:\n");
for (size_t i = 0; i < walker->op_count; i++)
printf("---> %lu\n", walker->ops[i]);
printf("-> preparing final write\n");
}
char outpath[256];
if (args->outpath)
strcpy(outpath, args->outpath);
else
strcpy(outpath, "a.fzlc");
if (args->verbose)
{
printf("-> output file path is: %s", outpath);
printf("-> writing file...");
}
fzl_writer_t *writer = fzl_mkwriter(outpath);
result = fzl_write(writer, walker);
if (result != 0)
{
printf("Error: %s\n", fzl_writer_geterror());
return -1;
}
if (args->verbose)
printf("ok!\n");
free(args);
fzl_freereader(rd);
fzl_freeparser(parser);
fzl_freeast(parser->root);
fzl_freewalker(walker);
fzl_freewriter(writer);
printf("done.\n");
return 0;