001: /*
002: * IfCmd.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: IfCmd.java,v 1.3 2005/11/08 05:20:53 mdejong Exp $
012: *
013: */
014:
015: package tcl.lang;
016:
017: // This class implements the built-in "if" command in Tcl.
018:
019: class IfCmd implements Command {
020:
021: // See Tcl user documentation for details.
022: // @exception TclException If incorrect number of arguments.
023:
024: public void cmdProc(Interp interp, TclObject[] objv)
025: throws TclException {
026: int i;
027: boolean value;
028:
029: i = 1;
030: while (true) {
031: // At this point in the loop, objv and argc refer to an
032: // expression to test, either for the main expression or
033: // an expression following an "elseif". The arguments
034: // after the expression must be "then" (optional) and a
035: // script to execute if the expression is true.
036:
037: if (i >= objv.length) {
038: throw new TclException(interp,
039: "wrong # args: no expression after \""
040: + objv[i - 1] + "\" argument");
041: }
042: try {
043: value = interp.expr.evalBoolean(interp, objv[i]
044: .toString());
045: } catch (TclException e) {
046: switch (e.getCompletionCode()) {
047: case TCL.ERROR:
048: interp
049: .addErrorInfo("\n (\"if\" test expression)");
050: break;
051: }
052: throw e;
053: }
054:
055: i++;
056: if ((i < objv.length)
057: && (objv[i].toString().equals("then"))) {
058: i++;
059: }
060: if (i >= objv.length) {
061: throw new TclException(interp,
062: "wrong # args: no script following \""
063: + objv[i - 1] + "\" argument");
064: }
065: if (value) {
066: try {
067: interp.eval(objv[i], 0);
068: } catch (TclException e) {
069: switch (e.getCompletionCode()) {
070: case TCL.ERROR:
071: interp
072: .addErrorInfo("\n (\"if\" then script line "
073: + interp.errorLine + ")");
074: break;
075: }
076: throw e;
077: }
078: return;
079: }
080:
081: // The expression evaluated to false. Skip the command, then
082: // see if there is an "else" or "elseif" clause.
083:
084: i++;
085: if (i >= objv.length) {
086: interp.resetResult();
087: return;
088: }
089: if (objv[i].toString().equals("elseif")) {
090: i++;
091: continue;
092: }
093: break;
094: }
095:
096: // Couldn't find a "then" or "elseif" clause to execute.
097: // Check now for an "else" clause. We know that there's at
098: // least one more argument when we get here.
099:
100: if (objv[i].toString().equals("else")) {
101: i++;
102: if (i >= objv.length) {
103: throw new TclException(interp,
104: "wrong # args: no script following \"else\" argument");
105: } else if (i != (objv.length - 1)) {
106: throw new TclException(interp,
107: "wrong # args: extra words after \"else\" clause in "
108: + "\"if\" command");
109: }
110: } else {
111: // Not else, if there is more than 1 more argument
112: // then generate an error.
113:
114: if (i != (objv.length - 1)) {
115: throw new TclException(interp,
116: "wrong # args: extra words after \"else\" clause in \"if\" command");
117: }
118: }
119: try {
120: interp.eval(objv[i], 0);
121: } catch (TclException e) {
122: switch (e.getCompletionCode()) {
123: case TCL.ERROR:
124: interp.addErrorInfo("\n (\"if\" else script line "
125: + interp.errorLine + ")");
126: break;
127: }
128: throw e;
129: }
130: }
131: }
|