add `new_uniq_sym` function

This commit is contained in:
Yuichi Nishiwaki 2013-11-25 23:38:39 -08:00
parent 4701f86ff0
commit 72cf45d4ea
3 changed files with 19 additions and 0 deletions

View File

@ -50,6 +50,7 @@ typedef struct {
struct xhash *sym_tbl;
const char **sym_pool;
size_t slen, scapa;
int uniq_sym_count;
/* positive for variables, negative for macros (bitwise-not) */
struct xhash *global_tbl;

View File

@ -1,5 +1,7 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include "picrin.h"
#include "picrin/pair.h"
@ -190,3 +192,18 @@ pic_macroexpand(pic_state *pic, pic_value obj)
return v;
}
static pic_sym
new_uniq_sym(pic_state *pic, pic_sym base)
{
int s = pic->uniq_sym_count++;
char *str;
pic_sym uniq;
str = (char *)pic_alloc(pic, strlen(pic_symbol_name(pic, base)), + (int)log10(s) + 2);
sprintf(str, "%s@%d", pic_symbol_name(pic, base), s);
uniq = pic_intern_cstr(pic, str);
pic_free(pic, str);
return uniq;
}

View File

@ -51,6 +51,7 @@ pic_open(int argc, char *argv[], char **envp)
pic->sym_pool = (const char **)calloc(PIC_SYM_POOL_SIZE, sizeof(const char *));
pic->slen = 0;
pic->scapa = pic->slen + PIC_SYM_POOL_SIZE;
pic->uniq_sym_count = 0;
/* irep */
pic->irep = (struct pic_irep **)calloc(PIC_IREP_SIZE, sizeof(struct pic_irep *));