- Add comments and test for it - Add semicolon as required end of some expressions - Add new arguments parser - Add multi-file compilation - Add colors in output
164 lines
4.0 KiB
C
164 lines
4.0 KiB
C
#include <common/fzl_colors.h>
|
|
#include <compiler/fzl_debug.h>
|
|
#include <compiler/fzl_parser.h>
|
|
#include <compiler/fzl_reader.h>
|
|
#include <compiler/fzl_walker.h>
|
|
#include <compiler/fzl_writer.h>
|
|
#include <compiler/utils/fzl_linked.h>
|
|
#include <compiler/utils/fzl_options.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
fzl_options_t *opts = fzl_mkoptions(argc, argv);
|
|
if (!opts)
|
|
return -1;
|
|
else if (!opts->should_continue)
|
|
return 0;
|
|
|
|
if (opts->verbose)
|
|
printf("%s> analyze\n-> reading file...%s", FZL_CDEBUG, FZL_CRST);
|
|
|
|
fzl_linked_t *linked = fzl_mklinked();
|
|
if (!linked)
|
|
{
|
|
char *lasterr = fzl_linked_geterror();
|
|
if (lasterr[0])
|
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, lasterr);
|
|
return -1;
|
|
}
|
|
|
|
bool read_result =
|
|
fzl_linked_add_files(linked, opts->files, opts->files_count);
|
|
if (!read_result)
|
|
{
|
|
char *lasterr = fzl_linked_geterror();
|
|
if (lasterr[0])
|
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, lasterr);
|
|
return -1;
|
|
}
|
|
|
|
if (opts->verbose)
|
|
{
|
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
|
printf("%s--> Red %ld bytes%s\n", FZL_CDEBUG, linked->total_sz, FZL_CRST);
|
|
printf("%s-> tokenizing...%s", FZL_CDEBUG, FZL_CRST);
|
|
}
|
|
|
|
fzl_reader_t *rd = fzl_mkreader(linked);
|
|
int result = fzl_read(rd);
|
|
if (result != 0)
|
|
{
|
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_reader_geterror());
|
|
return -1;
|
|
}
|
|
|
|
if (opts->verbose)
|
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
|
|
|
fzl_reader_ll_t *entry = rd->first;
|
|
|
|
if (opts->verbose)
|
|
{
|
|
printf("%s--> structure: %s", FZL_CDEBUG, FZL_CRST);
|
|
while (entry)
|
|
{
|
|
if (entry->token)
|
|
{
|
|
if (entry->token->store_value)
|
|
printf("<TOK:%d:%s>", entry->token->type,
|
|
(char *)entry->token->value);
|
|
else
|
|
printf("<TOK:%d>", entry->token->type);
|
|
}
|
|
|
|
entry = entry->next;
|
|
}
|
|
|
|
printf("\n");
|
|
printf("%s-> parsing...%s", FZL_CDEBUG, FZL_CRST);
|
|
}
|
|
|
|
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
|
result = fzl_parse(parser);
|
|
if (result != 0)
|
|
{
|
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_parser_geterror());
|
|
return -1;
|
|
}
|
|
|
|
if (opts->verbose)
|
|
{
|
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
|
fzl_debug_print_ast(parser->root);
|
|
printf("%s> building%s\n", FZL_CDEBUG, FZL_CRST);
|
|
printf("%s-> walking AST...%s\n", FZL_CDEBUG, FZL_CRST);
|
|
}
|
|
|
|
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
|
result = fzl_walk(walker);
|
|
if (result != 0)
|
|
{
|
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_walker_geterror());
|
|
return -1;
|
|
}
|
|
|
|
if (opts->verbose)
|
|
{
|
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
|
|
|
printf("%s--> Constants table%s:\n", FZL_CDEBUG, FZL_CRST);
|
|
for (size_t i = 0; i < walker->consts->size; i++)
|
|
printf("---> %s\n", walker->consts->array[i].as.string.chars);
|
|
|
|
printf("%s--> Ops%s:\n", FZL_CDEBUG, FZL_CRST);
|
|
printf("---> ");
|
|
for (size_t i = 0; i < walker->op_count; i++)
|
|
{
|
|
if (i > 0 && i < walker->op_count)
|
|
printf("->");
|
|
printf("%lu", walker->ops[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
printf("%s-> preparing final write%s\n", FZL_CDEBUG, FZL_CRST);
|
|
}
|
|
|
|
char outpath[256];
|
|
if (opts->outname)
|
|
strcpy(outpath, opts->outname);
|
|
else
|
|
strcpy(outpath, "a.fzb");
|
|
|
|
if (opts->verbose)
|
|
{
|
|
printf("%s-> output file path is%s: %s\n", FZL_CDEBUG, FZL_CRST, outpath);
|
|
printf("%s-> writing file...%s", FZL_CDEBUG, FZL_CRST);
|
|
}
|
|
|
|
fzl_writer_t *writer = fzl_mkwriter(outpath);
|
|
result = fzl_write(writer, walker);
|
|
if (result != 0)
|
|
{
|
|
printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, fzl_writer_geterror());
|
|
return -1;
|
|
}
|
|
|
|
if (opts->verbose)
|
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
|
|
|
fzl_freeoptions(opts);
|
|
fzl_freelinked(linked);
|
|
fzl_freereader(rd);
|
|
fzl_freeast(parser->root);
|
|
fzl_freeparser(parser);
|
|
fzl_freewalker(walker);
|
|
fzl_freewriter(writer);
|
|
|
|
printf("%sCompiled successfully%s.\n", FZL_COK, FZL_CRST);
|
|
return 0;
|
|
}
|