[Plib-cvs] plib/src/psl pslOpcodes.h,NONE,1.1 Makefile.am,1.5,1.6 psl.h,1.8,1.9 pslCodeGen.cxx,1.9,1
Brought to you by:
sjbaker
From: Steve B. <sj...@us...> - 2002-09-07 07:00:42
|
Update of /cvsroot/plib/plib/src/psl In directory usw-pr-cvs1:/tmp/cvs-serv31278/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 pslToken.cxx Added Files: pslOpcodes.h Log Message: Added string constants. --- NEW FILE: pslOpcodes.h --- /* Code Opcodes */ #define OPCODE_NOOP 0x00 #define OPCODE_PUSH_INT_CONSTANT 0x01 #define OPCODE_PUSH_FLOAT_CONSTANT 0x02 #define OPCODE_PUSH_STRING_CONSTANT 0x03 #define OPCODE_CALL 0x04 #define OPCODE_SUB 0x05 #define OPCODE_ADD 0x06 #define OPCODE_DIV 0x07 #define OPCODE_MULT 0x08 #define OPCODE_MOD 0x09 #define OPCODE_NEG 0x0A #define OPCODE_LESS 0x0B #define OPCODE_LESSEQUAL 0x0C #define OPCODE_GREATER 0x0D #define OPCODE_GREATEREQUAL 0x0E #define OPCODE_NOTEQUAL 0x0F #define OPCODE_EQUAL 0x10 #define OPCODE_JUMP_FALSE 0x11 #define OPCODE_JUMP 0x12 #define OPCODE_POP 0x13 #define OPCODE_HALT 0x14 #define OPCODE_CALLEXT 0x15 #define OPCODE_PAUSE 0x16 #define OPCODE_RETURN 0x17 #define OPCODE_PUSH_VARIABLE 0x18 #define OPCODE_POP_VARIABLE 0x19 #define OPCODE_SET_INT_VARIABLE 0x1A #define OPCODE_SET_FLOAT_VARIABLE 0x1B #define OPCODE_SET_STRING_VARIABLE 0x1C Index: Makefile.am =================================================================== RCS file: /cvsroot/plib/plib/src/psl/Makefile.am,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- Makefile.am 6 Sep 2002 17:20:56 -0000 1.5 +++ Makefile.am 7 Sep 2002 07:00:34 -0000 1.6 @@ -4,7 +4,7 @@ lib_LIBRARIES = libplibpsl.a include_HEADERS = psl.h pslContext.h pslLocal.h \ - pslCompiler.h pslSymbol.h + pslCompiler.h pslSymbol.h pslOpcodes.h libplibpsl_a_SOURCES = psl.cxx pslCodeGen.cxx pslContext.cxx \ pslCompiler.cxx pslSymbols.cxx pslToken.cxx \ Index: psl.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/psl.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- psl.h 6 Sep 2002 17:20:56 -0000 1.8 +++ psl.h 7 Sep 2002 07:00:35 -0000 1.9 @@ -37,12 +37,171 @@ class pslCompiler ; class pslProgram ; + +enum pslType +{ + PSL_INT = 0, + PSL_FLOAT = 1, + PSL_STRING = 3, + PSL_VOID = 4 +} ; [...188 lines suppressed...] ~pslProgram () ; pslContext *getContext () const { return context ; } pslOpcode *getCode () const { return code ; } - pslCompiler *getCompiler () const { return compiler ; } + pslCompiler *getCompiler () const { return compiler ; } pslExtension *getExtensions () const { return extensions ; } void *getUserData () const { return userData ; } @@ -83,7 +242,8 @@ int compile ( const char *fname ) ; int compile ( FILE *fd ) ; void reset () ; - pslResult step () ; + pslResult step () ; + pslResult trace () ; } ; Index: pslCodeGen.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCodeGen.cxx,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pslCodeGen.cxx 6 Sep 2002 17:20:56 -0000 1.9 +++ pslCodeGen.cxx 7 Sep 2002 07:00:35 -0000 1.10 @@ -67,28 +67,95 @@ void pslCompiler::pushConstant ( const char *c ) { + int isInteger = TRUE ; + + for ( const char *p = c ; *p != '\0' ; p++ ) + if ( *p == '.' || *p == 'f' || *p == 'F' ) + { + isInteger = FALSE ; + break ; + } [...75 lines suppressed...] } void pslCompiler::pushAssignment ( const char *c ) { int a = getVarSymbol ( c ) ; - pushCodeByte ( OPCODE_POP_VARIABLE | a ) ; + pushCodeByte ( OPCODE_POP_VARIABLE ) ; + pushCodeByte ( a ) ; } @@ -120,6 +187,7 @@ void pslCompiler::pushAdd () { pushCodeByte ( OPCODE_ADD ) ; } void pslCompiler::pushDivide () { pushCodeByte ( OPCODE_DIV ) ; } void pslCompiler::pushMultiply () { pushCodeByte ( OPCODE_MULT ) ; } +void pslCompiler::pushModulo () { pushCodeByte ( OPCODE_MOD ) ; } void pslCompiler::pushNegate () { pushCodeByte ( OPCODE_NEG ) ; } void pslCompiler::pushLess () { pushCodeByte ( OPCODE_LESS ) ; } Index: pslCompiler.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.cxx,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pslCompiler.cxx 6 Sep 2002 17:20:56 -0000 1.9 +++ pslCompiler.cxx 7 Sep 2002 07:00:35 -0000 1.10 @@ -244,8 +244,14 @@ if ( strcmp ( c, "static" ) == 0 ) return pushStaticVariableDeclaration () ; + if ( strcmp ( c, "string" ) == 0 ) + return pushLocalVariableDeclaration ( PSL_STRING ) ; + + if ( strcmp ( c, "int" ) == 0 ) + return pushLocalVariableDeclaration ( PSL_INT ) ; + if ( strcmp ( c, "float" ) == 0 ) - return pushLocalVariableDeclaration () ; [...66 lines suppressed...] + if ( strcmp ( c, "float" ) == 0 ) t = PSL_FLOAT ; else + if ( strcmp ( c, "string" ) == 0 ) t = PSL_STRING ; else + { + ulSetError ( UL_WARNING, + "PSL: Expected declaration of variable or function - but got '%s'", + c ) ; + return FALSE ; + } pslGetToken ( fn ) ; @@ -395,7 +423,7 @@ if ( c[0] == '=' || c[0] == ';' ) { pslUngetToken ( c ) ; - return pushGlobalVariableDeclaration ( fn ) ; + return pushGlobalVariableDeclaration ( fn, t ) ; } ulSetError ( UL_WARNING, Index: pslCompiler.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- pslCompiler.h 6 Sep 2002 19:17:23 -0000 1.4 +++ pslCompiler.h 7 Sep 2002 07:00:35 -0000 1.5 @@ -56,6 +56,7 @@ void pushAdd () ; void pushDivide () ; void pushMultiply () ; + void pushModulo () ; void pushNegate () ; void pushLess () ; void pushLessEqual () ; @@ -66,11 +67,21 @@ int pushJumpIfFalse ( int l ) ; int pushJump ( int l ) ; - void pushConstant ( const char *c ) ; - void pushVariable ( const char *c ) ; - void pushAssignment ( const char *c ) ; - void pushCall ( const char *c, int argc ) ; - void pushReturn () ; + void makeIntVariable ( const char *s ) ; + void makeFloatVariable ( const char *s ) ; + void makeStringVariable( const char *s ) ; + + void pushConstant ( const char *s ) ; + void pushIntConstant ( const char *s ) ; + void pushFloatConstant ( const char *s ) ; + void pushStringConstant( const char *s ) ; + + void pushVoidConstant () ; + + void pushVariable ( const char *s ) ; + void pushAssignment ( const char *s ) ; + void pushCall ( const char *s, int argc ) ; + void pushReturn () ; /* Higher level parsers. */ @@ -86,20 +97,20 @@ int pushPauseStatement () ; int pushWhileStatement () ; int pushIfStatement () ; - int pushFunctionCall ( const char *c ) ; - int pushAssignmentStatement ( const char *c ) ; + int pushFunctionCall ( const char *s ) ; + int pushAssignmentStatement ( const char *s ) ; int pushCompoundStatement () ; int pushStatement () ; int pushFunctionDeclaration ( const char *fn ) ; - int pushLocalVariableDeclaration () ; - int pushGlobalVariableDeclaration ( const char *fn ) ; + int pushLocalVariableDeclaration ( pslType t ) ; + int pushGlobalVariableDeclaration ( const char *fn, pslType t ) ; int pushStaticVariableDeclaration () ; int pushGlobalDeclaration () ; void pushProgram () ; - void print_opcode ( FILE *fd, unsigned char op ) const ; + int printOpcode ( FILE *fd, int addr ) const ; pslAddress getVarSymbol ( const char *s ) ; pslAddress setVarSymbol ( const char *s ) ; @@ -128,8 +139,8 @@ pslContext *context ; pslExtension *extensions ; - void fixup ( const char *s, pslAddress v ) ; - void addFwdRef ( const char *s, pslAddress where ) ; + void fixup ( const char *s, pslAddress v ) ; + void addFwdRef ( const char *s, pslAddress where ) ; void checkUnresolvedSymbols () ; void pushLocality () @@ -187,6 +198,8 @@ } pslExtension *getExtensions () const { return extensions ; } + + int printInstruction ( FILE *fd, int addr ) const ; void init () { Index: pslContext.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslContext.cxx,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- pslContext.cxx 6 Sep 2002 13:50:51 -0000 1.1 +++ pslContext.cxx 7 Sep 2002 07:00:35 -0000 1.2 @@ -28,15 +28,42 @@ { switch ( code [ pc ] ) { - case OPCODE_PUSH_CONSTANT : + case OPCODE_NOOP : + ulSetError ( UL_FATAL, "PSL: Suspicious opcode 0x00?!", code[pc] ) ; + pc++ ; + return PSL_PROGRAM_CONTINUE ; + + case OPCODE_PUSH_INT_CONSTANT : { [...344 lines suppressed...] + variable [ code[++pc] ] . setType ( PSL_STRING ) ; + pc++ ; + return PSL_PROGRAM_CONTINUE ; + default : - if ( ( code [ pc ] & 0xF0 ) == OPCODE_PUSH_VARIABLE ) - { - pushVariable ( variable [ code[pc] & 0x0F ] ) ; - pc++ ; - } - else - if ( ( code [ pc ] & 0xF0 ) == OPCODE_POP_VARIABLE ) - { - variable [ code[pc] & 0x0F ] = popVariable () ; - pc++ ; - } + ulSetError ( UL_FATAL, "PSL: Suspicious opcode 0x02x?!", code[pc] ) ; return PSL_PROGRAM_CONTINUE ; } } Index: pslContext.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslContext.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- pslContext.h 6 Sep 2002 18:36:21 -0000 1.2 +++ pslContext.h 7 Sep 2002 07:00:35 -0000 1.3 @@ -28,10 +28,10 @@ pslExtension *extensions ; pslProgram *program ; - pslVariable variable [ MAX_VARIABLE ] ; - pslVariable stack [ MAX_STACK ] ; - int sp ; - pslAddress pc ; + pslVariable variable [ MAX_VARIABLE ] ; + pslValue stack [ MAX_STACK ] ; + int sp ; + pslAddress pc ; public: @@ -40,25 +40,38 @@ code = p -> getCode () ; extensions = p -> getExtensions () ; program = p ; + reset () ; } ~pslContext () {} ; - void pushInt ( int x ) { stack [ sp++ ] . i = x ; } - void pushFloat ( float x ) { stack [ sp++ ] . f = x ; } - void pushVariable ( pslVariable x ) { stack [ sp++ ] = x ; } + void pushInt ( int x ) { stack [ sp++ ] . set ( x ) ; } + void pushFloat ( float x ) { stack [ sp++ ] . set ( x ) ; } + void pushString ( char *x ) { stack [ sp++ ] . set ( x ) ; } + void pushNumber ( pslNumber *x ) { stack [ sp++ ] . set ( x ) ; } - void popVoid () { --sp ; } - int popInt () { return stack [ --sp ] . i ; } - float popFloat () { return stack [ --sp ] . f ; } - pslVariable popVariable () { return stack [ --sp ] ; } + void popVoid () { --sp ; } + int popInt () { return stack [ --sp ] . getInt () ; } + float popFloat () { return stack [ --sp ] . getFloat () ; } + char *popString () { return stack [ --sp ] . getString () ; } - pslResult step () ; + void popNumber ( pslNumber *v ) { v -> set ( & stack [ --sp ] ) ; } + + pslResult step () ; + + pslResult trace () + { + program -> getCompiler () -> printInstruction ( stdout, pc ) ; + fflush ( stdout ) ; + return step () ; + } void reset () { - memset ( variable, 0, MAX_VARIABLE * sizeof ( pslVariable ) ) ; + for ( int i = 0 ; i < MAX_VARIABLE ; i++ ) + variable [ i ] . reset () ; + sp = 0 ; pc = 0 ; } Index: pslDump.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslDump.cxx,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- pslDump.cxx 6 Sep 2002 17:20:56 -0000 1.8 +++ pslDump.cxx 7 Sep 2002 07:00:35 -0000 1.9 @@ -29,50 +29,83 @@ { const char *s ; unsigned char opcode ; + int numArgBytes ; } ; static const OpcodeDecode opcodeDecode [] = { - { "PUSH_CONSTANT", OPCODE_PUSH_CONSTANT }, [...229 lines suppressed...] + fprintf ( fd, "\t\t\t%d,nargs=%d", code[addr+1] + ( code[addr+2] << 8 ), + code[addr+3] ) ; + break ; + + case OPCODE_JUMP_FALSE : + fprintf ( fd, "\t%d", code[addr+1] + ( code[addr+2] << 8 ) ) ; + break ; + + case OPCODE_JUMP : + fprintf ( fd, "\t\t\t%d", code[addr+1] + ( code[addr+2] << 8 ) ) ; + break ; + + default : break ; + } + + fprintf ( fd, "\n" ) ; + return skip ; } Index: pslExpression.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslExpression.cxx,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- pslExpression.cxx 6 Sep 2002 17:20:56 -0000 1.6 +++ pslExpression.cxx 7 Sep 2002 07:00:35 -0000 1.7 @@ -76,6 +76,12 @@ } } + if ( c [ 0 ] == '"' ) + { + pushStringConstant ( & c [ 1 ] ) ; + return TRUE ; + } + if ( isdigit ( c [ 0 ] ) || c [ 0 ] == '.' ) { pushConstant ( c ) ; @@ -113,7 +119,7 @@ pslGetToken ( c ) ; - if ( c [ 0 ] != '*' && c [ 0 ] != '/' ) + if ( c [ 0 ] != '*' && c [ 0 ] != '/' && c [ 0 ] != '%' ) { pslUngetToken ( c ) ; return TRUE ; @@ -125,7 +131,10 @@ if ( c [ 0 ] == '*' ) pushMultiply () ; else + if ( c [ 0 ] == '/' ) pushDivide () ; + else + pushModulo () ; } } Index: pslLocal.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslLocal.h,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- pslLocal.h 6 Sep 2002 17:20:56 -0000 1.11 +++ pslLocal.h 7 Sep 2002 07:00:36 -0000 1.12 @@ -40,42 +40,12 @@ #define MAX_ARGS 64 #define MAX_VARIABLE 256 #define MAX_LABEL 256 -#define MAX_TOKEN 256 +#define MAX_TOKEN 1024 #define MAX_CODE 512 #define MAX_STACK 256 #define MAX_NESTING 32 #define MAX_SYMBOL (MAX_VARIABLE + MAX_LABEL) -/* Code Opcodes */ - -/* Low nybble is variable address */ - -#define OPCODE_PUSH_VARIABLE 0xE0 -#define OPCODE_POP_VARIABLE 0xF0 - -/* Remaining opcodes must be in range 0x00 to 0xDF */ - -#define OPCODE_PUSH_CONSTANT 0x00 -#define OPCODE_CALL 0x01 -#define OPCODE_SUB 0x02 -#define OPCODE_ADD 0x03 -#define OPCODE_DIV 0x04 -#define OPCODE_MULT 0x05 -#define OPCODE_NEG 0x06 -#define OPCODE_LESS 0x07 -#define OPCODE_LESSEQUAL 0x08 -#define OPCODE_GREATER 0x09 -#define OPCODE_GREATEREQUAL 0x0A -#define OPCODE_NOTEQUAL 0x0B -#define OPCODE_EQUAL 0x0C -#define OPCODE_JUMP_FALSE 0x0D -#define OPCODE_JUMP 0x0E -#define OPCODE_POP 0x0F -#define OPCODE_HALT 0x10 -#define OPCODE_CALLEXT 0x11 -#define OPCODE_PAUSE 0x12 -#define OPCODE_RETURN 0x13 - /* Token Parser */ @@ -83,21 +53,14 @@ void pslGetToken ( char *c, FILE *fd = NULL ) ; void pslSetDefaultFile ( FILE *fd ) ; -/* - Address/Opcodes are: - - ???? ???? -- 8 bit opcode. - ???? xxxx -- 4 bit opcode with 4 bit variable address. - ???? ???? -- ditto - plus a float constant stored in the next 4 bytes. - ???? ???? -- ditto - plus a code address stored in the next 2 bytes. -*/ typedef unsigned short pslAddress ; extern int _pslInitialised ; +#include "pslOpcodes.h" #include "pslSymbol.h" -#include "pslContext.h" #include "pslCompiler.h" +#include "pslContext.h" Index: pslProgram.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslProgram.cxx,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- pslProgram.cxx 6 Sep 2002 17:20:56 -0000 1.6 +++ pslProgram.cxx 7 Sep 2002 07:00:36 -0000 1.7 @@ -65,6 +65,7 @@ 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 ) { Index: pslToken.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslToken.cxx,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- pslToken.cxx 6 Sep 2002 17:50:06 -0000 1.7 +++ pslToken.cxx 7 Sep 2002 07:00:36 -0000 1.8 @@ -104,6 +104,59 @@ int tp = 0 ; + if ( c == '"' ) + { + int isBkSlash = FALSE ; + + do + { + if ( c == '\\' ) + { + if ( isBkSlash ) + { + isBkSlash = FALSE ; + res [ tp++ ] = c ; + } + else + isBkSlash = TRUE ; + } + else + if ( isBkSlash ) + { + switch ( c ) + { + case '0' : res [ tp++ ] = '\0' ; break ; + case 'r' : res [ tp++ ] = '\r' ; break ; + case 'n' : res [ tp++ ] = '\n' ; break ; + case 'f' : res [ tp++ ] = '\f' ; break ; + case 'b' : res [ tp++ ] = '\b' ; break ; + case 'a' : res [ tp++ ] = '\a' ; break ; + default: res [ tp++ ] = c ; break ; + } + } + else + res [ tp++ ] = c ; + + c = getc ( fd ) ; + + if ( tp >= MAX_TOKEN - 1 ) + { + ulSetError ( UL_WARNING, + "PSL: Input string is bigger than %d characters!", + MAX_TOKEN - 1 ) ; + tp-- ; + } + } while ( ( isBkSlash || c != '"' ) && c != -1 ) ; + + if ( c == -1 ) + ulSetError ( UL_WARNING, + "PSL: Missing \\\" character" ) ; + + /* The trailing quotes character is not included into the string */ + res [ tp ] = '\0' ; + return ; + } + while ( isalnum ( c ) || c == '.' || c == '_' ) { res [ tp++ ] = c ; |