Files
fz-lang/src/compiler/fzl_token.c
T

41 lines
875 B
C

#include <compiler/fzl_token.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, size_t line, size_t col,
void *value, size_t value_len, char *filename)
{
char *valholder = NULL;
if (value && value_len > 0)
{
valholder = malloc(value_len + 1);
memcpy(valholder, value, value_len);
valholder[value_len] = '\0';
}
fzl_token_t *token = malloc(sizeof(fzl_token_t));
token->type = type;
token->ln = line;
token->cn = col;
token->store_value = (valholder != NULL);
token->value = valholder;
token->value_sz = value_len;
token->filename = filename;
return token;
}
// Free allocated token
void fzl_freetoken(fzl_token_t *tok)
{
if (!tok)
return;
if (tok->store_value)
free(tok->value);
free(tok);
}