scoped bindings

This commit is contained in:
Ellen Arvidsson 2025-06-24 16:31:23 +02:00
parent 396716de09
commit 858f5c3eed
17 changed files with 1185 additions and 821 deletions

View file

@ -5,50 +5,49 @@
#include "spho/scope.h"
#define SPHO_BIND_KIND_TP 1
#define SPHO_BIND_KIND_TPOP 2
#define SPHO_BIND_UNDECLARED 0x0
#define SPHO_BIND_UNDEFINED 0x01
#define SPHO_BIND_TP 0x11
#define SPHO_BIND_TP_OP 0x12
#define SPHO_BIND_MEMB_TP 0x13
union spho_bound {
struct spho_tp *tp;
struct spho_tpop *op;
union spho_prebind_val {
const void *undef;
const struct spho_tp *tp;
const struct spho_tp_op *op;
};
struct spho_binding {
struct spho_nom *nom;
struct spho_prebind_pair {
const struct spho_nom *nom;
int kind;
union spho_bound bound;
int kind;
union spho_prebind_val val;
};
struct spho_bind_scope {
struct spho_scope *sc;
struct spho_prebind {
struct spho_scope *sc;
size_t cap_bds;
size_t n_bds;
struct spho_binding *bds;
size_t sz;
struct spho_prebind_pair *binds;
};
struct spho_bind {
struct spho_ctx *ctx;
struct spho_bind_scope *binder;
struct spho_prebind *local;
struct spho_bind *parent;
};
struct spho_bind_scope *spho_bind_scope_create(struct spho_scope *);
struct spho_prebind *spho_prebind_create(struct spho_scope *);
int spho_bind_scope_add_tp(struct spho_bind_scope *, struct spho_nom *,
struct spho_tp *);
int spho_bind_scope_add_tpop(struct spho_bind_scope *, struct spho_nom *,
struct spho_tpop *);
int spho_prebind_undef(struct spho_prebind *, const struct spho_nom *);
int spho_prebind_tp(struct spho_prebind *, const struct spho_nom *,
const struct spho_tp *);
int spho_prebind_tp_op(struct spho_prebind *, const struct spho_nom *,
const struct spho_tp_op *);
int spho_prebind_member_tp(struct spho_prebind *, const struct spho_nom *,
const struct spho_tp *);
void spho_bind_scope_destroy(struct spho_bind_scope *);
struct spho_prebind *spho_prebind_dupl(const struct spho_prebind *);
struct spho_bind *spho_bind_create(struct spho_ctx *, struct spho_bind_scope *,
struct spho_bind *);
int spho_bind_lkp(int, struct spho_bind *, struct spho_nom *,
union spho_bound **);
void spho_bind_destroy(struct spho_bind *);
void spho_prebind_destroy(struct spho_prebind *);
#endif

View file

@ -14,7 +14,8 @@
#define SPHO_ERR_ARGINVAL 0x010003
#define SPHO_ERR_NOM_INUSE 0x020001
#define SPHO_ERR_NOM_NOTINSCOPE 0x020002
#define SPHO_ERR_BIND_EXISTS 0x030001
#define SPHO_ERR_BIND_DUPL 0x030001
#define SPHO_ERR_BIND_NOTFOUND 0x030002
#define SPHO_ERR_IS_SYSERR(err) (SPHO_ERR_SYS == err)
@ -42,54 +43,6 @@
(ctx)->errline = __LINE__; \
} while (0)
#define SPHO_STRINGIFY(a) #a
#define SPHO_MACRO_STR(b) SPHO_STRINGIFY(b)
#define __LINE__S SPHO_MACRO_STR(__LINE__)
#define SPHO_PRECOND(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_PRECOND(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#define SPHO_ASSERT(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_ASSERT(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#define SPHO_POSTCOND(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_POSTCOND(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#ifdef SPHO_ENABLE_DEBUG_PRINT
#define SPHO_DEBUG_PRINT(fmt, ...) \
do { \
fprintf(stderr, fmt __VA_OPT__(,) __VA_ARGS__); \
} while (0)
#else /* SPHO_ENABLE_DEBUG_PRINT */
#define SPHO_DEBUG_PRINT(fmt, ...)
#endif /* SPHO_ENABLE_DEBUG_PRINT */
#else /* SPHO_DEBUG */

View file

@ -80,6 +80,13 @@ struct spho_tp {
STAILQ_ENTRY(spho_tp) entries;
};
struct spho_tp_op {
struct spho_scope *sc;
struct spho_scope *op_sc;
struct spho_tp *op_tp;
};
STAILQ_HEAD(spho_tp_l, spho_tp);
struct spho_tp_ptr {
@ -90,18 +97,16 @@ struct spho_tp_ptr {
STAILQ_HEAD(spho_tp_ptr_l, spho_tp_ptr);
struct spho_tp_alloc {
struct spho_tp tp;
union {
struct spho_tp tp;
struct spho_tp_op tp_op;
} alloc;
TAILQ_ENTRY(spho_tp_alloc) allocs;
};
TAILQ_HEAD(spho_tp_alloc_l, spho_tp_alloc);
struct spho_tpop {
struct spho_scope *sc;
struct spho_tp *tp;
};
SLIST_HEAD(spho_scope_l, spho_scope);
/* defined in spho/bind.h */
@ -112,10 +117,12 @@ struct spho_scope {
struct spho_scope *parent;
struct spho_scope_l subs;
size_t n_noms;
struct spho_nom_l noms;
struct spho_tp_alloc_l tps;
struct spho_bind_scope *stat_bind;
struct spho_prebind *bind;
SLIST_ENTRY(spho_scope) next;
};
@ -138,8 +145,11 @@ int spho_scope_nom_lookup_str(struct spho_scope *, const char *, size_t,
int spho_scope_nom_lookup_str_strict(struct spho_scope *, const char *,
size_t, struct spho_nom **);
int spho_scope_tp_bind_init(struct spho_scope *);
int spho_scope_tp_bind_nom(struct spho_scope *, struct spho_nom *,
int spho_scope_prebind_init(struct spho_scope *);
int spho_scope_prebind_tp(struct spho_scope *, struct spho_nom *,
struct spho_tp *);
int spho_scope_prebind_tp_op(struct spho_scope *, struct spho_nom *,
struct spho_tp_op *);
int spho_scope_prebind_undef(struct spho_scope *, struct spho_nom *);
#endif /* _SPHO_SCOPE_H */

View file

@ -23,6 +23,64 @@ do { \
#else
#define SPHO_STRLCPY(dst, src, len) \
(size_t)snprintf(dst, len, "%s", src)
#endif
#endif /* ifdef SPHO_USE_STRLCPY */
#define SPHO_STRINGIFY(a) #a
#define SPHO_MACRO_STR(b) SPHO_STRINGIFY(b)
#define SPHO_RIP(msg) \
do { \
fprintf(stderr, "SPHO_RIP(" msg ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} while (0)
#ifdef SPHO_DEBUG
#define __LINE__S SPHO_MACRO_STR(__LINE__)
#define SPHO_PRECOND(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_PRECOND(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#define SPHO_ASSERT(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_ASSERT(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#define SPHO_POSTCOND(cond) \
do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_POSTCOND(" #cond ")@" \
__FILE__ ":" __LINE__S \
" failed. Aborting.\n"); \
abort(); \
} \
} while (0)
#ifdef SPHO_ENABLE_DEBUG_PRINT
#define SPHO_DEBUG_PRINT(fmt, ...) \
do { \
fprintf(stderr, fmt __VA_OPT__(,) __VA_ARGS__); \
} while (0)
#else
#define SPHO_DEBUG_PRINT(fmt, ...)
#endif /* ifdef SPHO_ENABLE_DEBUG_PRINT */
#endif /* ifdef SPHO_DEBUG */
#endif

View file

@ -19,6 +19,8 @@
#define SPHO_TP_FORM_FALSE 0x21
#define SPHO_TP_FORM_NAME 0x23
#define SPHO_TP_FORM_VAR 0x40
#define SPHO_TP_FORM_MASK 0xff
#define SPHO_TP_MOD_FIRST (SPHO_TP_FORM_MASK + 1)
@ -50,6 +52,9 @@ struct spho_tp *spho_tp_create_member(struct spho_scope *, struct spho_nom *,
struct spho_tp *spho_tp_create_var(struct spho_scope *, struct spho_nom *);
struct spho_tp *spho_tp_create_nominal(struct spho_scope *, struct spho_nom *);
struct spho_tp_op *spho_tp_op_create(struct spho_scope *,
struct spho_scope *, struct spho_tp *);
void spho_tp_destroy(struct spho_tp *);
#endif