APWTCL Arnulf's Preferred Web Tcl

Check-in [69acb6cfd9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:inital version
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:69acb6cfd9cd8fef342891c860fd90dee47d15be
User & Date: arnulf 2012-04-12 15:57:13
Context
2012-04-12
15:57
fixes and new code check-in: 7863e218e5 user: arnulf tags: trunk
15:57
inital version check-in: 69acb6cfd9 user: arnulf tags: trunk
13:05
initial version check-in: 914a8424cb user: arnulf tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added src/org/apwtcl/lang/cmd/SwitchCommand.java.































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*=======================================================
 * SwitchCommand.java 
 *
 * "A Tcl like language implementation in Java named APWTCL
 * ((Java) Arnulf's Preferred Web Tcl)"
 *
 * APWTCL SwitchCommand class
 *
 * Released under same BSD license as Tcl.
 * (Tcl BSD license found at <http://www.tcl.tk/software/tcltk/license.html>)
 *
 * Copyright 2012 Arnulf P. Wiedemann
 *
 */

package org.apwtcl.lang.cmd;

import java.util.ArrayList;
import org.apwtcl.lang.Interp;
import org.apwtcl.lang.ApwtclObj;
import org.apwtcl.lang.Token;
import org.apwtcl.lang.Variable;

public class SwitchCommand extends Token {

  /* ==================== SwitchCommand ================================== */
  public SwitchCommand() {
  }

  /* ==================== switchCmd ================================== */
  public int switchCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("switch called!"+args+"!");
    String usage = "?-switch ...? string {?pattern body ...? ?default body?}";
    if (args.size() < 3) {
      interp.wrongNumArgs(1, args, usage);
      return ERROR;
    }
    int body_start_idx = 1;
    boolean have_exact_opt = false;
    boolean have_glob_opt = false;
    boolean have_regexp_opt = false;
    boolean have_command_opt = false;
    boolean have_nocase_opt = false;
    boolean have_matchvar_opt = false;
    boolean have_indexvar_opt = false;
    var match_var = null;
    var index_var = null;
    var switch_string;
    var body;
    var obj;
    var text;
    var parser;
    var pattern;
    var token;
    var match_body = null;
    boolean have_match;
    ApwtclObj default_body = null;
    Parttern re = Pattern.compile("^[-].*");
    Matcher m = null;

    for (body_start_idx = 1; body_start_idx < args.length; body_start_idx++) {
      my_arg = args.get(body_start_idx).getString();
      m = re.matcher(my_arg);
      if (m.matches()) {
        if (my_arg.equals("-exact")) {
          if (have_command_opt) {
            interp.setResultFormatted("bad option \"-exact\": -command option already found");
	    return ERROR;
	  }
          if (have_exact_opt) {
            interp.setResultFormatted("bad option \"-exact\": -exact option already found");
	    return ERROR;
          }
          if (have_glob_opt) {
            interp.setResultFormatted("bad option \"-exact\": -glob option already found");
	    return ERROR;
          }
          if (have_regexp_opt) {
            interp.setResultFormatted("bad option \"-exact\": -regexp option already found");
	    return ERROR;
          }
          have_exact_opt = true;
        }
        if (my_arg.equals("-glob")) {
          if (have_command_opt) {
            interp.setResultFormatted("bad option \"-glob\": -command option already found");
	    return ERROR;
          }
          if (have_exact_opt) {
            interp.setResultFormatted("bad option \"-glob\": -exact option already found");
	    return ERROR;
          }
          if (have_regexp_opt) {
            interp.setResultFormatted("bad option \"-glob\": -regexp option already found");
	    return ERROR;
          }
          if (have_glob_opt) {
            interp.setResultFormatted("bad option \"-glob\": -glob option already found");
	    return ERROR;
          }
          have_glob_opt = true;
        }
        if (my_arg.equals("-regexp")) {
          if (have_command_opt) {
            interp.setResultFormatted("bad option \"-regexp\": -command option already found");
	    return ERROR;
          }
          if (have_regexp_opt) {
            interp.setResultFormatted("bad option \"-regexp\": -regexp option already found");
	    return ERROR;
          }
          if (have_exact_opt) {
            interp.setResultFormatted("bad option \"-regexp\": -exact option already found");
	    return ERROR;
          }
          if (have_glob_opt) {
            interp.setResultFormatted("bad option \"-regexp\": -glob option already found");
	    return ERROR;
          }
          have_regexp_opt = true;
        }
        if (my_arg.equals("-command")) {
          if (have_command_opt) {
            interp.setResultFormatted("bad option \"-command\": -command option already found");
	    return ERROR;
          }
          if (have_regexp_opt) {
            interp.setResultFormatted("bad option \"-command\": -regexp option already found");
	    return ERROR;
          }
          if (have_exact_opt) {
            interp.setResultFormatted("bad option \"-command\": -exact option already found");
	    return ERROR;
          }
          if (have_glob_opt) {
            interp.setResultFormatted("bad option \"-command\": -glob option already found");
	    return ERROR;
          }
          have_command_opt = true;
        }
        if (my_arg.equals("-nocase")) {
          if (have_nocase_opt != 0) {
            interp.setResultFormatted("bad option \"-nocase\": -nocase option already found");
	    return interp.ERROR;
          }
          have_nocase_opt = 1;
        }
        if (my_arg.equals("-matchvar")) {
          if (have_matchvar_opt) {
            interp.setResultFormatted("bad option \"-matchvar\": -matchvar option already found");
	    return ERROR;
          }
          if (!have_regexp_opt) {
            interp.setResultFormatted("-matchvar option requires -regexp option");
	    return ERROR;
          }
          have_matchvar_opt = true;
          if (body_start_idx > args.size() - 1) {
            interp.wrongNumArgs(1, args, usage);
	    return ERROR;
          }
          body_start_idx++;
          matchvar = args[body_start_idx].toString();
        }
        if (my_arg.equlas("-indexvar")) {
          if (have_indexvar_opt) {
            interp.setResultFormatted("bad option \"-indexvar\": -indexvar option already found");
	    return ERROR;
          }
          if (!have_regexp_opt) {
            interp.setResultFormatted("-indexvar option requires -regexp option");
	    return ERROR;
          }
          have_indexvar_opt = true;
          if (body_start_idx > args.size() - 1) {
            interp.wrongNumArgs(1, args, usage);
	    return ERROR;
          }
          body_start_idx++;
          indexvar = args.get(body_start_idx).toString();
        }
        if (my_arg.equals("--")) {
          body_start_idx++;
          break;
        }
      } else {
        break;
      }
    }
    if (body_start_idx == args.size() - 1) {
      interp.wrongNumArgs(1, args, usage);
      return ERROR;
    }
    if (!have_exact_opt && !have_glob_opt && !have_regexp_opt) {
      have_exact_opt = true;
    }
    /* if we have one argument left, it must be a list, otherwise we have the
     * list elements in the args array
     */
    ApwtclObj case_list;
    int i;
    ApwtclObj script = null;
    ApwtclObj str_obj = args.get(body_start_idx++);
    int pat_count = args.size() - body_start_idx;
    if (pat_count == 1) {
      ArrayList<ApwtclObj> vector = new ArrayList<ApwtclObj>();

      ArrayList<Integer>pat_count_ptr = new ArrayList<Integer>();
      interp.list_obj_type.listGetElements(args.get(args.size() - 1), pat_count_ptr, vector_ptr);
      pat_count = pat_count_ptr.get(0);
      case_list = vector.get(0);
    } else {
      ArrayList<ApwtclObj> my_args = new ArrayList<ApwtclObj>();
      for (int i = body_start_idx; i < args.size(); i++) {
         my_args.add(args.get(i));
      }
      case_list = my_args;
    }
    if (pat_count == 0 || pat_count % 2 != 0) {
      interp.wrongNumArgs(1, args, usage);
      return ERROR;
    }
    have_match = false;
    for (i = 0; script == null && i < pat_count; i += 2) {
      ApwtclObj pat_obj = case_list.get(i);

      if (!pat_obj.getString().equals("default") || i < (pat_count - 2)) {
        if (have_exact_opt) {
          if (str_obj.getString().equals(pat_obj.getString())) {
            script = case_list.get(i + 1);
	  }
	}
        if (have_glob_opt) {
          if (pat_obj.stringMatchObj(str_obj, false)) {
            script = case_list.get(i + 1);
	  }
	}
        if (have_regexp_opt) {
          command = interp.string_obj_type.newStringObj("regexp", -1, "PKG_CORE_6");
          have_command_opt = true;
	}
	ArrayList<ApwtclObj> subst_key_obj_ptr = new ArrayList<ApwtclObj>();
        if (interp.script_obj_type.substObj(str_obj, subst_key_obj_ptr, FUNCTION_FLAGS_NONE) != OK) {
	  return ERROR;
	}
	subst_key_obj = subst_key_obj_ptr.get(0);
        if (have_command_opt) {
          int rc = interp.eval_statement.commandMatchObj(command, pat_obj, subst_key_obj_ptr, false);
          /* After the execution of a command we need to
           * make sure to reconvert the object into a list
           * again. Only for the single-list style [switch]. */
          if (args.size() - i == 1) { 
            ArrayList<ApwtclObj> vector_ptr = new ArrayList<ApwtclObj>();
	    ArrayList<Integer> count_ptr = new ArrayList<Integer>();

            interp.list_obj_type.listGetElements(args.get(opt), count_ptr, vector_ptr);
	    pat_count = count_ptr.get(0);
            case_list = vector_ptr.get(0);
          }
          /* command is here already decref'd */
          if (rc < 0) {
//            return -rc;
            continue;
          }
          if (rc > 0) {
            script = case_list.get(i + 1);
	  }
        }
      } else {
        script = case_list.get(i + 1);
      }
    }
    for (; i < pat_count && script.getString().equals("-"); i += 2) {
      script = case_list.get(i + 1);
    }
    if (script != null && script.getString().equals("-")) {
        interp.setResultFormatted("no body specified for pattern \"%#s\"", case_list.get(i - 2));
        return ERROR;
    }
    interp.setEmptyResult();
    if (script!= null) {
      return interp.eval_statement.evalObj(script);
    }
    return OK;
  }

}