add support for creating a list of codes or atoms incrementally.

This commit is contained in:
Vítor Santos Costa
2008-12-22 13:49:44 +00:00
parent 7598b56c38
commit 8efcdf7eaa
8 changed files with 133 additions and 98 deletions

View File

@@ -218,23 +218,6 @@ mkfunction(iswupper)
mkfunction(iswpunct)
mkfunction(iswspace)
#ifdef __SWI_PROLOG__
#define INIT_DEF(Type, Name, Size) \
static void init_ ## Name (void) {} \
static const Type Name[] {
#define ADD_DEF2(Atom, Type) \
{ Atom, Type },
#define ADD_DEF5(Atom, Type, Reverse, Arity, Ctx) \
{ Atom, Type, Reverse, Arity, Ctx },
\
#define END_DEFS(Atom, F) \
{ Atom, F }
}
#endif
INIT_DEF(char_type, char_types, 26)
ADD_DEF2(ATOM_alnum, fiswalnum)
ADD_DEF2(ATOM_csym, fiscsym )

View File

@@ -1,6 +1,9 @@
#include "config.h"
#include <SWI-Prolog.h>
/* atom_t macro layer */
#define NULL_ATOM ((atom_t)0)
#include "atoms.h"
#if HAVE_STRING_H
#include <string.h>
#endif
@@ -262,11 +265,6 @@ PL_local_data_t lds;
#define TRY(goal) if ((goal) == FALSE) fail
/* atom_t macro layer */
#define NULL_ATOM ((atom_t)0)
#include "atoms.h"
atom_t source_file_name; /** source name of the current file that we are
consulting */
int source_line_no; /** guess.... */
@@ -489,7 +487,6 @@ extern int writeAtomToStream(IOSTREAM *so, atom_t at);
extern int valueExpression(term_t t, Number r ARG_LD);
extern word lookupAtom(const char *s, size_t len);
extern atom_t lookupUCSAtom(const pl_wchar_t *s, size_t len);
extern atom_t codeToAtom(int chrcode);
extern int toIntegerNumber(Number n, int flags);
extern int get_atom_ptr_text(Atom a, PL_chars_t *text);

View File

@@ -417,7 +417,7 @@ setOSFeatures(void)
/*******************************
* MEMORY *
*******************************/
#if SWI_PROLOG
#if __SWI_PROLOG__
uintptr_t
UsedMemory(void)
{

View File

@@ -34,7 +34,7 @@
#undef LD
#define LD LOCAL_LD
#ifdef SWI_PROLOG
#ifdef __SWI_PROLOG__
static inline word
valHandle__LD(term_t r ARG_LD)
{ Word p = valTermRef(r);
@@ -254,7 +254,7 @@ textToAtom(PL_chars_t *text)
}
#if SWI_PROLOG
#if __SWI_PROLOG__
word
textToString(PL_chars_t *text)
{ PL_canonise_text(text);
@@ -279,7 +279,7 @@ PL_unify_text(term_t term, term_t tail, PL_chars_t *text, int type)
return rval;
}
case PL_STRING:
#if SWI_PROLOG
#if __SWI_PROLOG__
{ word w = textToString(text);
return _PL_unify_atomic(term, w);
@@ -297,7 +297,6 @@ PL_unify_text(term_t term, term_t tail, PL_chars_t *text, int type)
}
} else
{ GET_LD
term_t l = PL_new_term_ref();
word p0, p;
switch(text->encoding)
@@ -305,36 +304,28 @@ PL_unify_text(term_t term, term_t tail, PL_chars_t *text, int type)
{ const unsigned char *s = (const unsigned char *)text->text.t;
const unsigned char *e = &s[text->length];
#if SWI_PROLOG
p0 = p = allocGlobal(text->length*3);
for( ; s < e; s++)
{ *p++ = FUNCTOR_dot2;
if ( type == PL_CODE_LIST )
*p++ = consInt(*s);
else
*p++ = codeToAtom(*s);
*p = consPtr(p+1, TAG_COMPOUND|STG_GLOBAL);
p++;
p0 = p = INIT_SEQ_CODES(text->length);
if ( type == PL_CODE_LIST ) {
for( ; s < e; s++)
p = EXTEND_SEQ_CODES(p, *s);
} else {
for( ; s < e; s++)
p = EXTEND_SEQ_ATOMS(p, *s);
}
#endif
break;
}
case ENC_WCHAR:
{ const pl_wchar_t *s = (const pl_wchar_t *)text->text.t;
const pl_wchar_t *e = &s[text->length];
#if SWI_PROLOG
p0 = p = allocGlobal(text->length*3);
for( ; s < e; s++)
{ *p++ = FUNCTOR_dot2;
if ( type == PL_CODE_LIST )
*p++ = consInt(*s);
else
*p++ = codeToAtom(*s);
*p = consPtr(p+1, TAG_COMPOUND|STG_GLOBAL);
p++;
p0 = p = INIT_SEQ_CODES(text->length);
if ( type == PL_CODE_LIST ) {
for( ; s < e; s++)
p = EXTEND_SEQ_CODES(p, *s);
} else {
for( ; s < e; s++)
p = EXTEND_SEQ_ATOMS(p, *s);
}
#endif
break;
}
case ENC_UTF8:
@@ -342,21 +333,22 @@ PL_unify_text(term_t term, term_t tail, PL_chars_t *text, int type)
const char *e = &s[text->length];
size_t len = utf8_strlen(s, text->length);
#if SWI_PROLOG
p0 = p = allocGlobal(len*3);
while(s<e)
{ int chr;
s = utf8_get_char(s, &chr);
*p++ = FUNCTOR_dot2;
if ( type == PL_CODE_LIST )
*p++ = consInt(chr);
else
*p++ = codeToAtom(chr);
*p = consPtr(p+1, TAG_COMPOUND|STG_GLOBAL);
p++;
p0 = p = INIT_SEQ_CODES(len);
if ( type == PL_CODE_LIST ) {
while (s < e) {
int chr;
s = utf8_get_char(s, &chr);
p = EXTEND_SEQ_CODES(p, chr);
}
} else {
while (s < e) {
int chr;
s = utf8_get_char(s, &chr);
p = EXTEND_SEQ_ATOMS(p, chr);
}
}
#endif
break;
}
case ENC_ANSI:
@@ -372,27 +364,21 @@ PL_unify_text(term_t term, term_t tail, PL_chars_t *text, int type)
n -= rc;
s += rc;
}
#if SWI_PROLOG
p0 = p = allocGlobal(len*3);
p0 = p = INIT_SEQ_CODES(len);
memset(&mbs, 0, sizeof(mbs));
n = text->length;
while(n > 0)
{ rc = mbrtowc(&wc, s, n, &mbs);
while(n > 0) {
rc = mbrtowc(&wc, s, n, &mbs);
*p++ = FUNCTOR_dot2;
if ( type == PL_CODE_LIST )
*p++ = consInt(wc);
p = EXTEND_SEQ_CODES(p, wc);
else
*p++ = codeToAtom(wc);
*p = consPtr(p+1, TAG_COMPOUND|STG_GLOBAL);
p++;
p = EXTEND_SEQ_ATOMS(p, wc);
s += rc;
n -= rc;
}
#endif
break;
}
default:
@@ -402,22 +388,7 @@ PL_unify_text(term_t term, term_t tail, PL_chars_t *text, int type)
}
}
#if SWI_PROLOG
setHandle(l, consPtr(p0, TAG_COMPOUND|STG_GLOBAL));
p--;
if ( tail )
{ setVar(*p);
if ( PL_unify(l, term) )
{ setHandle(tail, makeRefG(p));
return TRUE;
}
return FALSE;
} else
{ *p = ATOM_nil;
return PL_unify(l, term);
}
#endif
return CLOSE_SEQ_OF_CODES(p, p0, tail, term );
}
}
default:

View File

@@ -31,6 +31,25 @@ typedef YAP_Atom Atom;
typedef uintptr_t PL_atomic_t; /* same a word */
#ifdef __SWI_PROLOG__
/* just to make clear how it would look in SWI */
#define INIT_DEF(Type, Name, Size) \
static void init_ ## Name (void) {} \
static const Type Name[] {
#define ADD_DEF2(Atom, Type) \
{ Atom, Type },
#define ADD_DEF5(Atom, Type, Reverse, Arity, Ctx) \
{ Atom, Type, Reverse, Arity, Ctx },
\
#define END_DEFS(Atom, F) \
{ Atom, F }
}
#endif
#define INIT_DEF(Type, Name, Size) \
static Type Name[Size]; \
static void init_ ## Name (void) { \
@@ -109,20 +128,35 @@ typedef uintptr_t PL_atomic_t; /* same a word */
#define stopItimer()
/* TBD */
extern atom_t codeToAtom(int chrcode);
static inline word
INIT_SEQ_CODES(size_t n)
{
return 0L; /* TBD: shift */
return (word)YAP_OpenList(n);
}
static inline word
EXTEND_SEQ_CODES(word gstore, int c) {
return gstore;
return (word)YAP_ExtendList((YAP_Term)gstore, YAP_MkIntTerm(c));
}
static inline word
EXTEND_SEQ_ATOMS(word gstore, int c) {
return (word)YAP_ExtendList((YAP_Term)gstore, codeToAtom(c));
}
static inline int
CLOSE_SEQ_OF_CODES(word gstore, word lp, word t1, word t2) {
return TRUE;
CLOSE_SEQ_OF_CODES(word gstore, word lp, word arg2, word arg3) {
if (arg3 == (word)ATOM_nil) {
if (!YAP_CloseList((YAP_Term)gstore, YAP_TermNil()))
return FALSE;
} else {
if (!YAP_CloseList((YAP_Term)gstore, YAP_GetFromSlot(arg2)))
return FALSE;
}
return YAP_Unify(YAP_GetFromSlot(arg3), lp);
}
static inline Word