Cosa fa: Questo interprete di linguaggio permette di eseguire un programma scritto in Basic (con un set ridotto di istruzioni). Utilizzandolo in altre applicazioni, permette di sviluppare software con le fuzionalità analoghe a quelli che implementano gli script, macro o VBA Visual Basic for Application.
Il materiale accessibile da queste pagine fanno parte di una serie di progetti per i quali non è previsto ulteriore sviluppo, ma mantengono la propria validità per studio o base per implementarle in altri progetti. Il loro impiego in altri software e' libero. La loro riproduzione su siti e/o stampa deve avvenire nel rispetto della normativa del diritto d'autore. Per qualunque domanda, mandare una email utilizzando la sezione Contatti di questo sito.
Modulo:
Listato/Codice sorgente: // ---------------------------------------------------------------- #include <string.h> #include <stdlib.h> #include <iostream.h> #include <fstream.h> #include <ctype.h> #include <assert.h> #include <basstr.h> #include "ultypdef.hpp" #include "ultoken.hpp" #include "ulexetkn.hpp" #include "ulparser.hpp" #include "ulscan.hpp" #include "ulcompil.hpp" #include "uljumpb.hpp" ///////////////////////////////////////////////////////////////////////// enum { ERR_COMP_NOMEMORY = 1 , ERR_SCAN_NOMEMORY , ERR_PARS_NOMEMORY , ERR_JUMPB_NOMEMORY } ; //////////////////////////////////////////////////////////////////// int ULCompiler::compile( PROGRAM * pgm , ifstream * ifs ) { int status = 1 ; UserLangToken * ulScanTkn ; assert( pgm != NULL ) ; assert( pgm->signature == Program_signature ) ; if ( (ulScanTkn = new UserLangToken) != NULL ) { // ----- scanning assert( ulScanTkn != NULL ) ; assert( ifs != NULL ) ; if ( scan( ulScanTkn , ifs ) == -1 ) { status = 0 ; goto uscita ; } // ------ parsing assert( ulScanTkn != NULL ) ; assert( pgm != NULL ) ; assert( pgm->signature == Program_signature ) ; if ( parse( pgm , ulScanTkn ) == -1 ) { status = 0 ; goto uscita ; } // ------ jumper assert( pgm != NULL ) ; if ( jumpBuilder( pgm ) == -1 ) { status = 0 ; goto uscita ; } // --- COMPILAZIONE OK ! } else { dspError( ERR_COMP_NOMEMORY ) ; status = 0 ; } uscita : return status ; } //////////////////////////////////////////////////////////////////// int ULCompiler::scan( UserLangToken * ulScanTkn , ifstream * ifs ) { UserLangScanner * ulScanner ; int status = 1 ; // allocazione scanner if ( (ulScanner = new UserLangScanner) != NULL ) { // esecuzione scanner status = ulScanner->scan( ulScanTkn , ifs ) ; // disallocazione scanner delete ulScanner ; } else { dspError( ERR_SCAN_NOMEMORY ) ; status = 0 ; } return status ; } //////////////////////////////////////////////////////////////////// int ULCompiler::parse( PROGRAM * pgm , UserLangToken * ulScanTkn ) { int status = 1 ; UserLangParser * ulParser ; // allocazione scanner if ( (ulParser = new UserLangParser) != NULL ) { // esecuzione scanner status = ulParser->parse( pgm , ulScanTkn ) ; // disallocazione scanner delete ulParser ; } else { dspError( ERR_PARS_NOMEMORY ) ; status = 0 ; } return status ; } //////////////////////////////////////////////////////////////////// int ULCompiler::jumpBuilder( PROGRAM * pgm ) { int status = 1 ; UserLangJumpBuilder * ULJumpB ; // allocazione classe JumpBuilder if ( (ULJumpB = new UserLangJumpBuilder) != NULL ) { // esecuzione scanner assert( ULJumpB != NULL ) ; status = ULJumpB->jumpBuilder( pgm->exeStatement ) ; // disallocazione classe JumpBuilder assert( ULJumpB != NULL ) ; delete ULJumpB ; } else { dspError( ERR_JUMPB_NOMEMORY ) ; status = 0 ; } return status ; } //////////////////////////////////////////////////////////////////// void ULCompiler::dspError( int errCode ) { char * d ; switch( errCode ) { case ( ERR_COMP_NOMEMORY ) : d = "Memoria insufficiente per allocare il compilatore" ; break ; case ( ERR_SCAN_NOMEMORY ) : d = "Memoria insufficiente per allocare lo scanner" ; break ; case ( ERR_PARS_NOMEMORY ) : d = "Memoria insufficiente per allocare il parser" ; break ; case ( ERR_JUMPB_NOMEMORY ) : d = "Memoria insufficiente per allocare il jumper" ; break ; default : d = "Errore senza spiegazione" ; } cout << "COMPILER ERROR:" << d << endl ; } ////////////////////////////////////////////////////////////////////
File header/include: //////////////////////////////////////////////////////////////////// #if !defined(ULCompiler_DEF) #define ULCompiler_DEF //////////////////////////////////////////////////////////////////// class ULCompiler { private : SCANTOKEN * scanTkn ; public : private : int scan( UserLangToken * , ifstream * ) ; int parse( PROGRAM * pgm , UserLangToken * ) ; int jumpBuilder( PROGRAM * pgm ) ; void dspError( int errCode ) ; public : compile( PROGRAM * , ifstream * ) ; } ; //////////////////////////////////////////////////////////////////// #endif // !defined(ULCompiler_DEF) ////////////////////////////////////////////////////////////////////