Advertisement
Guest User

MikelSV / MSL-FL - Step 6

a guest
Oct 17th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.33 KB | None | 0 0
  1. // My Server Language - Fast Line
  2. Versions msl_fl_version[]={
  3.     "0.0.0.6", "17.10.2013 23:13",
  4.     "0.0.0.5", "16.10.2013 05:06",
  5.     "0.0.0.4", "15.10.2013 17:24",
  6.     "0.0.0.3", "14.10.2013 16:22",
  7.     "0.0.0.2", "14.10.2013 01:08",
  8.     "0.0.0.1", "13.10.2013 21:05"  
  9. };
  10.  
  11.  
  12.  
  13. // Values
  14. class msl_value : public OMatrixT<msl_value>{
  15. public:
  16.     // prev, next, up first, up end
  17.     msl_value *_p, *_n;//, *_a, *_e;
  18.     MString key, val;
  19.  
  20.     msl_value(){ _p=0; _n=0; }
  21.  
  22.     msl_value* New(){
  23.         msl_value *p=new msl_value;
  24.         if(!p) return 0;
  25.  
  26.         OMAdd(p);
  27.         return p;
  28.     }
  29.  
  30.     msl_value* Find(VString key){
  31.         if(!this) return 0;
  32.        
  33.         for(msl_value*p=_a; p; p=p->_n){
  34.             if(p->key==key) return p;
  35.         }
  36.  
  37.         return 0;
  38.     }
  39.  
  40.     msl_value* Set(VString key, VString val){
  41.         msl_value *p=Find(key);
  42.         if(p){
  43.             p->val=val;
  44.         } else{
  45.             p=New();
  46.             if(p){
  47.                 p->key=key; p->val=val;
  48.             }
  49.         }
  50.         return p;
  51.     }
  52.  
  53.     VString Get(VString key){
  54.         msl_value *p=Find(key);
  55.         if(p)
  56.             return p->val;
  57.         else
  58.             return VString();
  59.     }
  60.  
  61.     msl_value* SGet(VString key){
  62.         msl_value *p=Find(key);
  63.         if(!p){
  64.             p=New();
  65.             if(p){
  66.                 p->key=key; p->val.sz=0xffffffff;
  67.             }
  68.         }
  69.         return p;
  70.     }
  71.  
  72.     void Del(VString key){
  73.         msl_value *p=Find(key);
  74.         if(p){
  75.             OMDel(p); delete p;
  76.         }
  77.         return ;
  78.     }
  79.  
  80.     void Copy(msl_value *val){
  81.         Clear();
  82.         this->val=val->val;
  83.        
  84.         for(msl_value*p=val->_a; p; p=p->_n){
  85.             Set(p->key, p->val)->Copy(p);          
  86.         }
  87.         return ;
  88.     }
  89.  
  90.     void Move(msl_value &val){
  91.         Clear();
  92.         _a=val._a; _e=val._e; val._a=0; val._e=0;
  93.         this->val-=val.val;    
  94.         return ;
  95.     }
  96.  
  97.     ~msl_value(){ Clear(); }
  98.     void Clear(){ OMClear(); }
  99.  
  100. };
  101.  
  102. // Function Arguments
  103. class msl_fl_farg{
  104. public:
  105.     msl_value val;
  106.     //VString val;
  107. };
  108.  
  109. class msl_fl_fargs{
  110.     MString _args; // memory buffer
  111.     msl_fl_farg *args; //
  112.     int asz, usz; // all sz & use sz
  113.  
  114.     int UpSize(){
  115.         // reserv memory
  116.         _args.Reserv(sizeof(msl_fl_farg)*(asz+16));
  117.         // if error
  118.         if(!_args){
  119.             asz=0; usz=0;
  120.             return 0;
  121.         }
  122.         // update
  123.         asz+=16;
  124.         args=(msl_fl_farg*)_args.data;
  125.  
  126.         return 1;
  127.     }
  128.  
  129. public:
  130.     // constructor & destructor
  131.     msl_fl_fargs(){ args=0; asz=0; usz=0; }
  132.     ~msl_fl_fargs(){ }
  133.  
  134.     // vals[id];
  135.     msl_fl_farg &operator[](const int i){
  136.         if(i>usz){ globalerror("msl_fl_fargs epic fail"); }
  137.         return args[i];
  138.     }
  139.  
  140.     // add value
  141.     msl_fl_farg* Add(msl_value &val){
  142.         if(usz>=asz){ if(!UpSize()) return 0; }
  143.         // add
  144.         args[usz].val.Move(val);
  145.         return &args[usz++];
  146.     }
  147.  
  148.     int Sz(){ return usz; }
  149.    
  150. };
  151.  
  152. //#include "omatrix-msl_value.h"
  153.  
  154. // Hints:
  155. // msl_value. Механизм балансировки возвращаемых значений.  Чтобы не копировать лишний раз структуру с данными.
  156.  
  157.  
  158. // spaces
  159. // Do() - all process operations
  160. // Set () - set
  161. // Get () - get
  162.  
  163. //#define MSL_DOOPT_ERROR 0
  164. //#define MSL_DOOPT_STOPIT (MSL_DOOPT_ERROR)
  165. #define MSLCBR_NULL 0
  166. #define MSLCBR_CONT 1
  167. #define MSLCBR_BREAK 2
  168. #define MSLCBR_RET  3
  169.  
  170. class msl_fl{
  171.     // result
  172.     HLString output;
  173.  
  174.     // options
  175.     int do_opt, do_opt_stopit, do_opt_ifw, do_opt_cbr, do_opt_active;
  176.  
  177.     // values
  178.     msl_value global;
  179.     msl_value _nullvalue;
  180.  
  181.  
  182.     public:
  183.  
  184.     // init
  185.     msl_fl(){ do_opt=0; do_opt_stopit=0; do_opt_cbr=0; }
  186.     ~msl_fl(){  }
  187.    
  188.     // process
  189.     void Do(VString code){
  190.         unsigned char *line=code, *pline=line, *to=code.endu();
  191.         // stop it & active
  192.         do_opt_stopit=0; do_opt_active=1; do_opt_ifw=0; do_opt_cbr=0;
  193.         // value;
  194.         msl_value outval;
  195.  
  196.         while(line<to && !do_opt_stopit){
  197.             if(*line=='<' && line+1<to && *(line+1)=='?'){
  198.                 // set result
  199.                 SetOutput(VString(pline, line-pline));
  200.                 // *line='<?...' skip this 2 sumbols
  201.                 line+=2;
  202.  
  203.                 //while(!do_opt_stopit){
  204.                     // do msl
  205.                 //  DoCode(line, to, outval, ';');
  206.                 //  if(line>=to || *line!=';') break;
  207.                 //  line++;
  208.                 //}
  209.                 DoCodeMulti(line, to, outval);
  210.  
  211.                 // if(line='?>')
  212.                 if(line+1>=to || *line!='?' || *(line+1)!='>'){
  213.                     // oh no
  214.                     SetError("No find '?>'");
  215.                     // exit
  216.                     return ;
  217.                 } else{
  218.                     // *line='?>...' skip this 2 sumbols
  219.                     line+=2; pline=line;
  220.                 }
  221.             }
  222.             line++;
  223.         }
  224.  
  225.         if(do_opt_cbr){
  226.             if(do_opt_cbr==MSLCBR_CONT) SetError("for/while for continue; not found");
  227.             if(do_opt_cbr==MSLCBR_BREAK) SetError("for/while for break; not found");
  228.             if(do_opt_cbr==MSLCBR_RET) do_opt_cbr=MSLCBR_NULL;
  229.         }
  230.  
  231.         // set result
  232.         SetOutput(VString(pline, line-pline));
  233.         return ;
  234.     }
  235.  
  236.     // do code; code; code
  237.     void DoCodeMulti(unsigned char*&line, unsigned char *to, msl_value &outval, unsigned char ecode=1){
  238.         while(!do_opt_stopit){
  239.             // do msl
  240.             DoCode(line, to, outval, ';', ecode);
  241.             if(line>=to || *line!=';') break;
  242.             line++;
  243.         }
  244.         return ;
  245.     }
  246.  
  247.     void DoCode(unsigned char*&line, unsigned char *to, msl_value &outval, unsigned char ecode=1, unsigned char ecodet=1){
  248.         msl_value *value=0;
  249.         unsigned char *pline=0;
  250.  
  251.  
  252.         while(line<to && !do_opt_stopit){
  253.  
  254.             // skip space
  255.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  256.  
  257.             // $value
  258.             if(*line=='$'){
  259.                 if(value){ SetError("double $value"); return ; }
  260.                 value=DoCodeValue(++line, to);
  261.                 //outval.val=value->val;
  262.                 continue;
  263.             }else
  264.             // function
  265.             if(*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_'){
  266.                
  267.                 DoCodeFunction(line, to);
  268.                 continue;
  269.             }
  270.             // ecode
  271.             else if(*line==ecode || *line==ecodet){
  272.                 if(value) outval.val=value->val;
  273.                 return ;
  274.             }
  275.             // string
  276.             else if(*line=='"' || *line=='\''){
  277.                 pline=++line;
  278.                 if(*line=='"')
  279.                     while(line<to && *line!='"') line++;
  280.                 else
  281.                     while(line<to && *line!='\'') line++;
  282.                 if(line>=to){ SetError("closed \" or ' not found"); }
  283.                 outval.val=VString(pline, line-pline);
  284.                 line++;
  285.                 continue;
  286.             }
  287.             // numbers
  288.             else if(*line>='0' && *line<='9'){
  289.                 pline=line;
  290.                 while(line<to && *line>='0' && *line<='9') line++;
  291.                 outval.val=VString(pline, line-pline);
  292.                 continue;              
  293.             }
  294.             // operator=
  295.             else if(*line=='='){
  296.                 if(!value){ SetError("need $value="); return ; }
  297.                
  298.                 msl_value val;
  299.                 DoCode(++line, to, val, ecode, ecodet);
  300.                
  301.                 // move
  302.                 value->Clear();
  303.                 value->val=val.val;
  304.                 value->_a=val._a; value->_e=val._e; val._a=0; val._e=0;
  305.                 // set outval
  306.                 outval.Copy(value);
  307.                 continue;          
  308.             }
  309.             // operator--
  310.             else if(*line=='-' && line+1<to && *(line+1)=='-'){
  311.                 if(!value){ SetError("need $value--"); return ; }
  312.                 // $val--
  313.                 value->val=itos(value->val.toi()-1);
  314.                 // set outval
  315.                 outval.Copy(value);
  316.                 line+=2;
  317.                 continue;
  318.             }
  319.             // '?>' end do code
  320.             else if(*line=='?' && line+1<to && *(line+1)=='>'){
  321.                 return ;
  322.             }
  323.             // what the sumbol?
  324.             else{              
  325.                 SetError(HLString()+"Unknown sumbol: '"+VString(line, 1)+"'.");
  326.                 return ;
  327.             }
  328.  
  329.             // skip space
  330.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  331.             line++;
  332.         }
  333.  
  334.         return ;
  335.     }
  336.  
  337.     msl_value* DoCodeValue(unsigned char*&line, unsigned char *to){
  338.         unsigned char *pline=line;
  339.  
  340.         while(line<to && *line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_') line++;
  341.         if(line>=to){
  342.             SetError(HLString()+"EOF.");
  343.             return 0;
  344.         }
  345.        
  346.         // Get existing or create new
  347.         msl_value *val;
  348.        
  349.         if(do_opt_active)
  350.             val=global.SGet(VString(pline, line-pline));
  351.         else val=&_nullvalue;
  352.  
  353.         // skip space
  354.         while(1){
  355.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  356.             if(line<to && *line=='['){
  357.                 msl_value dval;
  358.                 DoCode(++line, to, dval, ']');
  359.  
  360.                 // next []
  361.                 if(do_opt_active) val=val->SGet(dval.val); else val=&_nullvalue;
  362.                 line++;
  363.             }
  364.             else return val;
  365.         }
  366.  
  367.         return 0;
  368.     }
  369.  
  370.     void DoCodeFunction(unsigned char*&line, unsigned char *to){
  371.         VString name; unsigned char *pline=line; msl_fl_fargs args;
  372.         unsigned char *code, *ecode;
  373.  
  374.         while(line<to){
  375.             // normal name
  376.             if(*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_'){}
  377.             else{
  378.                 name.setu(pline, line-pline);
  379.                 while(line<to){
  380.                     if(*line=='('){
  381.                         line++;
  382.                         code=line;
  383.  
  384.                         if(name=="for"){
  385.                              DoCodeFunctionFor(line, to); return ;
  386.                         }
  387.  
  388.                         if(!DoCodeFunctionArgs(line, to, args)) return ;
  389.                         ecode=line; line++;
  390.                         // Exec function
  391.                         if(!DoCodeFunctionExec(name, args)) return ;
  392.                         //line++;
  393.  
  394.                         // if, while, for functions
  395.                         if(do_opt_ifw){
  396.                             if(name=="if")
  397.                                 DoCodeFunctionIf(line, to);
  398.                             else if(name=="while")
  399.                                 DoCodeFunctionWhile(line, to, code, ecode);
  400.                             //else if(name=="for")
  401.                                 //DoCodeFunctionFor(line, to);
  402.                         }
  403.  
  404.                         return ;
  405.                     }
  406.                     else if(!(*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')){
  407.                         if(*line==';'){
  408.                             if(name=="continue"){ do_opt_cbr=MSLCBR_CONT; do_opt_active=0; return ; }
  409.                             if(name=="break"){ do_opt_cbr=MSLCBR_BREAK; do_opt_active=0;  return ; }
  410.                             if(name=="return"){ do_opt_cbr=MSLCBR_RET; do_opt_active=0;  return ; }
  411.                         }
  412.  
  413.                         SetError(HLString()+"function '"+name+"' open '(' not found.");
  414.                         return ;
  415.                     }
  416.  
  417.                     line++;
  418.                 }
  419.             }
  420.             line++;
  421.         }
  422.  
  423.         // line='functionname'EOF
  424.         SetError(HLString()+"end of function name: '"+VString(pline, line-pline)+"'");
  425.         return ;
  426.     }
  427.  
  428.     int DoCodeFunctionArgs(unsigned char *&line, unsigned char *to, msl_fl_fargs &args){
  429.         while(!do_opt_stopit){
  430.             msl_value val;
  431.             DoCode(line, to, val, ',', ')');
  432.             //if(!DoCodeFunctionArgs(line, to, args)) return ;
  433.             args.Add(val);
  434.             if(line>=to){ SetError("not found ')'. EOF"); return 0; }
  435.             if(*line!=',') break;
  436.             line++;
  437.         }
  438.         return 1;
  439.     }
  440.  
  441.     void DoCodeFunctionIf(unsigned char*&line, unsigned char *to){
  442.         msl_value val; unsigned char endcode;
  443.         // save values
  444.         int old_ifw=do_opt_ifw, old_active=do_opt_active; do_opt_ifw=0;
  445.  
  446.         // skip space
  447.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  448.         // single or {multi}
  449.         if(line>=to){ SetError("if(...) EOF"); return ; }
  450.        
  451.         // set active
  452.         do_opt_active=old_active && old_ifw==2;
  453.         // do if(){ code }
  454.         if(*(line)=='{'){ endcode='}'; line++; } else endcode=';';
  455.         DoCodeMulti(line, to, val, endcode); line++;
  456.  
  457.         // skip space
  458.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  459.         // test on 'else'
  460.         if(line+4<to && *line=='e' && *(line+1)=='l' && *(line+2)=='s' && *(line+3)=='e'){
  461.             // skip 'else'
  462.             line+=4;
  463.             // skip space
  464.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  465.             // set active
  466.             do_opt_active=old_active && old_ifw==1;
  467.             // do else{ code }
  468.             if(*(line)=='{'){ endcode='}'; line++; } else endcode=';';
  469.             DoCodeMulti(line, to, val, endcode); line++;
  470.         }
  471.  
  472.         // load old value
  473.         do_opt_active=old_active; do_opt_ifw=0;
  474.         return ;
  475.     }
  476.  
  477.     void DoCodeFunctionWhile(unsigned char *&line, unsigned char *to, unsigned char *code, unsigned char *ecode){
  478.         msl_value val; unsigned char *lline, *elline, *tline; unsigned char endcode;
  479.        
  480.         // save values
  481.         int old_ifw=do_opt_ifw, old_active=do_opt_active; do_opt_ifw=0;
  482.         // skip space
  483.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  484.         if(line>=to){ SetError("if(...) EOF"); return ; }
  485.  
  486.         // set active
  487.         do_opt_active=old_active && old_ifw==2; lline=line;
  488.         // do while(){ code }
  489.         if(*(line)=='{'){ endcode='}'; line++; } else endcode=';';
  490.         DoCodeMulti(line, to, val, endcode); line++;
  491.         elline=line;
  492.  
  493.         // while
  494.         while(old_active && old_ifw==2){
  495.             msl_value val;
  496.             // do while( it code )
  497.             tline=code;
  498.             DoCode(tline, ecode+1, val, ')');
  499.            
  500.             // cbr
  501.             if(do_opt_cbr){
  502.                 if(do_opt_cbr==MSLCBR_CONT){ do_opt_cbr=MSLCBR_NULL; }
  503.                 if(do_opt_cbr==MSLCBR_BREAK){ do_opt_cbr=MSLCBR_NULL; break; }
  504.                 if(do_opt_cbr==MSLCBR_RET){ break; }
  505.             }
  506.            
  507.             // result
  508.             if(!val.val || val.val=="0") break;
  509.  
  510.             // do while(1){ it code }
  511.             tline=lline;
  512.             if(*(tline)=='{'){ endcode='}'; tline++; } else endcode=';';
  513.             DoCodeMulti(tline, elline, val, endcode);
  514.  
  515.             // set active
  516.             do_opt_active=old_active && old_ifw==2;
  517.         }
  518.  
  519.         // load old value
  520.         do_opt_active=old_active; do_opt_ifw=0;
  521.         return ;
  522.     }
  523.  
  524.     int DoCodeFunctionFor(unsigned char *&line, unsigned char *to){
  525.         // save values
  526.         int old_active=do_opt_active; do_opt_active=0;
  527.         // for args
  528.         VString a[4]; int as=0;
  529.  
  530.         while(!do_opt_stopit && as<4){
  531.             msl_value val;
  532.             a[as].data=line;
  533.             DoCode(line, to, val, ';', ')');
  534.             a[as].sz=line-a[as].data; as++;
  535.  
  536.             if(line>=to){ SetError("not found ')'. EOF"); return 0; }
  537.             if(*line==')') break;
  538.             line++;
  539.         }
  540.         line++;
  541.  
  542.         if(as!=3){
  543.             SetError("for(args!=3)."); return 0;
  544.         }
  545.  
  546.         // Do {}
  547.         msl_value val; unsigned char *lline, *elline, *tline, *eline; unsigned char endcode;
  548.  
  549.         lline=line;
  550.         if(*(line)=='{'){ endcode='}'; line++; } else endcode=';';
  551.         DoCodeMulti(line, to, val, endcode); line++;
  552.         elline=line;
  553.  
  554.         // load old value
  555.         do_opt_active=old_active;
  556.  
  557.         // Go or Nooo...
  558.         if(old_active){
  559.             // do for(1)
  560.             tline=a[0].data; eline=a[0].endu()+1;
  561.             DoCode(tline, eline, val, ';');
  562.  
  563.             while(1){
  564.                 // do for(2), test
  565.                 tline=a[1].data; eline=a[1].endu()+1;
  566.                 DoCode(tline, eline, val, ';');
  567.                 if(!val.val || val.val=="0") break;
  568.            
  569.                 // do {}
  570.                 tline=lline; eline=elline;
  571.                 if(*(tline)=='{'){ endcode='}'; tline++; } else endcode=';';               
  572.                 DoCodeMulti(tline, eline, val, endcode);
  573.  
  574.                 // cbr
  575.                 if(do_opt_cbr){
  576.                     if(do_opt_cbr==MSLCBR_CONT){ do_opt_cbr=MSLCBR_NULL; }
  577.                     if(do_opt_cbr==MSLCBR_BREAK){ do_opt_cbr=MSLCBR_NULL; break; }
  578.                     if(do_opt_cbr==MSLCBR_RET){ break; }
  579.                 }
  580.  
  581.                 // set value
  582.                 do_opt_active=old_active;
  583.  
  584.                 // do for(3)
  585.                 tline=a[2].data; eline=a[2].endu()+1;
  586.                 DoCode(tline, eline, val, ')');            
  587.             }
  588.         }
  589.  
  590.         // load old value
  591.         do_opt_active=old_active; do_opt_ifw=0;
  592.         return 1;
  593.     }
  594.  
  595.     int DoCodeFunctionExec(VString name, msl_fl_fargs &args){
  596.         // all sections
  597.  
  598.         if((name=="if" || name=="while") && args.Sz()==1){
  599.             do_opt_ifw=args[0].val.val && args[0].val.val!="0"; do_opt_ifw++;
  600.             return 1;
  601.         }
  602.  
  603.         // if active
  604.         if(!do_opt_active) return 1;
  605.  
  606.         // exec
  607.         if(name=="print" || name=="echo"){
  608.             for(int i=0; i<args.Sz(); i++){
  609.                 print(args[i].val.val);
  610.             }
  611.             return 1;
  612.         }
  613.  
  614.         //
  615.         SetError(HLString()+"Function: '"+name+"' not found");
  616.         return 0;
  617.     }
  618.  
  619.     // global value
  620.     void SetValue(VString key, VString val){
  621.    
  622.  
  623.     }
  624.  
  625.     // get output
  626.     MString GetOutput(){
  627.         // return result;
  628.         return MString(output.oneline(), output.size());
  629.     }
  630.  
  631. protected:
  632.     // set output
  633.     void SetOutput(VString line){
  634.         // add line to result
  635.         output+line;
  636.         return ;
  637.     }
  638.  
  639.     void SetWarning(VString line){
  640.         // add error line to result
  641.         output+"MSL-FL Warning: "+line+"\r\n";
  642.         return ;
  643.     }
  644.  
  645.     void SetError(VString line){
  646.         // add error line to result
  647.         output+"MSL-FL Error: "+line+"\r\n";
  648.         // stop
  649.         do_opt_stopit=1;
  650.         return ;
  651.     }
  652.  
  653.     void SetEpic(VString line){
  654.         // add error line to result
  655.         output+"MSL-FL Epic Fail: "+line+"\r\n";
  656.         // stop
  657.         do_opt_stopit=1;
  658.         return ;
  659.     }
  660.  
  661. };
  662.  
  663.  
  664. int TestMSL(){
  665.     msl_fl msl;
  666.  
  667.     // this code is work
  668.     msl.Do("<? print('Hello World!!!'); for($i=55; $i; $i--){ print('1'); } ?>");
  669.     MString res=msl.GetOutput();
  670.     print(res);
  671.  
  672.     return 0;
  673. }
  674.  
  675. //Result to console: Hello World!!!1111111111111111111111111111111111111111111111111111111
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement