APWTCL Arnulf's Preferred Web Tcl

Check-in [e043a13a98]
Login

Check-in [e043a13a98]

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

Overview
Comment:initial version
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e043a13a98ab8abc18c4e31d7c2a4842ab6a160e
User & Date: arnulf 2012-04-19 09:45:34.793
Context
2012-04-19
13:02
initial version check-in: 45969a1e49 user: arnulf tags: trunk
09:45
initial version check-in: e043a13a98 user: arnulf tags: trunk
09:45
fixes and debug stuff check-in: bbfafd8d55 user: arnulf tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Added src/org/apwtcl/lang/cmd/ForCommand.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
/*=======================================================
 * ForCommand.java 
 *
 * "A Tcl like language implementation in Java named APWTCL
 * ((Java) Arnulf's Preferred Web Tcl)"
 *
 * APWTCL ForCommand 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.CallFrame;
import org.apwtcl.lang.Command;
import org.apwtcl.lang.Interp;
import org.apwtcl.lang.ApwtclObj;
import org.apwtcl.lang.Token;

public class ForCommand extends Token {

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

  /* ==================== forCmd ================================== */
  public int forCmd(Interp interp, ArrayList<ApwtclObj> args) {
//print("for called!"+args+"!");
    int retval;
    int bool = 1;
    ArrayList<Integer> bool_ptr = new ArrayList<Integer>();
    ApwtclObj var_name_ptr = null;
    ApwtclObj stop_var_name_ptr = null;
    boolean evalstart = false;
    boolean evalnext = false;
    boolean out = false;
    boolean testcond = false;

    if (args.size() != 5) {
      interp.wrongNumArgs(1, args, "start test next body");
      return ERROR;
    }
    /* Do the initialisation */
    if ((retval = interp.eval_statement.evalObj(args.get(1))) != OK) {
      return retval;
    }
    /* And do the first test now. Better for optimisation
     * if we can do next/test at the bottom of the loop
     */
    retval = interp.eval_expression.getBoolFromExpr(args.get(2), bool_ptr);
    bool = bool_ptr.get(0);
    /* Ready to do the body as follows:
     * while (1) {
     *     body // check retcode
     *     next // check retcode
     *     test // check retcode/test bool
     * }
     */

/*
if (false) {
    /* Check if the for is on the form:
     *      for ... {$i < CONST} {incr i}
     *      for ... {$i < $j} {incr i}
     */
/*
    if (retval == interp.OK && bool_ptr) {
      var incr_script;
      var expr;
      var stop = new Array();
      var current_val = new Array();
      var proc_epoch;
      var obj_ptr;
      var cmp_offset;


      /* Do it only if there aren't shared arguments */
/*
      expr = interp.eval_expression.getExpression(args[2]);
      incr_script = interp.script_obj_type.getScript(args[3]);
      /* Ensure proper lengths to start */
/*
      if (incr_script.len != 3 || !expr || expr.len != 3) {
        evalstart = true;
      }
      /* Ensure proper token types. */
/*
      if (!evalstart) {
        if (incr_script.script_object.tokens[1].token != interp.TOKEN_ESC ||
          expr.token_list[0].token != interp.TOKEN_VAR ||
          (expr.token_list[1].token != interp.TOKEN_EXPR_INT && expr.token_list[1].token != interp.TOKEN_VAR)) {
          evalstart = true;
        }
      }
      if (!evalstart) {
        if (expr.token_list[2].token == interp.TOKEN_EXPROP_LT) {
          cmp_offset = 0;
        } else {
          if (expr.token_list[2].token == interp.TOKEN_EXPROP_LTE) {
            cmp_offset = 1;
          } else {
            evalstart = true;
          }
        }
      }
      if (!evalstart) {
        /* Update command must be incr */
/*
        if (!incr_script.script_object.tokens[1].obj_ptr.getString() == "incr") {
          evalstart = true;
        }
      }
        /* incr, expression must be about the same variable */
/*
      if (!evalstart) {
        if (!interp.string_obj_type.stringEqObj(incr_script.script_object.tokens[2].obj_ptr, expr.token_list[0].obj_ptr)) {
          evalstart = true;
        }
      }
        /* Get the stop condition (must be a variable or integer) */
/*
      if (!evalstart) {
        if (expr.token_list[1].tken == interp.TOKEN_EXPR_INT) {
          if (interp.default_obj.getWide(expr.token_list[1].obj_ptr, stop) == interp.ERROR) {
            evalstart = true;
          }
	  stop = stop[0];
        } else {
            stop_var_name_ptr = expr.token_list[1].obj_ptr;
            stop_var_name_ptr.incrRefCount("I_PKG_CORECMD_2");
            /* Keep the compiler happy */
/*
            stop = 0;
        }
      }
      if (!evalstart) {
        /* Initialization */
/*
        proc_epoch = interp.proc_epoch;
        var_name_ptr = expr.token_list[0].obj_ptr;
        var_name_ptr.incrRefCount("I_PKG_CORECMD_3");

        obj_ptr = interp.variable_obj_type.getVariable(var_name_ptr, interp.FUNCTION_FLAGS_NONE);
	current_val = new Array();
        if (obj_ptr == null || interp.default_obj.getWide(obj_ptr, current_val) != interp.OK) {
          testcond = true;
        }
	current_val = current_val[0];
      }
      if (!testcond) {
        /* --- OPTIMIZED FOR --- */
/*
        while (retval == interp.OK) {
          /* === Check condition === */
          /* Note that currentVal is already set here */
          /* Immediate or Variable? get the 'stop' value if the latter. */
/*
          if (stop_var_name_ptr) {
            obj_ptr = interp.variable_obj_type.getVariable(stop_var_name_ptr, interp.FUNCTION_FLAGS_NONE);
            if (obj_ptr == null || interp.default_obj.getWide(obj_ptr, stop) != interp.OK) {
              testcond = true;
	      break;
            }
          }
          if (current_val >= stop + cmp_offset) {
            break;
          }
          /* Eval body */
/*
          retval = interp.eval_statement.evalObj(args[4]);
          if (retval == interp.OK || retval == interp.CONTINUE) {
            retval = interp.OK;
            /* If there was a change in procedures/command continue
             * with the usual [for] command implementation */
/*
            if (proc_epoch != interp.proc_epoch) {
              evalnext = true;
            }
	    if (!evalnext) {
              obj_ptr = interp.variable_obj_type.getVariable(var_name_ptr, interp.FUNCTION_FLAGS_LEAVE_ERRMSG);
              /* Increment */
/*
              if (obj_ptr == null) {
                retval = interp.ERROR;
                out = true;
		break;
              }
              if (!obj_ptr.isShared() && obj_ptr.obj_type == interp.int_obj_type) {
                currentVal = ++interp.default_obj.wideValue(obj_ptr);
                obj_ptr.invalidateStringRep();
              } else {
                current_value = new Array();
                if (interp.default_obj.getWide(obj_ptr, current_val) != interp.OK ||
                    interp.variable_obj_type.setVariable(var_name_ptr, interp.int_obj_type.newIntObj(++current_val[0])) != interp.OK) {
                  evalnext = true;
                }
              }
            }
          }
        }
        out = true;
      }
    }
}
*/
evalstart = true;
evalnext = true;
testcond = true;
    if (evalstart) {
      while (bool != 0 && (retval == OK || retval == CONTINUE)) {
        /* Body */
        retval = interp.eval_statement.evalObj(args.get(4));
        if (retval == OK || retval == CONTINUE) {
          /* increment */
          if (evalnext) {
            retval = interp.eval_statement.evalObj(args.get(3));
            if (retval == OK || retval == CONTINUE) {
                /* test */
              if (testcond) {
                bool_ptr = new ArrayList<Integer>();
                retval = interp.eval_expression.getBoolFromExpr(args.get(2), bool_ptr);
                bool = bool_ptr.get(0);
              }
            }
          }
        }
      }
    }
    if (out) {
      if (stop_var_name_ptr != null) {
        stop_var_name_ptr.decrRefCount("D_PKG_CORE_CMD_3");
      }
    }
    if (var_name_ptr != null) {
      var_name_ptr.decrRefCount("D_PKG_CORE_CMD_4");
    }
    if (retval == CONTINUE || retval == BREAK || retval == OK) {
      interp.setEmptyResult();
      return OK;
    }
    return retval;
  }

}