Hash :
6160bff2
Author :
Thomas de Grivel
Date :
2025-07-21T13:03:11
tests pass: refactor p_*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/* kc3
* Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "alloc.h"
#include "assert.h"
#include "list.h"
#include "psym.h"
#include "str.h"
#include "sym.h"
#include "tag_type.h"
p_sym * psym_init_1 (p_sym *sym, const char *p)
{
assert(sym);
assert(p);
*sym = sym_1(p);
return sym;
}
p_sym * psym_init_anon (p_sym *sym, const s_str *prefix)
{
s_list *list;
static uw serial = 0;
s_str *serial_str = NULL;
s_str str = {0};
const s_sym *tmp = NULL;
const s_str underscore = {{NULL}, 1, {"_"}};
list = list_new(list_new(list_new(NULL)));
list_next(list_next(list))->tag.type = TAG_STR;
serial_str = &list_next(list_next(list))->tag.data.str;
list_next(list)->tag.type = TAG_STR;
list_next(list)->tag.data.str = underscore;
list->tag.type = TAG_STR;
str_init_copy(&list->tag.data.str, prefix);
while (1) {
serial++;
str_clean(serial_str);
str_init_uw(serial_str, serial);
if (! str_init_concatenate_list(&str,
(const s_list * const *) &list)) {
err_puts("sym_new_anon: str_init_concatenate_list");
assert(! "sym_new_anon: str_init_concatenate_list");
break;
}
if (! sym_find(&str)) {
tmp = str_to_sym(&str);
str_clean(&str);
list_delete_all(list);
*sym = tmp;
return sym;
}
str_clean(&str);
}
str_clean(&str);
list_delete_all(list);
return NULL;
}
p_sym * psym_init_cast (p_sym *sym, const s_sym * const *type,
const s_tag *tag)
{
assert(sym);
assert(type);
assert(tag);
switch (tag->type) {
case TAG_STR:
return psym_init_str(sym, &tag->data.str);
case TAG_SYM:
return psym_init_copy(sym, &tag->data.psym);
default:
break;
}
err_write_1("psym_init_cast: cannot cast ");
err_write_1(tag_type_to_string(tag->type));
if (*type == &g_sym_Sym)
err_puts(" to Sym");
else {
err_write_1(" to ");
err_inspect_psym(type);
err_puts(" aka Sym");
}
assert(! "psym_init_cast: cannot cast to Sym");
return NULL;
}
p_sym * psym_init_copy (p_sym *sym, p_sym const *src)
{
assert(src);
assert(sym);
*sym = *src;
return sym;
}
p_sym * psym_init_str (p_sym *sym, const s_str *src)
{
const s_sym *tmp = NULL;
tmp = sym_find(src);
if (! tmp)
tmp = sym_new(src);
if (! tmp)
return NULL;
*sym = tmp;
return sym;
}
bool psym_type_is_integer (p_sym const *type)
{
return sym_type_is_integer(*type);
}
uw * psym_type_size (p_sym const *type, uw *size)
{
return sym_type_size(*type, size);
}