[Plib-cvs] plib/src/psl pslError.cxx,NONE,1.1 Makefile.am,1.7,1.8 psl.h,1.10,1.11 pslCodeGen.cxx,1.1
Brought to you by:
sjbaker
From: Steve B. <sj...@us...> - 2002-09-07 23:05:10
|
Update of /cvsroot/plib/plib/src/psl In directory usw-pr-cvs1:/tmp/cvs-serv21782/plib/src/psl Modified Files: Makefile.am psl.h pslCodeGen.cxx pslCompiler.cxx pslCompiler.h pslContext.cxx pslContext.h pslDump.cxx pslExpression.cxx pslLocal.h pslProgram.cxx pslSymbols.cxx pslToken.cxx Added Files: pslError.cxx Log Message: Removed ode_demo. Added better error reporting for psl. --- NEW FILE: pslError.cxx --- /* PLIB - A Suite of Portable Game Libraries Copyright (C) 1998,2002 Steve Baker This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA [...77 lines suppressed...] void pslContext::error ( const char *fmt, ... ) { va_list argp; va_start ( argp, fmt ) ; vsprintf ( _pslErrorBuffer, fmt, argp ) ; va_end ( argp ) ; if ( _pslErrorCB != NULL ) (*_pslErrorCB)( PSL_RUNTIME_ERROR, getProgName(), pc, _pslErrorBuffer ) ; else fprintf ( stderr, "PSL: \"%s\" PC=%d: *ERROR* - %s\n", getProgName(), pc, _pslErrorBuffer ) ; bumpErrors () ; } Index: Makefile.am =================================================================== RCS file: /cvsroot/plib/plib/src/psl/Makefile.am,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Makefile.am 7 Sep 2002 10:59:29 -0000 1.7 +++ Makefile.am 7 Sep 2002 23:05:06 -0000 1.8 @@ -8,7 +8,8 @@ libplibpsl_a_SOURCES = psl.cxx pslCodeGen.cxx pslContext.cxx \ pslCompiler.cxx pslSymbols.cxx pslToken.cxx \ - pslExpression.cxx pslProgram.cxx pslDump.cxx + pslExpression.cxx pslProgram.cxx pslDump.cxx \ + pslError.cxx INCLUDES = -I$(top_srcdir)/src/util Index: psl.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/psl.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- psl.h 7 Sep 2002 11:24:39 -0000 1.10 +++ psl.h 7 Sep 2002 23:05:06 -0000 1.11 @@ -32,6 +32,11 @@ } ; +#define PSL_COMPILETIME_WARNING 1 +#define PSL_COMPILETIME_ERROR 2 +#define PSL_RUNTIME_WARNING 3 +#define PSL_RUNTIME_ERROR 4 + typedef unsigned char pslOpcode ; class pslContext ; class pslCompiler ; @@ -223,10 +228,12 @@ void *userData ; + char *progName ; + public: - pslProgram ( pslExtension *ext ) ; - pslProgram ( pslProgram *src ) ; + pslProgram ( pslExtension *ext, char *_progName = NULL ) ; + pslProgram ( pslProgram *src, char *_progName = NULL ) ; ~pslProgram () ; @@ -235,6 +242,15 @@ pslCompiler *getCompiler () const { return compiler ; } pslExtension *getExtensions () const { return extensions ; } + char *getProgName () const { return progName ; } + + void setProgName ( const char *nm ) + { + delete [] progName ; + progName = new char [ strlen ( nm ) + 1 ] ; + strcpy ( progName, nm ) ; + } + void *getUserData () const { return userData ; } void setUserData ( void *ud ) { userData = ud ; } @@ -248,4 +264,6 @@ void pslInit () ; +void pslSetErrorCallback ( void (*CB) ( int, char *, int, char * ) ) ; + Index: pslCodeGen.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCodeGen.cxx,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- pslCodeGen.cxx 7 Sep 2002 07:00:35 -0000 1.10 +++ pslCodeGen.cxx 7 Sep 2002 23:05:06 -0000 1.11 @@ -24,33 +24,6 @@ #include "pslLocal.h" -int pslCompiler::compile ( const char *fname ) -{ - init () ; - - FILE *fd = fopen ( fname, "ra" ) ; - - if ( fd == NULL ) - { - perror ( "PSL:" ) ; - ulSetError ( UL_WARNING, "PSL: Failed while opening '%s' for reading.", - fname ); - return FALSE ; - } - - compile ( fd ) ; - fclose ( fd ) ; - return TRUE ; -} - - -int pslCompiler::compile ( FILE *fd ) -{ - pslSetDefaultFile ( fd ) ; - pushProgram () ; - return TRUE ; -} - void pslCompiler::pushCodeByte ( pslOpcode op ) { Index: pslCompiler.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.cxx,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- pslCompiler.cxx 7 Sep 2002 07:00:35 -0000 1.10 +++ pslCompiler.cxx 7 Sep 2002 23:05:06 -0000 1.11 @@ -25,20 +25,63 @@ #include "pslLocal.h" + +int pslCompiler::compile ( const char *fname ) +{ + FILE *fd = fopen ( fname, "ra" ) ; + + if ( fd == NULL ) + { + perror ( "PSL:" ) ; [...391 lines suppressed...] } - pslGetToken ( c ) ; + getToken ( c ) ; if ( c [ 0 ] != '{' ) - ulSetError ( UL_WARNING, - "PSL: Missing '{' in function '%s'", fn ) ; + error ( "Missing '{' in function '%s'", fn ) ; if ( ! pushCompoundStatement () ) - ulSetError ( UL_WARNING, - "PSL: Missing '}' in function '%s'", fn ) ; + error ( "Missing '}' in function '%s'", fn ) ; - pslGetToken ( c ) ; + getToken ( c ) ; /* If we fall off the end of the function, we still need a return value */ Index: pslCompiler.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- pslCompiler.h 7 Sep 2002 07:00:35 -0000 1.5 +++ pslCompiler.h 7 Sep 2002 23:05:06 -0000 1.6 @@ -46,6 +46,17 @@ class pslCompiler { + int line_no ; + + int getLineNo () { return line_no ; } + + int getChar ( FILE *fd ) ; + int unGetChar ( int c, FILE *fd ) ; + + void ungetToken ( const char *c ) ; + void getToken ( char *c, FILE *fd = NULL ) ; + void setDefaultFile ( FILE *fd ) ; + void pushCodeByte ( unsigned char b ) ; void pushCodeAddr ( pslAddress a ) ; @@ -122,6 +133,19 @@ private: + int num_errors ; + int num_warnings ; + + void bumpErrors () { num_errors++ ; } + void bumpWarnings () { num_warnings++ ; } + + char *progName ; + + const char *getProgName () { return progName ; } + + void error ( const char *fmt, ... ) ; + void warning ( const char *fmt, ... ) ; + int next_var ; int next_label ; int next_code ; @@ -146,7 +170,7 @@ void pushLocality () { if ( locality_sp >= MAX_NESTING-1 ) - ulSetError ( UL_WARNING, "PSL: Too many nested {}'s" ) ; + error ( "Too many nested {}'s" ) ; else locality_stack [ locality_sp++ ] = next_var ; } @@ -154,7 +178,7 @@ void popLocality () { if ( locality_sp <= 0 ) - ulSetError ( UL_FATAL, "PSL: Locality stack underflow !!" ) ; + error ( "Locality stack underflow !!" ) ; /* Delete any local symbols */ @@ -172,8 +196,11 @@ public: - pslCompiler ( pslOpcode *_code, pslExtension *_extn ) + pslCompiler ( pslOpcode *_code, pslExtension *_extn, const char *_progName ) { + progName = new char [ strlen ( _progName ) + 1 ] ; + strcpy ( progName, _progName ) ; + code = _code ; extensions = _extn ; @@ -205,6 +232,8 @@ { int i ; + line_no = 1 ; + for ( i = 0 ; i < MAX_CODE ; i++ ) code [ i ] = OPCODE_HALT ; for ( i = 0 ; i < MAX_SYMBOL ; i++ ) @@ -213,6 +242,8 @@ delete [] code_symtab [ i ] . symbol ; code_symtab [ i ] . symbol = NULL ; delete [] forward_ref [ i ] . symbol ; forward_ref [ i ] . symbol = NULL ; } + + num_errors = num_warnings = 0 ; locality_sp = 0 ; next_fwdref = 0 ; Index: pslContext.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslContext.cxx,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- pslContext.cxx 7 Sep 2002 07:00:35 -0000 1.2 +++ pslContext.cxx 7 Sep 2002 23:05:06 -0000 1.3 @@ -29,9 +29,9 @@ switch ( code [ pc ] ) { case OPCODE_NOOP : - ulSetError ( UL_FATAL, "PSL: Suspicious opcode 0x00?!", code[pc] ) ; + error ( "Suspicious opcode 0x00?!", code[pc] ) ; pc++ ; - return PSL_PROGRAM_CONTINUE ; + return PSL_PROGRAM_END ; case OPCODE_PUSH_INT_CONSTANT : { @@ -80,8 +80,7 @@ if ( required_argc >= 0 && argc != required_argc ) { - ulSetError ( UL_WARNING, - "PSL: Wrong number of parameters for function %s\n", + warning ( "Wrong number of parameters for function %s\n", extensions [ ext ] . symbol ) ; } @@ -155,14 +154,14 @@ if ( v1 -> getFloat() != 0.0f ) v2 -> set ( v2 -> getFloat() / v1 -> getFloat() ) ; else - ulSetError ( UL_WARNING, "PSL: Floating Point Divide by Zero!" ) ; + warning ( "Floating Point Divide by Zero!" ) ; } else { if ( v1 -> getInt() != 0 ) v2 -> set ( v2 -> getInt() / v1 -> getInt() ) ; else - ulSetError ( UL_WARNING, "PSL: Integer Divide by Zero!" ) ; + warning ( "Integer Divide by Zero!" ) ; } popVoid () ; pc++ ; @@ -176,14 +175,14 @@ if ( v1->getType() == PSL_FLOAT || v2->getType() == PSL_FLOAT ) { - ulSetError ( UL_WARNING, "PSL: Floating Point Modulo!" ) ; + warning ( "Floating Point Modulo!" ) ; } else { if ( v1 -> getInt() != 0 ) v2 -> set ( v2 -> getInt () % v1 -> getInt () ) ; else - ulSetError ( UL_WARNING, "PSL: Integer Modulo Zero!" ) ; + warning ( "Integer Modulo Zero!" ) ; } popVoid () ; @@ -353,8 +352,8 @@ return PSL_PROGRAM_CONTINUE ; default : - ulSetError ( UL_FATAL, "PSL: Suspicious opcode 0x02x?!", code[pc] ) ; - return PSL_PROGRAM_CONTINUE ; + error ( "Suspicious opcode 0x02x?!", code[pc] ) ; + return PSL_PROGRAM_END ; } } Index: pslContext.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslContext.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- pslContext.h 7 Sep 2002 11:24:39 -0000 1.4 +++ pslContext.h 7 Sep 2002 23:05:06 -0000 1.5 @@ -33,6 +33,17 @@ int sp ; pslAddress pc ; + int num_errors ; + int num_warnings ; + + void bumpErrors () { num_errors++ ; } + void bumpWarnings () { num_warnings++ ; } + + const char *getProgName () { return program -> getProgName () ; } + + void error ( const char *fmt, ... ) ; + void warning ( const char *fmt, ... ) ; + public: pslContext ( pslProgram *p ) Index: pslDump.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslDump.cxx,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pslDump.cxx 7 Sep 2002 07:00:35 -0000 1.9 +++ pslDump.cxx 7 Sep 2002 23:05:06 -0000 1.10 @@ -150,6 +150,17 @@ } printf ( "\n" ) ; + + if ( num_errors > 0 ) + printf ( "PROGRAM FAILED TO COMPILE WITH %d WARNINGS AND %d ERRORS\n", + num_warnings, num_errors ) ; + else + if ( num_warnings > 0 ) + printf ( "PROGRAM COMPILED WITH %d WARNINGS\n", num_warnings ) ; + else + printf ( "PROGRAM COMPILED OK\n" ) ; + + printf ( "\n" ) ; } Index: pslExpression.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslExpression.cxx,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- pslExpression.cxx 7 Sep 2002 07:00:35 -0000 1.7 +++ pslExpression.cxx 7 Sep 2002 23:05:06 -0000 1.8 @@ -28,23 +28,23 @@ int pslCompiler::pushPrimitive () { char c [ MAX_TOKEN ] ; - pslGetToken ( c ) ; + getToken ( c ) ; if ( c [ 0 ] == '(' ) { if ( ! pushExpression () ) { - ulSetError ( UL_WARNING, "PSL: Missing expression after '('" ) ; [...102 lines suppressed...] - pslGetToken ( c2 ) ; + getToken ( c2 ) ; if ( c2 [ 0 ] == '=' ) { @@ -201,11 +201,11 @@ c[2] = '\0' ; } else - pslUngetToken ( c2 ) ; + ungetToken ( c2 ) ; if (( c [ 0 ] == '!' || c [ 0 ] == '=' ) && c [ 1 ] != '=' ) { - pslUngetToken ( c2 ) ; + ungetToken ( c2 ) ; return TRUE ; } Index: pslLocal.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslLocal.h,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- pslLocal.h 7 Sep 2002 07:00:36 -0000 1.12 +++ pslLocal.h 7 Sep 2002 23:05:06 -0000 1.13 @@ -46,14 +46,6 @@ #define MAX_NESTING 32 #define MAX_SYMBOL (MAX_VARIABLE + MAX_LABEL) - -/* Token Parser */ - -void pslUngetToken ( const char *c ) ; -void pslGetToken ( char *c, FILE *fd = NULL ) ; -void pslSetDefaultFile ( FILE *fd ) ; - - typedef unsigned short pslAddress ; extern int _pslInitialised ; Index: pslProgram.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslProgram.cxx,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- pslProgram.cxx 7 Sep 2002 07:00:36 -0000 1.7 +++ pslProgram.cxx 7 Sep 2002 23:05:06 -0000 1.8 @@ -24,7 +24,7 @@ #include "pslLocal.h" -pslProgram::pslProgram ( pslExtension *ext ) +pslProgram::pslProgram ( pslExtension *ext, char *_prgnm ) { if ( ! _pslInitialised ) ulSetError ( UL_FATAL, @@ -34,18 +34,30 @@ extensions = ext ; - compiler = new pslCompiler ( code, ext ) ; - context = new pslContext ( this ) ; + progName = NULL ; - compiler -> init () ; + if ( _prgnm == NULL ) _prgnm = "PSLptogram" ; + + setProgName ( _prgnm ) ; + + compiler = new pslCompiler ( code, ext, getProgName () ) ; + context = new pslContext ( this ) ; + + compiler-> init () ; context -> reset () ; } -pslProgram::pslProgram ( pslProgram *src ) +pslProgram::pslProgram ( pslProgram *src, char *_prgnm ) { + progName = NULL ; + + if ( _prgnm == NULL ) _prgnm = src -> getProgName () ; + + setProgName ( _prgnm ) ; + code = src -> getCode () ; - compiler = src -> getCompiler () ; + compiler = src -> getCompiler () ; extensions = src -> getExtensions () ; userData = src -> getUserData () ; @@ -56,24 +68,33 @@ pslProgram::~pslProgram () { - delete compiler ; - delete context ; - delete [] code ; + delete [] progName ; + delete compiler ; + delete context ; + delete [] code ; } -void pslProgram::dump () const { compiler -> dump () ; } +void pslProgram::dump () const { compiler-> dump () ; } void pslProgram::reset () { context -> reset () ; } pslResult pslProgram::step () { return context -> step () ; } pslResult pslProgram::trace () { return context -> trace () ; } -int pslProgram::compile ( const char *fname ) + + +int pslProgram::compile ( const char *fname ) { - return compiler -> compile (fname) ; + if ( strcmp ( getProgName(), "PSLprogram" ) == 0 ) + setProgName ( fname ) ; + + return compiler -> compile ( fname ) ; } -int pslProgram::compile ( FILE *fd ) + +int pslProgram::compile ( FILE *fd ) { - return compiler -> compile( fd ) ; + return compiler -> compile ( fd ) ; } + + Index: pslSymbols.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslSymbols.cxx,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- pslSymbols.cxx 6 Sep 2002 18:51:53 -0000 1.11 +++ pslSymbols.cxx 7 Sep 2002 23:05:06 -0000 1.12 @@ -31,13 +31,13 @@ if ( strcmp ( s, symtab [ i ] . symbol ) == 0 && symtab [ i ] . locality >= locality_sp ) { - ulSetError ( UL_WARNING, "PSL: Multiple definition of '%s'.", s ) ; + error ( "Multiple definition of '%s'.", s ) ; return symtab [ i ] . address ; } if ( next_var >= MAX_VARIABLE-1 ) { - ulSetError ( UL_WARNING, "PSL: Too many variables." ) ; + error ( "Too many variables." ) ; next_var-- ; } @@ -56,7 +56,7 @@ if ( strcmp ( s, symtab [ i ] . symbol ) == 0 ) return symtab [ i ] . address ; - ulSetError ( UL_WARNING, "PSL: Undefined symbol '%s'.", s ) ; + error ( "Undefined symbol '%s'.", s ) ; return setVarSymbol ( s ) ; } @@ -85,7 +85,7 @@ if ( next_fwdref >= MAX_SYMBOL ) { - ulSetError ( UL_WARNING, "PSL: Too many unresolved forward references." ) ; + error ( "Too many unresolved forward references." ) ; return ; } @@ -119,8 +119,7 @@ { if ( forward_ref [ i ] . symbol != NULL ) { - ulSetError ( UL_WARNING, "PSL: '%s' does not exist.", - forward_ref [ i ] . symbol ) ; + error ( "'%s' does not exist.", forward_ref [ i ] . symbol ) ; /* Prevent multiple errors for same symbol */ @@ -151,13 +150,13 @@ for ( int i = 0 ; i < next_code_symbol ; i++ ) if ( strcmp ( s, code_symtab [ i ] . symbol ) == 0 ) { - ulSetError ( UL_WARNING, "PSL: Multiple definition of '%s'.", s ) ; + error ( "Multiple definition of '%s'.", s ) ; return ; } if ( next_code_symbol >= MAX_VARIABLE-1 ) { - ulSetError ( UL_WARNING, "PSL: Too many labels." ) ; + error ( "Too many labels." ) ; next_code_symbol-- ; } Index: pslToken.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslToken.cxx,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- pslToken.cxx 7 Sep 2002 07:00:36 -0000 1.8 +++ pslToken.cxx 7 Sep 2002 23:05:06 -0000 1.9 @@ -31,13 +31,33 @@ static int unget_stack_depth = 0 ; -void pslSetDefaultFile ( FILE *fd ) +void pslCompiler::setDefaultFile ( FILE *fd ) { defaultFile = fd ; } -void pslGetToken ( char *res, FILE *fd ) [...111 lines suppressed...] - ungetc ( c, fd ) ; + unGetChar ( c, fd ) ; res [ tp ] = '\0' ; } else @@ -184,12 +201,11 @@ } -void pslUngetToken ( const char *s ) +void pslCompiler::ungetToken ( const char *s ) { if ( unget_stack_depth >= MAX_UNGET-1 ) { - ulSetError ( UL_WARNING, - "PSL: Too many ungetTokens! This must be an *UGLY* PSL program!" ) ; + error ( "Too many ungetTokens! This must be an *UGLY* PSL program!" ) ; exit ( -1 ) ; } |