wraith-lang/src/parser.c

48 lines
821 B
C
Raw Normal View History

2021-04-28 23:01:48 +00:00
#include <stdio.h>
#include "y.tab.h"
#include "ast.h"
extern FILE *yyin;
extern int yydebug;
int Parse(char *inputFilename, Node **pRootNode, uint8_t parseVerbose)
{
int result;
yydebug = parseVerbose;
FILE *fp = fopen(inputFilename, "r");
if (fp != NULL)
{
yyin = fp;
2021-04-29 19:42:51 +00:00
result = yyparse(fp, pRootNode);
fclose(fp);
}
else
{
fprintf(stderr, "File not found.\n");
return 3;
}
2021-04-28 23:01:48 +00:00
/* TODO: free stack */
/* TODO: free AST on error */
if (result == 0)
{
if (parseVerbose)
{
PrintNode(*pRootNode, 0);
2021-04-28 23:01:48 +00:00
}
}
else if (result == 1)
{
fprintf(stderr, "Syntax error.\n");
}
else if (result == 2)
{
fprintf(stderr, "Out of memory.\n");
}
return result;
}