Advertisement
Guest User

ScriptEngine.hpp

a guest
Jan 25th, 2012
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.30 KB | None | 0 0
  1.  
  2. #if !defined(__SCRIPT_H__)
  3. #define __SCRIPT_H__
  4.  
  5. #pragma warning (disable:4786)  //STL Warning抑止
  6. #pragma warning (disable:4018)  //signed と unsigned の数値を比較
  7. #pragma warning (disable:4244)  //double' から 'float' に変換
  8. #include<list>
  9. #include<string>
  10. #include<map>
  11.  
  12. //重複宣言チェックをしない
  13. //#define __SCRIPT_H__NO_CHECK_DUPLICATED
  14.  
  15. //-------- 汎用
  16. namespace gstd
  17. {
  18.     std::string to_mbcs(std::wstring const & s);
  19.     std::wstring to_wide(std::string const & s);
  20.  
  21.     template < typename T >
  22.     class lightweight_vector
  23.     {
  24.     public:
  25.         unsigned length;
  26.         unsigned capacity;
  27.         T * at;
  28.  
  29.         lightweight_vector() : length(0), capacity(0), at(NULL)
  30.         {
  31.         }
  32.  
  33.         lightweight_vector(lightweight_vector const & source);
  34.  
  35.         ~lightweight_vector()
  36.         {
  37.             if(at != NULL)
  38.                 delete[] at;
  39.         }
  40.  
  41.         lightweight_vector & operator = (lightweight_vector const & source);
  42.  
  43.         void expand();
  44.  
  45.         void push_back(T const & value)
  46.         {
  47.             if(length == capacity) expand();
  48.             at[length] = value;
  49.             ++length;
  50.         }
  51.  
  52.         void pop_back()
  53.         {
  54.             --length;
  55.         }
  56.  
  57.         void clear()
  58.         {
  59.             length = 0;
  60.         }
  61.  
  62.         void release()
  63.         {
  64.             length = 0;
  65.             if(at != NULL)
  66.             {
  67.                 delete[] at;
  68.                 at = NULL;
  69.                 capacity = 0;
  70.             }
  71.         }
  72.  
  73.         unsigned size() const
  74.         {
  75.             return length;
  76.         }
  77.  
  78.         T & operator[] (unsigned i)
  79.         {
  80.             return at[i];
  81.         }
  82.  
  83.         T const & operator[] (unsigned i) const
  84.         {
  85.             return at[i];
  86.         }
  87.  
  88.         T & back()
  89.         {
  90.             return at[length - 1];
  91.         }
  92.  
  93.         T * begin()
  94.         {
  95.             return & at[0];
  96.         }
  97.  
  98.         void erase(T * pos);
  99.         void insert(T * pos, T const & value);
  100.     };
  101.  
  102.     template < typename T >
  103.     lightweight_vector < T >::lightweight_vector(lightweight_vector const & source)
  104.     {
  105.         length = source.length;
  106.         capacity = source.capacity;
  107.         if(source.capacity > 0)
  108.         {
  109.             at = new T[source.capacity];
  110.             for(int i = length - 1; i >= 0; --i)
  111.                 at[i] = source.at[i];
  112.         }
  113.         else
  114.         {
  115.             at = NULL;
  116.         }
  117.     }
  118.  
  119.     template < typename T >
  120.     lightweight_vector < T > & lightweight_vector < T >::operator = (lightweight_vector < T > const & source)
  121.     {
  122.         if(at != NULL) delete[] at;
  123.         length = source.length;
  124.         capacity = source.capacity;
  125.         if(source.capacity > 0)
  126.         {
  127.             at = new T[source.capacity];
  128.             for(int i = length - 1; i >= 0; --i)
  129.                 at[i] = source.at[i];
  130.         }
  131.         else
  132.         {
  133.             at = NULL;
  134.         }
  135.         return * this;
  136.     }
  137.  
  138.     template < typename T >
  139.     void lightweight_vector < T >::expand()
  140.     {
  141.         if(capacity == 0)
  142.         {
  143.             //delete[] at;
  144.             capacity = 4;
  145.             at = new T[4];
  146.         }
  147.         else
  148.         {
  149.             capacity *= 2;
  150.             T * n = new T[capacity];
  151.             for(int i = length - 1; i >= 0; --i)
  152.                 n[i] = at[i];
  153.             delete[] at;
  154.             at = n;
  155.         }
  156.     }
  157.  
  158.     template < typename T >
  159.     void lightweight_vector < T >::erase(T * pos)
  160.     {
  161.         --length;
  162.         for(T * i = pos; i < at + length; ++i)
  163.         {
  164.             * i = * (i + 1);
  165.         }
  166.     }
  167.  
  168.     template < typename T >
  169.     void lightweight_vector < T >::insert(T * pos, T const & value)
  170.     {
  171.         if(length == capacity)
  172.         {
  173.             unsigned pos_index = pos - at;
  174.             expand();
  175.             pos = at + pos_index;
  176.         }
  177.         for(T * i = at + length; i > pos; --i)
  178.         {
  179.             * i = * (i - 1);
  180.         }
  181.         * pos = value;
  182.         ++length;
  183.     }
  184.  
  185.     //-------- from here// ここから
  186.  
  187.     class type_data
  188.     {
  189.     public:
  190.         enum type_kind
  191.         {
  192.             tk_real, tk_char, tk_boolean, tk_array
  193.         };
  194.  
  195.         type_data(type_kind k, type_data * t = NULL) : kind(k), element(t)
  196.         {
  197.         }
  198.  
  199.         type_data(type_data const & source) : kind(source.kind), element(source.element)
  200.         {
  201.         }
  202.  
  203.         //let the default constructor //デストラクタはデフォルトに任せる
  204.  
  205.         type_kind get_kind()
  206.         {
  207.             return kind;
  208.         }
  209.  
  210.         type_data * get_element()
  211.         {
  212.             return element;
  213.         }
  214.  
  215.     private:
  216.         type_kind kind;
  217.         type_data * element;
  218.     };
  219.  
  220.     class value
  221.     {
  222.     private:
  223.         struct body
  224.         {
  225.             int ref_count;
  226.             type_data * type;
  227.             lightweight_vector<value> array_value;
  228.  
  229.             union
  230.             {
  231.                 long double real_value;
  232.                 wchar_t char_value;
  233.                 bool boolean_value;
  234.             };
  235.         };
  236.  
  237.         mutable body * data;
  238.  
  239.  
  240.     public:
  241.         value() : data(NULL)
  242.         {
  243.         }
  244.  
  245.         value(type_data * t, long double v)
  246.         {
  247.             data = new body;
  248.             data->ref_count = 1;
  249.             data->type = t;
  250.             data->real_value = v;
  251.         }
  252.  
  253.         value(type_data * t, wchar_t v)
  254.         {
  255.             data = new body;
  256.             data->ref_count = 1;
  257.             data->type = t;
  258.             data->char_value = v;
  259.         }
  260.  
  261.         value(type_data * t, bool v)
  262.         {
  263.             data = new body;
  264.             data->ref_count = 1;
  265.             data->type = t;
  266.             data->boolean_value = v;
  267.         }
  268.  
  269.         value(type_data * t, std::wstring v)
  270.         {
  271.             data = new body();
  272.             data->ref_count = 1;
  273.             data->type = t;
  274.             for(unsigned i = 0; i < v.size(); ++i)
  275.                 data->array_value.push_back(value(t->get_element(), v[i]));
  276.         }
  277.  
  278.         value(value const & source)
  279.         {
  280.             data = source.data;
  281.             if(data != NULL)
  282.                 ++(data->ref_count);
  283.         }
  284.  
  285.         ~value()
  286.         {
  287.             if(data != NULL)
  288.             {
  289.                 --(data->ref_count);
  290.                 if(data->ref_count == 0)
  291.                     delete data;
  292.             }
  293.         }
  294.  
  295.         value & operator = (value const & source)
  296.         {
  297.             if(source.data != NULL)
  298.             {
  299.                 ++(source.data->ref_count);
  300.             }
  301.             if(data != NULL)
  302.             {
  303.                 --(data->ref_count);
  304.                 if(data->ref_count == 0)
  305.                     delete data;
  306.             }
  307.             data = source.data;
  308.             return * this;
  309.         }
  310.  
  311.         bool has_data() const
  312.         {
  313.             return data != NULL;
  314.         }
  315.  
  316.         void set(type_data * t, long double v)
  317.         {
  318.             unique();
  319.             data->type = t;
  320.             data->real_value = v;
  321.         }
  322.  
  323.         void set(type_data * t, bool v)
  324.         {
  325.             unique();
  326.             data->type = t;
  327.             data->boolean_value = v;
  328.         }
  329.  
  330.         void append(type_data * t, value const & x)
  331.         {
  332.             unique();
  333.             data->type = t;
  334.             data->array_value.push_back(x);
  335.         }
  336.  
  337.         void concatenate(value const & x)
  338.         {
  339.             unique();
  340.             unsigned l = data->array_value.length;
  341.             unsigned r = x.data->array_value.length;
  342.             unsigned t = l + r;
  343.             if(l == 0)
  344.                 data->type = x.data->type;
  345.             while(data->array_value.capacity < t)
  346.                 data->array_value.expand();
  347.             for(unsigned i = 0; i < r; ++i)
  348.                 data->array_value[l + i] = x.data->array_value.at[i];
  349.             data->array_value.length = t;
  350.         }
  351.  
  352.         //the following return, not assign from struct: value
  353.         long double as_real() const
  354.         {
  355.             if(data == NULL)
  356.                 return 0.0L;
  357.             else
  358.             {
  359.                 switch(data->type->get_kind())
  360.                 {
  361.                     case type_data::tk_real:
  362.                         return data->real_value;
  363.                     case type_data::tk_char:
  364.                         return static_cast < long double > (data->char_value);
  365.                     case type_data::tk_boolean:
  366.                         return (data->boolean_value) ? 1.0L : 0.0L;
  367.                     case type_data::tk_array:
  368.                         if(data->type->get_element()->get_kind() == type_data::tk_char)
  369.                             return std::atof(to_mbcs(as_string()).c_str());
  370.                         else
  371.                             return 0.0L;
  372.                     default:
  373.                         return 0.0L;
  374.                 }
  375.             }
  376.         }
  377.         wchar_t as_char() const
  378.         {
  379.             if(data == NULL)
  380.                 return 0.0L;
  381.             else
  382.             {
  383.                 switch(data->type->get_kind())
  384.                 {
  385.                     case type_data::tk_real:
  386.                         return data->real_value;
  387.                     case type_data::tk_char:
  388.                         return data->char_value;
  389.                     case type_data::tk_boolean:
  390.                         return (data->boolean_value) ? L'1' : L'0';
  391.                     case type_data::tk_array:
  392.                         return L'\0';
  393.                     default:
  394.                         return L'\0';
  395.                 }
  396.             }
  397.         }
  398.         bool as_boolean() const
  399.         {
  400.             if(data == NULL)
  401.                 return false;
  402.             else
  403.             {
  404.                 switch(data->type->get_kind())
  405.                 {
  406.                     case type_data::tk_real:
  407.                         return data->real_value != 0.0L;
  408.                     case type_data::tk_char:
  409.                         return data->char_value != L'\0';
  410.                     case type_data::tk_boolean:
  411.                         return data->boolean_value;
  412.                     case type_data::tk_array:
  413.                         return data->array_value.size() != 0;
  414.                     default:
  415.                         return false;
  416.                 }
  417.             }
  418.         }
  419.         std::wstring as_string() const
  420.         {
  421.             if(data == NULL)
  422.                 return L"(VOID)";
  423.  
  424.             else
  425.             {
  426.                 switch(data->type->get_kind())
  427.                 {
  428.                     case type_data::tk_real:
  429.                         {
  430.                             wchar_t buffer[128];
  431.                             std::swprintf(buffer, L"%Lf", data->real_value);
  432.                             return std::wstring(buffer);
  433.                         }
  434.  
  435.                     case type_data::tk_char:
  436.                         {
  437.                             std::wstring result;
  438.                             result += data->char_value;
  439.                             return result;
  440.                         }
  441.  
  442.                     case type_data::tk_boolean:
  443.                         return (data->boolean_value) ? L"true" : L"false";
  444.  
  445.                     case type_data::tk_array:
  446.                         {
  447.                             if(data->type->get_element()->get_kind() == type_data::tk_char)
  448.                             {
  449.                                 std::wstring result;
  450.                                 for(unsigned i = 0; i < data->array_value.size(); ++i)
  451.                                     result += data->array_value[i].as_char();
  452.                                 return result;
  453.                             }
  454.                             else
  455.                             {
  456.                                 std::wstring result = L"[";
  457.                                 for(unsigned i = 0; i < data->array_value.size(); ++i)
  458.                                 {
  459.                                     result += data->array_value[i].as_string();
  460.                                     if(i != data->array_value.size() - 1)
  461.                                         result += L",";
  462.                                 }
  463.                                 result += L"]";
  464.                                 return result;
  465.                             }
  466.                         }
  467.                     default:
  468.                         return L"(INTERNAL-ERROR)";
  469.                 }
  470.             }
  471.         }
  472.         unsigned length_as_array() const
  473.         {
  474.             return data->array_value.size();
  475.         }
  476.         value const & index_as_array(unsigned i) const
  477.         {
  478.             return data->array_value[i];
  479.         }
  480.         value & index_as_array(unsigned i)
  481.         {
  482.             return data->array_value[i];
  483.         }
  484.         type_data * get_type() const
  485.         {
  486.  
  487.             return data->type;
  488.         }
  489.  
  490.         void unique() const
  491.         {
  492.             if(data == NULL)
  493.             {
  494.                 data = new body;
  495.                 data->ref_count = 1;
  496.                 data->type = NULL;
  497.             }
  498.             else if(data->ref_count > 1)
  499.             {
  500.                 --(data->ref_count);
  501.                 data = new body(* data);
  502.                 data->ref_count = 1;
  503.             }
  504.         }
  505.  
  506.         //danger! called from the outside
  507.         void overwrite(value const & source)
  508.         {
  509.  
  510.             * data = * source.data;
  511.             ++(data->ref_count);
  512.         }
  513.  
  514.     };
  515.  
  516.     class script_machine;
  517.  
  518.     typedef value (*callback)(script_machine * machine, int argc, value const * argv); //pointer to a function, returns value
  519.  
  520.     struct function
  521.     {
  522.         char const * name;
  523.         callback func;
  524.         unsigned arguments;
  525.     };
  526.  
  527.     class script_type_manager
  528.     {
  529.     private:
  530.  
  531.         std::list <type_data> types;    //doubly linked list // 中身のポインタを使うのでアドレスが変わらないようにlist
  532.         type_data * real_type;
  533.         type_data * char_type;
  534.         type_data * boolean_type;
  535.         type_data * string_type;
  536.     public:
  537.         script_type_manager()
  538.         {
  539.             real_type = & * types.insert(types.end(), type_data(type_data::tk_real));
  540.             char_type = & * types.insert(types.end(), type_data(type_data::tk_char));
  541.             boolean_type = & * types.insert(types.end(), type_data(type_data::tk_boolean));
  542.             string_type = & * types.insert(types.end(), type_data(type_data::tk_array, char_type));
  543.         }
  544.  
  545.         type_data * get_real_type()
  546.         {
  547.             return real_type;
  548.         }
  549.  
  550.         type_data * get_char_type()
  551.         {
  552.             return char_type;
  553.         }
  554.  
  555.         type_data * get_boolean_type()
  556.         {
  557.             return boolean_type;
  558.         }
  559.  
  560.         type_data * get_string_type()
  561.         {
  562.             return string_type;
  563.         }
  564.  
  565.         type_data * get_array_type(type_data * element)
  566.         {
  567.             for(std::list < type_data >::iterator i = types.begin(); i != types.end(); ++i)
  568.             {
  569.                 if(i->get_kind() == type_data::tk_array && i->get_element() == element)
  570.                 {
  571.                     return & * i;
  572.                 }
  573.             }
  574.             return & * types.insert(types.end(), type_data(type_data::tk_array, element));
  575.         }
  576.  
  577.     };
  578.  
  579.     class script_engine
  580.     {
  581.     public:
  582.  
  583.         //error
  584.         bool error;
  585.         std::string error_message;
  586.         int error_line;
  587.        
  588.         // intermediate code //中間コード
  589.         enum command_kind //parser tokens
  590.         {
  591.             pc_assign, pc_assign_writable, pc_break_loop, pc_break_routine, pc_call, pc_call_and_push_result, pc_case_begin,
  592.             pc_case_end, pc_case_if, pc_case_if_not, pc_case_next, pc_compare_e, pc_compare_g, pc_compare_ge, pc_compare_l,
  593.             pc_compare_le, pc_compare_ne, pc_dup, pc_dup2, pc_loop_ascent, pc_loop_back, pc_loop_count, pc_loop_descent,
  594.             pc_loop_if, pc_pop, pc_push_value, pc_push_variable, pc_push_variable_writable, pc_swap, pc_yield
  595.         };
  596.        
  597.         struct block;
  598.        
  599.         struct code
  600.         {
  601.             command_kind command;
  602.             int line;   //!!line in the source code
  603.             value data; //!!data used to push in pc_push_value
  604.        
  605.             union
  606.             {
  607.                 struct
  608.                 {
  609.                     int level;  //environment for a variable in assign/push_variable                    //assign/push_variableの変数の環境位置
  610.                     unsigned variable;  //index of the variable assign/push_variable                    //assign/push_variableの変数のインデックス
  611.                 };
  612.                 struct
  613.                 {
  614.                     block * sub;    //jump in call / call_and_push_result                               //call/call_and_push_resultの飛び先
  615.                     unsigned arguments; //the number of arguments in call/call_and_push_result          //call/call_and_push_resultの引数の数
  616.                 };
  617.                 struct
  618.                 {
  619.                     int ip; //loop_back return destination                                               //loop_backの戻り先
  620.                 };
  621.             };
  622.        
  623.             code()
  624.             {
  625.             }
  626.        
  627.             code(int the_line, command_kind the_command) : line(the_line), command(the_command)
  628.             {
  629.             }
  630.        
  631.             code(int the_line, command_kind the_command, int the_level, unsigned the_variable) : line(the_line), command(the_command), level(the_level), variable(the_variable)
  632.             {
  633.             }
  634.        
  635.             code(int the_line, command_kind the_command, block * the_sub, int the_arguments) : line(the_line), command(the_command), sub(the_sub),
  636.             arguments(the_arguments)
  637.             {
  638.             }
  639.        
  640.             code(int the_line, command_kind the_command, int the_ip) : line(the_line), command(the_command), ip(the_ip)
  641.             {
  642.             }
  643.        
  644.             code(int the_line, command_kind the_command, value const & the_data) : line(the_line), command(the_command), data(the_data)
  645.             {
  646.             }
  647.         };
  648.        
  649.         enum block_kind
  650.         {
  651.             bk_normal, bk_loop, bk_sub, bk_function, bk_microthread
  652.         };
  653.        
  654.         struct block //int level, arguments, string name, callback func,
  655.         {
  656.             int level;
  657.             int arguments;
  658.             std::string name;
  659.             callback func;
  660.             lightweight_vector<code> codes;
  661.             block_kind kind;
  662.        
  663.             block(int the_level, block_kind the_kind) : level(the_level), arguments(0), name(), func(NULL), codes(), kind(the_kind)
  664.             {
  665.             }
  666.         };
  667.        
  668.         std::list <block> blocks;   //中身のポインタを使うのでアドレスが変わらないようにlist //doubly linked list
  669.         block * main_block;
  670.         std::map < std::string, block * > events;  //events are those like @Initialize and @MainLoop
  671.        
  672.         block * new_block(int level, block_kind kind)
  673.         {
  674.             return & *blocks.insert(blocks.end(), (block(level,kind))); //dereference blocks.insert return the address (returns the address of newly
  675.             //created block x(level,kind)
  676.         }
  677.  
  678.         bool get_error()
  679.         {
  680.             return error;
  681.         }
  682.  
  683.         std::string & get_error_message()
  684.         {
  685.             return error_message;
  686.         }
  687.  
  688.         int get_error_line()
  689.         {
  690.             return error_line;
  691.         }
  692.  
  693.         //compatibility and type
  694.  
  695.         script_type_manager * type_manager;
  696.  
  697.         script_type_manager * get_type_manager()
  698.         {
  699.             return type_manager;
  700.         }
  701.  
  702.         type_data * get_real_type()
  703.         {
  704.             return type_manager->get_real_type();
  705.         }
  706.  
  707.         type_data * get_char_type()
  708.         {
  709.             return type_manager->get_char_type();
  710.         }
  711.  
  712.         type_data * get_boolean_type()
  713.         {
  714.             return type_manager->get_boolean_type();
  715.         }
  716.  
  717.         type_data * get_array_type(type_data * element)
  718.         {
  719.             return type_manager->get_array_type(element);
  720.         }
  721.  
  722.         type_data * get_string_type()
  723.         {
  724.             return type_manager->get_string_type();
  725.         }
  726.  
  727.         void * data;    // space for the client //クライアント用空間 //not really needed. DirectX perhaps?
  728.  
  729.         script_engine(script_type_manager * a_type_manager, std::string const & source, int funcc, function const * funcv);
  730.  
  731.         ~script_engine()
  732.         {
  733.             blocks.clear();
  734.         }
  735.  
  736.     };
  737.  
  738.     class script_machine
  739.     {
  740.     private:
  741.         script_machine();
  742.         script_machine(script_machine const & source);
  743.         script_machine & operator = (script_machine const & source);
  744.  
  745.         script_engine * engine;
  746.  
  747.         bool error;
  748.         std::string error_message;
  749.         int error_line;
  750.  
  751.         typedef lightweight_vector < value > variables_t;
  752.         typedef lightweight_vector < value > stack_t;
  753.  
  754.         struct environment
  755.         {
  756.             environment * pred, * succ;
  757.             environment * parent;
  758.             int ref_count;
  759.             script_engine::block * sub;
  760.             unsigned ip;
  761.             variables_t variables; //vector of type value
  762.             stack_t stack; //vector of type value
  763.             bool has_result;
  764.         };
  765.  
  766.         environment * first_using_environment;
  767.         environment * last_using_environment;
  768.         environment * first_garbage_environment;
  769.         environment * last_garbage_environment;
  770.         environment * new_environment(environment * parent, script_engine::block * b);
  771.         void dispose_environment(environment * object);
  772.  
  773.         lightweight_vector < environment * > threads; //a vector of pointers to environments, called threads
  774.         unsigned current_thread_index;
  775.         bool finished;
  776.         bool stopped;
  777.         bool resuming;
  778.  
  779.         void yield()
  780.         {
  781.             if(current_thread_index > 0)
  782.                 --current_thread_index;
  783.             else
  784.                 current_thread_index = threads.size() - 1;
  785.         }
  786.  
  787.         void advance();
  788.  
  789.  
  790.     public:
  791.         script_machine(script_engine * the_engine);
  792.         virtual ~script_machine();
  793.  
  794.         void run();
  795.         void call(std::string event_name);
  796.         void resume();
  797.  
  798.         void stop()
  799.         {
  800.             finished = true;
  801.             stopped = true;
  802.         }
  803.  
  804.         bool get_stopped()
  805.         {
  806.             return stopped;
  807.         }
  808.  
  809.         bool get_resuming()
  810.         {
  811.             return resuming;
  812.         }
  813.  
  814.         bool get_error()
  815.         {
  816.             return error;
  817.         }
  818.  
  819.         std::string & get_error_message()
  820.         {
  821.             return error_message;
  822.         }
  823.  
  824.         int get_error_line()
  825.         {
  826.             return error_line;
  827.         }
  828.  
  829.         void raise_error(std::string const & message)
  830.         {
  831.             error = true;
  832.             error_message = message;
  833.             finished = true;
  834.         }
  835.  
  836.         script_engine * get_engine()
  837.         {
  838.             return engine;
  839.         }
  840.  
  841.         bool has_event(std::string event_name);
  842.  
  843.         int get_current_line();
  844.  
  845.  
  846.     };
  847. }
  848.  
  849. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement