fix get_num init issues

This commit is contained in:
Vítor Santos Costa 2015-10-18 11:40:12 +01:00
parent 5762aa846c
commit 9fb7325f19

View File

@ -437,9 +437,7 @@ writing, writing a BOM can be requested using the option
static Term float_send(char *, int); static Term float_send(char *, int);
static Term get_num(int *, int *, struct stream_desc *, char *, UInt, int); static Term get_num(int *, int *, struct stream_desc *, char *, UInt, int);
static void static void Yap_setCurrentSourceLocation(struct stream_desc *s) {
Yap_setCurrentSourceLocation( struct stream_desc *s )
{
CACHE_REGS CACHE_REGS
#if HAVE_SOCKET #if HAVE_SOCKET
if (s->status & Socket_Stream_f) if (s->status & Socket_Stream_f)
@ -469,7 +467,8 @@ static char chtype0[NUMBER_OF_CHARS + 1] = {
BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
/* sp ! " # $ % & ' ( ) * + , - . / */ /* sp ! " # $ % & ' ( ) * + , - . / */
BS, SL, DC, SY, SY, CC, SY, QT, BK, BK, SY, SY, BK, SY, SY, SY, BS, SL, DC, SY, SY, CC, SY, QT, BK,
BK, SY, SY, BK, SY, SY, SY,
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, NU,
@ -581,6 +580,14 @@ typedef struct scanner_extra_alloc {
void *filler; void *filler;
} ScannerExtraBlock; } ScannerExtraBlock;
static void InitScannerMemory(void) {
CACHE_REGS
LOCAL_ErrorMessage = NULL;
LOCAL_Error_Size = 0;
LOCAL_ScannerStack = (char *)TR;
LOCAL_ScannerExtraBlocks = NULL;
}
static char *AllocScannerMemory(unsigned int size) { static char *AllocScannerMemory(unsigned int size) {
CACHE_REGS CACHE_REGS
char *AuxSpScan; char *AuxSpScan;
@ -687,7 +694,8 @@ static int send_error_message(char s[]) {
return 0; return 0;
} }
static wchar_t read_quoted_char(int *scan_nextp, struct stream_desc* inp_stream) { static wchar_t read_quoted_char(int *scan_nextp,
struct stream_desc *inp_stream) {
int ch; int ch;
/* escape sequence */ /* escape sequence */
@ -1095,38 +1103,91 @@ Term Yap_scan_num(StreamDesc *inp) {
Term out; Term out;
int sign = 1; int sign = 1;
int ch, cherr; int ch, cherr;
char *ptr; char *ptr, *mp;
int kind;
void *old_tr = TR;
LOCAL_ErrorMessage = NULL; InitScannerMemory();
LOCAL_ScannerStack = (char *)TR; LOCAL_VarTable = LOCAL_AnonVarTable = NULL;
LOCAL_ScannerExtraBlocks = NULL;
if (!(ptr = AllocScannerMemory(4096))) { if (!(ptr = AllocScannerMemory(4096))) {
LOCAL_ErrorMessage = "Trail Overflow"; LOCAL_ErrorMessage = "Trail Overflow";
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL; LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
return TermNil; return 0;
} }
TokEntry *tokptr = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
tokptr->TokPos = GetCurInpPos(inp);
ch = getchr(inp); ch = getchr(inp);
while (chtype(ch) == BS) {
ch = getchr(inp);
}
if (ch == '-') { if (ch == '-') {
sign = -1; sign = -1;
ch = getchr(inp); ch = getchr(inp);
} else if (ch == '+') { } else if (ch == '+') {
ch = getchr(inp); ch = getchr(inp);
} }
if (chtype(ch) != NU) { if (chtype(ch) == NU) {
Yap_clean_tokenizer((TokEntry *)LOCAL_ScannerStack, NULL, NULL);
return TermNil;
}
cherr = '\0'; cherr = '\0';
if (ASP - HR < 1024) if (ASP - HR < 1024) {
return TermNil; Yap_clean_tokenizer(old_tr, NULL, NULL);
LOCAL_ErrorMessage = "Stack Overflow";
LOCAL_Error_TYPE = RESOURCE_ERROR_STACK;
return 0;
}
out = get_num(&ch, &cherr, inp, ptr, 4096, sign); /* */ out = get_num(&ch, &cherr, inp, ptr, 4096, sign); /* */
}
if (LOCAL_ErrorMessage != NULL || ch != -1 || cherr) {
CACHE_REGS
char *s = ptr;
int sign = 1;
out = 0;
if (s[0] == '+') {
s++;
}
if (s[0] == '-') {
s++;
sign = -1;
}
if (strcmp(s, "inf") == 0) {
if (sign > 0) {
out = MkFloatTerm(INFINITY);
} else {
out = MkFloatTerm(-INFINITY);
}
}
if (strcmp(s, "nan") == 0) {
if (sign > 0) {
out = MkFloatTerm(NAN);
} else {
out = MkFloatTerm(-NAN);
}
}
if (out == 0) {
TokEntry *e, *ef;
size_t len = strlen(ptr);
mp = AllocScannerMemory(len + 1);
tokptr->Tok = Ord(kind = String_tok);
tokptr->TokInfo = Unsigned(mp);
e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
ef = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
tokptr->TokNext = e;
e->Tok = Error_tok;
if (!LOCAL_ErrorMessage)
LOCAL_ErrorMessage =
"syntax error while converting from a string to a number";
e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
e->TokPos = GetCurInpPos(inp);
e->TokNext = ef;
ef->Tok = Ord(kind = eot_tok);
ef->TokPos = GetCurInpPos(inp);
ef->TokNext = NULL;
LOCAL_tokptr = tokptr;
LOCAL_toktide = e;
LOCAL_ErrorMessage = NULL;
LOCAL_Error_Term = Yap_syntax_error(e, inp - GLOBAL_Stream);
LOCAL_Error_TYPE = SYNTAX_ERROR;
}
}
PopScannerMemory(ptr, 4096); PopScannerMemory(ptr, 4096);
Yap_clean_tokenizer((TokEntry *)LOCAL_ScannerStack, NULL, NULL); Yap_clean_tokenizer(old_tr, NULL, NULL);
if (LOCAL_ErrorMessage != NULL || ch != -1 || cherr)
return TermNil;
return out; return out;
} }
@ -1141,9 +1202,7 @@ Term Yap_scan_num(StreamDesc *inp) {
return l; \ return l; \
} }
const char * const char *Yap_tokRep(TokEntry *tokptr) {
Yap_tokRep(TokEntry *tokptr)
{
CACHE_REGS CACHE_REGS
Term info = tokptr->TokInfo; Term info = tokptr->TokInfo;
char *b, *buf = LOCAL_FileNameBuf2; char *b, *buf = LOCAL_FileNameBuf2;
@ -1154,13 +1213,14 @@ Yap_tokRep(TokEntry *tokptr)
case Name_tok: case Name_tok:
return (char *)RepAtom((Atom)info)->StrOfAE; return (char *)RepAtom((Atom)info)->StrOfAE;
case Number_tok: case Number_tok:
if ((b = Yap_TermToString(info, buf, sze, &length, &LOCAL_encoding, flags)) != buf) { if ((b = Yap_TermToString(info, buf, sze, &length, &LOCAL_encoding,
if (b) free(b); flags)) != buf) {
if (b)
free(b);
return NULL; return NULL;
} }
return buf; return buf;
case Var_tok: case Var_tok: {
{
VarEntry *varinfo = (VarEntry *)info; VarEntry *varinfo = (VarEntry *)info;
return varinfo->VarRep; return varinfo->VarRep;
} }
@ -1168,8 +1228,8 @@ Yap_tokRep(TokEntry *tokptr)
case BQString_tok: case BQString_tok:
return (char *)info; return (char *)info;
case WString_tok: case WString_tok:
case WBQString_tok: case WBQString_tok: {
{ wchar_t *op = (wchar_t *)info; wchar_t *op = (wchar_t *)info;
wchar_t c; wchar_t c;
unsigned char *bp = (unsigned char *)buf; unsigned char *bp = (unsigned char *)buf;
while ((c = *op++)) { while ((c = *op++)) {
@ -1182,8 +1242,7 @@ Yap_tokRep(TokEntry *tokptr)
return "<ERR>"; return "<ERR>";
case eot_tok: case eot_tok:
return "<EOT>"; return "<EOT>";
case Ponctuation_tok: case Ponctuation_tok: {
{
buf[1] = '\0'; buf[1] = '\0';
if ((info) == 'l') { if ((info) == 'l') {
buf[0] = '('; buf[0] = '(';
@ -1198,7 +1257,6 @@ Yap_tokRep(TokEntry *tokptr)
} }
} }
static void open_comment(int ch, StreamDesc *inp_stream USES_REGS) { static void open_comment(int ch, StreamDesc *inp_stream USES_REGS) {
CELL *h0 = HR; CELL *h0 = HR;
HR += 5; HR += 5;
@ -1214,8 +1272,7 @@ static void open_comment(int ch, StreamDesc *inp_stream USES_REGS) {
LOCAL_CommentsTail = h0 + 1; LOCAL_CommentsTail = h0 + 1;
h0 += 2; h0 += 2;
h0[0] = (CELL)FunctorMinus; h0[0] = (CELL)FunctorMinus;
h0[1] = Yap_StreamPosition(inp_stream-GLOBAL_Stream h0[1] = Yap_StreamPosition(inp_stream - GLOBAL_Stream);
);
h0[2] = TermNil; h0[2] = TermNil;
LOCAL_CommentsNextChar = h0 + 2; LOCAL_CommentsNextChar = h0 + 2;
LOCAL_CommentsBuff = (wchar_t *)malloc(1024 * sizeof(wchar_t)); LOCAL_CommentsBuff = (wchar_t *)malloc(1024 * sizeof(wchar_t));
@ -1244,9 +1301,7 @@ static void close_comment(USES_REGS1) {
// mark that we reached EOF, // mark that we reached EOF,
// next token will be end_of_file) // next token will be end_of_file)
static void static void mark_eof(struct stream_desc *inp_stream) {
mark_eof( struct stream_desc * inp_stream )
{
inp_stream->status |= Push_Eof_Stream_f; inp_stream->status |= Push_Eof_Stream_f;
} }
@ -1297,8 +1352,8 @@ static wchar_t *ch_to_wide(char *base, char *charp) {
} \ } \
} }
TokEntry *Yap_tokenizer( struct stream_desc *inp_stream, TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
bool store_comments, Term *tposp) { Term *tposp) {
CACHE_REGS CACHE_REGS
TokEntry *t, *l, *p; TokEntry *t, *l, *p;
@ -1308,12 +1363,9 @@ TokEntry *Yap_tokenizer( struct stream_desc *inp_stream,
wchar_t *wcharp; wchar_t *wcharp;
struct qq_struct_t *cur_qq = NULL; struct qq_struct_t *cur_qq = NULL;
LOCAL_ErrorMessage = NULL; InitScannerMemory();
LOCAL_Error_Size = 0;
LOCAL_VarTable = NULL; LOCAL_VarTable = NULL;
LOCAL_AnonVarTable = NULL; LOCAL_AnonVarTable = NULL;
LOCAL_ScannerStack = (char *)TR;
LOCAL_ScannerExtraBlocks = NULL;
l = NULL; l = NULL;
p = NULL; /* Just to make lint happy */ p = NULL; /* Just to make lint happy */
ch = getchr(inp_stream); ch = getchr(inp_stream);
@ -1420,7 +1472,8 @@ huge_var_error:
} }
add_ch_to_buff(ch); add_ch_to_buff(ch);
} }
while (ch == '\'' && isvar &&trueGlobalPrologFlag(VARIABLE_NAMES_MAY_END_WITH_QUOTES_FLAG)) { while (ch == '\'' && isvar &&
trueGlobalPrologFlag(VARIABLE_NAMES_MAY_END_WITH_QUOTES_FLAG)) {
if (charp == (char *)AuxSp - 1024) { if (charp == (char *)AuxSp - 1024) {
goto huge_var_error; goto huge_var_error;
} }
@ -1666,7 +1719,6 @@ quoted_string:
} else { } else {
t->Tok = Ord(kind = BQString_tok); t->Tok = Ord(kind = BQString_tok);
} }
} }
} else { } else {
if (wcharp) { if (wcharp) {
@ -1750,8 +1802,7 @@ quoted_string:
goto restart; goto restart;
} }
enter_symbol: enter_symbol:
if (och == '.' && if (och == '.' && (chtype(ch) == BS || chtype(ch) == EF || ch == '%')) {
(chtype(ch) == BS || chtype(ch) == EF || ch == '%')) {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
if (chtype(ch) == EF) if (chtype(ch) == EF)
mark_eof(inp_stream); mark_eof(inp_stream);
@ -1989,7 +2040,6 @@ enter_symbol:
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
return l; return l;
default: default:
#if DEBUG #if DEBUG
fprintf(stderr, "\n++++ token: wrong char type %c %d\n", ch, chtype(ch)); fprintf(stderr, "\n++++ token: wrong char type %c %d\n", ch, chtype(ch));
@ -1998,8 +2048,7 @@ enter_symbol:
} }
#if DEBUG #if DEBUG
if (GLOBAL_Option[2]) if (GLOBAL_Option[2])
fprintf(stderr, "[Token %d %s]", Ord(kind), fprintf(stderr, "[Token %d %s]", Ord(kind), Yap_tokRep(t));
Yap_tokRep( t ));
#endif #endif
if (LOCAL_ErrorMessage) { if (LOCAL_ErrorMessage) {
/* insert an error token to inform the system of what happened */ /* insert an error token to inform the system of what happened */
@ -2024,8 +2073,7 @@ enter_symbol:
return (l); return (l);
} }
void Yap_clean_tokenizer(TokEntry *tokstart, void Yap_clean_tokenizer(TokEntry *tokstart, VarEntry *vartable,
VarEntry *vartable,
VarEntry *anonvartable) { VarEntry *anonvartable) {
CACHE_REGS CACHE_REGS
struct scanner_extra_alloc *ptr = LOCAL_ScannerExtraBlocks; struct scanner_extra_alloc *ptr = LOCAL_ScannerExtraBlocks;