log-e-sappho/include/spho/scope.h

135 lines
2.6 KiB
C

#ifndef _SPHO_SCOPE_H
#define _SPHO_SCOPE_H
#include <sys/queue.h>
#include <stdlib.h>
#define SPHO_NOM_LEN 128
/* name */
struct spho_nom {
char s[SPHO_NOM_LEN];
SLIST_ENTRY(spho_nom) next; // TODO change to allocs
};
SLIST_HEAD(spho_nom_l, spho_nom);
/* binding
*
* bindings needs to be able to change, so we do not store it in names directly.
*/
#define TP_KONST_KEY_TRUE 0x43445 /* don't change!!!!! */
#define TP_KONST_KEY_FALSE 0x66a57 /* don't change!1! */
/* type data */
struct tp_binop_data {
struct spho_tp *left;
struct spho_tp *right;
};
struct tp_unop_data {
struct spho_tp *operand;
};
struct tp_binding_data {
struct spho_nom *nom;
struct spho_tp *bound;
};
struct tp_nom_data {
struct spho_nom *nom;
};
struct tp_appl_data {
struct spho_nom *nom;
struct spho_tp_l *args;
};
struct tp_konst_data {
int key;
const char *str;
};
extern const struct tp_konst_data tp_konst_true;
extern const struct tp_konst_data tp_konst_false;
#define SPHO_K_TRUE (&tp_konst_true)
#define SPHO_K_FALSE (&tp_konst_false)
/* type data union */
union tp_data {
struct tp_binop_data binop;
struct tp_unop_data unop;
struct tp_binding_data bind;
struct tp_appl_data appl;
struct tp_nom_data nom;
struct tp_konst_data konst;
};
/* sappho type */
struct spho_tp {
struct spho_scope *sc;
int kind;
union tp_data d;
STAILQ_ENTRY(spho_tp) entries;
};
STAILQ_HEAD(spho_tp_l, spho_tp);
struct spho_tp_ptr {
struct spho_tp *p;
STAILQ_ENTRY(spho_tp_ptr) entries;
};
STAILQ_HEAD(spho_tp_ptr_l, spho_tp_ptr);
struct spho_tp_alloc {
struct spho_tp tp;
TAILQ_ENTRY(spho_tp_alloc) allocs;
};
TAILQ_HEAD(spho_tp_alloc_l, spho_tp_alloc);
SLIST_HEAD(spho_scope_l, spho_scope);
/* defined in spho/bind.h */
struct spho_bind;
struct spho_scope {
struct spho_ctx *ctx;
struct spho_scope *parent;
struct spho_scope_l subs;
struct spho_nom_l noms;
struct spho_tp_alloc_l tps;
struct spho_bind *bind;
SLIST_ENTRY(spho_scope) next;
};
#define SPHO_SC_ERR(sc, err) SPHO_ERR((sc)->ctx, (err))
int spho_scope_init(struct spho_scope *, struct spho_ctx *,
struct spho_scope *);
void spho_scope_term(struct spho_scope *);
struct spho_scope *spho_scope_global(struct spho_ctx *);
struct spho_scope *spho_scope_create(struct spho_scope *);
void spho_scope_destroy(struct spho_scope *);
struct spho_nom *spho_scope_nom_add(struct spho_scope *, const char *, size_t);
int spho_scope_nom_get(struct spho_scope *, const char *, size_t,
struct spho_nom **);
int spho_scope_nom_lookup(struct spho_scope *, const char *, size_t,
struct spho_nom **);
#endif /* _SPHO_SCOPE_H */