コード例 #1
0
int main(int argc, char *argv[])
{

  // Disable deprecation warning for getenv call.
#ifdef CHAISCRIPT_MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#endif

  const char *usepath = getenv("CHAI_USE_PATH");
  const char *modulepath = getenv("CHAI_MODULE_PATH");

#ifdef CHAISCRIPT_MSVC
#pragma warning(pop)
#endif

  std::vector<std::string> usepaths;
  usepaths.push_back("");
  if (usepath)
  {
    usepaths.push_back(usepath);
  }

  std::vector<std::string> modulepaths;
  std::vector<std::string> searchpaths = default_search_paths();
  modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
  modulepaths.push_back("");
  if (modulepath)
  {
    modulepaths.push_back(modulepath);
  }

  //chaiscript::ChaiScript chai(modulepaths, usepaths);
  chaiscript::ChaiScript chai(chaiscript::Std_Lib::library(), usepaths);

  chai.add(chaiscript::fun(&myexit), "exit");
  chai.add(chaiscript::fun(&myexit), "quit");
  chai.add(chaiscript::fun(&help), "help");
  chai.add(chaiscript::fun(&throws_exception), "throws_exception");
  chai.add(chaiscript::fun(&get_eval_error), "get_eval_error");

  chai.add(chaiscript::fun(&helloWorld), "helloWorld");

  clock_t begin = clock();

  for (int i = 0; i < 1000; i++)
  {
    std::string str = helloWorld("Bob12345");
    fwrite(str.c_str(), 1, str.size(), stdout);
  }

  clock_t end = clock();
  double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

  //begin = clock();

  ////for (int i = 0; i < 1000; i++)
  ////{
  //// chai.eval("puts(helloWorld(\"Bob12345\"));");
  ////}
  //chai.eval_file("E:\\C++\\ChaiScript - 5.4.0\\samples\forx.chai");

  //end = clock();
  //elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  //printf("**MyProgram::time= %lf\n", elapsed_secs);

  for (int i = 0; i < argc; ++i) {
    if (i == 0 && argc > 1) {
      ++i;
    }

    std::string arg(i ? argv[i] : "--interactive");

    enum {
      eInteractive
        , eCommand
        , eFile
    } mode = eCommand;

    if (arg == "-c" || arg == "--command") {
      if ((i + 1) >= argc) {
        std::cout << "insufficient input following " << arg << std::endl;
        return EXIT_FAILURE;
      }
      else {
        arg = argv[++i];
      }
    }
    else if (arg == "-" || arg == "--stdin") {
      arg = "";
      std::string line;
      while (std::getline(std::cin, line)) {
        arg += line + '\n';
      }
    }
    else if (arg == "-v" || arg == "--version") {
      arg = "version()";
    }
    else if (arg == "-h" || arg == "--help") {
      arg = "help(-1)";
    }
    else if (arg == "-i" || arg == "--interactive") {
      mode = eInteractive;
    }
    else if (arg.find('-') == 0) {
      std::cout << "unrecognised argument " << arg << std::endl;
      return EXIT_FAILURE;
    }
    else {
      mode = eFile;
    }

    chaiscript::Boxed_Value val;
    try {
      switch (mode) {
        case eInteractive: interactive(chai); break;
        case eCommand: val = chai.eval(arg); break;
        case eFile: {
                      begin = clock();

                      val = chai.eval_file(arg);

                      end = clock();
                      double elapsed_secs1 = double(end - begin) / CLOCKS_PER_SEC;
                      printf("**C++::time= %.10f\n", elapsed_secs);
                      printf("**ChaiScript::time= %.10f\n", elapsed_secs1);
                      break;
                    }

      }
    }
    catch (const chaiscript::exception::eval_error &ee) {
      std::cout << ee.pretty_print();
      std::cout << std::endl;
      return EXIT_FAILURE;
    }
    catch (std::exception &e) {
      std::cout << e.what() << std::endl;
      return EXIT_FAILURE;
    }
  }

  return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: mgee/ChaiScript
int main(int argc, char *argv[])
{

  // Disable deprecation warning for getenv call.
#ifdef CHAISCRIPT_MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#endif

  const char *usepath = getenv("CHAI_USE_PATH");
  const char *modulepath = getenv("CHAI_MODULE_PATH");

#ifdef CHAISCRIPT_MSVC
#pragma warning(pop)
#endif

  std::vector<std::string> usepaths;
  usepaths.push_back("");
  if (usepath)
  {
    usepaths.push_back(usepath);
  }

  std::vector<std::string> modulepaths;
  std::vector<std::string> searchpaths = default_search_paths();
  modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
  modulepaths.push_back("");
  if (modulepath)
  {
    modulepaths.push_back(modulepath);
  }

  chaiscript::ChaiScript chai(modulepaths,usepaths);

  chai.add(chaiscript::fun(&myexit), "exit");
  chai.add(chaiscript::fun(&myexit), "quit");
  chai.add(chaiscript::fun(&help), "help");
  chai.add(chaiscript::fun(&version), "version");
  chai.add(chaiscript::fun(&throws_exception), "throws_exception");
  chai.add(chaiscript::fun(&get_eval_error), "get_eval_error");

  for (int i = 0; i < argc; ++i) {
    if ( i == 0 && argc > 1 ) {
      ++i;
    }

    std::string arg( i ? argv[i] : "--interactive" );

    enum { eInteractive
         , eCommand
         , eFile
    } mode = eCommand ;

    if  ( arg == "-c" || arg == "--command" ) {
      if ( (i+1) >= argc ) {
        std::cout << "insufficient input following " << arg << '\n';
        return EXIT_FAILURE;
      } else {
        arg = argv[++i];
      }
    } else if ( arg == "-" || arg == "--stdin" ) {
      arg = "" ;
      std::string line;
      while ( std::getline(std::cin, line) ) {
        arg += line + '\n' ;
      }
    } else if ( arg == "-v" || arg == "--version" ) {
      arg = "version(0)" ;
    } else if ( arg == "-h" || arg == "--help" ) {
      arg = "help(-1)";
    } else if ( arg == "-i" || arg == "--interactive" ) {
      mode = eInteractive ;
    } else if ( arg.find('-') == 0 ) {
      std::cout << "unrecognised argument " << arg << '\n';
      return EXIT_FAILURE;
    } else {
      mode = eFile;
    }

    chaiscript::Boxed_Value val ;
    try {
      switch ( mode ) {
        case eInteractive : interactive(chai); break;
        case eCommand     : val = chai.eval(arg); break;
        case eFile        : val = chai.eval_file(arg); break;
        default           : std::cout << "Unrecognized execution mode\n"; return EXIT_FAILURE;
      }
    }
    catch (const chaiscript::exception::eval_error &ee) {
      std::cout << ee.pretty_print();
      std::cout << '\n';
      return EXIT_FAILURE;
    }
    catch (std::exception &e) {
      std::cout << e.what() << '\n';
      return EXIT_FAILURE;
    }
  }

  return EXIT_SUCCESS;
}