[Plib-cvs] plib/src/psl Makefile.am,NONE,1.1 psl.cxx,NONE,1.1 psl.h,NONE,1.1 pslCodeGen.cxx,NONE,1.1
Brought to you by:
sjbaker
Update of /cvsroot/plib/plib/src/psl In directory usw-pr-cvs1:/tmp/cvs-serv22787/plib/src/psl Added Files: Makefile.am psl.cxx psl.h pslCodeGen.cxx pslExpression.cxx pslRun.cxx pslStatement.cxx pslSymbols.cxx pslToken.cxx Log Message: Added PSL. Modified documents and configure/make system accordingly. --- NEW FILE: Makefile.am --- if BUILD_PSL lib_LIBRARIES = libplibpsl.a include_HEADERS = psl.h pslPrivate.h libplibpsl_a_SOURCES = psl.cxx pslCodeGen.cxx pslRun.cxx \ pslStatement.cxx pslSymbols.cxx pslToken.cxx \ pslExpression.cxx pslProgram.cxx INCLUDES = -I$(top_srcdir)/src/sg -I$(top_srcdir)/src/util endif EXTRA_DIST = psl.dsp --- NEW FILE: psl.cxx --- #include "pslPrivate.h" int PSL_Parser::parse ( char *fname ) { init () ; FILE *fd = fopen ( fname, "ra" ) ; if ( fd == NULL ) { #ifdef SHOUT_ABOUT_PSL_ERRORS perror ( "PSL:" ) ; fprintf ( stderr, "PSL: Failed while opening '%s' for reading.\n", fname ); #endif return FALSE ; } parse ( fd ) ; fclose ( fd ) ; return TRUE ; } int PSL_Parser::parse ( FILE *fd ) { setDefaultFile ( fd ) ; pushProgram () ; return TRUE ; } --- NEW FILE: psl.h --- #include <stdio.h> enum PSL_Result { PSL_PROGRAM_END, PSL_PROGRAM_PAUSE, PSL_PROGRAM_CONTINUE } ; typedef unsigned char PSL_Opcode ; class PSL_Context ; class PSL_Parser ; class PSL_Extension { public: char *symbol ; int argc ; float (*func) ( int, float * ) ; } ; class PSL_Program { PSL_Opcode *code ; PSL_Context *context ; PSL_Parser *parser ; public: PSL_Program ( PSL_Extension *ext ) ; ~PSL_Program () ; void dump () ; int parse ( char *fname ) ; int parse ( FILE *fd ) ; void reset () ; PSL_Result step () ; } ; --- NEW FILE: pslCodeGen.cxx --- #include "pslPrivate.h" void PSL_Parser::pushCodeByte ( PSL_Opcode op ) { code [ next_code++ ] = op ; } void PSL_Parser::pushCodeAddr ( PSL_Address a ) { pushCodeByte ( a & 0xFF ) ; pushCodeByte ( ( a >> 8 ) & 0xFF ) ; } void PSL_Parser::pushConstant ( char *c ) { float f = atof ( c ) ; [...73 lines suppressed...] { int a = getCodeSymbol ( c ) ; pushCodeByte ( OPCODE_JUMP ) ; int res = next_code ; pushCodeAddr ( a ) ; return res ; } int PSL_Parser::pushPauseStatement() { pushCodeByte ( OPCODE_PAUSE ) ; return TRUE ; } --- NEW FILE: pslExpression.cxx --- #include "pslPrivate.h" int PSL_Parser::pushPrimitive () { char c [ MAX_TOKEN ] ; getToken ( c ) ; if ( c [ 0 ] == '(' ) { if ( ! pushExpression () ) { fprintf ( stderr, "PSL: Missing expression after '('\n" ) ; ungetToken ( c ) ; return FALSE ; } getToken ( c ) ; [...173 lines suppressed...] if ( c [ 1 ] == '=' ) pushGreaterEqual () ; else pushGreater () ; } else if ( c [ 0 ] == '!' ) pushNotEqual () ; else pushEqual () ; } } int PSL_Parser::pushExpression () { return pushRelExpression () ; } --- NEW FILE: pslRun.cxx --- #include "pslPrivate.h" PSL_Result PSL_Context::step () { switch ( code [ pc ] ) { case OPCODE_PUSH_CONSTANT : { char *ff = (char *) & ( stack [ sp++ ] ) ; ff[0] = code [ ++pc ] ; ff[1] = code [ ++pc ] ; ff[2] = code [ ++pc ] ; ff[3] = code [ ++pc ] ; pc++ ; } return PSL_PROGRAM_CONTINUE ; case OPCODE_POP : [...113 lines suppressed...] pc = code [ pc + 1 ] + ( code [ pc + 2 ] << 8 ) ; return PSL_PROGRAM_CONTINUE ; default : if ( ( code [ pc ] & 0xF0 ) == OPCODE_PUSH_VARIABLE ) { stack [ sp++ ] = variable [ code[pc] & 0x0F ] ; pc++ ; } else if ( ( code [ pc ] & 0xF0 ) == OPCODE_POP_VARIABLE ) { variable [ code[pc] & 0x0F ] = stack [ --sp ] ; pc++ ; } return PSL_PROGRAM_CONTINUE ; } } --- NEW FILE: pslStatement.cxx --- #include "pslPrivate.h" struct OpcodeDecode { char *s ; unsigned char opcode ; } ; OpcodeDecode opcodeDecode [] = { { "PUSH_CONSTANT", OPCODE_PUSH_CONSTANT }, { "CALL", OPCODE_CALL }, { "PAUSE", OPCODE_PAUSE }, { "JUMP_FALSE", OPCODE_JUMP_FALSE }, { "JUMP", OPCODE_JUMP }, [...347 lines suppressed...] printf ( "\n" ) ; printf ( "Variables:\n" ) ; for ( i = 0 ; i < MAX_SYMBOL ; i++ ) if ( symtab [ i ] . symbol != NULL ) { printf ( "\t%5s => %4d", symtab[i].symbol, symtab[i].address ) ; if ( i & 1 ) printf ( "\n" ) ; else printf ( " " ) ; } printf ( "\n" ) ; } --- NEW FILE: pslSymbols.cxx --- #include "pslPrivate.h" PSL_Address PSL_Parser::getVarSymbol ( char *s ) { for ( int i = 0 ; i < MAX_SYMBOL ; i++ ) { if ( symtab [ i ] . symbol == NULL ) { if ( next_var >= MAX_VARIABLE-1 ) { fprintf ( stderr, "PSL: Too many variables.\n" ) ; next_var-- ; } symtab [ i ] . set ( s, next_var++ ) ; return symtab [ i ] . address ; } else if ( strcmp ( s, symtab [ i ] . symbol ) == 0 ) return symtab [ i ] . address ; } fprintf ( stderr, "PSL: Too many symbols in one program.\n" ) ; return MAX_VARIABLE-1 ; } int PSL_Parser::getExtensionSymbol ( char *s ) { for ( int i = 0 ; extensions [ i ] . symbol != NULL ; i++ ) if ( strcmp ( s, extensions [ i ] . symbol ) == 0 ) return i ; return -1 ; } PSL_Address PSL_Parser::getCodeSymbol ( char *s ) { for ( int i = 0 ; i < MAX_SYMBOL ; i++ ) { if ( symtab [ i ] . symbol == NULL ) { symtab [ i ] . set ( s, 0 ) ; return 0 ; } else if ( strcmp ( s, symtab [ i ] . symbol ) == 0 ) return symtab [ i ] . address ; } fprintf ( stderr, "PSL: Too many symbols in one program.\n" ) ; return 0 ; } void PSL_Parser::setCodeSymbol ( char *s, PSL_Address v ) { for ( int i = 0 ; i < MAX_SYMBOL ; i++ ) { if ( symtab [ i ] . symbol == NULL ) { symtab [ i ] . set ( s, v ) ; return ; } else if ( strcmp ( s, symtab [ i ] . symbol ) == 0 ) { symtab [ i ] . address = v ; return ; } } fprintf ( stderr, "PSL: Too many symbols in one program.\n" ) ; } --- NEW FILE: pslToken.cxx --- #include "pslPrivate.h" #define MAX_UNGET 16 static FILE *defaultFile ; static char ungotten_token [ MAX_UNGET ][ MAX_TOKEN ] ; static int unget_stack_depth = 0 ; void setDefaultFile ( FILE *fd ) { defaultFile = fd ; } void getToken ( char *res, FILE *fd ) { if ( unget_stack_depth > 0 ) { strcpy ( res, ungotten_token [ --unget_stack_depth ] ) ; return ; } if ( fd == NULL ) fd = defaultFile ; int c ; do { c = getc ( fd ) ; if ( c < 0 ) { res [ 0 ] = '\0' ; return ; } } while ( isspace ( c ) ) ; int tp = 0 ; while ( isalnum ( c ) || c == '.' || c == '_' ) { res [ tp++ ] = c ; c = getc ( fd ) ; if ( tp >= MAX_TOKEN - 1 ) { fprintf ( stderr, "token: Input string is bigger than %d characters!\n", MAX_TOKEN - 1 ) ; tp-- ; } } if ( tp > 0 ) { ungetc ( c, fd ) ; res [ tp ] = '\0' ; } else { res [ 0 ] = c ; res [ 1 ] = '\0' ; } } void ungetToken ( char *s ) { if ( unget_stack_depth >= MAX_UNGET-1 ) { fprintf ( stderr, "token: Too many ungetTokens! This must be an *UGLY* PSL program!\n" ) ; exit ( -1 ) ; } strcpy ( ungotten_token[unget_stack_depth++], s ) ; } |