(GetTask task0, Map<term_t, Variable> vars_to_Vars)
| 1094 | } |
| 1095 | |
| 1096 | protected static Term getLoop(GetTask task0, Map<term_t, Variable> vars_to_Vars) { |
| 1097 | GetTask top = task0; // initially, our stack is just the given task |
| 1098 | StringHolder hString = new StringHolder(); // used & reused within switch |
| 1099 | StringHolder hName = new StringHolder(); // used & reused within switch |
| 1100 | IntHolder hArity = new IntHolder(); // used & reused within switch |
| 1101 | Int64Holder hInt64 = new Int64Holder(); // used & reused within switch |
| 1102 | DoubleHolder hDouble = new DoubleHolder(); // used & reused within switch |
| 1103 | ObjectHolder hObject = new ObjectHolder(); // used & reused within switch |
| 1104 | while (top != null) { // there is a top-most task to do next |
| 1105 | GetTask task = top; // in case we pop or push before we've finished referring to it |
| 1106 | term_t term; // temp for a term ref for the switch, set appropriately before |
| 1107 | Term t; // temp for a Term from the switch, stashed appropriately after |
| 1108 | // set up term for the switch, according to task, and pop task if complete this cycle |
| 1109 | if (task.n > 0) { // get n-th arg of compound in task.term |
| 1110 | term = Prolog.new_term_ref(); |
| 1111 | Prolog.get_arg(task.n, task.term, term); // set up term as ref to the n-th arg of compound |
| 1112 | if (task.n == task.arity) { // this recurrent task gets the last arg of a compound |
| 1113 | top = top.prev; // pop this recurrent args-of-compound task (complete this cycle) |
| 1114 | } |
| 1115 | } else if (top.n < 0) { // get (-n)th arg of dict in task.term |
| 1116 | term = Prolog.new_term_ref(); |
| 1117 | Prolog.get_arg(-task.n, task.term, term); // set up term as ref to (-n)-th arg of dict |
| 1118 | if (-task.n == task.arity) { // this recurrent task gets the last arg of a dict |
| 1119 | top = top.prev; // pop this recurrent args-of-dict task (complete this cycle) |
| 1120 | } |
| 1121 | } else { // special, initial case: just get a term |
| 1122 | term = task.term; // get the term in task.term |
| 1123 | top = top.prev; // pop this simple task (complete this cycle) |
| 1124 | } |
| 1125 | switch (Prolog.term_type(term)) { // get term into t (to be stashed appropriately later) |
| 1126 | case Prolog.VARIABLE: |
| 1127 | t = null; // provisionally, until/unless set within this for loop |
| 1128 | for (Iterator<term_t> it = vars_to_Vars.keySet().iterator(); it.hasNext();) { |
| 1129 | term_t varX = it.next(); // a previously seen Prolog variable |
| 1130 | if (Prolog.compare(varX, term) == 0) { // referenced variables are identical |
| 1131 | t = (Term) vars_to_Vars.get(varX); // yield this (reused) Variable |
| 1132 | break; // out of enclosing for loop |
| 1133 | } |
| 1134 | } |
| 1135 | if (t == null) { // the Prolog variable in term has not been seen so far in this get |
| 1136 | Variable Var = new Variable(); // allocate a new (sequentially named, e.g. "_163") Variable |
| 1137 | Var.term_ = term; // TODO understand this :-/ |
| 1138 | vars_to_Vars.put(term, Var); // enmap new Variable, keyed by term ref, lest we see it again |
| 1139 | t = Var; // yield this new Variable |
| 1140 | } // else a reused Variable was assigned to t in the for loop above |
| 1141 | break; |
| 1142 | case Prolog.ATOM: // specifically a "text" atom (see also Prolog.BLOB) |
| 1143 | if (Prolog.get_atom_chars(term, hString)) { |
| 1144 | t = new Atom(hString.value, "text"); |
| 1145 | } else { |
| 1146 | throw new JPLException("Prolog.get_atom_chars failed"); |
| 1147 | } |
| 1148 | break; |
| 1149 | case Prolog.STRING: |
| 1150 | if (Prolog.get_string_chars(term, hString)) { |
| 1151 | t = new Atom(hString.value, "string"); |
| 1152 | } else { |
| 1153 | throw new JPLException("Prolog.get_string_chars failed"); |
no test coverage detected