wraith-lang/src/main.c

82 lines
2.4 KiB
C
Raw Normal View History

2021-04-28 23:01:48 +00:00
#include <stdlib.h>
#include "../lib/dropt/dropt.h"
#include "parser.h"
#include "codegen.h"
#include "identcheck.h"
2021-04-28 23:01:48 +00:00
int main(int argc, char *argv[])
{
dropt_bool showHelp = 0;
dropt_bool parseVerbose = 0;
uint32_t optimizationLevel = 0;
char *inputFilename;
int exitCode = EXIT_SUCCESS;
dropt_option options[] = {
{ 'h', "help", "Shows help.", NULL, dropt_handle_bool, &showHelp, dropt_attr_halt },
{ 'v', "parse-verbose", "Shows verbose parser output.", NULL, dropt_handle_bool, &parseVerbose },
{ 'O', "optimize", "Sets optimization level of the output IR. Must be a value between 0 and 3.", "number", dropt_handle_uint, &optimizationLevel },
{ 0 } /* Required sentinel value. */
};
dropt_context *droptContext = dropt_new_context(options);
if (droptContext == NULL)
{
exitCode = EXIT_FAILURE;
}
else if (argc == 0)
{
printf("Must supply an input file.");
exitCode = EXIT_FAILURE;
}
else
{
char** rest = dropt_parse(droptContext, -1, &argv[1]);
if (dropt_get_error(droptContext) != dropt_error_none)
{
fprintf(stderr, "wraith: %s\n", dropt_get_error_message(droptContext));
exitCode = EXIT_FAILURE;
}
else if (showHelp)
{
printf("Usage: wraith [options] [--] [input_file]\n\n"
"Options:\n");
dropt_print_help(stdout, droptContext, NULL);
}
else
{
/* TODO: free AST after compilation */
inputFilename = *rest;
if (inputFilename == NULL)
{
fprintf(stderr, "ERROR: Must provide an input file.\n");
exitCode = EXIT_FAILURE;
}
else
{
Node *rootNode;
if (Parse(inputFilename, &rootNode, parseVerbose) != 0)
{
fprintf(stderr, "Parser error.\n");
exitCode = EXIT_FAILURE;
}
else
{
{
IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0);
printf("\n");
PrintNode(rootNode, /*tabCount=*/0);
}
exitCode = Codegen(rootNode, optimizationLevel);
2021-04-28 23:01:48 +00:00
}
}
}
}
return exitCode;
}