X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=rmac;a=blobdiff_plain;f=token.c;h=a1d3be8203501a32bb1c5420a6251918aed6f0e9;hp=27a7f3201f20f2b5dcde958c12b3df670e8b89a3;hb=322522e2a9c1469134226f5d978e7ba900c1893d;hpb=d16b8ea0ee65b2ad901ca6b0624c07e6e4930cc4 diff --git a/token.c b/token.c index 27a7f32..a1d3be8 100644 --- a/token.c +++ b/token.c @@ -32,7 +32,7 @@ WORD cfileno; // Current file number TOKEN * tok; // Ptr to current token TOKEN * etok; // Ptr past last token in tokbuf[] TOKEN tokeol[1] = {EOL}; // Bailout end-of-line token -char * string[TOKBUFSIZE]; // Token buffer string pointer storage +char * string[TOKBUFSIZE*2]; // Token buffer string pointer storage // File record, used to maintain a list of every include file ever visited #define FILEREC struct _filerec @@ -116,20 +116,91 @@ static char * riscregname[] = { }; +// Removing this, provided it doesn't cause unwanted side-effects :-P +#if 0 // // Make `fnum' the Current `curfname' +// NOTE: This is currently only called from error() in error.c // void setfnum(WORD fnum) { - FILEREC * fr; +#if 0 + // NOTE: fnum is ZERO based, this can cause problems if you're not careful! + FILEREC * fr = filerec; - for(fr=filerec; fr!=NULL && fnum--; fr=fr->frec_next); + DEBUG printf("[setfnum: fnum=%u]\n", fnum); + + // Advance to the correct record... + while (fr != NULL && fnum != 0) + { + fr = fr->frec_next; + fnum--; + } if (fr == NULL) curfname = "(*top*)"; else curfname = fr->frec_name; + + DEBUG printf("[setfnum: curfname=%s]\n", curfname); +#else + // Check for absolute top filename (this should never happen) + if (fnum == -1) + { + curfname = "(*top*)"; + return; + } + + FILEREC * fr = filerec; + + // Advance to the correct record... + while (fr != NULL && fnum != 0) + { + fr = fr->frec_next; + fnum--; + } + + // Check for file # record not found (this should never happen either) + if (fr == NULL) + { + curfname = "(*NOT FOUND*)"; + return; + } + + curfname = fr->frec_name; +#endif +} +#else +void SetFilenameForErrorReporting(void) +{ + WORD fnum = cfileno; + + // Check for absolute top filename (this should never happen) + if (fnum == -1) + { + curfname = "(*top*)"; + return; + } + + FILEREC * fr = filerec; + + // Advance to the correct record... + while (fr != NULL && fnum != 0) + { + fr = fr->frec_next; + fnum--; + } + + // Check for file # record not found (this should never happen either) + if (fr == NULL) + { + curfname = "(*NOT FOUND*)"; + return; + } + + curfname = fr->frec_name; } +#endif // @@ -143,8 +214,7 @@ INOBJ * a_inobj(int typ) // Allocate and initialize INOBJ first if (f_inobj == NULL) -// inobj = (INOBJ *)amem((LONG)sizeof(INOBJ)); - inobj = (INOBJ *)malloc(sizeof(INOBJ)); + inobj = malloc(sizeof(INOBJ)); else { inobj = f_inobj; @@ -155,8 +225,7 @@ INOBJ * a_inobj(int typ) { case SRC_IFILE: // Alloc and init an IFILE if (f_ifile == NULL) -// ifile = (IFILE *)amem((LONG)sizeof(IFILE)); - ifile = (IFILE *)malloc(sizeof(IFILE)); + ifile = malloc(sizeof(IFILE)); else { ifile = f_ifile; @@ -167,8 +236,7 @@ INOBJ * a_inobj(int typ) break; case SRC_IMACRO: // Alloc and init an IMACRO if (f_imacro == NULL) -// imacro = (IMACRO *)amem((LONG)sizeof(IMACRO)); - imacro = (IMACRO *)malloc(sizeof(IMACRO)); + imacro = malloc(sizeof(IMACRO)); else { imacro = f_imacro; @@ -178,8 +246,7 @@ INOBJ * a_inobj(int typ) inobj->inobj.imacro = imacro; break; case SRC_IREPT: // Alloc and init an IREPT -// inobj->inobj.irept = (IREPT *)amem((LONG)sizeof(IREPT)); - inobj->inobj.irept = (IREPT *)malloc(sizeof(IREPT)); + inobj->inobj.irept = malloc(sizeof(IREPT)); DEBUG printf("alloc IREPT\n"); break; } @@ -218,8 +285,9 @@ int ExpandMacro(char * src, char * dest, int destsiz) char numbuf[20]; // Buffer for text of CONSTs TOKEN * tk; SYM * arg; + char ** symbolString; - DEBUG { printf("EM: src=\"%s\"\n", src); } + DEBUG { printf("ExM: src=\"%s\"\n", src); } IMACRO * imacro = cur_inobj->inobj.imacro; int macnum = (int)(imacro->im_macro->sattr); @@ -284,7 +352,7 @@ int ExpandMacro(char * src, char * dest, int destsiz) goto copy_d; case '~': // ==> unique label string Mnnnn... - sprintf(numbuf, "M%ud", curuniq); + sprintf(numbuf, "M%u", curuniq); copystr: d = numbuf; copy_d: @@ -353,13 +421,25 @@ copy_d: // macro invocation) then it is ignored. i = (int)arg->svalue; arg_num: - DEBUG printf("~argnumber=%d\n", i); - + DEBUG printf("~argnumber=%d (argBase=%u)\n", i, imacro->argBase); tk = NULL; if (i < imacro->im_nargs) + { +#if 0 // tk = argp[i]; - tk = argPtrs[i]; +// tk = argPtrs[i]; + tk = argPtrs[imacro->argBase + i]; +#else + tk = imacro->argument[i].token; + symbolString = imacro->argument[i].string; +//DEBUG +//{ +// printf("ExM: Preparing to parse argument #%u...\n", i); +// dumptok(tk); +//} +#endif + } // \?arg yields: // 0 if the argument is empty or non-existant, @@ -399,13 +479,22 @@ arg_num: switch ((int)*tk++) { case SYMBOL: +#if 0 // d = (char *)*tk++; d = string[*tk++]; +#else + // This fix should be done for strings too + d = symbolString[*tk++]; +DEBUG printf("ExM: SYMBOL=\"%s\"", d); +#endif break; case STRING: +#if 0 // d = (char *)*tk++; d = string[*tk++]; - +#else + d = symbolString[*tk++]; +#endif if (dst >= edst) goto overflow; @@ -516,10 +605,12 @@ strcopy: } *dst = EOS; + DEBUG { printf("ExM: dst=\"%s\"\n", dest); } return OK; overflow: *dst = EOS; + DEBUG printf("*** OVERFLOW LINE ***\n%s\n", dest); return fatal("line too long as a result of macro expansion"); } @@ -614,7 +705,7 @@ int include(int handle, char * fname) // Verbose mode if (verb_flag) - printf("[Including: %s]\n", fname); + printf("[include: %s, cfileno=%u]\n", fname, cfileno); // Alloc and initialize include-descriptors inobj = a_inobj(SRC_IFILE); @@ -625,12 +716,14 @@ int include(int handle, char * fname) ifile->ifoldlineno = curlineno; // Save old line number ifile->ifoldfname = curfname; // Save old filename ifile->ifno = cfileno; // Save old file number - cfileno = filecount++; // Compute new file number + +// cfileno = filecount++; // Compute new file number + // NB: This *must* be preincrement, we're adding one to the filecount here! + cfileno = ++filecount; // Compute NEW file number curfname = strdup(fname); // Set current filename (alloc storage) curlineno = 0; // Start on line zero // Add another file to the file-record -// fr = (FILEREC *)amem((LONG)sizeof(FILEREC)); fr = (FILEREC *)malloc(sizeof(FILEREC)); fr->frec_next = NULL; fr->frec_name = curfname; @@ -641,6 +734,7 @@ int include(int handle, char * fname) last_fr->frec_next = fr; // Append to list of filerecs last_fr = fr; + DEBUG printf("[include: curfname: %s, cfileno=%u]\n", curfname, cfileno); return OK; } @@ -727,10 +821,14 @@ int fpop(void) ifile->if_link = f_ifile; f_ifile = ifile; close(ifile->ifhandle); // Close source file +if (verb_flag) printf("[fpop (pre): curfname=%s]\n", curfname); curfname = ifile->ifoldfname; // Set current filename +if (verb_flag) printf("[fpop (post): curfname=%s]\n", curfname); +if (verb_flag) printf("[fpop: (pre) cfileno=%d ifile->ifno=%d]\n", (int)cfileno, (int)ifile->ifno); curlineno = ifile->ifoldlineno; // Set current line# DEBUG printf("cfileno=%d ifile->ifno=%d\n", (int)cfileno, (int)ifile->ifno); cfileno = ifile->ifno; // Restore current file number +if (verb_flag) printf("[fpop: (post) cfileno=%d ifile->ifno=%d]\n", (int)cfileno, (int)ifile->ifno); break; case SRC_IMACRO: // Pop and release an IMACRO imacro = inobj->inobj.imacro; @@ -879,6 +977,7 @@ int tokln(void) case SRC_IFILE: if ((ln = getln()) == NULL) { +if (verb_flag) printf("tokln: Calling fpop() from SRC_IFILE...\n"); fpop(); // Pop input level goto retry; // Try for more lines } @@ -913,7 +1012,7 @@ int tokln(void) case SRC_IMACRO: if ((ln = getmln()) == NULL) { - exitmac(); // Exit macro (pop args, do fpop(), etc) + ExitMacro(); // Exit macro (pop args, do fpop(), etc) goto retry; // Try for more lines... } @@ -925,6 +1024,7 @@ int tokln(void) case SRC_IREPT: if ((ln = getrln()) == NULL) { +if (verb_flag) printf("tokln: Calling fpop() from SRC_IREPT...\n"); fpop(); goto retry; } @@ -1050,7 +1150,7 @@ int tokln(void) if (j < 0 || state < 0) { *tk++ = SYMBOL; -#warning +//#warning //problem here: nullspot is a char * but TOKEN is a uint32_t. On a 64-bit system, //this will cause all kinds of mischief. #if 0 @@ -1102,7 +1202,7 @@ int tokln(void) case '\"': // "string" c1 = ln[-1]; *tk++ = STRING; -#warning +//#warning // More char * stuffing (8 bytes) into the space of 4 (TOKEN). // Need to figure out how to fix this crap. #if 0 @@ -1513,7 +1613,7 @@ int d_goto(WORD unused) void DumpTokenBuffer(void) { TOKEN * t; - printf("Tokens: "); + printf("Tokens [%X]: ", sloc); for(t=tokbuf; *t!=EOL; t++) {