YAPListTerm::YAPListTerm(YAPTerm ts[], arity_t n) { CACHE_REGS BACKUP_H(); if (n == 0) t = TermNil; while (HR + n * 2 > ASP - 1024) { RECOVER_H(); if (!Yap_dogc(0, NULL PASS_REGS)) { t = TermNil; } BACKUP_H(); } t = AbsPair(HR); for (arity_t i = 0; i < n; i++) { HR[2 * i] = ts[i].gt(); HR[2 * i + 1] = AbsPair(HR + (2 * i + 2)); } }
Term Yap_MkNewPairTerm(void) { CACHE_REGS register CELL *p = HR; RESET_VARIABLE(HR); RESET_VARIABLE(HR+1); HR+=2; return (AbsPair(p)); }
static Int p_stream_to_codes(void) { int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2"); CELL *HBASE = H; CELL *h0 = &ARG4; if (sno < 0) return FALSE; while (!(Stream[sno].status & Eof_Stream_f)) { /* skip errors */ Int ch = Stream[sno].stream_getc(sno); Term t; if (ch == EOFCHAR) break; t = MkIntegerTerm(ch); h0[0] = AbsPair(H); *H = t; H+=2; h0 = H-1; if (H >= ASP-1024) { RESET_VARIABLE(h0); ARG4 = AbsPair(HBASE); ARG5 = (CELL)h0; if (!Yap_gcl((ASP-HBASE)*sizeof(CELL), 5, ENV, gc_P(P,CP))) { Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_stream_to_codes/3"); return FALSE; } /* build a legal term again */ h0 = (CELL *)ARG5; HBASE = RepPair(ARG4); } } UNLOCK(Stream[sno].streamlock); if (H == HBASE) return Yap_unify(ARG2,ARG3); RESET_VARIABLE(H-1); Yap_unify(H[-1],ARG3); return Yap_unify(AbsPair(HBASE),ARG2); }
static Int read_stream_to_codes(USES_REGS1) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "reaMkAtomTerm (AtomEofd_line_to_codes/2"); CELL *HBASE = HR; CELL *h0 = &ARG4; if (sno < 0) return FALSE; while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) { /* skip errors */ Int ch = GLOBAL_Stream[sno].stream_getc(sno); Term t; if (ch == EOFCHAR) break; t = MkIntegerTerm(ch); h0[0] = AbsPair(HR); *HR = t; HR += 2; h0 = HR - 1; yhandle_t news, news1, st = Yap_StartSlots(); if (HR >= ASP - 1024) { RESET_VARIABLE(h0); news = Yap_InitSlot(AbsPair(HBASE)); news1 = Yap_InitSlot((CELL)(h0)); if (!Yap_gcl((ASP - HBASE) * sizeof(CELL), 3, ENV, Yap_gcP())) { Yap_Error(RESOURCE_ERROR_STACK, ARG1, "read_stream_to_codes/3"); return false; } /* build a legal term again */ h0 = (CELL *)(Yap_GetFromSlot(news1)); HBASE = RepPair(Yap_GetFromSlot(news)); } Yap_CloseSlots(st); } UNLOCK(GLOBAL_Stream[sno].streamlock); if (HR == HBASE) return Yap_unify(ARG2, ARG3); RESET_VARIABLE(HR - 1); Yap_unify(HR[-1], ARG3); return Yap_unify(AbsPair(HBASE), ARG2); }
static void adjust_vector(CELL *pt, Int size) { /* the elements are where they should be */ CELL *ptf = pt + 2*(size-1); pt ++; while (pt < ptf) { pt[0] = AbsPair(pt+1); pt += 2; } /* close the list */ pt[0] = TermNil; }
static Int read_stream_to_terms(USES_REGS1) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2"); Term t, hd; yhandle_t tails, news; if (sno < 0) return FALSE; t = AbsPair(HR); RESET_VARIABLE(HR); Yap_InitSlot((CELL)(HR)); tails = Yap_InitSlot((CELL)(HR)); news = Yap_InitSlot((CELL)(HR)); HR++; while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) { RESET_VARIABLE(HR); RESET_VARIABLE(HR + 1); hd = (CELL)HR; Yap_PutInSlot(news, (CELL)(HR + 1)); HR += 2; while ((hd = Yap_read_term(sno, TermNil, 2)) == 0L) ; // just ignore failure CELL *pt = VarOfTerm(Yap_GetFromSlot(tails)); if (Deref(hd) == TermEOfCode) { *pt = Deref(ARG3); break; } else { CELL *newpt = (CELL *)Yap_GetFromSlot(news); *pt = AbsPair(newpt - 1); Yap_PutInSlot(tails, (CELL)newpt); } } UNLOCK(GLOBAL_Stream[sno].streamlock); return Yap_unify(t, ARG2); }
static Int p_msort( USES_REGS1 ) { /* use the heap to build a new list */ CELL *pt = HR; Term out; /* list size */ Int size; size = build_new_list(pt, Deref(ARG1) PASS_REGS); if (size < 0) return(FALSE); if (size < 2) return(Yap_unify(ARG1, ARG2)); pt = HR; /* because of possible garbage collection */ /* reserve the necessary space */ HR += size*2; simple_mergesort(pt, size, M_EVEN); adjust_vector(pt, size); out = AbsPair(pt); return(Yap_unify(out, ARG2)); }
Term Yap_MkNewApplTerm(Functor f, unsigned int n) /* build compound term with functor f and n * args a */ { CACHE_REGS CELL *t = HR; if (n == 0) return (MkAtomTerm(NameOfFunctor(f))); if (f == FunctorList) { RESET_VARIABLE(HR); RESET_VARIABLE(HR+1); HR+=2; return (AbsPair(t)); } *HR++ = (CELL) f; while (n--) { RESET_VARIABLE(HR); HR++; } return (AbsAppl(t)); }
static Int p_sort( USES_REGS1 ) { /* use the heap to build a new list */ CELL *pt = HR; Term out; /* list size */ Int size; size = build_new_list(pt, Deref(ARG1) PASS_REGS); if (size < 0) return(FALSE); if (size < 2) return(Yap_unify(ARG1, ARG2)); pt = HR; /* because of possible garbage collection */ /* make sure no one writes on our temp data structure */ HR += size*2; /* reserve the necessary space */ size = compact_mergesort(pt, size, M_EVEN); /* reajust space */ HR = pt+size*2; adjust_vector(pt, size); out = AbsPair(pt); return(Yap_unify(out, ARG2)); }