This commit is contained in:
parent
f6a49831f1
commit
c83e9cbd5a
12 changed files with 786 additions and 471 deletions
|
@ -17,11 +17,20 @@
|
|||
-------- */
|
||||
|
||||
#include "main.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
|
||||
#ifndef __CODEGEN_H__
|
||||
#include "codegen.h"
|
||||
#endif
|
||||
|
||||
|
||||
/* Std Lib
|
||||
------- */
|
||||
|
||||
#include <yacc.h>
|
||||
|
||||
|
||||
/* Data
|
||||
---- */
|
||||
|
||||
|
@ -42,24 +51,6 @@
|
|||
---- */
|
||||
|
||||
|
||||
int openInputFile(char *_filename)
|
||||
{
|
||||
int ret=1;
|
||||
|
||||
if((yyin=fopen(_filename,"rt"))==NULL)
|
||||
{
|
||||
ret=0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int closeInputFile()
|
||||
{
|
||||
fclose(yyin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
Function:
|
||||
Purpose:
|
||||
|
@ -68,84 +59,133 @@ int closeInputFile()
|
|||
---------------------------------------------------------------------- */
|
||||
extern int main(int argc, char *argv[])
|
||||
{
|
||||
int ret=-1;
|
||||
int ret;
|
||||
|
||||
|
||||
// Process args
|
||||
if(argc!=3)
|
||||
{
|
||||
printf("Script compiler thingy\n");
|
||||
printf("Usage: yl infile outfile\n");
|
||||
printf("So there..\n\n");
|
||||
ret=-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
char *inputfile;
|
||||
char *outputfile;
|
||||
char *infile;
|
||||
char *outfile;
|
||||
|
||||
infile=argv[1];
|
||||
outfile=argv[2];
|
||||
|
||||
inputfile=argv[1];
|
||||
outputfile=argv[2];
|
||||
|
||||
// Parse file
|
||||
printf("Parsing file: %s\n",inputfile);
|
||||
if(!openInputFile(inputfile))
|
||||
if(parseFile(infile,s_baseTreeNode)==YYEXIT_SUCCESS&&s_baseTreeNode)
|
||||
{
|
||||
printf("Cannot open input file!\n");
|
||||
if(openOutputFile(outfile))
|
||||
{
|
||||
int byteCount;
|
||||
byteCount=s_baseTreeNode->generateCode(true);
|
||||
closeOutputFile();
|
||||
printf("Generated %d bytes of code\n",byteCount*2);
|
||||
ret=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret=-1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(yyparse()!=YYEXIT_SUCCESS)
|
||||
{
|
||||
s_errorCount++;
|
||||
}
|
||||
closeInputFile();
|
||||
printf("%d line(s) parsed, %d error(s) found\n",s_linesProcessed,s_errorCount);
|
||||
|
||||
// Generate code
|
||||
if(s_baseTreeNode&&!s_errorCount)
|
||||
{
|
||||
printf("Creating output file: %s\n",outputfile);
|
||||
if(!openOutputFile(outputfile))
|
||||
{
|
||||
printf("Couldn't open output file!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
int codeSize=0;
|
||||
|
||||
codeSize=genCode(s_baseTreeNode,true);
|
||||
closeOutputFile();
|
||||
printf("Generated %d instructions(s)\n",codeSize);
|
||||
ret=0; // Huzzah!
|
||||
}
|
||||
}
|
||||
ret=-1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
/*
|
||||
int n=1;
|
||||
|
||||
openOutputFile("test.dat");
|
||||
mylexer lexer;
|
||||
myparser parser;
|
||||
if(parser.yycreate(&lexer))
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
Function:
|
||||
Purpose:
|
||||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
int mylexer::openInputFile(char *_filename)
|
||||
{
|
||||
int ret=1;
|
||||
|
||||
printf("Opening %s..\n",_filename);
|
||||
if((m_fhInput=fopen(_filename,"rt"))==NULL)
|
||||
{
|
||||
if(lexer.yycreate(&parser))
|
||||
printf("FATAL: Couldn't open file for reading\n");
|
||||
ret=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_charCount=0;
|
||||
m_lineCount=0;
|
||||
m_currentCharOnLine=0;
|
||||
m_errorCount=0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
Function:
|
||||
Purpose:
|
||||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
int mylexer::closeInputFile()
|
||||
{
|
||||
printf("Processed %d char(s), %d line(s)\n",m_charCount,m_lineCount);
|
||||
if(m_errorCount)
|
||||
{
|
||||
printf("Found %d error(s) :(\n",m_errorCount);
|
||||
}
|
||||
fclose(m_fhInput);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
Function:
|
||||
Purpose:
|
||||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
int mylexer::yygetchar()
|
||||
{
|
||||
char c;
|
||||
int ret;
|
||||
|
||||
if(fread(&c,sizeof(c),1,m_fhInput)==1)
|
||||
{
|
||||
m_charCount++;
|
||||
m_currentCharOnLine++;
|
||||
if(c=='\n')
|
||||
{
|
||||
n=parser.yyparse();
|
||||
m_lineCount++;
|
||||
m_currentCharOnLine=0;
|
||||
}
|
||||
ret=c;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(ferror(m_fhInput))
|
||||
{
|
||||
printf("FATAL: Read error!\n");
|
||||
}
|
||||
ret=-1;
|
||||
}
|
||||
|
||||
if(s_baseTreeNode)
|
||||
{
|
||||
s_baseTreeNode->generateCode(true);
|
||||
// Force compilation to stop after finding errors ( hmm.. )
|
||||
if(m_errorCount)
|
||||
{
|
||||
printf("Stopping compilation!\n");
|
||||
ret=-1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
closeOutputFile();
|
||||
|
||||
return n;
|
||||
*/
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
|
@ -154,6 +194,10 @@ closeOutputFile();
|
|||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
void myparser::setCurrentLexer(mylexer *_lexer)
|
||||
{
|
||||
m_currentLexer=_lexer;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
|
@ -162,6 +206,10 @@ closeOutputFile();
|
|||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
void myparser::setBaseNode(class CTreeNode *_baseNode)
|
||||
{
|
||||
m_baseNode=_baseNode;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
|
@ -170,6 +218,12 @@ closeOutputFile();
|
|||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
void myparser::yyerror(const char *_text)
|
||||
{
|
||||
fprintf(yyparseerr,"ERROR: %s\n",_text);
|
||||
fprintf(yyparseerr," line %d char %d\n",m_currentLexer->getCurrentLine(),m_currentLexer->getCurrentCharOnLine());
|
||||
m_currentLexer->error();
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
|
@ -178,6 +232,19 @@ closeOutputFile();
|
|||
Params:
|
||||
Returns:
|
||||
---------------------------------------------------------------------- */
|
||||
int myparser::yyparse()
|
||||
{
|
||||
int ret=YYEXIT_FAILURE;
|
||||
|
||||
if(yysetup()==0&&
|
||||
yywork()==YYEXIT_SUCCESS&&
|
||||
m_currentLexer->getErrorCount()==0)
|
||||
{
|
||||
ret=YYEXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue