J
janus
int TokenList_init(TokenList * tl, Lexer * lex)
{
IG_ASSERT(tl, "TokenList should be defined.");
IG_ASSERT(lex, "Lexer should be defined.");
tl->m_head = NULL;
Token * tok;
do
{
tok = (Token *) malloc(sizeof(Token));
Lexer_read(lex, tok);
tok->m_next = NULL;
if (NULL == tl->m_head) {
tl->m_head = tok;
tl->m_tail = tok;
}
else {
tl->m_tail->m_next = tok;
tl->m_tail = tok;
}
}
while (LEXER_EOF != tok -> m_type);
typedef struct TokenList
{
Token * m_head;
Token * m_tail;
Token * m_curr;
} TokenList;
typedef struct Token
{
int m_type; // an int representing the type of symbol
igstr * m_value; // the string value of the token
int m_line; // the line the token was found at
int m_column; // the column the token was found at
struct Token * m_next; // for later use
} Token;
I am studying the above code I stumbled on. I feel that this is wrong:
tl->m_tail->m_next = tok;
tl->m_tail = tok;
It should be:
tl->m_tail->m_next = tok;
and at the end of the loop we should have
tl->m_tail = tok;
{
IG_ASSERT(tl, "TokenList should be defined.");
IG_ASSERT(lex, "Lexer should be defined.");
tl->m_head = NULL;
Token * tok;
do
{
tok = (Token *) malloc(sizeof(Token));
Lexer_read(lex, tok);
tok->m_next = NULL;
if (NULL == tl->m_head) {
tl->m_head = tok;
tl->m_tail = tok;
}
else {
tl->m_tail->m_next = tok;
tl->m_tail = tok;
}
}
while (LEXER_EOF != tok -> m_type);
typedef struct TokenList
{
Token * m_head;
Token * m_tail;
Token * m_curr;
} TokenList;
typedef struct Token
{
int m_type; // an int representing the type of symbol
igstr * m_value; // the string value of the token
int m_line; // the line the token was found at
int m_column; // the column the token was found at
struct Token * m_next; // for later use
} Token;
I am studying the above code I stumbled on. I feel that this is wrong:
tl->m_tail->m_next = tok;
tl->m_tail = tok;
It should be:
tl->m_tail->m_next = tok;
and at the end of the loop we should have
tl->m_tail = tok;