APWTCL Arnulf's Preferred Web Tcl

Check-in [7e61f47e50]
Login

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:7e61f47e50b8fee331c6ac965bb7088e0c0d284c
User & Date: arnulf 2012-04-18 16:50:13
Context
2012-04-18
16:51
initial version check-in: ab46330ed7 user: arnulf tags: trunk
16:50
initial version check-in: 7e61f47e50 user: arnulf tags: trunk
2012-04-17
20:26
fixes and debug stuff check-in: b55c80e855 user: arnulf tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added src/org/apwtcl/lang/cmd/ArrayCommand.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
/*=======================================================
 * ArrayCommand.java 
 *
 * "A Tcl like language implementation in Java named APWTCL
 * ((Java) Arnulf's Preferred Web Tcl)"
 *
 * APWTCL ArrayCommand 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;
import org.apwtcl.lang.Variable;

public class ArrayCommand extends Token {

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

  /* ==================== initSubCmds ================================== */
  public int initSubCmds(Interp interp) {
//print("array initSubCmds called"+interp+"!");
    interp.command_obj_type.registerNativeSubCommand("::array", "exists", "ArrayCommand");
    interp.command_obj_type.registerNativeSubCommand("::array", "get", "ArrayCommand");
    interp.command_obj_type.registerNativeSubCommand("::array", "names", "ArrayCommand");
    interp.command_obj_type.registerNativeSubCommand("::array", "set", "ArrayCommand");
    interp.command_obj_type.registerNativeSubCommand("::array", "size", "ArrayCommand");
    interp.command_obj_type.registerNativeSubCommand("::array", "unset", "ArrayCommand");
    return OK;
  }

  /* ==================== arrayCmd ================================== */
  public int arrayCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array called!"+args+"!");
    return ERROR;
  }

  /* ==================== ararySubcmdsCmd ================================== */
  public int arraySubcmdsCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array subcmds called");
    ApwtclObj obj_ptr = interp.string_obj_type.newStringObj("array", -1, "ARRAY_COMMAND_1");
    ApwtclObj result_ptr;
    obj_ptr.incrRefCount("ARRAY_COMMAND_2");
    result_ptr = interp.command_obj_type.getSubCommands(obj_ptr);
    obj_ptr.decrRefCount("ARRAY_COMMAND_3");
    if (result_ptr == null) {
      return ERROR;
    }
    interp.setResult(result_ptr);
    print("sub cmds for !array!: "+result_ptr+"!");
    return OK;
  }

  /* ==================== arrayExistsCmd ================================== */
  public int arrayExistsCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array exists called");
    if (args.size() != 2) {
      interp.wrongNumArgs(2, args, "array_name");
      return ERROR;
    }
    /* Just a regular [info exists] */
    ArrayList<Variable> var_ptr_ptr = new ArrayList<Variable>();
    ApwtclObj ret = interp.variable_obj_type.getVariable(args.get(0), FUNCTION_FLAGS_NONE, var_ptr_ptr);
print("ret!"+ret+"!");
    interp.setResultInt((interp.variable_obj_type.getVariable(args.get(0), FUNCTION_FLAGS_NONE, var_ptr_ptr) != null) ? 1 : 0);
    return OK;
  }

  /* ==================== arrayGetCmd ================================== */
  public int arrayGetCmd(Interp interp, ArrayList<ApwtclObj> args) {
//print("array get called");
    int i;
    ArrayList<Integer> len_ptr = new ArrayList<Integer>();
    boolean all = false;
    ApwtclObj result_obj;
    ApwtclObj obj_ptr;
    ArrayList<ApwtclObj> dict_obj_ptr = new ArrayList<ApwtclObj>();
    ApwtclObj dict_obj;
    ArrayList<ArrayList<ApwtclObj>> dict_values_obj_ptr = new ArrayList<ArrayList<ApwtclObj>>();
    ArrayList<ApwtclObj> dict_values_lst;
    ArrayList<Variable> var_var_ptr = new ArrayList<Variable>();
    Variable result_var_ptr;
    int len;

    if (args.size() != 2 && args.size() != 3) {
      interp.wrongNumArgs(1, args, "array_name ?pattern?");
      return ERROR;
    }
    obj_ptr = interp.variable_obj_type.getVariable(args.get(1), FUNCTION_FLAGS_NONE, var_var_ptr);
    result_var_ptr = var_var_ptr.get(0);
    if (result_var_ptr == null) {
        return ERROR;
    }

    if (args.size() == 2 || (args.get(1).getString().equals("*"))) {
      all = true;
    }
    /* if the variable is no array or the array has no elements or the variable is not
     * initialized return an empty list
     */
    if (!result_var_ptr.isArray() || result_var_ptr.isUndefined()) {
      interp.setResult(interp.empty_string_obj);
      return OK;
    }
    /* If it is a dictionary or list with an even number of elements, nothing else to do */
    if (all) {
      if (interp.dict_obj_type.isDict(obj_ptr) || (obj_ptr.isListObj() && interp.list_obj_type.listLength(obj_ptr) % 2 == 0)) {
        interp.setResult(obj_ptr);
        return OK;
      }
    }
    if (interp.dict_obj_type.dictKeysVector(obj_ptr, null, dict_obj_ptr, FUNCTION_FLAGS_LEAVE_ERR_MSG) != OK) {
      return ERROR;
    }
    dict_obj = dict_obj_ptr.get(0);
    if (interp.dict_obj_type.dictPairs(dict_obj, dict_values_obj_ptr, len_ptr) != OK) {
      return ERROR;
    }
    dict_values_lst = dict_values_obj_ptr.get(0);
    len = len_ptr.get(0);
    if (all) {
      /* Return the whole array */
      interp.setResult(dict_obj);
    } else {
      /* Only return the matching values */
      result_obj = interp.list_obj_type.newListObj(null, 0);
      for (i = 0; i < len; i += 2) {
        if (args.get(1).stringMatchObj(dict_values_lst.get(i), 0) == 0) {
          interp.list_obj_type.listAppendElement(result_obj, dict_values_lst.get(i));
          interp.list_obj_type.listAppendElement(result_obj, dict_values_lst.get(i + 1));
        }
      }
      interp.setResult(result_obj);
    }
    return OK;
  }

  /* ==================== arrayNamesCmd ================================== */
  public int arrayNamesCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array names called");
    print("array names not yet implemented");
    return ERROR;
  }

  /* ==================== arraySetCmd ================================== */
  public int arraySetCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array set called");
    interp.setResultString("array set not yet reimplemented");
    return ERROR;
  }

  /* ==================== arraySizeCmd ================================== */
  public int arraySizeCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array size called");
    interp.setResultString("array size not yet reimplemented");
    return ERROR;
  }

  /* ==================== arrayUnsetCmd ================================== */
  public int arrayUnsetCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("array unset called");
    interp.setResultString("array unset not yet reimplemented");
    return ERROR;
  }
}

Added src/org/apwtcl/lang/cmd/SplitCommand.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
/*=======================================================
 * SplitCommand.java 
 *
 * "A Tcl like language implementation in Java named APWTCL
 * ((Java) Arnulf's Preferred Web Tcl)"
 *
 * APWTCL SplitCommand 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 SplitCommand extends Token {

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

  /* ==================== splitCmd ================================== */
  public int splitCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("split called!"+args+"!");
    ApwtclObj script_obj_ptr;
    String str;
    String split_chars;
    int no_match_start;
    int split_len;
    int str_len;
    ApwtclObj res_obj_ptr;
    int len;
    int idx = 0;
    ArrayList<Character> c_ptr = null;

    if (args.size() != 2 && args.size() != 3) {
      interp.wrongNumArgs(1, args, "string ?splitChars?");
      return ERROR;
    }

    str = args.get(1).getString();
    len = args.get(1).getStringLength();
    if (len == 0) {
      return OK;
    }
    str_len = interp.default_obj.utf8_length(args.get(1));

    /* Init */
    if (args.size() == 2) {
      split_chars = " \n\t\r";
      split_len = 4;
    } else {
      split_chars = args.get(2).getString();
      split_len = interp.default_obj.utf8_length(args.get(2));
    }
    no_match_start = idx;
    res_obj_ptr = interp.list_obj_type.newListObj(null, 0);
    /* Split */
    if (split_len != 0) {
      ApwtclObj obj_ptr;
      while (str_len-- > 0) {
        c_ptr = new ArrayList<Character>();
        int sc_idx = 0;
        String sc = split_chars;
        int sc_len = split_len;
        int sl = interp.default_obj.utf8_tounicode(str.substring(idx), c_ptr);
        int sc_idx2 = 0;
        while (sc_len-- > 0) {
          ArrayList<Character> pc_ptr = new ArrayList<Character>();
          sc_idx += interp.default_obj.utf8_tounicode(sc.charAt(sc_idx), pc_ptr);
          if (c_ptr.get(0) == pc_ptr.get(0)) {
            obj_ptr = interp.string_obj_type.newStringObj(str.substring(no_match_start), (idx - no_match_start), "PKG_CORE_3");
            interp.list_obj_type.listAppendElement(res_obj_ptr, obj_ptr);
            no_match_start = idx + sl;
            break;
          }
        }
        idx += sl;
      }
      obj_ptr = interp.string_obj_type.newStringObj(str.substring(no_match_start), (idx - no_match_start), "PKG_CORE_4");
      interp.list_obj_type.listAppendElement(res_obj_ptr, obj_ptr);
    } else {
      /* This handles the special case of splitchars eq {}
       * Optimise by sharing common (ASCII) characters
       */
      while (str_len-- > 0) {
    	c_ptr = new ArrayList<Character>();
        int n = interp.default_obj.utf8_tounicode(str, c_ptr);
//        interp.list_obj_type.listAppendElement(res_obj_ptr, interp.string_obj_type.newStringObjUtf8(str.substring(idx), 1, "PKG_CORE_5"));
        interp.list_obj_type.listAppendElement(res_obj_ptr, interp.string_obj_type.newStringObj(str.substring(idx), 1, "PKG_CORE_5"));
        idx += n;
      }
    }
    interp.setResult(res_obj_ptr);
    return OK;
  }

}