forked from cosmonaut/wraith-lang
120 lines
4.5 KiB
C
120 lines
4.5 KiB
C
#include <stdlib.h>
|
|
#include "../lib/dropt/dropt.h"
|
|
|
|
#include "parser.h"
|
|
#include "codegen.h"
|
|
#include "identcheck.h"
|
|
|
|
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
|
|
{
|
|
{ // This shit only works if you're using iftest.w
|
|
IdNode *idTree = MakeIdTree(rootNode, NULL);
|
|
PrintIdTree(idTree, /*tabCount=*/0);
|
|
|
|
printf("\nSeeking to variable 'myStructInt'\n");
|
|
IdNode *node = LookdownId(idTree, Variable, "myStructInt");
|
|
if (node == NULL) printf("Failed.\n");
|
|
|
|
printf("\n[attempting to look up MyStruct]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "MyStruct"));
|
|
|
|
printf("\n[attempting to look up MyFunc]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "MyFunc"));
|
|
|
|
printf("\n[attempting to look up myStructInt]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "myStructInt"));
|
|
|
|
printf("\n[attempting to look up Program]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "Program"));
|
|
|
|
printf("\n[attempting to look up Foo]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "Foo"));
|
|
|
|
printf("\n[attempting to look up Main]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "Main"));
|
|
|
|
printf("\n[attempting to look up myInt]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "myInt"));
|
|
|
|
printf("\n[attempting to look up signTag]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "signTag"));
|
|
|
|
printf("\n[attempting to look up myBool]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "myBool"));
|
|
|
|
printf("\n[attempting to look up lol]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "lol"));
|
|
|
|
printf("\n[attempting to look up someInt]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "someInt"));
|
|
|
|
printf("\n[attempting to look up Bar]: \n");
|
|
PrintIdNode(LookupId(node, /*prev=*/NULL, "Bar"));
|
|
}
|
|
exitCode = Codegen(rootNode, optimizationLevel);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return exitCode;
|
|
}
|