]> Creatis software - bbtk.git/blobdiff - kernel/appli/bbs2cpp/bbs2.cxx
#3499 Export to JavaScript
[bbtk.git] / kernel / appli / bbs2cpp / bbs2.cxx
index c193d24bf6c8103749b99d678feef37da3e2fc3d..e0b303c66f86faafa706245c1c1810a94aadd4cd 100644 (file)
 
 #include <stdio.h>
 
-#include "bbtkInterpreter.h"
-#include "bbtkInterpreterPy.h"
 #include "bbtkUtilities.h"
+#include "bbtkInterpreter.h"
+#include "bbtkInterpreterJavaScript.h"
+#include "bbtkInterpreterPython.h"
 
 int main(int argc, char* argv[])
 {  
     if (argc<3)
     {
-        printf("bbs2 <filename.bbs> <cxx|py>\n");
+        printf("bbs2 <filename.bbs> <cxx|py|js>\n");
         return 1;
     }
-    std::string file,path;
+    std::string fileBase,file,path;
     std::string extention( argv[2] );
-    file = bbtk::Utilities::ExtractScriptName(argv[1],path);
-    file += std::string(".")+extention;
+    fileBase = bbtk::Utilities::ExtractScriptName(argv[1],path);
+    file = fileBase + std::string(".") + extention;
 
     if ( extention.compare("cxx")==0)
     {
@@ -78,28 +79,56 @@ int main(int argc, char* argv[])
     if ( extention.compare("py")==0)
     {
         printf("bbs -> py\n");
-         bbtk::InterpreterPy::Pointer I = bbtk::InterpreterPy::New();
+         bbtk::InterpreterPython::Pointer I = bbtk::InterpreterPython::New();
          I->SetThrow(true);
          try
          {
              I->InterpretFile(argv[1]);
-             std::vector<std::string> pythonBBTK=  ((bbtk::InterpreterPy*)(I.get()))->pythonBBTK  ;
+             std::vector<std::string> pythonBBTK=  ((bbtk::InterpreterPython*)(I.get()))->pythonBBTK  ;
              FILE *ff=fopen(file.c_str(),"w+");
              fprintf(ff,"from bbtk.bbtkBlackBox import *\n" );
-             fprintf(ff,"class appliTest02:\n" );
+             fprintf(ff,"class %s:\n",fileBase.c_str() );
              fprintf(ff,"    def Run(self):\n" );
-             fprintf(ff,"        mCB  = ComplexBlackBox('appliTest02')\n");
+             fprintf(ff,"        mCBpy  =  ComplexBlackBox('%s')\n",fileBase.c_str());
              int i,size=pythonBBTK.size();
              for (i=0; i<size;i++)
              {
-                 fprintf(ff,"        mCB.%s\n", pythonBBTK[i].c_str() );
+                 fprintf(ff,"        mCBpy.%s\n", pythonBBTK[i].c_str() );
              }// for i
-             fprintf(ff,"        mCB.Execute()\n" );
+             fprintf(ff,"        mCBpy.Execute()\n" );
              fclose(ff);
          } catch (bbtk::Exception e) {
              e.Print();
          } // try
-    } // py
+    } // if py
+
+    if ( extention.compare("js")==0)
+    {
+               printf("bbs -> js\n");
+        bbtk::InterpreterJavaScript::Pointer I = bbtk::InterpreterJavaScript::New();
+        I->SetThrow(true);
+        try
+        {
+               I->InterpretFile(argv[1]);
+            std::vector<std::string> javascriptBBTK=  ((bbtk::InterpreterJavaScript*)(I.get()))->javascriptBBTK  ;
+            FILE *ff=fopen(file.c_str(),"w+");
+            fprintf(ff,"import * as bbtk from './bbtk.js' \n" );
+            fprintf(ff,"export  class %s{\n",fileBase.c_str() );
+            fprintf(ff,"    Run(){\n" );
+            fprintf(ff,"        let mCBjs  = new bbtk.ComplexBlackBox('%s')\n",fileBase.c_str());
+            int i,size=javascriptBBTK.size();
+            for (i=0; i<size;i++)
+            {
+                fprintf(ff,"        mCBjs.%s\n", javascriptBBTK[i].c_str() );
+            }// for i
+            fprintf(ff,"        mCBjs.Execute()\n" );
+            fprintf(ff,"    } \n" );
+            fprintf(ff,"} \n" );
+            fclose(ff);
+        } catch (bbtk::Exception e) {
+            e.Print();
+        } // try
+   } // if js
     
   return 0;
 }