Hash :
d04f9d06
Author :
Thomas de Grivel
Date :
2022-12-30T19:17:02
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/* c3
* Copyright 2022 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers 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 <assert.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "buf.h"
#include "buf_inspect.h"
#include "character.h"
#include "compare.h"
#include "str.h"
#include "sym.h"
void sym_delete (s_sym *sym);
s_str * sym_inspect_reserved (const s_sym *sym, s_str *dest);
sw sym_inspect_reserved_size (const s_sym *sym);
s_sym_list * sym_list_new (s_sym *sym, s_sym_list *next);
static s_sym_list * g_sym_list = NULL;
const s_sym * sym_1 (const s8 *p)
{
s_str stra;
str_init_1(&stra, NULL, p);
return str_to_sym(&stra);
}
e_bool sym_character_is_reserved (character c)
{
return (character_is_space(c) ||
c == '#' ||
c == '(' ||
c == ')' ||
c == ',' ||
c == '.' ||
c == ';' ||
c == '[' ||
c == ']' ||
c == '{' ||
c == '}');
}
void sym_delete (s_sym *sym)
{
str_clean(&sym->str);
free(sym);
}
void sym_delete_all ()
{
s_sym_list *sym_list;
sym_list = g_sym_list;
while (sym_list) {
s_sym_list *tmp;
tmp = sym_list;
sym_list = sym_list->next;
sym_delete(tmp->sym);
free(tmp);
}
g_sym_list = NULL;
}
const s_sym * sym_find (const s_str *str)
{
s_sym_list *sym_list;
sym_list = g_sym_list;
while (sym_list) {
s_sym *sym = sym_list->sym;
if (compare_str(str, &sym->str) == 0)
return sym;
sym_list = sym_list->next;
}
return NULL;
}
e_bool sym_has_reserved_characters (const s_sym *sym)
{
character c;
sw r;
s_str stra;
str_init(&stra, NULL, sym->str.size, sym->str.ptr.p);
while ((r = str_read_character(&stra, &c)) > 0) {
if (sym_character_is_reserved(c))
return true;
}
if (r < 0)
return true;
return false;
}
s_str * sym_inspect (const s_sym *sym, s_str *dest)
{
sw size;
s_buf tmp;
size = buf_inspect_sym_size(sym);
if (size < 0) {
assert(! "error");
return NULL;
}
buf_init_alloc(&tmp, size);
buf_inspect_sym(&tmp, sym);
assert(tmp.wpos == tmp.size);
return buf_to_str(&tmp, dest);
}
e_bool sym_is_module (const s_sym *sym)
{
character c;
if (str_peek_character(&sym->str, &c) > 0)
return character_is_uppercase(c);
return false;
}
s_sym_list * sym_list_new (s_sym *sym, s_sym_list *next)
{
s_sym_list *sym_list;
sym_list = malloc(sizeof(s_sym_list));
if (! sym_list)
err(1, "out of memory");
sym_list->sym = sym;
sym_list->next = next;
return sym_list;
}
const s_sym * sym_new (const s_str *src)
{
s_sym *sym;
sym = malloc(sizeof(s_sym));
if (! sym)
err(1, "out of memory");
str_init_dup(&sym->str, src);
g_sym_list = sym_list_new(sym, g_sym_list);
return sym;
}