Example #1
0
        /// <summary>
        /// Main function that iterates over the token list.
        /// </summary>
        /// <param name="lambda">Generic lambda function that will be executed whenever an EOL is reached in the script file.</param>
        public void Interpret(ProcessLineDelegate lambda)
        {
            Token current = head;

            Text.StringBuilder accum = new System.Text.StringBuilder();
            while (current != null)
            {
                switch (current.kind)
                {
                case 1:
                    accum.Append(current.val);
                    break;

                case 2:
                    //In this case, variable is not an argument, thus just replace it with its value;
                {
                    string s; _VARS.TryGetValue(current.val, out s);
                    accum.Append(s);
                }
                break;

                case 3:
                    accum.Append(ParseFunction(ref current).val);
                    break;
                }
                if (cline < current.line || current.kind == 7)
                {
                    //Send string
                    if (accum.ToString().Trim() != string.Empty)
                    {
                        try
                        {
                            if (lambda != null)
                            {
                                lambda(accum.ToString(), this);
                            }
                            accum = new System.Text.StringBuilder();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    cline = current.line;
                }
                if (current.next != null)
                {
                    current = current.next;
                }
                else
                {
                    break;
                }
            }
        }
Example #2
0
 void Scan(IO.TextReader input)
 {
     while (input.Peek() != -1)
     {
         char ch = (char)input.Peek();
         if (ch == '%')
         {
             //eat '%'
             input.Read();
             Text.StringBuilder accum = new Text.StringBuilder();
             ch = (char)input.Peek();
             while (ch != '%')
             {
                 accum.Append((char)input.Read());
                 if (input.Peek() == -1)
                 {
                     break;
                 }
                 ch = (char)input.Peek();
             }
             input.Read(); //eat '%'
             AppendToken(accum.ToString(), line, 2);
         }
         else if (ch == ',')
         {
             input.Read();
         }
         else if (ch == '$')
         {
             //omg a function
             Text.StringBuilder accum = new Text.StringBuilder();
             input.Read();
             ch = (char)input.Peek();
             while (char.IsLetterOrDigit(ch) || ch == '_')
             {
                 accum.Append(ch);
                 input.Read();
                 if (input.Peek() == -1)
                 {
                     break;
                 }
                 ch = (char)input.Peek();
             }
             if (ch != '(')
             {
                 throw new Exception("Invalid character in function name");
             }
             AppendToken(accum.ToString(), line, 3);
         }
         else if (ch == '(')
         {
             input.Read();
             AppendToken("", line, 4);
         }
         else if (ch == ')')
         {
             input.Read();
             AppendToken("", line, 8);
         }
         else if (ch == '\r')
         {
             input.Read(); //eat CR
             char c = (char)input.Peek();
             if (c == '\n')
             {
                 input.Read(); //eat LF
                 AppendToken("", line, 7);
             }
             else
             {
                 line++;
             }
         }
         else if (ch == '#')
         {
             input.Read();
             AppendToken("", line, 9);
         }
         else
         {
             Text.StringBuilder accum = new System.Text.StringBuilder();
             while (ch != '$' & ch != '%' & ch != '\r' & ch != ')')
             {
                 if (ch == '\\')
                 {
                     //skip escape char and insert next one immediately
                     input.Read();
                 }
                 accum.Append((char)input.Read());
                 if (input.Peek() == -1)
                 {
                     break;
                 }
                 else if (input.Peek() == ',')
                 {
                     input.Read();
                     break;
                 }
                 ch = (char)input.Peek();
             }
             AppendToken(accum.ToString(), line, 1);
         }
     }
 }