APWTCL Arnulf's Preferred Web Tcl

Check-in [45969a1e49]
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:45969a1e493b788198a87d94de608a042450a85a
User & Date: arnulf 2012-04-19 13:02:11
Context
2012-04-20
12:34
fixes check-in: 64926c5351 user: arnulf tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

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

  /* ==================== linsertCmd ================================== */
  public int linsertCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("linsert called!"+args+"!");
    ArrayList<Integer> idx_ptr = new ArrayList<Integer>();
    int idx;
    int len;
    boolean err = false;
    ApwtclObj list_ptr;

    if (args.size() < 4) {
        interp.wrongNumArgs(1, args, "list index element ?element ...?");
        return ERROR;
    }
    list_ptr = args.get(1);
    if (list_ptr.isShared())
        list_ptr = list_ptr.duplicateObj();
    if (interp.index_obj_type.getIndex(args.get(2), idx_ptr) != OK) {
      err = true;
    }
    if (!err) {
      idx = idx_ptr.get(0);
      len = interp.list_obj_type.listLength(list_ptr);
      if (idx >= len) {
          idx = len;
      } else {
        if (idx < 0) {
          idx = len + idx + 1;
        }
      }
      ArrayList<ApwtclObj> my_args = new ArrayList<ApwtclObj>();
      for (int i = 3; i < args.size(); i++) {
        my_args.add(args.get(i));
      }
      interp.list_obj_type.listInsertElements(list_ptr, idx, my_args.size(), my_args);
      interp.setResult(list_ptr);
      return OK;
    }
    if (list_ptr != args.get(1)) {
        list_ptr.freeNewObj("LINSERT_COMMAND_");
    }
    return ERROR;
  }

}