Я работаю на автомате через коммутатор. Однако, кажется, что от go до MACRO_READ_TEXT
, MACRO_READ_FIRSTCHAR
, MACRO_READ_NAME
, MACRO_READ_ARG
и MACRO_ARG_RECEIVED
все в одном - go, а не по одной итерации переключателя за раз (так как это должен).
Итак, для начала вот сам переключатель.
switch(curMacState)
{
case MACRO_READ_TEXT: // Just a regular character
printf("macroreadtext \n");
if( curChar == '\\' )
{
macroStartIndex = curIndex;
macroStartIndexFound = true;
curMacState = MACRO_READ_FIRSTCHAR;
break;
}
else
{
curMacState = MACRO_READ_TEXT;
break;
}
break;
case MACRO_READ_FIRSTCHAR:
printf("macroreadfirstchar \n");
if( isalpha(curChar) || isdigit(curChar) )
{
// Since realloc just allocates PAST the null character, we will need to
// simply write over the null character, and then add our own later.
// Location of the null character:
int macroNameNullIndex = strlen( macroName );
char* newMacroName = realloc(macroName, sizeof(macroName) + sizeof(char)); // Realloc 1 extra char.
macroName = newMacroName;
macroName[macroNameNullIndex] = curChar;
macroName[macroNameNullIndex + 1] = '\0';
curMacState = MACRO_READ_NAME;
//break;
}
else
{
curMacState = MACRO_READ_TEXT;
//break;
}
break;
case MACRO_READ_NAME:
printf("macroreadname \n");
if( isalpha(curChar) || isdigit(curChar) )
{
int macroNameNullIndex = strlen( macroName );
char* newMacroName = realloc(macroName, sizeof(macroName) + sizeof(char)); // Realloc 1 extra char.
macroName = newMacroName;
macroName[macroNameNullIndex] = curChar;
macroName[macroNameNullIndex + 1] = '\0';
curMacState = MACRO_READ_NAME;
//break;
}
else if( curChar == '{' )
{
curMacState = MACRO_READ_ARG_FIRSTCHAR;
//break;
}
else
{
//break;
}
break;
case MACRO_READ_ARG_FIRSTCHAR:
printf("MACRO_READ_ARG_FIRSTCHAR \n");
if( curChar == '}' )
{
// dont mark end of macro yet.
curMacState = MACRO_READ_ARG_RECEIVED;
//break;
}
else // read in cur char to an arg
{
macroArgTable[argIndex] = malloc( sizeof(char) * 2 ); // [_]['\0']
char* curArg = macroArgTable[argIndex];
curArg[0] = curChar;
curMacState = MACRO_READ_ARG_NAME;
//break;
}
break;
case MACRO_READ_ARG_NAME:
printf("MACRO_READ_ARG_NAME \n");
if( curChar == '}' )
{
curMacState = MACRO_READ_ARG_RECEIVED;
//break;
}
else // read in cur char to an arg
{
char* curArg = macroArgTable[argIndex];
int argNullIndex = strlen( curArg );
char* newCurArg = realloc(curArg, sizeof(curArg) + sizeof(char)); // Realloc 1 extra char.
curArg = newCurArg;
curArg[argNullIndex] = curChar;
curArg[argNullIndex + 1] = '\0';
curMacState = MACRO_READ_ARG_NAME;
//break;
}
break;
case MACRO_READ_ARG_RECEIVED:
printf("MACRO_READ_ARG_RECEIVED \n");
argIndex++;
if( curChar == '{' )
{
curMacState = MACRO_READ_ARG_FIRSTCHAR;
//break;
}
else
{
// Mark end of macro (technically the last char, the closing bracket.)
macroEndIndex = curIndex - 1;
macroEndIndexFound = true;
//char* newStrFileContents = processMacro( strFileContents, macroName, macroArgTable );
//strFileContents = newStrFileContents;
// this should return a str
curMacState = MACRO_READ_TEXT;
macroStartIndexFound = false;
macroEndIndexFound = false;
break;
}
break;
default: // Just a regular character
break;
}
Все работает нормально, пока в первый раз не будет прочитано "\" и не будет вызван MACRO_READ_FIRSTCHAR. Когда я запускаю это через gdb, я вижу, что все состояния просто распечатываются сразу в этой точке останова. (Пример вывода GDB до и после этой проблемы)
Breakpoint 1, main (argc=2, argv=0x7fffffffd578) at proj1.c:215
215 printf("macroreadtext \n");
(gdb) c
Continuing.
macroreadtext
Breakpoint 1, main (argc=2, argv=0x7fffffffd578) at proj1.c:215
215 printf("macroreadtext \n");
(gdb) c
Continuing.
macroreadtext
Breakpoint 1, main (argc=2, argv=0x7fffffffd578) at proj1.c:215
215 printf("macroreadtext \n");
(gdb) c
Continuing.
macroreadtext
macroreadfirstchar
macroreadname
macroreadname
macroreadname
MACRO_READ_ARG_FIRSTCHAR
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_RECEIVED
MACRO_READ_ARG_FIRSTCHAR
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_NAME
MACRO_READ_ARG_RECEIVED
Breakpoint 1, main (argc=2, argv=0x7fffffffd578) at proj1.c:215
215 printf("macroreadtext \n");
(gdb) c
Continuing.
macroreadtext
Breakpoint 1, main (argc=2, argv=0x7fffffffd578) at proj1.c:215
215
printf("macroreadtext \n");