2021-04-28 23:01:48 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../lib/dropt/dropt.h"
|
|
|
|
|
|
|
|
#include "parser.h"
|
2021-04-28 23:10:17 +00:00
|
|
|
#include "codegen.h"
|
2021-05-07 02:53:28 +00:00
|
|
|
#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
|
|
|
|
{
|
2021-05-13 04:54:09 +00:00
|
|
|
{
|
2021-05-07 20:35:28 +00:00
|
|
|
IdNode *idTree = MakeIdTree(rootNode, NULL);
|
|
|
|
PrintIdTree(idTree, /*tabCount=*/0);
|
2021-05-13 04:54:09 +00:00
|
|
|
printf("\n");
|
2021-05-15 22:34:15 +00:00
|
|
|
PrintNode(rootNode, /*tabCount=*/0);
|
2021-05-07 20:22:51 +00:00
|
|
|
}
|
2021-04-28 23:10:17 +00:00
|
|
|
exitCode = Codegen(rootNode, optimizationLevel);
|
2021-04-28 23:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
}
|