Exemple #1
0
int kbc()
{
  

  char ac[1024];

  
  /*
  sprintf(ac, "c:\\basic\\examples\\kbasic\\sub\\sub2.kbasic");
  sprintf(ac, "c:\\basic\\examples\\kbasic\\important\\type.kbasic");
  sprintf(ac, "c:\\basic\\examples\\kbasic\\examples\\array.kbasic");
  sprintf(ac, "c:\\basic\\examples\\kbasic\\keyword\\goto.kbasic");
  */

  bool bSilent = false;
  bool bDebug = false;
  bool bUseCache = false;   
  char *acLicense = "";
  char *acProjectName = "";
  char *acLogin = "";  
  char *acProjectType = "";
  char *acTranslation = "";
  char *acSQL = "";
  char *acMenu = "";
  
  sLogin = "";
  sProjectName = "";
  sProjectType = "";
  sTranslation = "";
  sSQL = "";
  sMenu = "";

  int i = 1;
  for (; i < MAIN_argc; i++){

    const char *ac = MAIN_argv[i];
   
    if (utility::my_stricmp(ac, "-Silent") == 0) bSilent = true;
    else if (utility::my_stricmp(ac, "-Debug") == 0) bDebug = true;
    else if (utility::my_stricmp(ac, "-UseCache") == 0) bUseCache = true;
    
    else if (utility::my_strnicmp(ac, "-License", 8) == 0){
      acLicense = (char *) ac + 8;
      if (acLicense == 0) acLicense = "";
      /*
    } else if (utility::my_strnicmp(ac, "-ProjectName", 12) == 0){
       

      acProjectName = (char *) ac + 12;
         //CONSOLE printError(ac);
       //  CONSOLE printError("\n");
      if (acProjectName == 0) sProjectName = ""; else sProjectName = acProjectName; 
*/
    } else if (utility::my_strnicmp(ac, "-Login", 6) == 0){
       
      acLogin = (char *) ac + 6;
      if (acLogin == 0) sLogin = ""; else sLogin = acLogin; 

    } else if (utility::my_strnicmp(ac, "-ProjectType", 12) == 0){
       

      acProjectType = (char *) ac + 12;
         //CONSOLE printError(ac);
       //  CONSOLE printError("\n");
      if (acProjectType == 0) sProjectType = ""; else sProjectType = acProjectType; 
        //  CONSOLE printError("2222222222\n");


    } else if (utility::my_strnicmp(ac, "-Translation", 12) == 0){


      acTranslation = (char *) ac + 12;
      if (acTranslation == 0) sTranslation = ""; else sTranslation = acTranslation; 
    
    } else if (utility::my_strnicmp(ac, "-SQL", 4) == 0){


      acSQL = (char *) ac + 4;
      if (acSQL == 0) sSQL = ""; else sSQL = acSQL; 
     // printf(acSQL);
    
    } else if (utility::my_strnicmp(ac, "-Menu", 5) == 0){

      acMenu = (char *) ac + 5;
      if (acMenu == 0) sMenu = ""; else sMenu = acMenu;      
                
    }
    else break;
  }

  /*
  bSilent = false;
  bDebug = false;
  bUseCache = true;   
  sProjectName = "KBasic Default Project";
  sProjectType = "With Forms MainWindow (you must have myMenuBar created)";
  sTranslation = "&Window;Help;C&ontents;A&bout;Cl&ose;Close &All;&Tile;&Cascade;Arrange &icons;Ne&xt;Pre&vious;";
  sSQL = "QPSQL;localhost;test;postgres;test";

*/

 // if (bUseCache) printf("!!!!!");


 // sprintf(ac, "");
  //if (MAIN_argc > 1 && i <= MAIN_argc) sprintf(ac, "%s", MAIN_argv[i]);
  
  //sprintf(ac, "C:/kbasic16/kbide/examples/kbasic/builtin/__class__.kbasic");

  //if (utility::my_stricmp(ac, "") == 0) return 0;

  //bUseCache = false;
  {
    sKBasicPath = MAIN_argv[0];  
    bool bDebug = false;
#ifdef WINDOWS // HIDE
    bDebug = sKBasicPath.contains("Debug"); // HIDE
#endif // HIDE

    sKBasicPath = sKBasicPath.replace("\\", "/");
#ifdef WINDOWS // HIDE
    sKBasicPath = sKBasicPath.replace("/Debug", "", false); // HIDE
    sKBasicPath = sKBasicPath.replace("/Release", "", false); // HIDE
    sKBasicPath = sKBasicPath.replace(".exe", "", false); // HIDE
#endif // HIDE

    if (bDebug){
      int n = sKBasicPath.findRev("/");
      if (n >= 0) sKBasicPath = sKBasicPath.left(n);

      sKBasicPath = sKBasicPath.replace("kbc", "kbide/ide/cache");
    } else {
      sKBasicPath = sKBasicPath.replace("kbc", "ide/cache");
    }
    
    if (sKBasicPath.isEmpty()) return 0;
  }

  // if (checkLicense(acLicense) == false) return 0;


//  checkSerialNo();

	textbuffer *myTextbuffer = new textbuffer(); // needed to truncate big input strings or zero terminate them
 
  cache *my_cache = new cache();
  token *myToken = new token();
	scanner *myScanner = new scanner(myToken, my_cache);

	char *acText;
  bool b = true;

  if (utility::readSourceFile(&acText, cachePath("project.name").ascii(), myTextbuffer)){
    acText = utility::eatUTF16DOM(acText);
    sProjectName = acText;
  }

//  if (b && (b = utility::readSourceFile(&acText, "c:/kbasic16/kbide/ide/cache/C__kbasic16_kbide_examples_kbasic_builtin___class__.kbasic.scanner", myTextbuffer))){}


  { // delete runtime.parser file
    XString m = cachePath("runtime.parser");
    //CONSOLE printError(cachePath("runtime.parser").ascii());
    XFile f(m.ascii());
    if (!f.open(Truncate)){
      CONSOLE printError("Could not create runtime parser file: ");
      CONSOLE printError(m.ascii());
      b = false;
    } else {
      f.close();
    }
  }

  XString m = cachePath("compiler.output");
  finfo.setName(m.ascii());  
  char acInfo[1024];

  //if (utility::my_stricmp(ac, "") == 0)
  if (b){


    {      
      if (!finfo.open(Truncate)){
        CONSOLE printError("Could not write compiler output file:");
        CONSOLE printError(m.ascii());
        b = false;
      } 
    }
    finfo.close();

    XString sFiles = "";

    if (b){ // read compiler.input file
      XString m = cachePath("compiler.input");
      XFile f(m.ascii());
      if (!f.open(ReadOnly)){
        CONSOLE printError("Could not read compiler input file:");
        CONSOLE printError(m.ascii());
        b = false;
      } else {

        int i = 0;
        char acBuffer[10240];
        int nSize = f.size();
        while (i < nSize && (i = f.readBlock(acBuffer, 10240)) > 0){  
          acBuffer[i] = 0;

          const char *ac = utility::eatUTF16DOM(acBuffer);
          sFiles += ac;
        }
        //sFiles = sFiles.appendNull();
        
        f.close();
      }
    }

    if (my_cache){
      my_cache->setMode(bSilent, bDebug); // important, setting right file name for caching

      // sort input files so that non-cached files are at the end, otherwise IDs of classes will collide
      if (bUseCache){

        #define MAX_INPUTFILES 512

        struct inputfile {
          XString sFilename;
          int nDate;
        };
        inputfile files[MAX_INPUTFILES];

        int nCounter = 0;
        
  
        int n = 0;
        int n0 = 0;

        bool bFirst = true;
        do {
          if (nCounter >= MAX_INPUTFILES){
            nCounter = 0;
            CONSOLE printError("too many input files to compile (> 512)");
            break;
          }

          n = sFiles.find("\n", n);
          XString s = sFiles.mid(n0, n >= 0 ? n - n0 : sFiles.length());
          if (n == -1 & bFirst == false) break;

          XFileInfo k(s.ascii());
          int r = k.lastModified();

          files[nCounter].sFilename = s;
          files[nCounter].nDate = r;
          nCounter++;
          
          n0 = n + 1;
          n++;

          if (bFirst) bFirst = false;

        } while (true);

        bool bRedo = false;
        do {
          bRedo = false;

          for (int i = 0; i < nCounter; i++){

            if (i + 1 < nCounter){
              if (files[i].nDate > files[i + 1].nDate){
                inputfile file = files[i + 1];
                files[i + 1] = files[i];
                files[i] = file;

                bRedo = true;
                break;
              }
            }

          }
        } while (bRedo);

        sFiles = "";
        for (int i = 0; i < nCounter; i++){
          sFiles = sFiles + files[i].sFilename + "\n";          
        }
      }
        
    }



    
    if (finfo.open(Truncate)){ sprintf(acInfo, "Scanning..."); finfo.writeBlock(acInfo, strlen(acInfo)); finfo.close(); }
    
    if (finfo.open(Truncate)){ sprintf(acInfo, "%s", cachePath("default.kbasic").ascii()); finfo.writeBlock(acInfo, strlen(acInfo)); finfo.close(); }

    if (b && (b = utility::readSourceFile(&acText, cachePath("default.kbasic").ascii(), myTextbuffer))){

      acText = utility::eatUTF16DOM(acText);
    }

    if (b && (b = myScanner->scan(acText, (char *) cachePath("default.kbasic").ascii(), bSilent, bDebug, false, bUseCache))){} // filename

    if (b){
      int n = 0;
      int n0 = 0;

      bool bFirst = true;
      do {
        n = sFiles.find("\n", n);
        XString s = sFiles.mid(n0, n >= 0 ? n - n0 : sFiles.length());
        if (n == -1 & bFirst == false) break;
        bFirst = false;
        n0 = n + 1;
        n++;
        
        const char *ac99 = s.ascii();

        if (b && (b = utility::readSourceFile(&acText, ac99, myTextbuffer))){
          acText = utility::eatUTF16DOM(acText);
        }


        XString sFile = ac99;

        if (sFile.endsWith(".kbasic_form")){
     
          XString sLanguage = "";
          XString sExt = getFilenameExtension(sFile);
          XString q = sFile.left(sFile.length() - sExt.length() - 1);

          if (q.count(".") > 1){
            sLanguage = getFilenameExtension(q);
            if (sLanguage.contains("/") || sLanguage.contains("\\")){
              sLanguage = "";
            }
          }
        
          if (sLanguage.length() == 0){
   //       if (!sTranslation.contains(sLanguage, false)){
            acText = prepareSourceCode(ac99, acText, sFile); 
          } else {
/*
            char ac[1111];
            sprintf(ac, "omit %s %s", sFile.ascii(), sLanguage.ascii());
            CONSOLE printError(ac);*/
            continue;
//            acText = "";
          }

        } else if (sFile.endsWith(".kbasic_menubar")){

          XString sLanguage = "";
          XString sExt = getFilenameExtension(sFile);
          XString q = sFile.left(sFile.length() - sExt.length() - 1);

          if (q.count(".") > 1){
            sLanguage = getFilenameExtension(q);
            if (sLanguage.contains("/") || sLanguage.contains("\\")){
              sLanguage = "";
            }
          }
        
          if (sLanguage.length() == 0){
   //       if (!sTranslation.contains(sLanguage, false)){
            acText = prepareSourceCode(ac99, acText, sFile); 
          } else {
            continue;
//            acText = "";
          }

        } else if (sFile.endsWith(".kbasic_toolbar")){

          XString sLanguage = "";
          XString sExt = getFilenameExtension(sFile);
          XString q = sFile.left(sFile.length() - sExt.length() - 1);

          if (q.count(".") > 1){
            sLanguage = getFilenameExtension(q);
            if (sLanguage.contains("/") || sLanguage.contains("\\")){
              sLanguage = "";
            }
          }
        
          if (sLanguage.length() == 0){
   //       if (!sTranslation.contains(sLanguage, false)){
            acText = prepareSourceCode(ac99, acText, sFile); 
          } else {

            continue;
//            acText = "";
          }

        } else if (sFile.endsWith(".kbasic_report")){

          XString sLanguage = "";
          XString sExt = getFilenameExtension(sFile);
          XString q = sFile.left(sFile.length() - sExt.length() - 1);

          if (q.count(".") > 1){
            sLanguage = getFilenameExtension(q);
            if (sLanguage.contains("/") || sLanguage.contains("\\")){
              sLanguage = "";
            }
          }
        
          if (sLanguage.length() == 0){
   //       if (!sTranslation.contains(sLanguage, false)){
            acText = prepareSourceCode(ac99, acText, sFile); 
          } else {
            continue;
            //acText = "";
          }

        } else if (sFile.endsWith(".kbasic_module")){
#ifdef WINDOWS // HIDE
  if (sFile.endsWith(".linux.kbasic_module")) continue; // HIDE
  if (sFile.endsWith(".mac.kbasic_module")) continue; // HIDE
#elif defined(MAC) // HIDE
  if (sFile.endsWith(".linux.kbasic_module")) continue; // HIDE
  if (sFile.endsWith(".windows.kbasic_module")) continue; // HIDE
#elif defined(LINUX) // HIDE
  if (sFile.endsWith(".windows.kbasic_module")) continue; // HIDE
  if (sFile.endsWith(".mac.kbasic_module")) continue; // HIDE
#endif // HIDE

          // UNHIDE if (sFile.endsWith(".windows.kbasic_module")) continue;
          // UNHIDE if (sFile.endsWith(".mac.kbasic_module")) continue;

          acText = prepareSourceCode(ac99, acText, sFile);
        } else if (sFile.endsWith(".kbasic_class")){
#ifdef WINDOWS // HIDE
  if (sFile.endsWith(".linux.kbasic_class")) continue; // HIDE
  if (sFile.endsWith(".mac.kbasic_class")) continue; // HIDE
#elif defined(MAC) // HIDE
  if (sFile.endsWith(".linux.kbasic_class")) continue; // HIDE
  if (sFile.endsWith(".windows.kbasic_class")) continue; // HIDE
#elif defined(LINUX) // HIDE
  if (sFile.endsWith(".windows.kbasic_class")) continue; // HIDE
  if (sFile.endsWith(".mac.kbasic_class")) continue; // HIDE
#endif // HIDE

          // UNHIDE if (sFile.endsWith(".windows.kbasic_class")) continue; // HIDE
          // UNHIDE if (sFile.endsWith(".mac.kbasic_class")) continue; // HIDE

          acText = prepareSourceCode(ac99, acText, sFile);
        } else if (sFile.endsWith(".kbasic")){
#ifdef WINDOWS // HIDE
  if (sFile.endsWith(".linux.kbasic")) continue; // HIDE
  if (sFile.endsWith(".mac.kbasic")) continue; // HIDE
#elif defined(MAC) // HIDE
  if (sFile.endsWith(".linux.kbasic")) continue; // HIDE
  if (sFile.endsWith(".windows.kbasic")) continue; // HIDE
#elif defined(LINUX) // HIDE
  if (sFile.endsWith(".windows.kbasic")) continue; // HIDE
  if (sFile.endsWith(".mac.kbasic")) continue; // HIDE
#endif // HIDE
  
  // UNHIDEif (sFile.endsWith(".windows.kbasic")) continue;
  // UNHIDEif (sFile.endsWith(".mac.kbasic")) continue;

        }
        if (finfo.open(Truncate)){ finfo.writeBlock((void *) ac99, strlen(ac99)); finfo.close(); }
        if (b && (b = myScanner->scanAdditional(acText, (char *) ac99, bSilent, bDebug, bUseCache))){} // filename
        if (!b) break;
      } while (true);
    }

    if (b) {
      if (finfo.open(Truncate)){ sprintf(acInfo, "%s", cachePath("default2.kbasic").ascii()); finfo.writeBlock(acInfo, strlen(acInfo)); finfo.close(); }

      if (b && (b = utility::readSourceFile(&acText, cachePath("default2.kbasic").ascii(), myTextbuffer))){

        acText = utility::eatUTF16DOM(acText);
      }
      if (b && (b = myScanner->scanAdditional(acText, (char *) cachePath("default2.kbasic").ascii(), bSilent, bDebug, bUseCache))){} // filename
    }

    myScanner->addEOFToken();
/*
  } else {
    if (b && (b = utility::readSourceFile(&acText, ac, myTextbuffer))){}
    if (b && (b = myScanner->scan(acText, ac, bSilent, bDebug, true, bUseCache))){} // filename*/
  }




  //delete myToken;

//	if (utility::readSourceFile(&acText, ac, myTextbuffer)){
//		if (myScanner->scanAdditional(acText, filename)){

  

  if (b){

    if (finfo.open(Truncate)){ sprintf(acInfo, "Parsing..."); finfo.writeBlock(acInfo, strlen(acInfo)); finfo.close(); }

		pcode *myPcode = new pcode("");
  //  myPcode->setInfoWindow(myInfoConsole);

		parser *myParser = new parser(myPcode, my_cache);
  //  myParser->setInfoWindow(myInfoConsole);

    
    //bool b = false;

    /*
         if (strcmp(acDir, "examples\\veryoldbasic\\") == 0 && (b = myParser->parseVeryOldBasic(myToken, bSilent, bDebug))){}
    else if (strcmp(acDir, "examples\\oldbasic\\") == 0 && (b = myParser->parseOldBasic(myToken, bSilent, bDebug))){}
    else if (strcmp(acDir, "examples\\kbasic\\") == 0 && (b = myParser->parseKBasic(myToken, bSilent, bDebug))){}
    else */
    //bUseCache = false;

  //  CONSOLE printError("myParser->parseKBasic(myToken, bSilent, bDebug, bUseCache);");
    b = myParser->parseKBasic(myToken, bSilent, bDebug, bUseCache);

		if (b){
  //    CONSOLE printError("b=true");

      XString m;// = path(sKBasicPath);
      m = cachePath("runtime.pcode");

      XFile f(m.ascii());
      if (!f.open(Truncate)){
        CONSOLE printError("Could not create runtime pcode file");
        b = false;
      } else {
        //const char *aa = myPcode->ba->getMem();
        int n = myPcode->ba->getSize();
        n = f.writeBlock (myPcode->ba->getMem(), n);

       
        f.close();
      }

    }


		delete myParser;
		delete myPcode;
   

	}	

  

  delete myScanner;
	delete myToken;
	delete myTextbuffer;
  delete my_cache;

	{

    XString m;
    m = cachePath("compiler.done");

    XFile f(m.ascii());
    if (!f.open(Truncate)){
      CONSOLE printError("Could not create runtime done file");
      b = false;
    } else {     
      f.close();
    }
  
  }

  return b ? 0 : -1;
		
}