wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * ForeachCmd.java |
||
3 | * |
||
4 | * Copyright (c) 1997 Cornell University. |
||
5 | * Copyright (c) 1997 Sun Microsystems, Inc. |
||
6 | * |
||
7 | * See the file "license.terms" for information on usage and |
||
8 | * redistribution of this file, and for a DISCLAIMER OF ALL |
||
9 | * WARRANTIES. |
||
10 | * |
||
11 | * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
12 | * |
||
13 | * RCS @(#) $Id: ForeachCmd.java,v 1.4 1999/08/07 06:44:04 mo Exp $ |
||
14 | * |
||
15 | */ |
||
16 | using System; |
||
17 | namespace tcl.lang |
||
18 | { |
||
19 | |||
20 | /// <summary> This class implements the built-in "Foreach" command in Tcl.</summary> |
||
21 | |||
22 | class ForeachCmd : Command |
||
23 | { |
||
24 | /// <summary> Tcl_ForeachObjCmd -> ForeachCmd.cmdProc |
||
25 | /// |
||
26 | /// This procedure is invoked to process the "foreach" Tcl command. |
||
27 | /// See the user documentation for details on what it does. |
||
28 | /// |
||
29 | /// </summary> |
||
30 | /// <param name="interp">the current interpreter. |
||
31 | /// </param> |
||
32 | /// <param name="objv">command arguments. |
||
33 | /// </param> |
||
34 | /// <exception cref=""> TclException if script causes error. |
||
35 | /// </exception> |
||
36 | |||
37 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] objv ) |
||
38 | { |
||
39 | if ( objv.Length < 4 || ( objv.Length % 2 ) != 0 ) |
||
40 | { |
||
41 | throw new TclNumArgsException( interp, 1, objv, "varList list ?varList list ...? command" ); |
||
42 | } |
||
43 | |||
44 | // foreach {n1 n2} {1 2 3 4} {n3} {1 2} {puts $n1-$n2-$n3} |
||
45 | // name[0] = {n1 n2} value[0] = {1 2 3 4} |
||
46 | // name[1] = {n3} value[0] = {1 2} |
||
47 | |||
48 | TclObject[] name = new TclObject[( objv.Length - 2 ) / 2]; |
||
49 | TclObject[] value = new TclObject[( objv.Length - 2 ) / 2]; |
||
50 | |||
51 | int c, i, j, base_; |
||
52 | int maxIter = 0; |
||
53 | TclObject command = objv[objv.Length - 1]; |
||
54 | bool done = false; |
||
55 | |||
56 | for ( i = 0; i < objv.Length - 2; i += 2 ) |
||
57 | { |
||
58 | int x = i / 2; |
||
59 | name[x] = objv[i + 1]; |
||
60 | value[x] = objv[i + 2]; |
||
61 | |||
62 | int nSize = TclList.getLength( interp, name[x] ); |
||
63 | int vSize = TclList.getLength( interp, value[x] ); |
||
64 | |||
65 | if ( nSize == 0 ) |
||
66 | { |
||
67 | throw new TclException( interp, "foreach varlist is empty" ); |
||
68 | } |
||
69 | |||
70 | int iter = ( vSize + nSize - 1 ) / nSize; |
||
71 | if ( maxIter < iter ) |
||
72 | { |
||
73 | maxIter = iter; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | for ( c = 0; !done && c < maxIter; c++ ) |
||
78 | { |
||
79 | // Set up the variables |
||
80 | |||
81 | for ( i = 0; i < objv.Length - 2; i += 2 ) |
||
82 | { |
||
83 | int x = i / 2; |
||
84 | int nSize = TclList.getLength( interp, name[x] ); |
||
85 | base_ = nSize * c; |
||
86 | for ( j = 0; j < nSize; j++ ) |
||
87 | { |
||
88 | // Test and see if the name variable is an array. |
||
89 | |||
90 | |||
91 | Var[] result = Var.lookupVar( interp, name[x].ToString(), null, 0, null, false, false ); |
||
92 | Var var = null; |
||
93 | |||
94 | if ( result != null ) |
||
95 | { |
||
96 | if ( result[1] != null ) |
||
97 | { |
||
98 | var = result[1]; |
||
99 | } |
||
100 | else |
||
101 | { |
||
102 | var = result[0]; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | try |
||
107 | { |
||
108 | if ( base_ + j >= TclList.getLength( interp, value[x] ) ) |
||
109 | { |
||
110 | interp.setVar( TclList.index( interp, name[x], j ), TclString.newInstance( "" ), 0 ); |
||
111 | } |
||
112 | else |
||
113 | { |
||
114 | interp.setVar( TclList.index( interp, name[x], j ), TclList.index( interp, value[x], base_ + j ), 0 ); |
||
115 | } |
||
116 | } |
||
117 | catch ( TclException e ) |
||
118 | { |
||
119 | |||
120 | throw new TclException( interp, "couldn't set loop variable: \"" + TclList.index( interp, name[x], j ) + "\"" ); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | // Execute the script |
||
126 | |||
127 | try |
||
128 | { |
||
129 | interp.eval( command, 0 ); |
||
130 | } |
||
131 | catch ( TclException e ) |
||
132 | { |
||
133 | switch ( e.getCompletionCode() ) |
||
134 | { |
||
135 | |||
136 | case TCL.CompletionCode.BREAK: |
||
137 | done = true; |
||
138 | break; |
||
139 | |||
140 | |||
141 | case TCL.CompletionCode.CONTINUE: |
||
142 | continue; |
||
143 | |||
144 | |||
145 | case TCL.CompletionCode.ERROR: |
||
146 | interp.addErrorInfo( "\n (\"foreach\" body line " + interp.errorLine + ")" ); |
||
147 | throw; |
||
148 | |||
149 | |||
150 | default: |
||
151 | throw; |
||
152 | |||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | interp.resetResult(); |
||
158 | return TCL.CompletionCode.RETURN; |
||
159 | } |
||
160 | } |
||
161 | } |