- some more preparations on the parser code.

This commit is contained in:
Christoph Oelckers 2020-05-12 16:55:25 +02:00
parent 53e4b8bf19
commit 4a5953adb4
2 changed files with 168 additions and 97 deletions

View file

@ -314,6 +314,76 @@ void getlabel(void)
label[(labelcnt << 6) + i] = 0; label[(labelcnt << 6) + i] = 0;
} }
//---------------------------------------------------------------------------
//
// script buffer access wrappers. These are here to reduce the affected code
// when the time comes to refactor the buffer into a dynamic array.
//
//---------------------------------------------------------------------------
#if 0
static void setscriptvalue(int offset, int value)
{
script[offset] = value;
}
// store addresses as offsets
static void setscriptaddress(int offset, int* address)
{
setscriptvalue(offset, int(address - script.Data()));
}
static void appendscriptvalue(int value)
{
script.Push(value);
}
static void appendscriptaddress(int* address)
{
addscriptvalue(int(address - script.Data());
}
static void popscriptvalue()
{
script.Pop();
}
static int scriptoffset(
#else
// Helpers to write to the old script buffer while using the new interface. Allows to test the parser before implementing the rest.
void scriptWriteValue(int32_t const value);
void scriptWriteAtOffset(int32_t const value, intptr_t addr);
void scriptWritePointer(intptr_t const value, intptr_t addr);
static void setscriptvalue(int offset, int value)
{
scriptWriteAtOffset(value, offset);
}
// store addresses as offsets
static void setscriptaddress(int offset, int* address)
{
scriptWritePointer((intptr_t)address, offset);
}
static void appendscriptvalue(int value)
{
scriptWriteValue(value);
}
static void addscriptaddress(int* address)
{
assert(0);
}
static void popscriptvalue()
{
scriptptr--;
}
#endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// //
// //
@ -346,9 +416,8 @@ int transword(void) //Returns its code #
i = getkeyword(tempbuf); i = getkeyword(tempbuf);
if (i >= 0) if (i >= 0)
{ {
*scriptptr = i; appendscriptvalue(i);
textptr += l; textptr += l;
scriptptr++;
return i; return i;
} }
@ -414,8 +483,7 @@ void transnum(void)
{ {
if (strcmp(tempbuf, label + (i << 6)) == 0) if (strcmp(tempbuf, label + (i << 6)) == 0)
{ {
*scriptptr = labelcode[i]; appendscriptvalue(labelcode[i]);
scriptptr++;
textptr += l; textptr += l;
return; return;
} }
@ -446,22 +514,32 @@ void transnum(void)
{ {
// conversion was not successful. // conversion was not successful.
} }
*scriptptr = int(value); // truncate the parsed value to 32 bit. appendscriptvalue(int(value)); // truncate the parsed value to 32 bit.
scriptptr++;
textptr += l; textptr += l;
} }
//---------------------------------------------------------------------------
//
// just to reduce some excessive boilerplate. This block reappeared
// endlessly in parsecommand
//
//---------------------------------------------------------------------------
void checkforkeyword()
{
if (getkeyword(label + (labelcnt << 6)) >= 0)
{
errorcount++;
ReportError(ERROR_ISAKEYWORD);
}
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// //
// //
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
static TArray<char> parsebuffer; // global so that the storage is persistent across calls. static TArray<char> parsebuffer; // global so that the storage is persistent across calls.
int parsecommand(int tw) // for now just run an externally parsed command. int parsecommand(int tw) // for now just run an externally parsed command.
@ -488,7 +566,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; //End return 0; //End
#if 0 #if 0
case concmd_blockcomment: //Rem endrem case concmd_blockcomment: //Rem endrem
scriptptr--; popscriptvalue();
j = line_number; j = line_number;
do do
{ {
@ -507,8 +585,8 @@ int parsecommand(int tw) // for now just run an externally parsed command.
if (parsing_actor == 0 && parsing_state == 0) if (parsing_actor == 0 && parsing_state == 0)
{ {
getlabel(); getlabel();
scriptptr--; popscriptvalue();
labelcode[labelcnt] = (intptr_t)scriptptr; labelcode[labelcnt] = addrof(scriptptr);
labelcnt++; labelcnt++;
parsing_state = 1; parsing_state = 1;
@ -517,13 +595,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
getlabel(); getlabel();
checkforkeyword();
if (getkeyword(label + (labelcnt << 6)) >= 0)
{
errorcount++;
ReportError(ERROR_ISAKEYWORD);
return 0;
}
int lnum = findlabel(label + (labelcnt << 6)); int lnum = findlabel(label + (labelcnt << 6));
@ -532,8 +604,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
Printf(TEXTCOLOR_RED " * ERROR!(%s, line %d) State '%s' not found.\n", fn, line_number, label + (labelcnt << 6)); Printf(TEXTCOLOR_RED " * ERROR!(%s, line %d) State '%s' not found.\n", fn, line_number, label + (labelcnt << 6));
errorcount++; errorcount++;
} }
*scriptptr = lnum; appendscriptvalue(lnum);
scriptptr++;
return 0; return 0;
case concmd_sound: case concmd_sound:
@ -632,8 +703,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
while (j < 4) while (j < 4)
{ {
*scriptptr = 0; appendscriptvalue(0);
scriptptr++;
j++; j++;
} }
return 0; return 0;
@ -647,15 +717,14 @@ int parsecommand(int tw) // for now just run an externally parsed command.
while (keyword() == -1) while (keyword() == -1)
{ {
transnum(); transnum();
scriptptr--; popscriptvalue();
j |= *scriptptr; j |= *scriptptr;
} }
*scriptptr = j; appendscriptvalue(j);
scriptptr++;
} }
else else
{ {
scriptptr--; popscriptvalue();
getlabel(); getlabel();
// Check to see it's already defined // Check to see it's already defined
@ -682,17 +751,16 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
for (k = j; k < 2; k++) for (k = j; k < 2; k++)
{ {
*scriptptr = 0; appendscriptvalue(0);
scriptptr++;
} }
} }
return 0; return 0;
case concmd_music: case concmd_music:
{ {
scriptptr--; popscriptvalue();
transnum(); // Volume Number (0/4) transnum(); // Volume Number (0/4)
scriptptr--; popscriptvalue();
k = *scriptptr - 1; k = *scriptptr - 1;
if (k == -1) k = MAXVOLUMES; if (k == -1) k = MAXVOLUMES;
@ -727,7 +795,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
case concmd_include: case concmd_include:
{ {
scriptptr--; popscriptvalue();
while (isaltok(*textptr) == 0) while (isaltok(*textptr) == 0)
{ {
if (*textptr == 0x0a) line_number++; if (*textptr == 0x0a) line_number++;
@ -778,7 +846,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
transnum(); transnum();
else else
{ {
scriptptr--; popscriptvalue();
getlabel(); getlabel();
if (getkeyword(label + (labelcnt << 6)) >= 0) if (getkeyword(label + (labelcnt << 6)) >= 0)
@ -808,19 +876,17 @@ int parsecommand(int tw) // for now just run an externally parsed command.
while (keyword() == -1) while (keyword() == -1)
{ {
transnum(); transnum();
scriptptr--; popscriptvalue();
k |= *scriptptr; k |= *scriptptr;
} }
*scriptptr = k; appendscriptvalue(k);
scriptptr++;
return 0; return 0;
} }
else transnum(); else transnum();
} }
for (k = j; k < 3; k++) for (k = j; k < 3; k++)
{ {
*scriptptr = 0; appendscriptvalue(0);
scriptptr++;
} }
} }
return 0; return 0;
@ -830,7 +896,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
transnum(); transnum();
else else
{ {
scriptptr--; popscriptvalue();
getlabel(); getlabel();
// Check to see it's already defined // Check to see it's already defined
@ -859,8 +925,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
for (k = j; k < 5; k++) for (k = j; k < 5; k++)
{ {
*scriptptr = 0; appendscriptvalue(0);
scriptptr++;
} }
} }
return 0; return 0;
@ -879,11 +944,11 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
num_squigilly_brackets = 0; num_squigilly_brackets = 0;
scriptptr--; popscriptvalue();
parsing_actor = scriptptr; parsing_actor = scriptptr;
transnum(); transnum();
scriptptr--; popscriptvalue();
actorscrptr[*scriptptr] = parsing_actor; actorscrptr[*scriptptr] = parsing_actor;
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
@ -895,11 +960,10 @@ int parsecommand(int tw) // for now just run an externally parsed command.
while (keyword() == -1) while (keyword() == -1)
{ {
transnum(); transnum();
scriptptr--; popscriptvalue();
j |= *scriptptr; j |= *scriptptr;
} }
*scriptptr = j; appendscriptvalue(j);
scriptptr++;
break; break;
} }
else else
@ -933,12 +997,12 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
num_squigilly_brackets = 0; num_squigilly_brackets = 0;
scriptptr--; popscriptvalue();
parsing_event = scriptptr; parsing_event = scriptptr;
parsing_actor = scriptptr; parsing_actor = scriptptr;
transnum(); transnum();
scriptptr--; popscriptvalue();
j = *scriptptr; // type of event j = *scriptptr; // type of event
if (j< 0 || j> EVENT_MAXEVENT) if (j< 0 || j> EVENT_MAXEVENT)
{ {
@ -967,15 +1031,15 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
num_squigilly_brackets = 0; num_squigilly_brackets = 0;
scriptptr--; popscriptvalue();
parsing_actor = scriptptr; parsing_actor = scriptptr;
transnum(); transnum();
scriptptr--; popscriptvalue();
j = *scriptptr; j = *scriptptr;
transnum(); transnum();
scriptptr--; popscriptvalue();
actorscrptr[*scriptptr] = parsing_actor; actorscrptr[*scriptptr] = parsing_actor;
actortype[*scriptptr] = j; actortype[*scriptptr] = j;
@ -988,11 +1052,10 @@ int parsecommand(int tw) // for now just run an externally parsed command.
while (keyword() == -1) while (keyword() == -1)
{ {
transnum(); transnum();
scriptptr--; popscriptvalue();
j |= *scriptptr; j |= *scriptptr;
} }
*scriptptr = j; appendscriptvalue(j);
scriptptr++;
break; break;
} }
else else
@ -1065,7 +1128,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
else else
{ {
scriptptr--; popscriptvalue();
warningcount++; warningcount++;
Printf(TEXTCOLOR_RED " * WARNING.(%s, line %d) Found 'else' with no 'if', ignored.\n", fn, line_number); Printf(TEXTCOLOR_RED " * WARNING.(%s, line %d) Found 'else' with no 'if', ignored.\n", fn, line_number);
} }
@ -1102,7 +1165,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
} }
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
transnum(); // the number to check against... transnum(); // the number to check against...
return 0; return 0;
@ -1138,7 +1201,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
} }
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
// get the ID of the DEF // get the ID of the DEF
getlabel(); //GetGameVarLabel(); getlabel(); //GetGameVarLabel();
@ -1168,7 +1231,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
//#endif //#endif
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
return 0; return 0;
case concmd_ifvarvarg: case concmd_ifvarvarg:
@ -1193,7 +1256,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
ReportError(ERROR_NOTAGAMEDEF); ReportError(ERROR_NOTAGAMEDEF);
return 0; return 0;
} }
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
// get the ID of the DEF // get the ID of the DEF
getlabel(); //GetGameVarLabel(); getlabel(); //GetGameVarLabel();
@ -1213,7 +1276,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
ReportError(ERROR_NOTAGAMEDEF); ReportError(ERROR_NOTAGAMEDEF);
return 0; return 0;
} }
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
tempscrptr = scriptptr; tempscrptr = scriptptr;
scriptptr++; //Leave a spot for the fail location scriptptr++; //Leave a spot for the fail location
@ -1255,7 +1318,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
ReportError(ERROR_NOTAGAMEVAR); ReportError(ERROR_NOTAGAMEVAR);
return 0; return 0;
} }
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
transnum(); // the number to check against... transnum(); // the number to check against...
@ -1280,12 +1343,10 @@ int parsecommand(int tw) // for now just run an externally parsed command.
// syntax: addlogvar <var> // syntax: addlogvar <var>
// source file. // source file.
*scriptptr = (long)g_currentSourceFile; appendscriptvalue(g_currentSourceFile); // the ID of the DEF (offset into array...)
scriptptr++;
// prints the line number in the log file. // prints the line number in the log file.
*scriptptr = line_number; appendscriptvalue(line_number);
scriptptr++;
// get the ID of the DEF // get the ID of the DEF
getlabel(); //GetGameVarLabel(); getlabel(); //GetGameVarLabel();
@ -1305,7 +1366,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
ReportError(ERROR_NOTAGAMEDEF); ReportError(ERROR_NOTAGAMEDEF);
return 0; return 0;
} }
*scriptptr++ = i; // the ID of the DEF (offset into array...) appendscriptvalue(i); // the ID of the DEF (offset into array...)
return 0; return 0;
@ -1313,12 +1374,10 @@ int parsecommand(int tw) // for now just run an externally parsed command.
// syntax: addlog // syntax: addlog
// source file. // source file.
*scriptptr = (long)g_currentSourceFile; appendscriptvalue(g_currentSourceFile);
scriptptr++;
// prints the line number in the log file. // prints the line number in the log file.
*scriptptr = line_number; appendscriptvalue(line_number);
scriptptr++;
return 0; return 0;
case concmd_ifpinventory: case concmd_ifpinventory:
@ -1386,11 +1445,10 @@ int parsecommand(int tw) // for now just run an externally parsed command.
do do
{ {
transnum(); transnum();
scriptptr--; popscriptvalue();
j |= *scriptptr; j |= *scriptptr;
} while (keyword() == -1); } while (keyword() == -1);
*scriptptr = j; appendscriptvalue(j);
scriptptr++;
} }
tempscrptr = scriptptr; tempscrptr = scriptptr;
@ -1424,7 +1482,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
return 1; return 1;
case concmd_betaname: case concmd_betaname:
scriptptr--; popscriptvalue();
j = 0; j = 0;
// not used anywhere, just parse over it. // not used anywhere, just parse over it.
while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) // JBF 20040127: end of file checked while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) // JBF 20040127: end of file checked
@ -1433,7 +1491,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
} }
return 0; return 0;
case concmd_comment: case concmd_comment:
scriptptr--; //Negate the rem popscriptvalue(); //Negate the rem
while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) // JBF 20040127: end of file checked while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) // JBF 20040127: end of file checked
textptr++; textptr++;
@ -1441,9 +1499,9 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
case concmd_definevolumename: case concmd_definevolumename:
scriptptr--; popscriptvalue();
transnum(); transnum();
scriptptr--; popscriptvalue();
j = *scriptptr; j = *scriptptr;
while (*textptr == ' ' || *textptr == '\t') textptr++; while (*textptr == ' ' || *textptr == '\t') textptr++;
@ -1459,9 +1517,9 @@ int parsecommand(int tw) // for now just run an externally parsed command.
gVolumeNames[j] = FStringTable::MakeMacro(textptr, i); gVolumeNames[j] = FStringTable::MakeMacro(textptr, i);
return 0; return 0;
case concmd_defineskillname: case concmd_defineskillname:
scriptptr--; popscriptvalue();
transnum(); transnum();
scriptptr--; popscriptvalue();
j = *scriptptr; j = *scriptptr;
while (*textptr == ' ') textptr++; while (*textptr == ' ') textptr++;
@ -1478,12 +1536,12 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
case concmd_definelevelname: case concmd_definelevelname:
scriptptr--; popscriptvalue();
transnum(); transnum();
scriptptr--; popscriptvalue();
j = *scriptptr; j = *scriptptr;
transnum(); transnum();
scriptptr--; popscriptvalue();
k = *scriptptr; k = *scriptptr;
while (*textptr == ' ') textptr++; while (*textptr == ' ') textptr++;
@ -1526,7 +1584,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
case concmd_definequote: case concmd_definequote:
scriptptr--; popscriptvalue();
transnum(); transnum();
k = *(scriptptr - 1); k = *(scriptptr - 1);
if (k >= MAXQUOTES) if (k >= MAXQUOTES)
@ -1534,7 +1592,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
Printf(TEXTCOLOR_RED " * ERROR!(%s, line %d) Quote number exceeds limit of %d.\n", line_number, MAXQUOTES); Printf(TEXTCOLOR_RED " * ERROR!(%s, line %d) Quote number exceeds limit of %d.\n", line_number, MAXQUOTES);
errorcount++; errorcount++;
} }
scriptptr--; popscriptvalue();
i = 0; i = 0;
while (*textptr == ' ') while (*textptr == ' ')
textptr++; textptr++;
@ -1550,10 +1608,10 @@ int parsecommand(int tw) // for now just run an externally parsed command.
return 0; return 0;
case concmd_definesound: case concmd_definesound:
{ {
scriptptr--; popscriptvalue();
transnum(); transnum();
k = *(scriptptr - 1); k = *(scriptptr - 1);
scriptptr--; popscriptvalue();
i = 0; i = 0;
while (*textptr == ' ') while (*textptr == ' ')
textptr++; textptr++;
@ -1568,19 +1626,19 @@ int parsecommand(int tw) // for now just run an externally parsed command.
transnum(); transnum();
int ps = *(scriptptr - 1); int ps = *(scriptptr - 1);
scriptptr--; popscriptvalue();
transnum(); transnum();
int pe = *(scriptptr - 1); int pe = *(scriptptr - 1);
scriptptr--; popscriptvalue();
transnum(); transnum();
int pr = *(scriptptr - 1); int pr = *(scriptptr - 1);
scriptptr--; popscriptvalue();
transnum(); transnum();
int m = *(scriptptr - 1); int m = *(scriptptr - 1);
scriptptr--; popscriptvalue();
transnum(); transnum();
int vo = *(scriptptr - 1); int vo = *(scriptptr - 1);
scriptptr--; popscriptvalue();
S_DefineSound(k, parsebuffer.Data(), ps, pe, pr, m, vo, 1.f); S_DefineSound(k, parsebuffer.Data(), ps, pe, pr, m, vo, 1.f);
return 0; return 0;
} }
@ -1660,7 +1718,7 @@ int parsecommand(int tw) // for now just run an externally parsed command.
case concmd_gamestartup: case concmd_gamestartup:
{ {
#if 0 // cannot be activated before the old CON code is tossed. #if 0 // cannot be activated before the old CON code is tossed.
auto parseone = []() { transnum(); scriptptr--; return *scriptptr; } auto parseone = []() { transnum(); popscriptvalue(); return *scriptptr; }
ud_const_visibility = parseone(); ud_const_visibility = parseone();
impact_damage = parseone(); impact_damage = parseone();
max_player_health = parseone(); max_player_health = parseone();

View file

@ -364,25 +364,38 @@ static void C_GetNextLabelName(void)
Printf("%s:%d: debug: label `%s'.\n",g_scriptFileName,line_number,label+(labelcnt<<6)); Printf("%s:%d: debug: label `%s'.\n",g_scriptFileName,line_number,label+(labelcnt<<6));
} }
static inline void scriptWriteValue(int32_t const value) void scriptWriteValue(int32_t const value)
{ {
BITPTR_CLEAR(scriptptr-apScript); BITPTR_CLEAR(scriptptr-apScript);
*scriptptr++ = value; *scriptptr++ = value;
} }
// addresses passed to these functions must be within the block of memory pointed to by apScript // addresses passed to these functions must be within the block of memory pointed to by apScript
static inline void scriptWriteAtOffset(int32_t const value, intptr_t * const addr) void scriptWriteAtOffset(int32_t const value, intptr_t * const addr)
{ {
BITPTR_CLEAR(addr-apScript); BITPTR_CLEAR(addr-apScript);
*(addr) = value; *(addr) = value;
} }
static inline void scriptWritePointer(intptr_t const value, intptr_t * const addr) void scriptWritePointer(intptr_t const value, intptr_t * const addr)
{ {
BITPTR_SET(addr-apScript); BITPTR_SET(addr-apScript);
*(addr) = value; *(addr) = value;
} }
// addresses passed to these functions must be within the block of memory pointed to by apScript
void scriptWriteAtOffset(int32_t const value, intptr_t addr)
{
BITPTR_CLEAR(addr);
apScript[addr] = value;
}
void scriptWritePointer(intptr_t const value, intptr_t addr)
{
BITPTR_SET(addr);
apScript[addr] = value;
}
static int32_t C_GetKeyword(void) static int32_t C_GetKeyword(void)
{ {
int32_t i; int32_t i;