int main( int argc, const char **argv ) { chaiscript::ChaiScript chai(chaiscript::Std_Lib::library()); if( argc > 1 ) { FILE *fp = fopen(argv[1], "rb"); if( fp ) { fseek(fp, 0L, SEEK_END); size_t size = ftell(fp); fseek(fp, 0L, SEEK_SET); char *buffer = (char *)malloc( size + 1 ); if( buffer ) { fread( buffer, size, 1, fp ); buffer[ size ] = '\0'; try { chai.eval(buffer); } catch(const chaiscript::exception::eval_error &e) { std::cout << e.pretty_print() << std::endl; } } free(buffer); fclose(fp); } } else { chai.add(chaiscript::fun(&helloWorld), "helloWorld"); chai.eval("puts(helloWorld(\"Bob\"));"); } }
int main(int argc, char** argv) { chaiscript::ChaiScript chai(chaiscript::Std_Lib::library()); chai.add(chaiscript::fun(&sumar),"sumar"); chai.eval("puts(sumar(4,5));"); return 0; }
int main() { chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); chai.add(chaiscript::fun(&do_something), "do_something"); return chai.eval<int>("do_something(101)") == 101 % 2?EXIT_SUCCESS:EXIT_FAILURE; }
int main() { int i, j, n; memset(p, 1, N); for(i = 2; i < N; i++) if(p[i]) for(j = i + i; j < N; j += i) p[j] = 0; while(scanf("%d", &n), n) printf("%d\n", chai(n)); return 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; }
int main(int argc, char *argv[]) { std::vector<std::string> usepaths; std::vector<std::string> modulepaths; // Disable deprecation warning for getenv call. #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable : 4996) #endif const char *usepath = getenv("CHAI_USE_PATH"); const char *modulepath = getenv("CHAI_MODULE_PATH"); #ifdef BOOST_MSVC #pragma warning(pop) #endif usepaths.push_back(""); if (usepath) { usepaths.push_back(usepath); } std::string searchpath = default_search_path(); modulepaths.push_back(searchpath); 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 << 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(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 << 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 : val = chai.eval_file(arg); break; default : std::cout << "Unrecognized execution mode" << std::endl; return EXIT_FAILURE; } } 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; }
int main() { // Disable deprecation warning for getenv call. #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable : 4996) #endif const char *usepath = getenv("CHAI_USE_PATH"); const char *modulepath = getenv("CHAI_MODULE_PATH"); #ifdef BOOST_MSVC #pragma warning(pop) #endif std::vector<std::string> usepaths; usepaths.push_back(""); if (usepath) { usepaths.push_back(usepath); } std::vector<std::string> modulepaths; modulepaths.push_back(""); if (modulepath) { modulepaths.push_back(modulepath); } chaiscript::ChaiScript chai(modulepaths,usepaths); boost::thread_group threads; // Ensure at least two, but say only 7 on an 8 core processor int num_threads = std::max<unsigned int>(boost::thread::hardware_concurrency() - 1, 2u); for (int i = 0; i < num_threads; ++i) { threads.create_thread(boost::bind(&do_work, boost::ref(chai), i)); } threads.join_all(); for (int i = 0; i < num_threads; ++i) { std::stringstream ss; ss << i; if (chai.eval<int>("getvalue(" + ss.str() + ")") != expected_value(4000)) { return EXIT_FAILURE; } if (chai.eval<int>("getid(" + ss.str() + ")") != i) { return EXIT_FAILURE; } } return EXIT_SUCCESS; }
bool Planner::add_workers(const gubg::file::File &workers_chaiscript) { MSS_BEGIN(bool, logns); MSS(!workers_chaiscript.empty(), std::cout << "ERROR: You have to specify the workers file, someone has to do it, you know..." << std::endl); L("Loading workers from " << workers_chaiscript); using Parser = chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>; chaiscript::ChaiScript_Basic chai(chaiscript::Std_Lib::library(), std::make_unique<Parser>()); chai.add(fun(&gubg::planning::Planning::addWorker, &planning), "add_worker"); chai.add(fun(&Planner::add_absence, this), "absence"); chai.add(fun(&Planner::set_nr_working_days, this), "set_nr_working_days"); { std::ostringstream error; try { chai.eval_file(workers_chaiscript.name()); } catch (const exception::file_not_found_error &) { error << "ERROR: Could not find workers file " << workers_chaiscript; } catch (const exception::eval_error &) { error << "ERROR: Could not load workers from " << workers_chaiscript; } const auto error_str = error.str(); MSS(error_str.empty(), std::cout << error_str << std::endl); } for (auto d: workDays(nr_working_days)) planning.addDay(d); for (const auto &p: absence_per_worker) for (auto d: p.second) planning.absence(p.first, d); MSS_END(); /* using namespace gubg::planning; */ /* planning.addWorker("geertf", 0.8); */ /* planning.addWorker("wimc", 0.8); */ /* planning.addWorker("hermanh", 0.8); */ /* planning.addWorker("emilev", 0.8); */ /* planning.addWorker("ismaild", 0.8); */ /* planning.addWorker("marnicr", 0.8); */ /* for (auto d: workDays(1000)) */ /* { */ /* planning.addDay(d); */ /* } */ #if 0 #ifdef PAST for (auto d: dayRange(Day(2013, 7, 4), Day(2013, 7, 18))) planning.absence("gfa", d); for (auto d: dayRange(Day(2013, 7, 22), Day(2013, 8, 2))) planning.absence("wba", d); planning.absence("wba", Day(2013, 8, 16)); planning.absence("wba", Day(2013, 11, 28)); planning.absence("wba", Day(2014, 1, 30)); #endif planning.absence("gfa", Day(2014, 3, 6)); planning.absence("gfa", Day(2014, 3, 7)); planning.absence("wba", Day(2014, 3, 7)); for (auto d: dayRange(Day(2014, 8, 4), Day(2014, 8, 14))) planning.absence("wba", d); { #ifdef PAST auto verlof = dayRange(Day(2013, 12, 25), Day(2014, 1, 1)); verlof.push_back(Day(2013, 8, 15)); verlof.push_back(Day(2013, 11, 1)); verlof.push_back(Day(2013, 11, 11)); #endif auto verlof = dayRange(Day(2014, 12, 25), Day(2015, 1, 1)); verlof.push_back(Day(2014, 5, 1)); verlof.push_back(Day(2014, 5, 2)); verlof.push_back(Day(2014, 5, 29)); verlof.push_back(Day(2014, 5, 30)); verlof.push_back(Day(2014, 6, 9)); verlof.push_back(Day(2014, 7, 21)); verlof.push_back(Day(2014, 8, 15)); verlof.push_back(Day(2014, 11, 11)); for (auto d: verlof) { planning.absence("gfa", d); planning.absence("wba", d); } } //Quality week for (auto d: dayRange(Day(2014, 7, 7), Day(2014, 7, 11))) { planning.absence("gfa", d); planning.absence("wba", d); } #endif }
int main() { // Disable deprecation warning for getenv call. #ifdef CHAISCRIPT_MSVC #ifdef max // Why Microsoft? why? #undef max #endif #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; modulepaths.push_back(""); if (modulepath) { modulepaths.push_back(modulepath); } chaiscript::ChaiScript chai(modulepaths,usepaths); std::vector<std::shared_ptr<std::thread> > threads; // Ensure at least two, but say only 7 on an 8 core processor int num_threads = std::max(std::thread::hardware_concurrency() - 1, 2u); for (int i = 0; i < num_threads; ++i) { threads.push_back(std::shared_ptr<std::thread>(new std::thread(do_work, std::ref(chai), i))); } for (int i = 0; i < num_threads; ++i) { threads[i]->join(); } for (int i = 0; i < num_threads; ++i) { std::stringstream ss; ss << i; if (chai.eval<int>("getvalue(" + ss.str() + ")") != expected_value(4000)) { return EXIT_FAILURE; } if (chai.eval<int>("getid(" + ss.str() + ")") != i) { return EXIT_FAILURE; } } return EXIT_SUCCESS; }