[Plib-cvs] plib/src/psl pslCompiler.cxx,1.7,1.8 pslDump.cxx,1.6,1.7 pslLocal.h,1.9,1.10 pslParser.h,
Brought to you by:
sjbaker
From: Steve B. <sj...@us...> - 2002-09-06 16:44:54
|
Update of /cvsroot/plib/plib/src/psl In directory usw-pr-cvs1:/tmp/cvs-serv26424/plib/src/psl Modified Files: pslCompiler.cxx pslDump.cxx pslLocal.h pslParser.h pslSymbol.h pslSymbols.cxx Log Message: Local variables (including nested locals) now works. Index: pslCompiler.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.cxx,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- pslCompiler.cxx 6 Sep 2002 15:07:44 -0000 1.7 +++ pslCompiler.cxx 6 Sep 2002 16:44:51 -0000 1.8 @@ -207,22 +207,29 @@ { char c [ MAX_TOKEN ] ; + pushLocality () ; + while ( pushStatement () ) { pslGetToken ( c ) ; if ( c[0] != ';' ) + { + popLocality () ; return FALSE ; + } } pslGetToken ( c ) ; if ( c[0] == '}' ) { + popLocality () ; pslUngetToken ( ";" ) ; return TRUE ; } + popLocality () ; pslUngetToken ( c ) ; return FALSE ; } @@ -295,9 +302,24 @@ int pslParser::pushLocalVariableDeclaration () { - ulSetError ( UL_WARNING, - "PSL: Local Variables are Not Supported Yet." ) ; - return FALSE ; + char c [ MAX_TOKEN ] ; + char s [ MAX_TOKEN ] ; + + pslGetToken ( s ) ; + + setVarSymbol ( s ) ; + + pslGetToken ( c ) ; + + if ( c[0] == '=' ) + { + pslUngetToken ( c ) ; + pushAssignmentStatement ( s ) ; + return TRUE ; + } + + pslUngetToken ( c ) ; + return TRUE ; } Index: pslDump.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslDump.cxx,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- pslDump.cxx 6 Sep 2002 13:50:51 -0000 1.6 +++ pslDump.cxx 6 Sep 2002 16:44:51 -0000 1.7 @@ -147,7 +147,7 @@ } printf ( "\n" ) ; - printf ( "Variables:\n" ) ; + printf ( "Global Variables:\n" ) ; for ( i = 0 ; i < MAX_SYMBOL ; i++ ) if ( symtab [ i ] . symbol != NULL ) @@ -162,7 +162,7 @@ } printf ( "\n" ) ; - printf ( "Labels:\n" ) ; + printf ( "Functions:\n" ) ; for ( i = 0 ; i < MAX_SYMBOL ; i++ ) if ( code_symtab [ i ] . symbol != NULL ) Index: pslLocal.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslLocal.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pslLocal.h 6 Sep 2002 15:07:44 -0000 1.9 +++ pslLocal.h 6 Sep 2002 16:44:51 -0000 1.10 @@ -37,12 +37,13 @@ /* Limits */ -#define MAX_ARGS 16 -#define MAX_VARIABLE 16 -#define MAX_LABEL 16 -#define MAX_TOKEN 256 -#define MAX_CODE 512 -#define MAX_STACK 32 +#define MAX_ARGS 64 +#define MAX_VARIABLE 256 +#define MAX_LABEL 256 +#define MAX_TOKEN 256 +#define MAX_CODE 512 +#define MAX_STACK 256 +#define MAX_NESTING 32 #define MAX_SYMBOL (MAX_VARIABLE + MAX_LABEL) /* Code Opcodes */ Index: pslParser.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslParser.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- pslParser.h 6 Sep 2002 13:50:51 -0000 1.1 +++ pslParser.h 6 Sep 2002 16:44:51 -0000 1.2 @@ -80,12 +80,12 @@ void print_opcode ( FILE *fd, unsigned char op ) const ; - pslAddress getVarSymbol ( const char *s ) ; - pslAddress setVarSymbol ( const char *s ) ; + pslAddress getVarSymbol ( const char *s ) ; + pslAddress setVarSymbol ( const char *s ) ; - pslAddress getCodeSymbol ( const char *s, pslAddress fixupLoc ) ; - void setCodeSymbol ( const char *s, pslAddress v ) ; - int getExtensionSymbol ( const char *s ) ; + pslAddress getCodeSymbol ( const char *s, pslAddress fixupLoc ) ; + void setCodeSymbol ( const char *s, pslAddress v ) ; + int getExtensionSymbol ( const char *s ) ; private: @@ -99,6 +99,9 @@ pslSymbol code_symtab [ MAX_SYMBOL ] ; pslFwdRef forward_ref [ MAX_SYMBOL ] ; + int locality_stack [ MAX_NESTING ] ; + int locality_sp ; + pslOpcode *code ; pslContext *context ; pslExtension *extensions ; @@ -107,6 +110,33 @@ void addFwdRef ( const char *s, pslAddress where ) ; void checkUnresolvedSymbols () ; + void pushLocality () + { + if ( locality_sp >= MAX_NESTING-1 ) + ulSetError ( UL_WARNING, "PSL: Too many nested {}'s" ) ; + else + locality_stack [ locality_sp++ ] = next_var ; + } + + void popLocality () + { + if ( locality_sp <= 0 ) + ulSetError ( UL_FATAL, "PSL: Locality stack underflow !!" ) ; + + /* Delete any local symbols */ + + for ( int i = locality_stack [ locality_sp-1 ] ; + i < next_var ; i++ ) + { + delete symtab [ i ] . symbol ; + symtab [ i ] . symbol = NULL ; + } + + /* Put the next_var pointer back where it belongs */ + + next_var = locality_stack [ --locality_sp ] ; + } + public: pslParser ( pslOpcode *_code, pslExtension *_extn ) @@ -149,6 +179,7 @@ delete forward_ref [ i ] . symbol ; forward_ref [ i ] . symbol = NULL ; } + locality_sp = 0 ; next_fwdref = 0 ; next_label = 0 ; next_code_symbol = 0 ; Index: pslSymbol.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslSymbol.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- pslSymbol.h 6 Sep 2002 13:50:51 -0000 1.1 +++ pslSymbol.h 6 Sep 2002 16:44:51 -0000 1.2 @@ -2,20 +2,23 @@ class pslSymbol { public: - char *symbol ; - pslAddress address ; + char *symbol ; + pslAddress address ; + int locality ; pslSymbol () { - symbol = NULL ; - address = 0 ; + symbol = NULL ; + address = 0 ; + locality = 0 ; } - void set ( const char *s, pslAddress v ) + void set ( const char *s, pslAddress v, int loc ) { - symbol = new char [ strlen ( s ) + 1 ] ; + symbol = new char [ strlen ( s ) + 1 ] ; strcpy ( symbol, s ) ; - address = v ; + address = v ; + locality = loc ; } ~pslSymbol () { delete symbol ; } Index: pslSymbols.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslSymbols.cxx,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- pslSymbols.cxx 6 Sep 2002 13:50:51 -0000 1.8 +++ pslSymbols.cxx 6 Sep 2002 16:44:51 -0000 1.9 @@ -28,7 +28,8 @@ pslAddress pslParser::setVarSymbol ( const char *s ) { for ( int i = 0 ; i < next_var ; i++ ) - if ( strcmp ( s, symtab [ i ] . symbol ) == 0 ) + if ( strcmp ( s, symtab [ i ] . symbol ) == 0 && + symtab [ i ] . locality >= locality_sp ) { ulSetError ( UL_WARNING, "PSL: Multiple definition of '%s'.", s ) ; return symtab [ i ] . address ; @@ -40,7 +41,7 @@ next_var-- ; } - symtab [ next_var ] . set ( s, next_var ) ; + symtab [ next_var ] . set ( s, next_var, locality_sp ) ; return symtab [ next_var++ ] . address ; } @@ -49,7 +50,9 @@ pslAddress pslParser::getVarSymbol ( const char *s ) { - for ( int i = 0 ; i < next_var ; i++ ) + /* Search Backwards so most local variable shows up first */ + + for ( int i = next_var-1 ; i >= 0 ; i-- ) if ( strcmp ( s, symtab [ i ] . symbol ) == 0 ) return symtab [ i ] . address ; @@ -158,7 +161,7 @@ next_code_symbol-- ; } - code_symtab [ next_code_symbol++ ] . set ( s, v ) ; + code_symtab [ next_code_symbol++ ] . set ( s, v, locality_sp ) ; fixup ( s, v ) ; } |