Exemple #1
0
        /// <summary> See Tcl user documentation for details.</summary>

        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            if ((argv.Length < 2) || (argv.Length > 3))
            {
                throw new TclNumArgsException(interp, 1, argv, "script ?count?");
            }

            int count;
            if (argv.Length == 2)
            {
                count = 1;
            }
            else
            {
                count = TclInteger.Get(interp, argv[2]);
            }

            long startTime = System.DateTime.Now.Ticks;
            for (int i = 0; i < count; i++)
            {
                interp.Eval(argv[1], 0);
            }
            long endTime = System.DateTime.Now.Ticks;
            long uSecs = (((endTime - startTime) / 10) / count);
            if (uSecs == 1)
            {
                interp.SetResult(TclString.NewInstance("1 microsecond per iteration"));
            }
            else
            {
                interp.SetResult(TclString.NewInstance(uSecs + " microseconds per iteration"));
            }
            return TCL.CompletionCode.RETURN;
        }
Exemple #2
0
        /// <summary> See Tcl user documentation for details.</summary>
        /// <exception cref=""> TclException If incorrect number of arguments.
        /// </exception>
        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            int i;
            bool value;

            i = 1;
            while (true)
            {
                /*
                * At this point in the loop, argv and argc refer to an
                * expression to test, either for the main expression or
                * an expression following an "elseif".  The arguments
                * after the expression must be "then" (optional) and a
                * script to execute if the expression is true.
                */

                if (i >= argv.Length)
                {

                    throw new TclException(interp, "wrong # args: no expression after \"" + argv[i - 1] + "\" argument");
                }
                try
                {

                    value = interp._expr.EvalBoolean(interp, argv[i].ToString());
                }
                catch (TclException e)
                {
                    switch (e.GetCompletionCode())
                    {

                        case TCL.CompletionCode.ERROR:
                            interp.AddErrorInfo("\n    (\"if\" test expression)");
                            break;
                    }
                    throw;
                }

                i++;

                if ((i < argv.Length) && (argv[i].ToString().Equals("then")))
                {
                    i++;
                }
                if (i >= argv.Length)
                {

                    throw new TclException(interp, "wrong # args: no script following \"" + argv[i - 1] + "\" argument");
                }
                if (value)
                {
                    try
                    {
                        interp.Eval(argv[i], 0);
                    }
                    catch (TclException e)
                    {
                        switch (e.GetCompletionCode())
                        {

                            case TCL.CompletionCode.ERROR:
                                interp.AddErrorInfo("\n    (\"if\" then script line " + interp._errorLine + ")");
                                break;
                        }
                        throw;
                    }
                    return TCL.CompletionCode.RETURN;
                }

                /*
                * The expression evaluated to false.  Skip the command, then
                * see if there is an "else" or "elseif" clause.
                */

                i++;
                if (i >= argv.Length)
                {
                    interp.ResetResult();
                    return TCL.CompletionCode.RETURN;
                }

                if (argv[i].ToString().Equals("elseif"))
                {
                    i++;
                    continue;
                }
                break;
            }

            /*
            * Couldn't find a "then" or "elseif" clause to execute.
            * Check now for an "else" clause.  We know that there's at
            * least one more argument when we get here.
            */


            if (argv[i].ToString().Equals("else"))
            {
                i++;
                if (i >= argv.Length)
                {
                    throw new TclException(interp, "wrong # args: no script following \"else\" argument");
                }
                else if (i != (argv.Length - 1))
                {
                    throw new TclException(interp, "wrong # args: extra words after \"else\" clause in " + "\"if\" command");
                }
            }
            try
            {
                interp.Eval(argv[i], 0);
            }
            catch (TclException e)
            {
                switch (e.GetCompletionCode())
                {

                    case TCL.CompletionCode.ERROR:
                        interp.AddErrorInfo("\n    (\"if\" else script line " + interp._errorLine + ")");
                        break;
                }
                throw;
            }
            return TCL.CompletionCode.RETURN;
        }
Exemple #3
0
        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            int i, mode, body;
            bool matched;
            string inString;
            TclObject[] switchArgv = null;

            mode = EXACT;
            for (i = 1; i < argv.Length; i++)
            {

                if (!argv[i].ToString().StartsWith("-"))
                {
                    break;
                }
                int opt = TclIndex.Get(interp, argv[i], validCmds, "option", 1);
                if (opt == LAST)
                {
                    i++;
                    break;
                }
                else if (opt > LAST)
                {
                    throw new TclException(interp, "SwitchCmd.cmdProc: bad option " + opt + " index to validCmds");
                }
                else
                {
                    mode = opt;
                }
            }

            if (argv.Length - i < 2)
            {
                throw new TclNumArgsException(interp, 1, argv, "?switches? string pattern body ... ?default body?");
            }

            inString = argv[i].ToString();
            i++;

            // If all of the pattern/command pairs are lumped into a single
            // argument, split them out again.

            if (argv.Length - i == 1)
            {
                switchArgv = TclList.getElements(interp, argv[i]);
                i = 0;
            }
            else
            {
                switchArgv = argv;
            }

            for (; i < switchArgv.Length; i += 2)
            {
                if (i == (switchArgv.Length - 1))
                {
                    throw new TclException(interp, "extra switch pattern with no body");
                }

                // See if the pattern matches the string.

                matched = false;

                string pattern = switchArgv[i].ToString();

                if ((i == switchArgv.Length - 2) && pattern.Equals("default"))
                {
                    matched = true;
                }
                else
                {
                    switch (mode)
                    {

                        case EXACT:
                            matched = inString.Equals(pattern);
                            break;

                        case GLOB:
                            matched = Util.StringMatch(inString, pattern);
                            break;

                        case REGEXP:
                            matched = Util.regExpMatch(interp, inString, switchArgv[i]);
                            break;
                    }
                }
                if (!matched)
                {
                    continue;
                }

                // We've got a match.  Find a body to execute, skipping bodies
                // that are "-".

                for (body = i + 1; ; body += 2)
                {
                    if (body >= switchArgv.Length)
                    {

                        throw new TclException(interp, "no body specified for pattern \"" + switchArgv[i] + "\"");
                    }

                    if (!switchArgv[body].ToString().Equals("-"))
                    {
                        break;
                    }
                }

                try
                {
                    interp.Eval(switchArgv[body], 0);
                    return TCL.CompletionCode.RETURN;
                }
                catch (TclException e)
                {
                    if (e.GetCompletionCode() == TCL.CompletionCode.ERROR)
                    {

                        interp.AddErrorInfo("\n    (\"" + switchArgv[i] + "\" arm line " + interp._errorLine + ")");
                    }
                    throw;
                }
            }

            // Nothing matched:  return nothing.
            return TCL.CompletionCode.RETURN;
        }
Exemple #4
0
        /// <summary> This procedure is invoked to process the "subst" Tcl command.
        /// See the user documentation for details on what it does.
        /// 
        /// </summary>
        /// <param name="interp">the current interpreter.
        /// </param>
        /// <param name="argv">command arguments.
        /// </param>
        /// <exception cref=""> TclException if wrong # of args or invalid argument(s).
        /// </exception>

        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            int currentObjIndex, len, i;
            int objc = argv.Length - 1;
            bool doBackslashes = true;
            bool doCmds = true;
            bool doVars = true;
            StringBuilder result = new StringBuilder();
            string s;
            char c;

            for (currentObjIndex = 1; currentObjIndex < objc; currentObjIndex++)
            {

                if (!argv[currentObjIndex].ToString().StartsWith("-"))
                {
                    break;
                }
                int opt = TclIndex.Get(interp, argv[currentObjIndex], validCmds, "switch", 0);
                switch (opt)
                {

                    case OPT_NOBACKSLASHES:
                        doBackslashes = false;
                        break;

                    case OPT_NOCOMMANDS:
                        doCmds = false;
                        break;

                    case OPT_NOVARS:
                        doVars = false;
                        break;

                    default:
                        throw new TclException(interp, "SubstCmd.cmdProc: bad option " + opt + " index to cmds");

                }
            }
            if (currentObjIndex != objc)
            {
                throw new TclNumArgsException(interp, currentObjIndex, argv, "?-nobackslashes? ?-nocommands? ?-novariables? string");
            }

            /*
            * Scan through the string one character at a time, performing
            * command, variable, and backslash substitutions.
            */


            s = argv[currentObjIndex].ToString();
            len = s.Length;
            i = 0;
            while (i < len)
            {
                c = s[i];

                if ((c == '[') && doCmds)
                {
                    ParseResult res;
                    try
                    {
                        interp._evalFlags = Parser.TCL_BRACKET_TERM;
                        interp.Eval(s.Substring(i + 1, (len) - (i + 1)));
                        TclObject interp_result = interp.GetResult();
                        interp_result.Preserve();
                        res = new ParseResult(interp_result, i + interp._termOffset);
                    }
                    catch (TclException e)
                    {
                        i = e.errIndex + 1;
                        throw;
                    }
                    i = res.nextIndex + 2;

                    result.Append(res.Value.ToString());
                    res.Release();
                }
                else if (c == '\r')
                {
                    /*
                    * (ToDo) may not be portable on Mac
                    */

                    i++;
                }
                else if ((c == '$') && doVars)
                {
                    ParseResult vres = Parser.parseVar(interp, s.Substring(i, (len) - (i)));
                    i += vres.nextIndex;

                    result.Append(vres.Value.ToString());
                    vres.Release();
                }
                else if ((c == '\\') && doBackslashes)
                {
                    BackSlashResult bs = Tcl.Lang.Interp.backslash(s, i, len);
                    i = bs.NextIndex;
                    if (bs.IsWordSep)
                    {
                        break;
                    }
                    else
                    {
                        result.Append(bs.C);
                    }
                }
                else
                {
                    result.Append(c);
                    i++;
                }
            }

            interp.SetResult(result.ToString());
            return TCL.CompletionCode.RETURN;
        }
Exemple #5
0
        /*
        * This procedure is invoked to process the "for" Tcl command.
        * See the user documentation for details on what it does.
        *
        * @param interp the current interpreter.
        * @param argv command arguments.
        * @exception TclException if script causes error.
        */

        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length != 5)
            {
                throw new TclNumArgsException(interp, 1, argv, "start test next command");
            }

            TclObject start = argv[1];

            string test = argv[2].ToString();
            TclObject next = argv[3];
            TclObject command = argv[4];

            bool done = false;
            try
            {
                interp.Eval(start, 0);
            }
            catch (TclException e)
            {
                interp.AddErrorInfo("\n    (\"for\" initial command)");
                throw;
            }

            while (!done)
            {
                if (!interp._expr.EvalBoolean(interp, test))
                {
                    break;
                }

                try
                {
                    interp.Eval(command, 0);
                }
                catch (TclException e)
                {
                    switch (e.GetCompletionCode())
                    {

                        case TCL.CompletionCode.BREAK:
                            done = true;
                            break;


                        case TCL.CompletionCode.CONTINUE:
                            break;


                        case TCL.CompletionCode.ERROR:
                            interp.AddErrorInfo("\n    (\"for\" body line " + interp._errorLine + ")");
                            throw;


                        default:
                            throw;

                    }
                }

                if (!done)
                {
                    try
                    {
                        interp.Eval(next, 0);
                    }
                    catch (TclException e)
                    {
                        switch (e.GetCompletionCode())
                        {

                            case TCL.CompletionCode.BREAK:
                                done = true;
                                break;


                            case TCL.CompletionCode.CONTINUE:
                                break;


                            default:
                                interp.AddErrorInfo("\n    (\"for\" loop-end command)");
                                throw;

                        }
                    }
                }
            }

            interp.ResetResult();
            return TCL.CompletionCode.RETURN;
        }
Exemple #6
0
    internal static void eval( Interp interp, Interp slaveInterp, int objIx, TclObject[] objv )
    {
      TCL.CompletionCode result;

      slaveInterp.preserve();
      slaveInterp.allowExceptions();

      try
      {
        if ( objIx + 1 == objv.Length )
        {
          slaveInterp.Eval( objv[objIx], 0 );
        }
        else
        {
          TclObject obj = TclList.NewInstance();
          for ( int ix = objIx; ix < objv.Length; ix++ )
          {
            TclList.Append( interp, obj, objv[ix] );
          }
          obj.Preserve();
          slaveInterp.Eval( obj, 0 );
          obj.Release();
        }
        result = slaveInterp._returnCode;
      }
      catch ( TclException e )
      {
        result = e.GetCompletionCode();
      }

      slaveInterp.release();
      interp.transferResult( slaveInterp, result );
    }
Exemple #7
0
        /// <summary> Executes a "case" statement. See Tcl user
        /// documentation for details.
        /// 
        /// </summary>
        /// <param name="interp">the current interpreter.
        /// </param>
        /// <param name="argv">command arguments.
        /// </param>
        /// <exception cref=""> TclException If incorrect number of arguments.
        /// </exception>

        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length < 3)
            {
                throw new TclNumArgsException(interp, 1, argv, "string ?in? patList body ... ?default body?");
            }

            int i;
            int body;
            TclObject[] caseArgv;
            string inString;


            inString = argv[1].ToString();
            caseArgv = argv;
            body = -1;


            if (argv[2].ToString().Equals("in"))
            {
                i = 3;
            }
            else
            {
                i = 2;
            }

            /*
            * If all of the pattern/command pairs are lumped into a single
            * argument, split them out again.
            */

            if (argv.Length - i == 1)
            {
                caseArgv = TclList.getElements(interp, argv[i]);
                i = 0;
            }

            {
                for (; i < caseArgv.Length; i += 2)
                {
                    int j;

                    if (i == (caseArgv.Length - 1))
                    {
                        throw new TclException(interp, "extra case pattern with no body");
                    }

                    /*
                    * Check for special case of single pattern (no list) with
                    * no backslash sequences.
                    */


                    string caseString = caseArgv[i].ToString();
                    int len = caseString.Length;
                    for (j = 0; j < len; j++)
                    {
                        char c = caseString[j];
                        if (System.Char.IsWhiteSpace(c) || (c == '\\'))
                        {
                            break;
                        }
                    }
                    if (j == len)
                    {
                        if (caseString.Equals("default"))
                        {
                            body = i + 1;
                        }
                        if (Util.StringMatch(inString, caseString))
                        {
                            body = i + 1;
                            goto match_loop_brk;
                        }
                        continue;
                    }

                    /*
                    * Break up pattern lists, then check each of the patterns
                    * in the list.
                    */

                    int numPats = TclList.getLength(interp, caseArgv[i]);
                    for (j = 0; j < numPats; j++)
                    {

                        if (Util.StringMatch(inString, TclList.index(interp, caseArgv[i], j).ToString()))
                        {
                            body = i + 1;
                            goto match_loop_brk;
                        }
                    }
                }
            }

        match_loop_brk:
            ;


            if (body != -1)
            {
                try
                {
                    interp.Eval(caseArgv[body], 0);
                }
                catch (TclException e)
                {
                    if (e.GetCompletionCode() == TCL.CompletionCode.ERROR)
                    {

                        interp.AddErrorInfo("\n    (\"" + caseArgv[body - 1] + "\" arm line " + interp._errorLine + ")");
                    }
                    throw;
                }
            }
            return TCL.CompletionCode.RETURN;
        }
Exemple #8
0
        public void traceProc(Interp interp, string part1, string part2, TCL.VarFlag flags)
        {
            if (((this.flags & flags) != 0) && ((flags & TCL.VarFlag.INTERP_DESTROYED) == 0))
            {
                StringBuilder sbuf = new StringBuilder(command);

                try
                {
                    Util.appendElement(interp, sbuf, part1);
                    if ((System.Object)part2 != null)
                    {
                        Util.appendElement(interp, sbuf, part2);
                    }
                    else
                    {
                        Util.appendElement(interp, sbuf, "");
                    }

                    if ((flags & TCL.VarFlag.TRACE_READS) != 0)
                    {
                        Util.appendElement(interp, sbuf, "r");
                    }
                    else if ((flags & TCL.VarFlag.TRACE_WRITES) != 0)
                    {
                        Util.appendElement(interp, sbuf, "w");
                    }
                    else if ((flags & TCL.VarFlag.TRACE_UNSETS) != 0)
                    {
                        Util.appendElement(interp, sbuf, "u");
                    }
                }
                catch (TclException e)
                {
                    throw new TclRuntimeError("unexpected TclException: " + e.Message, e);
                }

                // Execute the command.

                interp.Eval(sbuf.ToString(), 0);
            }
        }