diff --git a/Utils/Scripter/codegen.cpp b/Utils/Scripter/codegen.cpp index d8fcc1631..4afd3f5ac 100644 --- a/Utils/Scripter/codegen.cpp +++ b/Utils/Scripter/codegen.cpp @@ -17,8 +17,14 @@ -------- */ #include "codegen.h" + +#ifndef _LEXER_H #include "lexer.h" +#endif + +#ifndef _PARSER_H #include "parser.h" +#endif /* Std Lib @@ -70,7 +76,7 @@ extern int parseFile(char *_filename,CTreeNode *_baseNode) { if(s_lexer.yycreate(&s_parser)) { - if(s_lexer.openInputFile(_filename)) + if(s_lexer.openInputFile(_filename)==true) { s_parser.setCurrentLexer(&s_lexer); s_parser.setBaseNode(_baseNode); diff --git a/Utils/Scripter/lexer.cpp b/Utils/Scripter/lexer.cpp index 55ea252ab..082f4b5d1 100644 --- a/Utils/Scripter/lexer.cpp +++ b/Utils/Scripter/lexer.cpp @@ -28,7 +28,7 @@ Date: 07 December 2000 YYLEXNAME::YYLEXNAME() { yytables(); -#line 52 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 51 "C:\\spongebob\\Utils\\scripter\\lexer.l" // place any extra initialisation code here @@ -78,7 +78,7 @@ void YYLEXNAME::yyunput(int ch) int YYLEXNAME::yyaction(int action) { -#line 64 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 63 "C:\\spongebob\\Utils\\scripter\\lexer.l" // extract yylval for use later on in actions YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr; @@ -88,154 +88,154 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr; switch (action) { case 1: { -#line 71 "C:\\spongebob\\Utils\\scripter\\lexer.l" -if(preprocessorCmd(yytext+1)!=0)error(); +#line 70 "C:\\spongebob\\Utils\\scripter\\lexer.l" +if(preprocessorCmd(yytext+1)!=true)error(); #line 94 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 2: { -#line 72 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 71 "C:\\spongebob\\Utils\\scripter\\lexer.l" printf("# commands must be at start of line! ( line %d )\n",getCurrentLine());error(); #line 101 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 3: { -#line 74 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 73 "C:\\spongebob\\Utils\\scripter\\lexer.l" return STOP; #line 108 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 4: { -#line 75 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 74 "C:\\spongebob\\Utils\\scripter\\lexer.l" return IF; #line 115 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 5: { -#line 76 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 75 "C:\\spongebob\\Utils\\scripter\\lexer.l" return ELSE; #line 122 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 6: { -#line 77 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 76 "C:\\spongebob\\Utils\\scripter\\lexer.l" return PAUSE; #line 129 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 7: { -#line 78 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 77 "C:\\spongebob\\Utils\\scripter\\lexer.l" return PRINT; #line 136 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 8: { -#line 79 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 78 "C:\\spongebob\\Utils\\scripter\\lexer.l" return ASSIGN; #line 143 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 9: { -#line 80 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 79 "C:\\spongebob\\Utils\\scripter\\lexer.l" return EQUAL; #line 150 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 10: { -#line 81 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 80 "C:\\spongebob\\Utils\\scripter\\lexer.l" return NOTEQUAL; #line 157 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 11: { -#line 82 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 81 "C:\\spongebob\\Utils\\scripter\\lexer.l" return PLUS; #line 164 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 12: { -#line 83 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 82 "C:\\spongebob\\Utils\\scripter\\lexer.l" return END_STMT; #line 171 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 13: { -#line 84 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 83 "C:\\spongebob\\Utils\\scripter\\lexer.l" return OPEN_PAR; #line 178 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 14: { -#line 85 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 84 "C:\\spongebob\\Utils\\scripter\\lexer.l" return CLOSE_PAR; #line 185 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 15: { -#line 86 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 85 "C:\\spongebob\\Utils\\scripter\\lexer.l" return BEGIN_CS; #line 192 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 16: { -#line 87 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 86 "C:\\spongebob\\Utils\\scripter\\lexer.l" return END_CS; #line 199 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 17: { -#line 89 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 88 "C:\\spongebob\\Utils\\scripter\\lexer.l" yylval.variableIdx=lookupVarName(yytext+1);return VARIABLE; #line 206 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 18: { -#line 90 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 89 "C:\\spongebob\\Utils\\scripter\\lexer.l" yylval.value=atoi(yytext);return VALUE; #line 213 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 19: { -#line 93 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 92 "C:\\spongebob\\Utils\\scripter\\lexer.l" #line 220 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 20: { -#line 94 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 93 "C:\\spongebob\\Utils\\scripter\\lexer.l" #line 227 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 21: { -#line 95 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 94 "C:\\spongebob\\Utils\\scripter\\lexer.l" #line 234 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } break; case 22: { -#line 97 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 96 "C:\\spongebob\\Utils\\scripter\\lexer.l" printf("UNEXPECTED CHAR: '%s' in line %d ( char %d )\n",yytext,getCurrentLine(),getCurrentCharOnLine());error(); #line 241 "C:\\spongebob\\Utils\\scripter\\lexer.cpp" } @@ -253,7 +253,7 @@ printf("UNEXPECTED CHAR: '%s' in line %d ( char %d )\n",yytext,getCurrentLine(), #pragma warn .rch // to the old state #endif #endif -#line 99 "C:\\spongebob\\Utils\\scripter\\lexer.l" +#line 98 "C:\\spongebob\\Utils\\scripter\\lexer.l" diff --git a/Utils/Scripter/lexer.h b/Utils/Scripter/lexer.h index 18255df59..b7bb9697c 100644 --- a/Utils/Scripter/lexer.h +++ b/Utils/Scripter/lexer.h @@ -27,8 +27,7 @@ public: int getCurrentCharOnLine() {return m_currentCharOnLine;} int getErrorCount() {return m_errorCount;} - void error() {m_errorCount++;} - + void error(); // Overridden lexer functions int yygetchar(); @@ -42,7 +41,7 @@ private: class myparser *m_currentParser; -#line 46 "C:\\spongebob\\Utils\\scripter\\lexer.h" +#line 45 "C:\\spongebob\\Utils\\scripter\\lexer.h" }; #ifndef YYLEXNAME diff --git a/Utils/Scripter/lexer.l b/Utils/Scripter/lexer.l index 45656510a..9974bf263 100644 --- a/Utils/Scripter/lexer.l +++ b/Utils/Scripter/lexer.l @@ -32,8 +32,7 @@ Date: 07 December 2000 int getCurrentCharOnLine() {return m_currentCharOnLine;} int getErrorCount() {return m_errorCount;} - void error() {m_errorCount++;} - + void error(); // Overridden lexer functions int yygetchar(); @@ -68,7 +67,7 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr; // place your Lex rules here -^#.* {if(preprocessorCmd(yytext+1)!=0)error();} +^#.* {if(preprocessorCmd(yytext+1)!=true)error();} # {printf("# commands must be at start of line! ( line %d )\n",getCurrentLine());error();} stop {return STOP;} diff --git a/Utils/Scripter/main.cpp b/Utils/Scripter/main.cpp index 026a25e50..5ab7db5df 100644 --- a/Utils/Scripter/main.cpp +++ b/Utils/Scripter/main.cpp @@ -17,9 +17,10 @@ -------- */ #include "main.h" + +#ifndef _LEXER_H #include "lexer.h" -#include "parser.h" -#include "pfile.h" +#endif #ifndef __CODEGEN_H__ #include "codegen.h" @@ -51,7 +52,6 @@ Vars ---- */ - /*---------------------------------------------------------------------- Function: Purpose: @@ -102,173 +102,5 @@ extern int main(int argc, char *argv[]) } -/*---------------------------------------------------------------------- - Function: - Purpose: - Params: - Returns: - ---------------------------------------------------------------------- */ -int mylexer::openInputFile(char *_filename) -{ - /* - int ret=1; - - printf("Opening %s..\n",_filename); - if((m_fhInput=fopen(_filename,"rt"))==NULL) - { - 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; - */ - - return openPFile(_filename); -} - - -/*---------------------------------------------------------------------- - 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; - */ - return closePFile(); -} - - -/*---------------------------------------------------------------------- - Function: - Purpose: - Params: - Returns: - ---------------------------------------------------------------------- */ -int mylexer::yygetchar() -{ - char c; - int ret; - FILE *fh; - - - fh=getPFileFh(); - if(fh) - { - if(fread(&c,sizeof(c),1,fh)==1) - { - m_charCount++; - 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; - } - } - else - { - ret=-1; - } - - return ret; -} - - -/*---------------------------------------------------------------------- - Function: - Purpose: - Params: - Returns: - ---------------------------------------------------------------------- */ -void myparser::setCurrentLexer(mylexer *_lexer) -{ - m_currentLexer=_lexer; -} - - -/*---------------------------------------------------------------------- - Function: - Purpose: - Params: - Returns: - ---------------------------------------------------------------------- */ -void myparser::setBaseNode(class CTreeNode *_baseNode) -{ - m_baseNode=_baseNode; -} - - -/*---------------------------------------------------------------------- - Function: - Purpose: - 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(); -} - - -/*---------------------------------------------------------------------- - Function: - Purpose: - Params: - Returns: - ---------------------------------------------------------------------- */ -int myparser::yyparse() -{ - int ret=YYEXIT_FAILURE; - - if(yysetup()==0&& - yywork()==YYEXIT_SUCCESS&& - m_currentLexer->getErrorCount()==0) - { - ret=YYEXIT_SUCCESS; - } - - return ret; -} - - /*=========================================================================== end */ diff --git a/Utils/Scripter/mylexer.cpp b/Utils/Scripter/mylexer.cpp index 5fbd39195..c95ef3b8c 100644 --- a/Utils/Scripter/mylexer.cpp +++ b/Utils/Scripter/mylexer.cpp @@ -16,8 +16,17 @@ Includes -------- */ +#ifndef _LEXER_H #include "lexer.h" +#endif + +#ifndef _PARSER_H #include "parser.h" +#endif + +#ifndef __PFILE_H__ +#include "pfile.h" +#endif /* Std Lib @@ -69,6 +78,19 @@ int mylexer::closeInputFile() } +/*---------------------------------------------------------------------- + Function: + Purpose: + Params: + Returns: + ---------------------------------------------------------------------- */ +void mylexer::error() +{ + fprintf(yyerr,"ERROR AT LINE %d, COLUMN %d\n",getCurrentLine(),getCurrentCharOnLine()); + m_errorCount++; +} + + /*---------------------------------------------------------------------- Function: Purpose: @@ -113,7 +135,7 @@ int mylexer::yygetchar() // Force compilation to stop after finding errors ( hmm.. ) if(m_errorCount) { - printf("Stopping compilation!\n"); + printf("Stopping compilation due to errors!\n"); ret=-1; } } diff --git a/Utils/Scripter/myparser.cpp b/Utils/Scripter/myparser.cpp index a2d755da7..b052e5495 100644 --- a/Utils/Scripter/myparser.cpp +++ b/Utils/Scripter/myparser.cpp @@ -16,12 +16,21 @@ Includes -------- */ -#include "myparser.h" +#ifndef _PARSER_H +#include "parser.h" +#endif + +#ifndef _LEXER_H +#include "lexer.h" +#endif /* Std Lib ------- */ +#include + + /* Data ---- */ @@ -47,6 +56,57 @@ Params: Returns: ---------------------------------------------------------------------- */ +void myparser::setCurrentLexer(mylexer *_lexer) +{ + m_currentLexer=_lexer; +} + + +/*---------------------------------------------------------------------- + Function: + Purpose: + Params: + Returns: + ---------------------------------------------------------------------- */ +void myparser::setBaseNode(class CTreeNode *_baseNode) +{ + m_baseNode=_baseNode; +} + + +/*---------------------------------------------------------------------- + Function: + Purpose: + 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(); +} + + +/*---------------------------------------------------------------------- + Function: + Purpose: + Params: + Returns: + ---------------------------------------------------------------------- */ +int myparser::yyparse() +{ + int ret=YYEXIT_FAILURE; + + if(yysetup()==0&& + yywork()==YYEXIT_SUCCESS&& + m_currentLexer->getErrorCount()==0) + { + ret=YYEXIT_SUCCESS; + } + + return ret; +} /*=========================================================================== diff --git a/Utils/Scripter/pfile.cpp b/Utils/Scripter/pfile.cpp index 5e40a5dcd..441c0ebce 100644 --- a/Utils/Scripter/pfile.cpp +++ b/Utils/Scripter/pfile.cpp @@ -152,13 +152,13 @@ CPFile::CPFile() ---------------------------------------------------------------------- */ int CPFile::open(char *_filename) { - int ret=1; + int ret; printf("Opening %s..\n",_filename); if((m_fh=fopen(_filename,"rt"))==NULL) { printf("FATAL: Couldn't open file for reading\n"); - ret=0; + ret=false; } else { @@ -166,7 +166,9 @@ int CPFile::open(char *_filename) strcpy(m_filename,_filename); m_next=s_stack; s_stack=this; + ret=true; } + return ret; } @@ -193,7 +195,8 @@ int CPFile::close() { s_stack=NULL; } - return 1; + + return true; } diff --git a/Utils/Scripter/prepro.cpp b/Utils/Scripter/prepro.cpp index 6a9948704..44272047c 100644 --- a/Utils/Scripter/prepro.cpp +++ b/Utils/Scripter/prepro.cpp @@ -17,7 +17,10 @@ -------- */ #include "prepro.h" + +#ifndef __PFILE_H__ #include "pfile.h" +#endif /* Std Lib @@ -57,11 +60,13 @@ typedef struct _macro Function Prototypes ------------------- */ -static int ppf_define(char *_cmd); -static int ppf_include(char *_cmd); -static int ppf_print(char *_cmd); +static int ppf_define(char *_cmd); +static int ppf_include(char *_cmd); +static int ppf_print(char *_cmd); +static int ppf_undefine(char *_cmd); static int addMacro(char *_name,char *_replacement); +static int removeMacro(char *_name); /*---------------------------------------------------------------------- @@ -69,9 +74,10 @@ static int addMacro(char *_name,char *_replacement); ---- */ static PreproCmd s_preproCmds[]= { - { "define", ppf_define }, - { "include", ppf_include }, - { "print", ppf_print }, + { "define", ppf_define }, + { "include", ppf_include }, + { "print", ppf_print }, + { "undef", ppf_undefine }, }; static int s_numPreproCmds=sizeof(s_preproCmds)/sizeof(PreproCmd); @@ -86,7 +92,7 @@ static char s_seps[]=" \t"; Function: Purpose: Params: - Returns: + Returns: true on success, false on failure ---------------------------------------------------------------------- */ extern int preprocessorCmd(char *_cmd) { @@ -96,7 +102,7 @@ extern int preprocessorCmd(char *_cmd) pp=s_preproCmds; for(i=0;im_cmd,strlen(pp->m_cmd))==0) + if(strncmp(_cmd,pp->m_cmd,strlen(pp->m_cmd))==0) { return pp->m_func(_cmd); } @@ -104,7 +110,7 @@ extern int preprocessorCmd(char *_cmd) } printf("UNKNOWN PREPROCESSOR CMD '%s'\n",_cmd); - return 1; + return true; } @@ -140,7 +146,7 @@ static int ppf_include(char *_cmd) includeFile=strtok(_cmd,s_seps); includeFile=strtok(NULL,s_seps); - return openPFile(includeFile)==1?0:1; + return openPFile(includeFile); } @@ -156,13 +162,28 @@ static int ppf_print(char *_cmd) printMessage=strtok(_cmd,s_seps); printMessage=strtok(NULL,s_seps); - + printf("#print %s\n",printMessage); - return 0; + return true; } +/*---------------------------------------------------------------------- + Function: + Purpose: + Params: + Returns: + ---------------------------------------------------------------------- */ +static int ppf_undefine(char *_cmd) +{ + char *macroName; + + macroName=strtok(_cmd,s_seps); + macroName=strtok(NULL,s_seps); + + return removeMacro(macroName); +} /*---------------------------------------------------------------------- @@ -176,24 +197,63 @@ static int addMacro(char *_name,char *_replacement) Macro *mac,*newMac; mac=s_macros; - while(mac&&mac->m_next) + while(mac) { - if(strcmp(_name,mac->m_replacement)==0) + if(strcmp(_name,mac->m_name)==0) { printf("MACRO '%s' ALREADY DEFINED\n",_name); - return -1; + return false; } + mac=mac->m_next; } newMac=(Macro*)malloc(sizeof(Macro)); - newMac->m_name=(char*)malloc(strlen(_name+1)); - newMac->m_replacement=(char*)malloc(strlen(_name+1)); + newMac->m_name=(char*)malloc(strlen(_name)+1); + newMac->m_replacement=(char*)malloc(strlen(_replacement)+1); strcpy(newMac->m_name,_name); strcpy(newMac->m_replacement,_replacement); newMac->m_next=s_macros; s_macros=newMac; - return 0; + return true; +} + + +/*---------------------------------------------------------------------- + Function: + Purpose: + Params: + Returns: + ---------------------------------------------------------------------- */ +static int removeMacro(char *_name) +{ + Macro *mac,*prev; + + mac=s_macros; + prev=NULL; + while(mac) + { + if(strcmp(_name,mac->m_name)==0) + { + if(prev) + { + prev->m_next=mac->m_next; + } + else + { + s_macros=mac->m_next; + } + free(mac->m_name); + free(mac->m_replacement); + free(mac); + return true; + } + prev=mac; + mac=mac->m_next; + } + + printf("MACRO '%s' NOT DEFINED, CANNOT UNDEF IT\n",_name); + return false; } @@ -209,6 +269,5 @@ extern char *lookupMacro(char *_name) } - /*=========================================================================== end */ diff --git a/Utils/Scripter/scripter.dsp b/Utils/Scripter/scripter.dsp index 58e3a5e20..da5894317 100644 --- a/Utils/Scripter/scripter.dsp +++ b/Utils/Scripter/scripter.dsp @@ -140,6 +140,14 @@ SOURCE=.\main.h # End Source File # Begin Source File +SOURCE=.\mylexer.cpp +# End Source File +# Begin Source File + +SOURCE=.\myparser.cpp +# End Source File +# Begin Source File + SOURCE=.\pfile.cpp # End Source File # Begin Source File diff --git a/Utils/Scripter/var.cpp b/Utils/Scripter/var.cpp index b61896817..6b88fc4c9 100644 --- a/Utils/Scripter/var.cpp +++ b/Utils/Scripter/var.cpp @@ -74,7 +74,7 @@ extern int lookupVarName(char *_name) // Global vars for(i=0;i