recursive lockstep example

This commit is contained in:
Ellen Arvidsson 2025-06-13 17:36:36 +02:00
parent 26d0d3158f
commit e036fa16f8
15 changed files with 536 additions and 133 deletions

View file

@ -5,24 +5,49 @@
#include "spho/scope.h"
#define SPHO_BIND_KIND_TP 1
#define SPHO_BIND_KIND_TPOP 2
union spho_bound {
struct spho_tp *tp;
struct spho_tpop *op;
};
struct spho_binding {
struct spho_nom *nom;
struct spho_tp *tp;
STAILQ_ENTRY(spho_binding) entries;
int kind;
union spho_bound bound;
};
struct spho_bind_scope {
struct spho_scope *sc;
size_t cap_bds;
size_t n_bds;
struct spho_binding *bds;
};
struct spho_bind {
STAILQ_HEAD(spho_binding_l, spho_binding) head;
struct spho_ctx *ctx;
struct spho_bind_scope *binder;
struct spho_bind *parent;
};
struct spho_bind_scope *spho_bind_scope_create(struct spho_scope *);
struct spho_bind *spho_bind_create(struct spho_ctx *, struct spho_bind *);
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_bind_add(struct spho_bind *, struct spho_nom *, struct spho_tp *);
void spho_bind_scope_destroy(struct spho_bind_scope *);
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 *);

View file

@ -14,6 +14,7 @@
#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_IS_SYSERR(err) (SPHO_ERR_SYS == err)

View file

@ -15,10 +15,6 @@ struct spho_nom {
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! */
@ -33,11 +29,6 @@ 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;
};
@ -52,6 +43,16 @@ struct tp_konst_data {
const char *str;
};
struct tp_forall_data {
struct spho_nom *nom;
struct spho_tp *tp;
};
struct tp_member_data {
struct spho_nom *nom;
struct spho_tp *tp;
};
extern const struct tp_konst_data tp_konst_true;
extern const struct tp_konst_data tp_konst_false;
@ -62,10 +63,11 @@ extern const struct tp_konst_data tp_konst_false;
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;
struct tp_forall_data fa;
struct tp_member_data memb;
};
/* sappho type */
@ -95,6 +97,10 @@ struct spho_tp_alloc {
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);
@ -109,7 +115,7 @@ struct spho_scope {
struct spho_nom_l noms;
struct spho_tp_alloc_l tps;
struct spho_bind *bind;
struct spho_bind_scope *stat_bind;
SLIST_ENTRY(spho_scope) next;
};
@ -127,9 +133,13 @@ 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 **);
int spho_scope_nom_lookup_str(struct spho_scope *, const char *, size_t,
struct spho_nom **);
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 *,
struct spho_tp *);
#endif /* _SPHO_SCOPE_H */

View file

@ -6,4 +6,23 @@
#include "spho/err.h"
#include "spho/ctx.h"
#define SPHO_UTIL_SLIST_DESTROY(l, elmtype, next, term) \
do { \
struct elmtype *elm; \
while (! SLIST_EMPTY(l)) { \
elm = SLIST_FIRST(l); \
SLIST_REMOVE_HEAD(l, next); \
term(elm); \
free(elm); \
} \
} while (0)
#ifdef SPHO_USE_STRLCPY
#define SPHO_STRLCPY(dst, src, len) strlcpy(dst, src, len)
#else
#define SPHO_STRLCPY(dst, src, len) \
(size_t)snprintf(dst, len, "%s", src)
#endif
#endif

View file

@ -13,6 +13,7 @@
#define SPHO_TP_FORM_FORALL 0x15
#define SPHO_TP_FORM_APPL 0x16
#define SPHO_TP_FORM_MEMBER 0x17
#define SPHO_TP_FORM_NOMINAL 0x18
#define SPHO_TP_FORM_TRUE 0x20
#define SPHO_TP_FORM_FALSE 0x21
@ -22,8 +23,8 @@
#define SPHO_TP_MOD_FIRST (SPHO_TP_FORM_MASK + 1)
#define SPHO_TP_MOD_VAR (SPHO_TP_FLAG_FIRST)
#define SPHO_TP_MOD_NOMINAL (SPHO_TP_FLAG_FIRST << 1)
// #define SPHO_TP_MOD_VAR (SPHO_TP_FLAG_FIRST)
// #define SPHO_TP_MOD_NOMINAL (SPHO_TP_FLAG_FIRST << 1)
#define SPHO_TP_ERR(tp, err) SPHO_ERR((tp)->sc->ctx, (err))

View file

@ -1,25 +0,0 @@
#ifndef _SPHO_UTIL_H
#define _SPHO_UTIL_H
#define SPHO_UTIL_SLIST_DESTROY(l, elmtype, next, term) \
do { \
struct elmtype *elm; \
while (! SLIST_EMPTY(l)) { \
elm = SLIST_FIRST(l); \
SLIST_REMOVE_HEAD(l, next); \
term(elm); \
free(elm); \
} \
} while (0)
#ifdef SPHO_USE_STRLCPY
#define SPHO_STRLCPY(dst, src, len) strlcpy(dst, src, len)
#else
#define SPHO_STRLCPY(dst, src, len) \
(size_t)snprintf(dst, len, "%s", src)
#endif
#endif