forked from cosmonaut/wraith-lang
48 lines
821 B
C
48 lines
821 B
C
#include <stdio.h>
|
|
|
|
#include "ast.h"
|
|
#include "y.tab.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;
|
|
result = yyparse(fp, pRootNode);
|
|
fclose(fp);
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "File not found.\n");
|
|
return 3;
|
|
}
|
|
|
|
/* TODO: free stack */
|
|
/* TODO: free AST on error */
|
|
|
|
if (result == 0)
|
|
{
|
|
if (parseVerbose)
|
|
{
|
|
PrintNode(*pRootNode, 0);
|
|
}
|
|
}
|
|
else if (result == 1)
|
|
{
|
|
fprintf(stderr, "Syntax error.\n");
|
|
}
|
|
else if (result == 2)
|
|
{
|
|
fprintf(stderr, "Out of memory.\n");
|
|
}
|
|
|
|
return result;
|
|
}
|