49 lines
726 B
C
49 lines
726 B
C
#include "token.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
struct token
|
|
token_create (enum token_type type, unsigned int line, const char *lexeme)
|
|
{
|
|
struct token t = { 0 };
|
|
|
|
t.type = type;
|
|
t.lexeme = buffer_create ();
|
|
t.line = line;
|
|
buffer_append (lexeme, &t.lexeme);
|
|
|
|
return t;
|
|
}
|
|
|
|
struct token *
|
|
token_create_heap (enum token_type type, unsigned int line, const char *lexeme)
|
|
{
|
|
struct token *t = calloc (1, sizeof (struct token));
|
|
|
|
t->type = type;
|
|
t->lexeme = buffer_create ();
|
|
t->line = line;
|
|
buffer_append (lexeme, &t->lexeme);
|
|
|
|
return t;
|
|
}
|
|
|
|
void
|
|
token_free (struct token *t)
|
|
{
|
|
if (!t)
|
|
return;
|
|
|
|
buffer_free (&t->lexeme);
|
|
}
|
|
|
|
void
|
|
token_free_heap (struct token *t)
|
|
{
|
|
if (!t)
|
|
return;
|
|
|
|
buffer_free (&t->lexeme);
|
|
free (t);
|
|
}
|