ECLiPSe offers the user the possibility to create compound terms on the global stack and unify them with Prolog terms. The global stack is the place where the system stores all compound terms and strings to which Prolog variables are bound (recorded terms, arrays and asserted clauses are stored elsewhere). Care must be taken when manipulating the global stack, since the system has no means of checking the correctness of user's data or data changes. The use of the global stack should therefore be an exception, as most of the compound terms can be much easier manipulated by regular Prolog procedures. The global stack is accessed using the pointer
which points to the global stack top (to the first unused item). The global stack grows towards higher addresses.pword *TG;
CAUTION: Two points must be taken care of when using the global stack, otherwise the integrity of the whole system is no longer guaranteed.
For illustration, here follows the code for the external procedure transform/2 whose input is a structure and whose output argument is unified with a structure which is identical to the input one except that is has one more argument at the end which is a free variable:pword *pw = TG; /* pointer to the structure */ Push_Struct_Frame(Did("p", 2)); /* allocate 3 pwords and */ /* initialize the functor */ Make_Float(&pw[1], 3.0); /* fill in the first argument */ Make_Nil(&pw[2]); /* fill in the second argument */
int p_transform(val1, tag1, val2, tag2) value val1, val2; type tag1, tag2; { pword *p = TG; word32 did1; /* the DID of the structure */ int arity; /* its arity */ int i; /* the first argument must be a structure */ Check_Structure(tag1); /* the second argument must be a structure or a variable */ Check_Output_Structure(tag2); /* val1 points to the functor */ did1 = val1.ptr->val.did; arity = DidArity(did1); /* reserve space for the functor and (arity + 1) args */ Push_Struct_Frame(Did(DidName(did1), arity + 1)); /* copy the arguments */ for (i = 1; i <= arity; i++) { p[i] = val1.ptr[i]; } /* now create the free variable in the last argument */ Make_Var(&p[arity + 1]); /* and unify with the second argument */ Return_Unify_Structure(val2, tag2, p); }