Advertisement
Guest User

Sample.cpp

a guest
Jan 25th, 2012
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include"ScriptEngine.hpp"
  2. #include<string>
  3. #include<iostream>
  4. #include<fstream>
  5. #include<sstream>
  6. #include<vector>
  7.  
  8. //----------------------------------------------------------------
  9. // function for sample script
  10. gstd::value func_print(gstd::script_machine* machine, int argc, gstd::value const * argv)
  11. {
  12.     std::wstring wstr = argv[0].as_string();
  13.     std::string str = gstd::to_mbcs(wstr);
  14.     std::cout << str << std::endl;
  15.     return gstd::value();
  16. }
  17.  
  18. gstd::value func_min(gstd::script_machine* machine, int argc, gstd::value const * argv)
  19. {
  20.     long double v1 = argv[0].as_real();
  21.     long double v2 = argv[1].as_real();
  22.     long double res = v1 <= v2 ? v1 : v2;
  23.     return gstd::value(machine->get_engine()->get_real_type(), res);
  24. }
  25.  
  26. gstd::value func_max(gstd::script_machine* machine, int argc, gstd::value const * argv)
  27. {
  28.     long double v1 = argv[0].as_real();
  29.     long double v2 = argv[1].as_real();
  30.     long double res = v1 >= v2 ? v1 : v2;
  31.     return gstd::value(machine->get_engine()->get_real_type(), res);
  32. }
  33.  
  34. gstd::value func_to_string(gstd::script_machine* machine, int argc, gstd::value const * argv)
  35. {
  36.     std::wstring res = argv[0].as_string();
  37.     return gstd::value(machine->get_engine()->get_string_type(), res);
  38. }
  39.  
  40. gstd::function const sampleScriptFunction[] =  
  41. {
  42.     {"print", func_print, 1},
  43.     {"min", func_min, 2},
  44.     {"max", func_max, 2},
  45.     {"to_string", func_to_string, 1},
  46. };
  47. //----------------------------------------------------------------
  48.  
  49. //----------------------------------------------------------------
  50. // main
  51. void RunSample();
  52. int main()
  53. {
  54.     try
  55.     {
  56.         RunSample();
  57.     }
  58.     catch(std::exception& e)
  59.     {
  60.         std::cout << e.what() << std::endl;
  61.     }
  62.  
  63.     return 0;
  64. }
  65.  
  66. void RunSample()
  67. {
  68.     //--------------------------------
  69.     //sample script source
  70.  
  71.     std::ifstream ifile("script.txt");
  72.     std::string source((std::istreambuf_iterator<char>(ifile)), std::istreambuf_iterator<char>());
  73.  
  74.     //--------------------------------
  75.     //error handle
  76.     struct ErrorHandle
  77.     {
  78.         static void CheckEngineError(gstd::script_engine& engine)
  79.         {
  80.             if(engine.get_error())
  81.             {
  82.                 int line = engine.get_error_line();
  83.                 std::ostringstream os;
  84.                 os << std::dec << line;
  85.                
  86.                 std::string error = "engine error:line=" + os.str() + " " + engine.get_error_message();
  87.                 throw std::exception(error.c_str());
  88.             }
  89.         }
  90.  
  91.         static void CheckMachineError(gstd::script_machine& machine)
  92.         {
  93.             if(machine.get_error())
  94.             {
  95.                 int line = machine.get_error_line();
  96.                 std::ostringstream os;
  97.                 os << std::dec << line;
  98.                
  99.                 std::string error = "machine error:line=" + os.str() + " " + machine.get_error_message();
  100.                 throw std::exception(error.c_str());
  101.             }
  102.         }
  103.     };
  104.  
  105.     //--------------------------------
  106.     //script function
  107.     std::vector<gstd::function> func;
  108.     int funcCount = sizeof(sampleScriptFunction)/sizeof(gstd::function);
  109.     func.resize(funcCount);
  110.     memcpy(&func[0], &sampleScriptFunction[0], sizeof(gstd::function) * funcCount);
  111.  
  112.     //--------------------------------
  113.     //create script engine
  114.     gstd::script_type_manager typeManager;
  115.     gstd::script_engine engine(&typeManager, source.c_str(), func.size(), &func[0]);
  116.     ErrorHandle::CheckEngineError(engine);
  117.  
  118.     //--------------------------------
  119.     //create script machine
  120.     gstd::script_machine machine(&engine);
  121.     machine.run();
  122.     ErrorHandle::CheckMachineError(machine);
  123.  
  124.     //--------------------------------
  125.     //call @Initialize
  126.     if(machine.has_event("Initialize"))
  127.     {
  128.         machine.call("Initialize");
  129.         ErrorHandle::CheckMachineError(machine);
  130.     }
  131.  
  132.     //--------------------------------
  133.     //call @MainLoop 7times
  134.     for(int iLoop = 0 ; iLoop < 7 ; iLoop++)
  135.     {
  136.         if(machine.has_event("MainLoop"))
  137.         {
  138.             machine.call("MainLoop");
  139.             ErrorHandle::CheckMachineError(machine);
  140.         }      
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement