Advertisement
Guest User

MikelSV

a guest
Oct 14th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.07 KB | None | 0 0
  1. // My Server Language - Fast Line
  2. Versions msl_fl_version[]={
  3.     "0.0.0.3", "14.10.2013 16:22",
  4.     "0.0.0.2", "14.10.2013 01:08",
  5.     "0.0.0.1", "13.10.2013 21:05"
  6. };
  7.  
  8.  
  9.  
  10. // Values
  11. class msl_value : public OMatrixT<msl_value>{
  12. public:
  13.     // prev, next, up first, up end
  14.     msl_value *_p, *_n;//, *_a, *_e;
  15.     MString key, val;
  16.  
  17.     msl_value(){ _p=0; _n=0; }
  18.  
  19.     msl_value* New(){
  20.         msl_value *p=new msl_value;
  21.         if(!p) return 0;
  22.  
  23.         OMAdd(p);
  24.         return p;
  25.     }
  26.  
  27.     msl_value* Find(VString key){
  28.         if(!this) return 0;
  29.        
  30.         for(msl_value*p=_a; p; p=p->_n){
  31.             if(p->key==key) return p;
  32.         }
  33.  
  34.         return 0;
  35.     }
  36.  
  37.     msl_value* Set(VString key, VString val){
  38.         msl_value *p=Find(key);
  39.         if(p){
  40.             p->val=val;
  41.         } else{
  42.             p=New();
  43.             if(p){
  44.                 p->key=key; p->val=val;
  45.             }
  46.         }
  47.         return p;
  48.     }
  49.  
  50.     VString Get(VString key){
  51.         msl_value *p=Find(key);
  52.         if(p)
  53.             return p->val;
  54.         else
  55.             return VString();
  56.     }
  57.  
  58.     msl_value* SGet(VString key){
  59.         msl_value *p=Find(key);
  60.         if(!p){
  61.             p=New();
  62.             if(p){
  63.                 p->key=key; p->val.sz=0xffffffff;
  64.             }
  65.         }
  66.         return p;
  67.     }
  68.  
  69.     void Del(VString key){
  70.         msl_value *p=Find(key);
  71.         if(p){
  72.             OMDel(p); delete p;
  73.         }
  74.         return ;
  75.     }
  76.  
  77.     void Copy(msl_value *val){
  78.         Clear();
  79.         this->val=val->val;
  80.        
  81.         for(msl_value*p=val->_a; p; p=p->_n){
  82.             Set(p->key, p->val)->Copy(p);          
  83.         }
  84.         return ;
  85.     }
  86.  
  87.     void Move(msl_value &val){
  88.         Clear();
  89.         _a=val._a; _e=val._e; val._a=0; val._e=0;
  90.         this->val-=val.val;    
  91.         return ;
  92.     }
  93.  
  94.     ~msl_value(){ Clear(); }
  95.     void Clear(){ OMClear(); }
  96.  
  97. };
  98.  
  99. // Function Arguments
  100. class msl_fl_farg{
  101. public:
  102.     msl_value val;
  103.     //VString val;
  104. };
  105.  
  106. class msl_fl_fargs{
  107.     MString _args; // memory buffer
  108.     msl_fl_farg *args; //
  109.     int asz, usz; // all sz & use sz
  110.  
  111.     int UpSize(){
  112.         // reserv memory
  113.         _args.Reserv(sizeof(msl_fl_farg)*(asz+16));
  114.         // if error
  115.         if(!_args){
  116.             asz=0; usz=0;
  117.             return 0;
  118.         }
  119.         // update
  120.         asz+=16;
  121.         args=(msl_fl_farg*)_args.data;
  122.  
  123.         return 1;
  124.     }
  125.  
  126. public:
  127.     // constructor & destructor
  128.     msl_fl_fargs(){ args=0; asz=0; usz=0; }
  129.     ~msl_fl_fargs(){ }
  130.  
  131.     // vals[id];
  132.     msl_fl_farg &operator[](const int i){
  133.         if(i>usz){ globalerror("msl_fl_fargs epic fail"); }
  134.         return args[i];
  135.     }
  136.  
  137.     // add value
  138.     msl_fl_farg* Add(msl_value &val){
  139.         if(usz>=asz){ if(!UpSize()) return 0; }
  140.         // add
  141.         args[usz].val.Move(val);
  142.         return &args[usz++];
  143.     }
  144.  
  145.     int Sz(){ return usz; }
  146.    
  147. };
  148.  
  149. //#include "omatrix-msl_value.h"
  150.  
  151. // Hints:
  152. // msl_value. Механизм балансировки возвращаемых значений.  Чтобы не копировать лишний раз структуру с данными.
  153.  
  154.  
  155. // spaces
  156. // Do() - all process operations
  157. // Set () - set
  158. // Get () - get
  159.  
  160. //#define MSL_DOOPT_ERROR 0
  161. //#define MSL_DOOPT_STOPIT (MSL_DOOPT_ERROR)
  162.  
  163. class msl_fl{
  164.     HLString output; // result
  165.  
  166.     int do_opt, do_opt_stopit;
  167.  
  168.     msl_value global;
  169.  
  170.     public:
  171.  
  172.     // init
  173.     msl_fl(){ do_opt=0; do_opt_stopit=0; }
  174.  
  175.     ~msl_fl(){  }
  176.    
  177.     // process
  178.     void Do(VString code){
  179.         unsigned char *line=code, *pline=line, *to=code.endu();
  180.         // stop it
  181.         do_opt_stopit=0;
  182.         // value;
  183.         msl_value outval;
  184.  
  185.         while(line<to && !do_opt_stopit){
  186.             if(*line=='<' && line+1<to && *(line+1)=='?'){
  187.                 // set result
  188.                 SetOutput(VString(pline, line-pline));
  189.                 // *line='<?...' skip this 2 sumbols
  190.                 line+=2;
  191.  
  192.                 while(!do_opt_stopit){
  193.                     // do msl
  194.                     DoCode(line, to, outval, ';');
  195.                     if(line>=to || *line!=';') break;
  196.                     line++;
  197.                 }
  198.  
  199.                 // if(line='?>')
  200.                 if(line+1>=to || *line!='?' || *(line+1)!='>'){
  201.                     // oh no
  202.                     SetError("No find '?>'");
  203.                     // exit
  204.                     return ;
  205.                 } else{
  206.                     // *line='?>...' skip this 2 sumbols
  207.                     line+=2; pline=line;
  208.                 }
  209.             }
  210.             line++;
  211.         }
  212.         // set result
  213.         SetOutput(VString(pline, line-pline));
  214.         return ;
  215.     }
  216.  
  217.     void DoCode(unsigned char*&line, unsigned char *to, msl_value &outval, unsigned char ecode=1, unsigned char ecodet=1){
  218.         msl_value *value=0;
  219.         unsigned char *pline=0;
  220.  
  221.  
  222.         while(line<to && !do_opt_stopit){
  223.  
  224.             // skip space
  225.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  226.  
  227.             // $value
  228.             if(*line=='$'){
  229.                 if(value){ SetError("double $value"); return ; }
  230.                 value=DoCodeValue(++line, to);
  231.                 continue;
  232.             }else
  233.             // function
  234.             if(*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_'){
  235.                
  236.                 DoCodeFunction(line, to);
  237.                 continue;
  238.             }
  239.             // ecode
  240.             else if(*line==ecode || *line==ecodet){
  241.                 if(value) outval.val=value->val;
  242.                 return ;
  243.             }
  244.             // string
  245.             else if(*line=='"' || *line=='\''){
  246.                 pline=++line;
  247.                 if(*line=='"')
  248.                     while(line<to && *line!='"') line++;
  249.                 else
  250.                     while(line<to && *line!='\'') line++;
  251.                 if(line>=to){ SetError("closed \" or ' not found"); }
  252.                 outval.val=VString(pline, line-pline);
  253.                 line++;
  254.                 continue;
  255.             }
  256.             // operator=
  257.             else if(*line=='='){               
  258.                 if(!value){ SetError("need $value="); return ; }
  259.                
  260.                 msl_value val;
  261.                 DoCode(++line, to, val, ecode, ecodet);
  262.                
  263.                 // move
  264.                 value->Clear();
  265.                 value->val=val.val;
  266.                 value->_a=val._a; value->_e=val._e; val._a=0; val._e=0;
  267.                 // set outval
  268.                 outval.Copy(value);
  269.                 continue;          
  270.             }
  271.             // '?>' end do code
  272.             else if(*line=='?' && line+1<to && *(line+1)=='>'){
  273.                 return ;
  274.             }
  275.             // what the sumbol?
  276.             else{              
  277.                 SetError(HLString()+"Unknown sumbol: '"+VString(line, 1)+"'.");
  278.                 return ;
  279.             }
  280.  
  281.             // skip space
  282.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  283.             line++;
  284.         }
  285.  
  286.         return ;
  287.     }
  288.  
  289.     msl_value* DoCodeValue(unsigned char*&line, unsigned char *to){
  290.         unsigned char *pline=line;
  291.  
  292.         while(line<to && *line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_') line++;
  293.         if(line>=to){
  294.             SetError(HLString()+"EOF.");
  295.             return 0;
  296.         }
  297.        
  298.         // Get existing or create new
  299.         msl_value *val=global.SGet(VString(pline, line-pline));
  300.  
  301.         // skip space
  302.         while(1){
  303.             while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  304.             if(line<to && *line=='['){
  305.                 msl_value dval;
  306.                 DoCode(++line, to, dval, ']');
  307.  
  308.                 // next []
  309.                 val=val->SGet(dval.val);
  310.                 line++;
  311.             }
  312.             else return val;
  313.         }
  314.  
  315.         return 0;
  316.     }
  317.  
  318.     void DoCodeFunction(unsigned char*&line, unsigned char *to){
  319.         VString name; unsigned char *pline=line; msl_fl_fargs args;
  320.  
  321.         while(line<to){
  322.             // normal name
  323.             if(*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_'){}
  324.             else{
  325.                 name.setu(pline, line-pline);
  326.                 while(line<to){
  327.                     if(*line=='('){
  328.                         line++;
  329.                        
  330.                         while(!do_opt_stopit){
  331.                             msl_value val;
  332.                             DoCode(line, to, val, ',', ')');
  333.                             //if(!DoCodeFunctionArgs(line, to, args)) return ;
  334.                             args.Add(val);
  335.                             if(line>=to){ SetError("not found ')'. EOF"); return ; }
  336.                             if(*line!=',') break;
  337.                             line++;
  338.                         }
  339.                         line++;
  340.  
  341.                         if(!DoCodeFunctionExec(name, args)) return ;
  342.                         line++;
  343.                         return ;
  344.                     }
  345.                     else if(!(*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')){
  346.                         SetError(HLString()+"function '"+name+"' open '(' not found.");
  347.                         return ;
  348.                     }
  349.  
  350.                     line++;
  351.                 }
  352.             }
  353.             line++;
  354.         }
  355.  
  356.         // line='functionname'EOF
  357.         SetError(HLString()+"end of function name: '"+VString(pline, line-pline)+"'");
  358.         return ;
  359.     }
  360.  
  361. /*  int DoCodeFunctionArgs_DEL(unsigned char*&line, unsigned char *to, msl_fl_fargs &args){
  362.         unsigned char *pline;
  363.         // skip space
  364.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  365.  
  366.         pline=line;
  367.  
  368.         if(*line=='\''){
  369.             line++;
  370.             while(line<to && *line!='\'') line++;
  371.             if(!DoCodeFunctionAddArg(pline+1, line, to, args)) return 0;
  372.             line++;
  373.         }else  
  374.         if(*line=='"'){
  375.             line++;
  376.             while(line<to && *line!='"') line++;
  377.             if(!DoCodeFunctionAddArg(pline+1, line, to, args)) return 0;
  378.             line++;
  379.         } else
  380.         if(*line=='$'){
  381.             while(line<to && (*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_')) line++;
  382.             if(!DoCodeFunctionAddArg(pline, line, to, args)) return 0;
  383.         }
  384.         else{
  385.             if(line>=to){
  386.                 SetError("EOF");
  387.                 return 0;
  388.             }else{
  389.                 SetError(HLString()+"Unknown data: '"+VString(line, 1)+"'.");
  390.                 return 0;
  391.             }
  392.         }
  393.  
  394.         // skip space
  395.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  396.  
  397.         // Lol recursion :)
  398.         if(line<to && *line==','){
  399.             line++;
  400.             return DoCodeFunctionArgs(line, to, args);
  401.         }
  402.        
  403.         // if line=')' ok
  404.         if(line>=to || *line!=')'){
  405.             SetError(HLString()+"')' not foud.");
  406.             return 0;
  407.         }
  408.  
  409.         // line=')', skip
  410.         line++;
  411.  
  412.         return 1;
  413.     }
  414.  
  415.     int DoCodeFunctionAddArg(unsigned char *pline, unsigned char *line, unsigned char *to, msl_fl_fargs &args){
  416.         if(line>=to){
  417.             SetError(HLString()+"end "+VString(pline, 1)+" not found");
  418.             return 0;
  419.         } else{
  420.             if(!args.Add(VString(pline, line-pline))){
  421.                 SetEpic("add value");
  422.                 return 0;
  423.             }
  424.         }
  425.         return 1;
  426.     }*/
  427.  
  428.     int DoCodeFunctionExec(VString name, msl_fl_fargs &args){
  429.         if(name=="print" || name=="echo"){
  430.             for(int i=0; i<args.Sz(); i++){
  431.                 print(args[i].val.val);
  432.             }
  433.             return 1;
  434.         }
  435.  
  436.         SetError(HLString()+"Function: '"+name+"' not found");
  437.         return 0;
  438.     }
  439.  
  440.     // global value
  441.     void SetValue(VString key, VString val){
  442.    
  443.  
  444.     }
  445.  
  446.     // get output
  447.     MString GetOutput(){
  448.         // return result;
  449.         return MString(output.oneline(), output.size());
  450.     }
  451.  
  452. protected:
  453.     // set output
  454.     void SetOutput(VString line){
  455.         // add line to result
  456.         output+line;
  457.         return ;
  458.     }
  459.  
  460.     void SetWarning(VString line){
  461.         // add error line to result
  462.         output+"MSL-FL Warning: "+line+"\r\n";
  463.         return ;
  464.     }
  465.  
  466.     void SetError(VString line){
  467.         // add error line to result
  468.         output+"MSL-FL Error: "+line+"\r\n";
  469.         // stop
  470.         do_opt_stopit=1;
  471.         return ;
  472.     }
  473.  
  474.     void SetEpic(VString line){
  475.         // add error line to result
  476.         output+"MSL-FL Epic Fail: "+line+"\r\n";
  477.         // stop
  478.         do_opt_stopit=1;
  479.         return ;
  480.     }
  481.  
  482. };
  483.  
  484.  
  485. int TestMSL(){
  486.     msl_fl msl;
  487.    
  488.     // this code not work yet
  489.     msl.Do("<? $hello='Hello World!'; print($hello); ?>");
  490.     MString res=msl.GetOutput();
  491.     print(res);
  492.  
  493.     return 0;
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement