APWTCL Arnulf's Preferred Web Tcl

Check-in [ed72d1990a]
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:ed72d1990a8b7a2bd0e4a05a335c698c8f6338db
User & Date: arnulf 2012-04-13 09:52:23
Context
2012-04-13
14:08
fixes and a lot of new code for variable handling check-in: f0cede5197 user: arnulf tags: trunk
09:52
initial version check-in: ed72d1990a user: arnulf tags: trunk
07:27
fixes. t1.test - t19.test now running successful check-in: 07e8f34eb1 user: arnulf tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

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

  /* ==================== variableCmd ================================== */
  public int variableCmd(Interp interp, ArrayList<ApwtclObj> args) {
//print("variable called!"+args+"!");
    String var_name;
    String tail;
    int i;
    int j;
    var cp;
    Variable var_ptr;
    ArrayList<Variable> array_ptr = new ArrayList<Variable>();
    var var_value_ptr;
    int result;
    ApwtclPtr var_name_ptr;
    var tail_ptr;

    if (args.size() < 2) {
      interp.wrongNumArgs(interp, 1, args, "variable ?value variable value ...?");
      return ERROR;
    }
    for (i = 1 ; i < args.size() ; i += 2) {
      /*
       * Look up each variable in the current namespace context, creating it
       * if necessary.
       */
      var_name_ptr = args.get(i);
      var_name = var_name_ptr.getString();
      var_ptr = interp.variable_obj_type.objLookupVarEx(var_name_ptr, null,
        (NAMESPACE_ONLY | FUNCTION_FLAGS_LEAVE_ERR_MSG), "define",
        /*createPart1*/ 1, /*createPart2*/ 0, array_ptr);
      if (array_ptr[0] != null) {
        /*
         * Variable cannot be an element in an array. If arrayPtr is
         * non-NULL, it is, so throw up an error and return.
         */
        interp.objVarErrMsg(var_name_ptr, null, "define", is_array_element, -1);
	ArrayList<String> ll = new ArrayList<String>();
	ll.add("TCL");
	ll.add("UPVAR");
	ll.add("LOCAL_ELEMENT");
        interp.setErrorCode(ll);
        return ERROR;
      }
      if (var_ptr == null) {
        return ERROR;
      }
      /*
       * Mark the variable as a namespace variable and increment its
       * reference count so that it will persist until its namespace is
       * destroyed or until the variable is unset.
       */
      interp.variable_obj_type.setVarNamespaceVar(var_ptr);
      /*
       * If a value was specified, set the variable to that value.
       * Otherwise, if the variable is new, leave it undefined. (If the
       * variable already exists and no value was specified, leave its value
       * unchanged; just create the local link if we're in a RAPL procedure).
       */
      if (i + 1 < args.length) {	/* A value was specified. */
        var_value_ptr = interp.variable_obj_type.ptrSetVar(var_ptr, array_ptr, var_name_ptr, null, args.get(i + 1),
            NAMESPACE_LOOKUP_NAMESPACE_ONLY | FUNCTION_FLAGS_LEAVE_ERR_MSG, -1);
        if (var_value_ptr == null) {
          return interp.ERROR;
        }
      }
      /*
       * If we are executing inside a Rapl procedure, create a local variable
       * linked to the new namespace variable "var_name".
       */
      if (interp.var_frame_ptr.hasLocalVars()) {
        /*
         * var_name might have a scope qualifier, but the name for the
         * local "link" variable must be the simple name at the tail.
         *
         * Locate tail in one pass: drop any prefix after two *or more*
         * consecutive ":" characters).
         */
        tail = var_name;
        i = 0;
        j = i;
        for (; i < var_name.length(); i++) {
          if (tail.charAt(i) == ':') {
	    j = i;
            while (tail.charAt(j) == ':') {
	      j++;
            }
          }
        }
        tail = tail.substring(j);
        /*
         * Create a local link "tail" to the variable "var_name" in the
         * current namespace.
         */
        if (tail == var_name) {
          tail_ptr = var_name_ptr;
        } else {
	  tail_ptr = interp.string_obj_type.newStringObj(tail, -1, "PKG_CORE_8");
	  tail_ptr.incrRefCount("I_PKG_CORECMD_6");
        }
        result = interp.variable_obj_type.objMakeUpvar(null, var_name_ptr, /*other_p2*/ null,
	      /*other_flags*/ interp.NAMESPACE_LOOKUP_NAMESPACE_ONLY,
	      /*my_name*/ tail_ptr, /*my_flags*/ 0, -1);
        if (tail != var_name) {
	  tail_ptr.decrRefCount("D_PKG_CORE_CMD_7");
        }
        if (result != OK) {
          return result;
        }
      }
    }
    return OK;
  }

}