Advertisement
Guest User

MikelSV / MSL-FL Step 7

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