001: /*
002: * ForCmd.java
003: *
004: * Copyright (c) 1997 Cornell University.
005: * Copyright (c) 1997 Sun Microsystems, Inc.
006: *
007: * See the file "license.terms" for information on usage and
008: * redistribution of this file, and for a DISCLAIMER OF ALL
009: * WARRANTIES.
010: *
011: * RCS: @(#) $Id: ForCmd.java,v 1.2 2005/11/16 21:19:13 mdejong Exp $
012: *
013: */
014:
015: package tcl.lang;
016:
017: /**
018: * This class implements the built-in "for" command in Tcl.
019: */
020:
021: class ForCmd implements Command {
022: /*
023: * This procedure is invoked to process the "for" Tcl command.
024: * See the user documentation for details on what it does.
025: *
026: * @param interp the current interpreter.
027: * @param argv command arguments.
028: * @exception TclException if script causes error.
029: */
030:
031: public void cmdProc(Interp interp, TclObject argv[])
032: throws TclException {
033: if (argv.length != 5) {
034: throw new TclNumArgsException(interp, 1, argv,
035: "start test next command");
036: }
037:
038: TclObject start = argv[1];
039: String test = argv[2].toString();
040: TclObject next = argv[3];
041: TclObject command = argv[4];
042:
043: boolean done = false;
044: try {
045: interp.eval(start, 0);
046: } catch (TclException e) {
047: interp.addErrorInfo("\n (\"for\" initial command)");
048: throw e;
049: }
050:
051: while (!done) {
052: if (!interp.expr.evalBoolean(interp, test)) {
053: break;
054: }
055:
056: try {
057: interp.eval(command, 0);
058: } catch (TclException e) {
059: switch (e.getCompletionCode()) {
060: case TCL.BREAK:
061: done = true;
062: break;
063:
064: case TCL.CONTINUE:
065: break;
066:
067: case TCL.ERROR:
068: interp.addErrorInfo("\n (\"for\" body line "
069: + interp.errorLine + ")");
070: throw e;
071:
072: default:
073: throw e;
074: }
075: }
076:
077: if (!done) {
078: try {
079: interp.eval(next, 0);
080: } catch (TclException e) {
081: switch (e.getCompletionCode()) {
082: case TCL.BREAK:
083: done = true;
084: break;
085:
086: case TCL.ERROR:
087: interp
088: .addErrorInfo("\n (\"for\" loop-end command)");
089: throw e;
090:
091: default:
092: throw e;
093: }
094: }
095: }
096: }
097:
098: interp.resetResult();
099: }
100: }
|