extend the abstract syntax API

This commit is contained in:
krangelov
2021-08-06 12:43:30 +02:00
parent 87f1e24384
commit dc1644563f
11 changed files with 257 additions and 25 deletions

View File

@@ -187,21 +187,24 @@ public:
};
template <class V>
Namespace<V> namespace_empty() {
Namespace<V> namespace_empty()
{
return 0;
}
template <class V>
Namespace<V> namespace_singleton(ref<V> value) {
Namespace<V> namespace_singleton(ref<V> value)
{
return Node<V>::new_node(value);
}
template <class V>
Namespace<V> namespace_insert(Namespace<V> map, ref<V> value) {
Namespace<V> namespace_insert(Namespace<V> map, ref<V> value)
{
if (map == 0)
return Node<V>::new_node(value);
int cmp = textcmp(value->name,map->value->name);
int cmp = textcmp(&value->name,&map->value->name);
if (cmp < 0) {
Namespace<V> left = namespace_insert(map->left, value);
return Node<V>::balanceL(map->value,left,map->right);
@@ -213,7 +216,8 @@ Namespace<V> namespace_insert(Namespace<V> map, ref<V> value) {
}
template <class V>
ref<V> namespace_lookup(Namespace<V> map, const char *name) {
ref<V> namespace_lookup(Namespace<V> map, const char *name)
{
while (map != 0) {
int cmp = strcmp(name,map->value->name);
if (cmp < 0)
@@ -227,9 +231,21 @@ ref<V> namespace_lookup(Namespace<V> map, const char *name) {
}
template <class V>
size_t namespace_size(Namespace<V> map) {
size_t namespace_size(Namespace<V> map)
{
if (map == 0)
return 0;
return map->sz;
}
template <class V>
void namespace_iter(Namespace<V> map, PgfItor* itor)
{
if (map == 0)
return;
namespace_iter(map->left, itor);
itor->fn(itor, &map->value->name, &(*map->value));
namespace_iter(map->right, itor);
}
#endif

View File

@@ -66,3 +66,40 @@ PgfText *pgf_abstract_name(PgfPGF* pgf)
{
return textdup(&(*pgf->get_root<PgfPGFRoot>()->abstract.name));
}
PGF_API
void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor)
{
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.cats, itor);
}
PGF_API
void pgf_iter_functions(PgfPGF* pgf, PgfItor* itor)
{
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.funs, itor);
}
struct PgfItorHelper : PgfItor
{
PgfText *cat;
PgfItor *itor;
};
static
void iter_by_cat_helper(PgfItor* itor, PgfText* key, void* value)
{
PgfItorHelper* helper = (PgfItorHelper*) itor;
PgfAbsFun* absfun = (PgfAbsFun*) value;
if (textcmp(helper->cat, &absfun->type->name) == 0)
helper->itor->fn(helper->itor, key, value);
}
PGF_API
void pgf_iter_functions_by_cat(PgfPGF* pgf, PgfText* cat, PgfItor* itor)
{
PgfItorHelper helper;
helper.fn = iter_by_cat_helper;
helper.cat = cat;
helper.itor = itor;
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.funs, &helper);
}

View File

@@ -43,6 +43,13 @@ typedef struct {
char text[];
} PgfText;
/* A generic structure to pass a callback for iteration over a collection */
typedef struct PgfItor PgfItor;
struct PgfItor {
void (*fn)(PgfItor* self, PgfText* key, void *value);
};
typedef struct PgfPGF PgfPGF;
/* All functions that may fail take a reference to a PgfExn structure.
@@ -79,4 +86,16 @@ PgfPGF *pgf_read(const char* fpath, PgfExn* err);
PGF_API_DECL
void pgf_free(PgfPGF *pgf);
PGF_API_DECL
PgfText *pgf_abstract_name(PgfPGF* pgf);
PGF_API_DECL
void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor);
PGF_API_DECL
void pgf_iter_functions(PgfPGF* pgf, PgfItor* itor);
PGF_API
void pgf_iter_functions_by_cat(PgfPGF* pgf, PgfText* cat, PgfItor* itor);
#endif // PGF_H_

View File

@@ -1,20 +1,18 @@
#include "data.h"
PGF_INTERNAL
int textcmp(PgfText &t1, PgfText &t2)
int textcmp(PgfText *t1, PgfText *t2)
{
for (size_t i = 0; ; i++) {
if (i >= t1.size)
return (i - t2.size);
if (i >= t2.size)
if (i >= t1->size)
return (i - t2->size);
if (i >= t2->size)
return 1;
if (t1.text[i] > t2.text[i])
if (t1->text[i] > t2->text[i])
return 1;
else if (t1.text[i] < t2.text[i])
else if (t1->text[i] < t2->text[i])
return -1;
i++;
}
}
@@ -52,4 +50,42 @@ pgf_utf8_decode(const uint8_t** src_inout)
return u;
}
PGF_API void
pgf_utf8_encode(uint32_t ucs, uint8_t** buf)
{
uint8_t* p = *buf;
if (ucs < 0x80) {
p[0] = (uint8_t) ucs;
*buf = p+1;
} else if (ucs < 0x800) {
p[0] = 0xc0 | (ucs >> 6);
p[1] = 0x80 | (ucs & 0x3f);
*buf = p+2;
} else if (ucs < 0x10000) {
p[0] = 0xe0 | (ucs >> 12);
p[1] = 0x80 | ((ucs >> 6) & 0x3f);
p[2] = 0x80 | (ucs & 0x3f);
*buf = p+3;
} else if (ucs < 0x200000) {
p[0] = 0xf0 | (ucs >> 18);
p[1] = 0x80 | ((ucs >> 12) & 0x3f);
p[2] = 0x80 | ((ucs >> 6) & 0x3f);
p[3] = 0x80 | (ucs & 0x3f);
*buf = p+4;
} else if (ucs < 0x4000000) {
p[0] = 0xf8 | (ucs >> 24);
p[1] = 0x80 | ((ucs >> 18) & 0x3f);
p[2] = 0x80 | ((ucs >> 12) & 0x3f);
p[3] = 0x80 | ((ucs >> 6) & 0x3f);
p[4] = 0x80 | (ucs & 0x3f);
*buf = p+5;
} else {
p[0] = 0xfc | (ucs >> 30);
p[1] = 0x80 | ((ucs >> 24) & 0x3f);
p[2] = 0x80 | ((ucs >> 18) & 0x3f);
p[3] = 0x80 | ((ucs >> 12) & 0x3f);
p[4] = 0x80 | ((ucs >> 6) & 0x3f);
p[5] = 0x80 | (ucs & 0x3f);
*buf = p+6;
}
}

View File

@@ -2,7 +2,7 @@
#define TEXT_H
PGF_INTERNAL_DECL
int textcmp(PgfText &t1, PgfText &t2);
int textcmp(PgfText *t1, PgfText *t2);
PGF_INTERNAL_DECL
PgfText* textdup(PgfText *t1);