Advertisement
Guest User

MikelSV

a guest
Oct 13th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.10 KB | None | 0 0
  1. // My Server Language - Fast Line
  2. Versions msl_fl_version[]={
  3.     "0.0.0.2", "14.10.2013 1:08",
  4.     "0.0.0.1", "13.10.2013 21:05"
  5. };
  6.  
  7.  
  8. // Function Arguments
  9. class msl_fl_farg{
  10. public:
  11.     VString val;
  12. };
  13.  
  14. class msl_fl_fargs{
  15.     MString _args; // memory buffer
  16.     msl_fl_farg *args; //
  17.     int asz, usz; // all sz & use sz
  18.  
  19.     int UpSize(){
  20.         // reserv memory
  21.         _args.Reserv(sizeof(msl_fl_farg)*(asz+16));
  22.         // if error
  23.         if(!_args){
  24.             asz=0; usz=0;
  25.             return 0;
  26.         }
  27.         // update
  28.         asz+=16;
  29.         args=(msl_fl_farg*)_args.data;
  30.  
  31.         return 1;
  32.     }
  33.  
  34. public:
  35.     // constructor & destructor
  36.     msl_fl_fargs(){ args=0; asz=0; usz=0; }
  37.     ~msl_fl_fargs(){ }
  38.  
  39.     // vals[id];
  40.     msl_fl_farg &operator[](const int i){
  41.         if(i>usz){ globalerror("msl_fl_fargs epic fail"); }
  42.         return args[i];
  43.     }
  44.  
  45.     // add value
  46.     int Add(VString val){
  47.         if(usz>=asz){ if(!UpSize()) return 0; }
  48.         // add
  49.         args[usz].val=val; usz++;
  50.         return 1;
  51.     }
  52.  
  53.     int Sz(){ return usz; }
  54.    
  55. };
  56.  
  57. // spaces
  58. // Do() - all process operations
  59. // Set () - set
  60. // Get () - get
  61.  
  62. //#define MSL_DOOPT_ERROR 0
  63. //#define MSL_DOOPT_STOPIT (MSL_DOOPT_ERROR)
  64.  
  65. class msl_fl{
  66.     HLString output; // result
  67.  
  68.     int do_opt, do_opt_stopit;
  69.  
  70.     public:
  71.  
  72.     // init
  73.     msl_fl(){ do_opt=0; do_opt_stopit=0; }
  74.  
  75.     ~msl_fl(){  }
  76.    
  77.     // process
  78.     void Do(VString code){
  79.         unsigned char *line=code, *pline=line, *to=code.endu();
  80.         // stop it
  81.         do_opt_stopit=0;
  82.  
  83.         while(line<to && !do_opt_stopit){
  84.             if(*line=='<' && line+1<to && *(line+1)=='?'){
  85.                 // set result
  86.                 SetOutput(VString(pline, line-pline));
  87.                 // *line='<?...' skip this 2 sumbols
  88.                 line+=2;
  89.                 // do msl
  90.                 DoCode(line, to);
  91.                 // if(line='?>')
  92.                 if(line+1>=to || *line!='?' || *(line+1)!='>'){
  93.                     // oh no
  94.                     SetError("No find '?>'");
  95.                     // exit
  96.                     return ;
  97.                 } else{
  98.                     // *line='?>...' skip this 2 sumbols
  99.                     line+=2; pline=line;
  100.                 }
  101.             }
  102.             line++;
  103.         }
  104.         // set result
  105.         SetOutput(VString(pline, line-pline));
  106.         return ;
  107.     }
  108.  
  109.     void DoCode(unsigned char*&line, unsigned char *to){
  110.  
  111.         while(line<to && !do_opt_stopit){
  112.  
  113.             if(*line=='$'){
  114.                 // value
  115.             }else if(*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_'){
  116.                 // function
  117.                 DoCodeFunction(line, to);
  118.             } else if(*line==' ' || *line=='\t' || *line=='\r' || *line=='\n'){
  119.                 // ignore
  120.             }else if(*line=='?' && line+1<to && *(line+1)=='>'){
  121.                 // '?>' end do code
  122.                 return ;
  123.             }else{
  124.                 // what the sumbol?
  125.                 SetError(HLString()+"Unknown sumbol: '"+VString(line, 1)+"'.");
  126.                 return ;
  127.             }
  128.  
  129.             line++;
  130.         }
  131.  
  132.         return ;
  133.     }
  134.  
  135.     void DoCodeFunction(unsigned char*&line, unsigned char *to){
  136.         unsigned char *pline=line;
  137.         VString name; msl_fl_fargs args;
  138.  
  139.         while(line<to){
  140.             // normal name
  141.             if(*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_'){}
  142.             else{
  143.                 name.setu(pline, line-pline);
  144.                 while(line<to){
  145.                     if(*line=='('){
  146.                         line++;
  147.                         if(!DoCodeFunctionArgs(line, to, args)) return ;
  148.                         if(!DoCodeFunctionExec(name, args)) return ;
  149.                         line++;
  150.                         return ;
  151.                     }
  152.                     else if(!(*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')){
  153.                         SetError(HLString()+"function '"+name+"' open '(' not found.");
  154.                         return ;
  155.                     }
  156.  
  157.                     line++;
  158.                 }
  159.             }
  160.             line++;
  161.         }
  162.  
  163.         // line='functionname'EOF
  164.         SetError(HLString()+"end of function name: '"+VString(pline, line-pline)+"'");
  165.         return ;
  166.     }
  167.  
  168.     int DoCodeFunctionArgs(unsigned char*&line, unsigned char *to, msl_fl_fargs &args){
  169.         unsigned char *pline;
  170.         // skip space
  171.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  172.  
  173.         pline=line;
  174.  
  175.         if(*line=='\''){
  176.             line++;
  177.             while(line<to && *line!='\'') line++;
  178.             if(!DoCodeFunctionAddArg(pline+1, line, to, args)) return 0;
  179.             line++;
  180.         }else  
  181.         if(*line=='"'){
  182.             line++;
  183.             while(line<to && *line!='"') line++;
  184.             if(!DoCodeFunctionAddArg(pline+1, line, to, args)) return 0;
  185.             line++;
  186.         } else
  187.         if(*line=='$'){
  188.             while(line<to && (*line>='a' && *line<='z' || *line>='A' && *line<='Z' || *line=='_')) line++;
  189.             if(!DoCodeFunctionAddArg(pline, line, to, args)) return 0;
  190.         } else{
  191.             if(line>=to){
  192.                 SetError("EOF");
  193.                 return 0;
  194.             }else{
  195.                 SetError(HLString()+"Unknown data: '"+VString(line, 1)+"'.");
  196.                 return 0;
  197.             }
  198.         }
  199.  
  200.         while(line<to && (*line==' ' || *line=='\t' || *line=='\r' || *line=='\n')) line++;
  201.  
  202.         // Lol recursion :)
  203.         if(line<to && *line==',') return DoCodeFunctionArgs(line, to, args);
  204.        
  205.         // if line=')' ok
  206.         if(line>=to || *line!=')'){
  207.             SetError(HLString()+"')' not foud.");
  208.             return 0;
  209.         }
  210.  
  211.         // line=')', skip
  212.         line++;
  213.  
  214.         return 1;
  215.     }
  216.  
  217.     int DoCodeFunctionAddArg(unsigned char *pline, unsigned char *line, unsigned char *to, msl_fl_fargs &args){
  218.         if(line>=to){
  219.             SetError(HLString()+"end "+VString(pline, 1)+" not found");
  220.             return 0;
  221.         } else{
  222.             if(!args.Add(VString(pline, line-pline))){
  223.                 SetEpic("add value");
  224.                 return 0;
  225.             }
  226.         }
  227.         return 1;
  228.     }
  229.  
  230.     int DoCodeFunctionExec(VString name, msl_fl_fargs &args){
  231.         if(name=="print" || name=="echo"){
  232.             for(int i=0; i<args.Sz(); i++){
  233.                 print(args[i].val);
  234.             }
  235.             return 1;
  236.         }
  237.  
  238.         SetError(HLString()+"Function: '"+name+"' not found");
  239.         return 0;
  240.     }
  241.  
  242.     // global value
  243.     void SetValue(VString key, VString val){
  244.    
  245.  
  246.     }
  247.  
  248.     // get output
  249.     MString GetOutput(){
  250.         // return result;
  251.         return MString(output.oneline(), output.size());
  252.     }
  253.  
  254. protected:
  255.     // set output
  256.     void SetOutput(VString line){
  257.         // add line to result
  258.         output+line;
  259.         return ;
  260.     }
  261.  
  262.     void SetWarning(VString line){
  263.         // add error line to result
  264.         output+"MSL-FL Warning: "+line+"\r\n";
  265.         return ;
  266.     }
  267.  
  268.     void SetError(VString line){
  269.         // add error line to result
  270.         output+"MSL-FL Error: "+line+"\r\n";
  271.         // stop
  272.         do_opt_stopit=1;
  273.         return ;
  274.     }
  275.  
  276.     void SetEpic(VString line){
  277.         // add error line to result
  278.         output+"MSL-FL Epic Fail: "+line+"\r\n";
  279.         // stop
  280.         do_opt_stopit=1;
  281.         return ;
  282.     }
  283.  
  284. };
  285.  
  286.  
  287.  
  288. ## TEST CODE
  289. int TestMSL(){
  290.     msl_fl msl;
  291.  
  292.     // this code not work yet
  293.     msl.Do("<? print('Hello World!'); ?>");
  294.     MString res=msl.GetOutput();
  295.     print(res);
  296.  
  297.  
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement