This commit is contained in:
parent
2f68dceedd
commit
03ac6cafba
8 changed files with 219 additions and 32 deletions
|
@ -50,6 +50,9 @@ static FILE *s_fhOutput=NULL;
|
||||||
|
|
||||||
CTreeNode *s_baseTreeNode=NULL;
|
CTreeNode *s_baseTreeNode=NULL;
|
||||||
|
|
||||||
|
static mylexer s_lexer;
|
||||||
|
static myparser s_parser;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------------------
|
/*----------------------------------------------------------------------
|
||||||
|
@ -61,20 +64,18 @@ CTreeNode *s_baseTreeNode=NULL;
|
||||||
extern int parseFile(char *_filename,CTreeNode *_baseNode)
|
extern int parseFile(char *_filename,CTreeNode *_baseNode)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
mylexer lexer;
|
|
||||||
myparser parser;
|
|
||||||
|
|
||||||
ret=YYEXIT_FAILURE;
|
ret=YYEXIT_FAILURE;
|
||||||
if(parser.yycreate(&lexer))
|
if(s_parser.yycreate(&s_lexer))
|
||||||
{
|
{
|
||||||
if(lexer.yycreate(&parser))
|
if(s_lexer.yycreate(&s_parser))
|
||||||
{
|
{
|
||||||
if(lexer.openInputFile(_filename))
|
if(s_lexer.openInputFile(_filename))
|
||||||
{
|
{
|
||||||
parser.setCurrentLexer(&lexer);
|
s_parser.setCurrentLexer(&s_lexer);
|
||||||
parser.setBaseNode(_baseNode);
|
s_parser.setBaseNode(_baseNode);
|
||||||
ret=parser.yyparse();
|
ret=s_parser.yyparse();
|
||||||
lexer.closeInputFile();
|
// s_lexer.closeInputFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ public:
|
||||||
int yygetchar();
|
int yygetchar();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FILE *m_fhInput;
|
// FILE *m_fhInput;
|
||||||
int m_charCount;
|
int m_charCount;
|
||||||
int m_lineCount;
|
int m_lineCount;
|
||||||
int m_currentCharOnLine;
|
int m_currentCharOnLine;
|
||||||
|
|
|
@ -39,7 +39,7 @@ Date: 07 December 2000
|
||||||
int yygetchar();
|
int yygetchar();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FILE *m_fhInput;
|
// FILE *m_fhInput;
|
||||||
int m_charCount;
|
int m_charCount;
|
||||||
int m_lineCount;
|
int m_lineCount;
|
||||||
int m_currentCharOnLine;
|
int m_currentCharOnLine;
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "pfile.h"
|
||||||
|
|
||||||
#ifndef __CODEGEN_H__
|
#ifndef __CODEGEN_H__
|
||||||
#include "codegen.h"
|
#include "codegen.h"
|
||||||
|
@ -109,6 +110,7 @@ extern int main(int argc, char *argv[])
|
||||||
---------------------------------------------------------------------- */
|
---------------------------------------------------------------------- */
|
||||||
int mylexer::openInputFile(char *_filename)
|
int mylexer::openInputFile(char *_filename)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
int ret=1;
|
int ret=1;
|
||||||
|
|
||||||
printf("Opening %s..\n",_filename);
|
printf("Opening %s..\n",_filename);
|
||||||
|
@ -125,6 +127,9 @@ int mylexer::openInputFile(char *_filename)
|
||||||
m_errorCount=0;
|
m_errorCount=0;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
*/
|
||||||
|
|
||||||
|
return openPFile(_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -136,6 +141,7 @@ int mylexer::openInputFile(char *_filename)
|
||||||
---------------------------------------------------------------------- */
|
---------------------------------------------------------------------- */
|
||||||
int mylexer::closeInputFile()
|
int mylexer::closeInputFile()
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
printf("Processed %d char(s), %d line(s)\n",m_charCount,m_lineCount);
|
printf("Processed %d char(s), %d line(s)\n",m_charCount,m_lineCount);
|
||||||
if(m_errorCount)
|
if(m_errorCount)
|
||||||
{
|
{
|
||||||
|
@ -143,6 +149,8 @@ int mylexer::closeInputFile()
|
||||||
}
|
}
|
||||||
fclose(m_fhInput);
|
fclose(m_fhInput);
|
||||||
return 1;
|
return 1;
|
||||||
|
*/
|
||||||
|
return closePFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -156,31 +164,46 @@ int mylexer::yygetchar()
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
int ret;
|
int ret;
|
||||||
|
FILE *fh;
|
||||||
|
|
||||||
if(fread(&c,sizeof(c),1,m_fhInput)==1)
|
|
||||||
|
fh=getPFileFh();
|
||||||
|
if(fh)
|
||||||
{
|
{
|
||||||
m_charCount++;
|
if(fread(&c,sizeof(c),1,fh)==1)
|
||||||
m_currentCharOnLine++;
|
|
||||||
if(c=='\n')
|
|
||||||
{
|
{
|
||||||
m_lineCount++;
|
m_charCount++;
|
||||||
m_currentCharOnLine=0;
|
m_currentCharOnLine++;
|
||||||
|
if(c=='\n')
|
||||||
|
{
|
||||||
|
m_lineCount++;
|
||||||
|
m_currentCharOnLine=0;
|
||||||
|
}
|
||||||
|
ret=c;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret=-1;
|
||||||
|
if(ferror(fh))
|
||||||
|
{
|
||||||
|
printf("FATAL: Read error!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
closePFile();
|
||||||
|
return yygetchar();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force compilation to stop after finding errors ( hmm.. )
|
||||||
|
if(m_errorCount)
|
||||||
|
{
|
||||||
|
printf("Stopping compilation!\n");
|
||||||
|
ret=-1;
|
||||||
}
|
}
|
||||||
ret=c;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(ferror(m_fhInput))
|
|
||||||
{
|
|
||||||
printf("FATAL: Read error!\n");
|
|
||||||
}
|
|
||||||
ret=-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Force compilation to stop after finding errors ( hmm.. )
|
|
||||||
if(m_errorCount)
|
|
||||||
{
|
|
||||||
printf("Stopping compilation!\n");
|
|
||||||
ret=-1;
|
ret=-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,10 @@
|
||||||
/* Std Lib
|
/* Std Lib
|
||||||
------- */
|
------- */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
/* Data
|
/* Data
|
||||||
---- */
|
---- */
|
||||||
|
|
||||||
|
@ -33,6 +37,32 @@
|
||||||
Structure defintions
|
Structure defintions
|
||||||
-------------------- */
|
-------------------- */
|
||||||
|
|
||||||
|
class CPFile
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CPFile();
|
||||||
|
|
||||||
|
int open(char *_filename);
|
||||||
|
int close();
|
||||||
|
|
||||||
|
FILE *getFh() {return m_fh;}
|
||||||
|
|
||||||
|
static class CPFile *getCurrentFile() {return s_stack;}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
FILE *m_fh;
|
||||||
|
char *m_filename;
|
||||||
|
int m_charCount;
|
||||||
|
int m_lineCount;
|
||||||
|
int m_currentCharOnLine;
|
||||||
|
int m_errorCount;
|
||||||
|
|
||||||
|
class CPFile *m_next;
|
||||||
|
static class CPFile *s_stack;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------------------
|
/*----------------------------------------------------------------------
|
||||||
Function Prototypes
|
Function Prototypes
|
||||||
------------------- */
|
------------------- */
|
||||||
|
@ -41,12 +71,130 @@
|
||||||
Vars
|
Vars
|
||||||
---- */
|
---- */
|
||||||
|
|
||||||
|
CPFile *CPFile::s_stack=NULL;
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------------------
|
/*----------------------------------------------------------------------
|
||||||
Function:
|
Function:
|
||||||
Purpose:
|
Purpose:
|
||||||
Params:
|
Params:
|
||||||
Returns:
|
Returns:
|
||||||
---------------------------------------------------------------------- */
|
---------------------------------------------------------------------- */
|
||||||
|
extern int openPFile(char *_filename)
|
||||||
|
{
|
||||||
|
CPFile *pf;
|
||||||
|
pf=new CPFile;
|
||||||
|
return pf->open(_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------
|
||||||
|
Function:
|
||||||
|
Purpose:
|
||||||
|
Params:
|
||||||
|
Returns:
|
||||||
|
---------------------------------------------------------------------- */
|
||||||
|
extern int closePFile()
|
||||||
|
{
|
||||||
|
CPFile *pf;
|
||||||
|
int ret;
|
||||||
|
pf=CPFile::getCurrentFile();
|
||||||
|
ret=pf->close();
|
||||||
|
delete pf;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------
|
||||||
|
Function:
|
||||||
|
Purpose:
|
||||||
|
Params:
|
||||||
|
Returns:
|
||||||
|
---------------------------------------------------------------------- */
|
||||||
|
extern FILE *getPFileFh()
|
||||||
|
{
|
||||||
|
CPFile*pf;
|
||||||
|
|
||||||
|
pf=CPFile::getCurrentFile();
|
||||||
|
if(pf)
|
||||||
|
return pf->getFh();
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------
|
||||||
|
Function:
|
||||||
|
Purpose:
|
||||||
|
Params:
|
||||||
|
Returns:
|
||||||
|
---------------------------------------------------------------------- */
|
||||||
|
CPFile::CPFile()
|
||||||
|
{
|
||||||
|
m_fh=NULL;
|
||||||
|
m_filename=NULL;
|
||||||
|
m_charCount=0;
|
||||||
|
m_lineCount=0;
|
||||||
|
m_currentCharOnLine=0;
|
||||||
|
m_errorCount=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------
|
||||||
|
Function:
|
||||||
|
Purpose:
|
||||||
|
Params:
|
||||||
|
Returns:
|
||||||
|
---------------------------------------------------------------------- */
|
||||||
|
int CPFile::open(char *_filename)
|
||||||
|
{
|
||||||
|
int ret=1;
|
||||||
|
|
||||||
|
printf("Opening %s..\n",_filename);
|
||||||
|
if((m_fh=fopen(_filename,"rt"))==NULL)
|
||||||
|
{
|
||||||
|
printf("FATAL: Couldn't open file for reading\n");
|
||||||
|
ret=0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_filename=(char*)malloc(strlen(_filename)+1);
|
||||||
|
strcpy(m_filename,_filename);
|
||||||
|
m_next=s_stack;
|
||||||
|
s_stack=this;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------
|
||||||
|
Function:
|
||||||
|
Purpose:
|
||||||
|
Params:
|
||||||
|
Returns:
|
||||||
|
---------------------------------------------------------------------- */
|
||||||
|
int CPFile::close()
|
||||||
|
{
|
||||||
|
printf("Processed %d char(s), %d line(s) in file %s\n",m_charCount,m_lineCount,m_filename);
|
||||||
|
if(m_errorCount)
|
||||||
|
{
|
||||||
|
printf("Found %d error(s) :(\n",m_errorCount);
|
||||||
|
}
|
||||||
|
fclose(m_fh);
|
||||||
|
if(m_next)
|
||||||
|
{
|
||||||
|
s_stack=m_next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s_stack=NULL;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*===========================================================================
|
/*===========================================================================
|
||||||
|
|
|
@ -21,6 +21,9 @@
|
||||||
/* Std Lib
|
/* Std Lib
|
||||||
------- */
|
------- */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------------------
|
/*----------------------------------------------------------------------
|
||||||
Tyepdefs && Defines
|
Tyepdefs && Defines
|
||||||
------------------- */
|
------------------- */
|
||||||
|
@ -37,6 +40,11 @@
|
||||||
Functions
|
Functions
|
||||||
--------- */
|
--------- */
|
||||||
|
|
||||||
|
extern int openPFile(char *_filename);
|
||||||
|
extern int closePFile();
|
||||||
|
extern FILE *getPFileFh();
|
||||||
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------- */
|
/*---------------------------------------------------------------------- */
|
||||||
|
|
||||||
#endif /* __PFILE_H__ */
|
#endif /* __PFILE_H__ */
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
-------- */
|
-------- */
|
||||||
|
|
||||||
#include "prepro.h"
|
#include "prepro.h"
|
||||||
|
#include "pfile.h"
|
||||||
|
|
||||||
|
|
||||||
/* Std Lib
|
/* Std Lib
|
||||||
|
@ -139,9 +140,7 @@ static int ppf_include(char *_cmd)
|
||||||
includeFile=strtok(_cmd,s_seps);
|
includeFile=strtok(_cmd,s_seps);
|
||||||
includeFile=strtok(NULL,s_seps);
|
includeFile=strtok(NULL,s_seps);
|
||||||
|
|
||||||
// printf("include: '%s'\n",includeFile);
|
return openPFile(includeFile)==1?0:1;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -140,6 +140,14 @@ SOURCE=.\main.h
|
||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=.\pfile.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=.\pfile.h
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
SOURCE=.\prepro.cpp
|
SOURCE=.\prepro.cpp
|
||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
|
|
Loading…
Add table
Reference in a new issue