Beispiel #1
0
        /// <summary>
        /// Executes a script.
        /// </summary>
        /// <param name="script">
        /// Script text.
        /// </param>
        /// <param name="exception">
        /// Thrown exception, if any, will be stored there.
        /// </param>
        /// <returns>
        /// True on success. False on failure.
        /// </returns>
        public bool Execute(string script, out ScriptExecutionException exception)
        {
            string line = null;
            int    i    = -1;

            try{
                string[] lines = script.Split('\n');
                for (i = 0; i < lines.Length; i++)
                {
                    line = lines[i].Trim();
                    if (line.Length == 0)
                    {
                        continue;
                    }
                    else if (line[0] == ';')
                    {
                        OnComment(line.Substring(1));
                    }
                    else
                    {
                        string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        int[]    args  = new int[parts.Length - 1];
                        for (int j = 1; j < parts.Length; j++)
                        {
                            if (!int.TryParse(parts[j], out args[j - 1]))
                            {
                                throw new ScriptExecutionException(i + 1, line, string.Format("Cannot parse argument \"{0}\".", parts[j]), null);
                            }
                        }
                        OnFunction(parts[0], args);
                    }
                }
                exception = null;
                return(true);
            }catch (ScriptExecutionException e)
            {
                exception = e;
                return(false);
            }catch (Exception e)
            {
                exception = new ScriptExecutionException(i + 1, line, e.Message, e);
                return(false);
            }
        }
 /// <summary>
 /// Executes a script.
 /// </summary>
 /// <param name="script">
 /// Script text.
 /// </param>
 /// <param name="exception">
 /// Thrown exception, if any, will be stored there.
 /// </param>
 /// <returns>
 /// True on success. False on failure.
 /// </returns>
 public bool Execute(string script, out ScriptExecutionException exception)
 {
     string line = null;
     int i = -1;
     try{
         string[] lines = script.Split('\n');
         for(i = 0; i < lines.Length; i++)
         {
             line = lines[i].Trim();
             if(line.Length == 0)
             {
                 continue;
             }else if(line[0] == ';')
             {
                 OnComment(line.Substring(1));
             }else{
                 string[] parts = line.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                 int[] args = new int[parts.Length-1];
                 for(int j = 1; j < parts.Length; j++)
                 {
                     if(!int.TryParse(parts[j], out args[j-1]))
                     {
                         throw new ScriptExecutionException(i+1, line, string.Format("Cannot parse argument \"{0}\".", parts[j]), null);
                     }
                 }
                 OnFunction(parts[0], args);
             }
         }
         exception = null;
         return true;
     }catch(ScriptExecutionException e)
     {
         exception = e;
         return false;
     }catch(Exception e)
     {
         exception = new ScriptExecutionException(i+1, line, e.Message, e);
         return false;
     }
 }