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

3
.gitignore vendored
View file

@ -66,6 +66,9 @@ compile_commands.json
# clangd # clangd
.clangd .clangd
# ctags tagfile
tags
# other stuff # other stuff
local/ local/

View file

@ -6,7 +6,8 @@ set(CMAKE_C_STANDARD_REQUIRED True)
string(JOIN " " CMAKE_C_FLAGS string(JOIN " " CMAKE_C_FLAGS
"-Wall -Wextra -Wformat=2" "-Wall -Wextra -Wformat=2"
"-Wconversion -Wsign-conversion -Wimplicit-fallthrough" "-Wconversion -Wsign-conversion -Wimplicit-fallthrough"
"-Werror=implicit -Werror=incompatible-pointer-types" "-Werror=implicit"
"-Werror=incompatible-pointer-types"
"-Werror=int-conversion") "-Werror=int-conversion")
set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_VERBOSE_MAKEFILE ON)
@ -21,19 +22,19 @@ set(SRC_DIR src)
set(SPHO_HEADER_DIR ${INCLUDE_DIR}/spho) set(SPHO_HEADER_DIR ${INCLUDE_DIR}/spho)
set(SPHO_HEADER set(SPHO_HEADER
${SPHO_HEADER_DIR}/bind.h
${SPHO_HEADER_DIR}/ctx.h ${SPHO_HEADER_DIR}/ctx.h
${SPHO_HEADER_DIR}/err.h ${SPHO_HEADER_DIR}/err.h
${SPHO_HEADER_DIR}/scope.h ${SPHO_HEADER_DIR}/scope.h
${SPHO_HEADER_DIR}/tp.h
${SPHO_HEADER_DIR}/bind.h
${SPHO_HEADER_DIR}/spho.h ${SPHO_HEADER_DIR}/spho.h
${SPHO_HEADER_DIR}/tp.h
) )
set(SPHO_SRC set(SPHO_SRC
${SRC_DIR}/spho/bind.c
${SRC_DIR}/spho/ctx.c ${SRC_DIR}/spho/ctx.c
${SRC_DIR}/spho/scope.c ${SRC_DIR}/spho/scope.c
${SRC_DIR}/spho/tp.c ${SRC_DIR}/spho/tp.c
${SRC_DIR}/spho/bind.c
) )
check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY) check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
@ -54,19 +55,18 @@ target_compile_definitions(devcheck PRIVATE
set(MSPH_SRC set(MSPH_SRC
${SRC_DIR}/msph/msph.c
${SRC_DIR}/msph/sphophi.c
${SRC_DIR}/msph/token.c ${SRC_DIR}/msph/token.c
${SRC_DIR}/msph/tree.c ${SRC_DIR}/msph/tree.c
${SRC_DIR}/msph/sphophi.c
${SRC_DIR}/msph/msph.c
) )
set(MSPH_HEADER set(MSPH_HEADER
${INCLUDE_DIR}/msph/token.h
${INCLUDE_DIR}/msph/tree.h
${INCLUDE_DIR}/msph/common.h ${INCLUDE_DIR}/msph/common.h
${INCLUDE_DIR}/msph/err.h ${INCLUDE_DIR}/msph/err.h
${INCLUDE_DIR}/msph/decor.h
${INCLUDE_DIR}/msph/sphophi.h ${INCLUDE_DIR}/msph/sphophi.h
${INCLUDE_DIR}/msph/token.h
${INCLUDE_DIR}/msph/tree.h
) )
add_executable(msph ${MSPH_SRC} ${MSPH_HEADER}) add_executable(msph ${MSPH_SRC} ${MSPH_HEADER})

View file

@ -1,33 +0,0 @@
#ifndef _MSPH_DECOR_H
#define _MSPH_DECOR_H
struct msph_tp_decor {
struct spho_tp *tp;
};
#define TP_DECOR_INIT(ptr) \
do { \
(ptr)->tp = NULL; \
} while (0)
struct msph_scope_decor {
struct spho_scope *sc;
};
#define SCOPE_DECOR_INIT(ptr) \
do { \
(ptr)->sc = NULL; \
} while (0)
struct msph_nom_decor {
struct spho_nom *nom;
};
#define NOM_DECOR_INIT(ptr) \
do { \
(ptr)->nom = NULL; \
} while (0)
#define GET_SCOPE(ptr) ((ptr)->dec.sc)
#endif /* _MSPH_DECOR_H */

View file

@ -7,7 +7,7 @@ int msph_sphophi_init(struct msph_tree_root *);
int msph_sphophi_scope(struct msph_tree_root *); int msph_sphophi_scope(struct msph_tree_root *);
int msph_sphophi_nom_lookup(struct msph_tree_root *); int msph_sphophi_nom_lookup(struct msph_tree_root *);
int msph_sphophi_tp(struct msph_tree_root *); int msph_sphophi_tp(struct msph_tree_root *);
int msph_sphophi_bind_static_tp_names(struct msph_tree_root *); int msph_sphophi_tp_prebind(struct msph_tree_root *);
int msph_sphophi(struct msph_tree_root *); int msph_sphophi(struct msph_tree_root *);

View file

@ -5,59 +5,16 @@
#include "msph/common.h" #include "msph/common.h"
#include "msph/token.h" #include "msph/token.h"
#include "msph/decor.h"
/* /* tree decorations */
* TYPES: struct msph_decor {
* Conj struct spho_scope *sc;
* Disj struct spho_tp *tp;
* Impl struct spho_nom *nom;
* Arrow };
* Box
* (Sub)
* Forall
*
* True
* False
* Var
* Nominal
*
* Record
*
* DEFINITIONS/DIRECTIVES:
* Type definition (type A[X..] = T)
* Nominal definition (nominal N)
* Member definition (member mname : T)
*
* {
* member a: A
* member b: B
* }
*
* Subtyping assert (assert A <: B, asserts that A <: B)
*
* EXTRA DEFINITIONS
* Class definition (class C[...] { ... }, shorthand)
*
* EXPRESSIONS
* Conj
* Disj
* Impl
* Arrow
* Box
* (Sub)
* Forall
*
* True
* False
* Var
* Nominal
*
* Trait ({ ... }, creates scoping)
*/
#define MSPH_TREE_ROOT (0x0000 | MSPH_TREE_FLAG_SCOPE) #define MSPH_TREE_ROOT 0x0000
#define MSPH_TREE_UNIT (0x0001 | MSPH_TREE_FLAG_SCOPE) #define MSPH_TREE_UNIT 0x0001
#define MSPH_TREE_BODY 0x0010 #define MSPH_TREE_BODY 0x0010
@ -70,31 +27,76 @@
#define MSPH_TREE_TPEXPR 0x0040 #define MSPH_TREE_TPEXPR 0x0040
#define MSPH_TREE_TRUE 0x0041 #define MSPH_TREE_TRUE 0x0041
#define MSPH_TREE_FALSE 0x0042 #define MSPH_TREE_FALSE 0x0042
#define MSPH_TREE_NAME 0x0043 #define MSPH_TREE_TPNAME 0x0043
#define MSPH_TREE_APPL 0x0044 #define MSPH_TREE_TPAPPL 0x0044
#define MSPH_TREE_TRAIT (0x0045 | MSPH_TREE_FLAG_SCOPE) #define MSPH_TREE_TRAIT 0x0045
#define MSPH_TREE_CONJ 0x0046 #define MSPH_TREE_CONJ 0x0046
#define MSPH_TREE_DISJ 0x0047 #define MSPH_TREE_DISJ 0x0047
#define MSPH_TREE_IMPL 0x0048 #define MSPH_TREE_IMPL 0x0048
#define MSPH_TREE_ARROW 0x0049 #define MSPH_TREE_ARROW 0x0049
#define MSPH_TREE_BOX 0x004a #define MSPH_TREE_BOX 0x004a
#define MSPH_TREE_FORALL (0x004b | MSPH_TREE_FLAG_SCOPE) #define MSPH_TREE_FORALL 0x004b
#define MSPH_TREE_PAREN 0x004c #define MSPH_TREE_PAREN 0x004c
#define MSPH_TREE_IDENT 0x0080 #define MSPH_TREE_IDENT 0x0100
#define MSPH_TREE_NAMEDECL 0x0101
#define MSPH_TREE_NAMEREF 0x0102
#define MSPH_TREE_MASK_ID 0x0fff #define MSPH_TREE_FLAG_OFFSET 16
#define MSPH_TREE_MASK_FLAGS 0xf000
#define MSPH_TREE_FLAG_SCOPE 0x1000 #define MSPH_TREE_FLAG_SCOPE (1 << (MSPH_TREE_FLAG_OFFSET))
#define MSPH_TREE_FLAG_STATIC_BIND 0x2000 #define MSPH_TREE_FLAG_TP (1 << (MSPH_TREE_FLAG_OFFSET + 1))
#define MSPH_TREE_FLAG_NOM (1 << (MSPH_TREE_FLAG_OFFSET + 2))
#define MSPH_TREE_SCOPE(type) (type & MSPH_TREE_FLAG_SCOPE) #define MSPH_TREE_MASK_FLAGS ((~0x0u) << MSPH_TREE_FLAG_OFFSET)
#define MSPH_TREE_STATIC_BIND(type) (type & MSPH_TREE_FLAG_STATIC_BIND) #define MSPH_TREE_MASK_ID ((~0x0u) ^ MSPH_TREE_MASK_FLAGS)
#define ADD_FLAG(tree, flag) \
do { \
TREE(tree)->type |= flag; \
} while (0)
#define HAS_FLAG(tree, flag) (TREE(tree)->type & (flag))
#define TREE(tree) ((struct msph_tree *)tree)
#define TEXT(tree) ((struct msph_tree_text *)tree)
#define IDENT(tree) ((struct msph_tree_ident *)tree)
#define TPEXPR(tree) ((struct msph_tree_tpexpr *)tree)
#define HAS_SCOPE(tree) HAS_FLAG((tree), MSPH_TREE_FLAG_SCOPE)
#define HAS_TP(tree) HAS_FLAG((tree), MSPH_TREE_FLAG_TP)
#define HAS_NOM(tree) HAS_FLAG((tree), MSPH_TREE_FLAG_NOM)
#define HAS_PARAMS(tpdef) (! STAILQ_EMPTY(&(tpdef)->params))
#define TREE_ID(tree) (((struct msph_tree *)tree)->type & \
MSPH_TREE_MASK_ID)
#define DECOR(tree) (TREE(tree)->decor)
#define SCOPE(tree) (TREE(tree)->decor.sc)
#define TP(tree) (TREE(tree)->decor.tp)
#define NOM(tree) (TREE(tree)->decor.nom)
#define DECOR_INIT(tree) \
do { \
DECOR(tree) = (struct msph_decor) { NULL, NULL, NULL }; \
} while (0)
struct msph_tree { struct msph_tree {
int type; unsigned int type;
struct msph_tree *parent; struct msph_tree *parent;
struct msph_decor decor;
};
struct msph_tree_text {
struct msph_tree hd_tr;
struct msph_text_pos pos;
};
struct msph_tree_ident {
struct msph_tree_text hd_txt;
char str[MSPH_IDENT_LEN];
}; };
struct msph_tree_root; struct msph_tree_root;
@ -108,7 +110,7 @@ struct msph_tree_ident;
struct msph_tree_tpexpr; struct msph_tree_tpexpr;
struct msph_tree_root { struct msph_tree_root {
struct msph_tree htr; struct msph_tree hd_tr;
struct msph_ctx *ctx; struct msph_ctx *ctx;
@ -116,155 +118,144 @@ struct msph_tree_root {
}; };
struct msph_tree_unit { struct msph_tree_unit {
struct msph_tree htr; struct msph_tree hd_tr;
char name[MSPH_NAME_LEN]; char name[MSPH_NAME_LEN];
struct msph_tree_body *body; struct msph_tree_body *body;
struct msph_scope_decor dec;
STAILQ_ENTRY(msph_tree_unit) entries; STAILQ_ENTRY(msph_tree_unit) entries;
}; };
struct msph_tree_text {
struct msph_tree htr;
struct msph_text_pos pos;
};
struct msph_tree_body { struct msph_tree_body {
struct msph_tree htr; struct msph_tree hd_tr;
STAILQ_HEAD(msph_tree_dir_l, msph_tree_dir) head; STAILQ_HEAD(msph_tree_dir_l, msph_tree_dir) head;
}; };
struct msph_tree_dir { struct msph_tree_dir {
struct msph_tree_text htxt; struct msph_tree_text hd_txt;
STAILQ_ENTRY(msph_tree_dir) entries; STAILQ_ENTRY(msph_tree_dir) entries;
}; };
struct msph_tree_tpdef { struct msph_tree_tpdef {
struct msph_tree_dir hdir; struct msph_tree_dir hd_dir;
struct msph_tree_ident *id; struct msph_tree_namedecl *name;
STAILQ_HEAD(msph_tree_namedecl_l, msph_tree_namedecl) params;
struct msph_tree_tpexpr *tp; struct msph_tree_tpexpr *tp;
}; };
struct msph_tree_nomindecl { struct msph_tree_nomindecl {
struct msph_tree_dir hdir; struct msph_tree_dir hd_dir;
struct msph_tree_ident *id; struct msph_tree_namedecl *name;
struct msph_tree_tpexpr *tp;
}; };
struct msph_tree_membdecl { struct msph_tree_membdecl {
struct msph_tree_dir hdir; struct msph_tree_dir hd_dir;
struct msph_tree_ident *id; struct msph_tree_namedecl *name;
struct msph_tree_tpexpr *tp; struct msph_tree_tpexpr *tp;
}; };
struct msph_tree_assert { struct msph_tree_assert {
struct msph_tree_dir hdir; struct msph_tree_dir hd_dir;
struct msph_tree_tpexpr *ltp; struct msph_tree_tpexpr *ltp;
struct msph_tree_tpexpr *rtp; struct msph_tree_tpexpr *rtp;
}; };
struct msph_tree_tpexpr { struct msph_tree_namedecl {
struct msph_tree_text htxt; struct msph_tree_ident hd_id;
STAILQ_ENTRY(msph_tree_tpexpr) entries; STAILQ_ENTRY(msph_tree_namedecl) entries;
struct msph_tp_decor dec;
}; };
struct msph_tree_tpexpr {
struct msph_tree_text hd_txt;
STAILQ_ENTRY(msph_tree_tpexpr) entries;
};
struct msph_tree_true { struct msph_tree_true {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
}; };
struct msph_tree_false { struct msph_tree_false {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
}; };
struct msph_tree_name { struct msph_tree_nameref {
struct msph_tree_tpexpr htpe; struct msph_tree_ident hd_id;
struct msph_tree_ident *id;
}; };
struct msph_tree_appl { struct msph_tree_tpname {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_ident *id; struct msph_tree_nameref *name;
STAILQ_HEAD(msph_tree_tpexpr_l, msph_tree_tpexpr) head; };
struct msph_tree_tpappl {
struct msph_tree_tpexpr hd_tpe;
struct msph_tree_nameref *name;
STAILQ_HEAD(msph_tree_tpexpr_l, msph_tree_tpexpr) args;
}; };
struct msph_tree_trait { struct msph_tree_trait {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_body *body; struct msph_tree_body *body;
struct msph_scope_decor dec;
}; };
struct msph_tree_conj { struct msph_tree_conj {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_tpexpr *ltp; struct msph_tree_tpexpr *ltp;
struct msph_tree_tpexpr *rtp; struct msph_tree_tpexpr *rtp;
}; };
struct msph_tree_disj { struct msph_tree_disj {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_tpexpr *ltp; struct msph_tree_tpexpr *ltp;
struct msph_tree_tpexpr *rtp; struct msph_tree_tpexpr *rtp;
}; };
struct msph_tree_impl { struct msph_tree_impl {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_tpexpr *ltp; struct msph_tree_tpexpr *ltp;
struct msph_tree_tpexpr *rtp; struct msph_tree_tpexpr *rtp;
}; };
struct msph_tree_arrow { struct msph_tree_arrow {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_tpexpr *ltp; struct msph_tree_tpexpr *ltp;
struct msph_tree_tpexpr *rtp; struct msph_tree_tpexpr *rtp;
}; };
struct msph_tree_box { struct msph_tree_box {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_tpexpr *inner; struct msph_tree_tpexpr *inner;
}; };
struct msph_tree_forall { struct msph_tree_forall {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_ident *id; struct msph_tree_namedecl *name;
struct msph_tree_tpexpr *inner; struct msph_tree_tpexpr *inner;
struct msph_scope_decor dec;
}; };
struct msph_tree_paren { struct msph_tree_paren {
struct msph_tree_tpexpr htpe; struct msph_tree_tpexpr hd_tpe;
struct msph_tree_tpexpr *inner; struct msph_tree_tpexpr *inner;
}; };
struct msph_tree_ident {
struct msph_tree_text htxt;
char str[MSPH_IDENT_LEN];
STAILQ_ENTRY(msph_tree_ident) entries;
struct msph_nom_decor dec;
};
struct msph_tree_root *msph_tree_makeroot(struct msph_ctx *); struct msph_tree_root *msph_tree_makeroot(struct msph_ctx *);
@ -272,7 +263,4 @@ int msph_tree_parse(struct msph_token_stream *, struct msph_tree_root *);
ssize_t msph_tree_fprint(FILE *, struct msph_tree *); ssize_t msph_tree_fprint(FILE *, struct msph_tree *);
#define T(ptr) ((struct msph_tree *)ptr)
#define TXT(ptr) ((struct msph_tree_text *)ptr)
#endif #endif

View file

@ -5,50 +5,49 @@
#include "spho/scope.h" #include "spho/scope.h"
#define SPHO_BIND_KIND_TP 1 #define SPHO_BIND_UNDECLARED 0x0
#define SPHO_BIND_KIND_TPOP 2 #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 { union spho_prebind_val {
struct spho_tp *tp; const void *undef;
struct spho_tpop *op; const struct spho_tp *tp;
const struct spho_tp_op *op;
}; };
struct spho_binding { struct spho_prebind_pair {
struct spho_nom *nom; const struct spho_nom *nom;
int kind; int kind;
union spho_bound bound; union spho_prebind_val val;
}; };
struct spho_bind_scope { struct spho_prebind {
struct spho_scope *sc; struct spho_scope *sc;
size_t cap_bds; size_t sz;
size_t n_bds; struct spho_prebind_pair *binds;
struct spho_binding *bds;
}; };
struct spho_bind { struct spho_bind {
struct spho_ctx *ctx; struct spho_prebind *local;
struct spho_bind_scope *binder;
struct spho_bind *parent; 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 *, int spho_prebind_undef(struct spho_prebind *, const struct spho_nom *);
struct spho_tp *); int spho_prebind_tp(struct spho_prebind *, const struct spho_nom *,
int spho_bind_scope_add_tpop(struct spho_bind_scope *, struct spho_nom *, const struct spho_tp *);
struct spho_tpop *); 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 *, void spho_prebind_destroy(struct spho_prebind *);
struct spho_bind *);
int spho_bind_lkp(int, struct spho_bind *, struct spho_nom *,
union spho_bound **);
void spho_bind_destroy(struct spho_bind *);
#endif #endif

View file

@ -14,7 +14,8 @@
#define SPHO_ERR_ARGINVAL 0x010003 #define SPHO_ERR_ARGINVAL 0x010003
#define SPHO_ERR_NOM_INUSE 0x020001 #define SPHO_ERR_NOM_INUSE 0x020001
#define SPHO_ERR_NOM_NOTINSCOPE 0x020002 #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) #define SPHO_ERR_IS_SYSERR(err) (SPHO_ERR_SYS == err)
@ -42,54 +43,6 @@
(ctx)->errline = __LINE__; \ (ctx)->errline = __LINE__; \
} while (0) } 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 */ #else /* SPHO_DEBUG */

View file

@ -80,6 +80,13 @@ struct spho_tp {
STAILQ_ENTRY(spho_tp) entries; 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); STAILQ_HEAD(spho_tp_l, spho_tp);
struct spho_tp_ptr { struct spho_tp_ptr {
@ -90,18 +97,16 @@ struct spho_tp_ptr {
STAILQ_HEAD(spho_tp_ptr_l, spho_tp_ptr); STAILQ_HEAD(spho_tp_ptr_l, spho_tp_ptr);
struct spho_tp_alloc { 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_ENTRY(spho_tp_alloc) allocs;
}; };
TAILQ_HEAD(spho_tp_alloc_l, 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); SLIST_HEAD(spho_scope_l, spho_scope);
/* defined in spho/bind.h */ /* defined in spho/bind.h */
@ -112,10 +117,12 @@ struct spho_scope {
struct spho_scope *parent; struct spho_scope *parent;
struct spho_scope_l subs; struct spho_scope_l subs;
size_t n_noms;
struct spho_nom_l noms; struct spho_nom_l noms;
struct spho_tp_alloc_l tps; struct spho_tp_alloc_l tps;
struct spho_bind_scope *stat_bind; struct spho_prebind *bind;
SLIST_ENTRY(spho_scope) next; 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 *, int spho_scope_nom_lookup_str_strict(struct spho_scope *, const char *,
size_t, struct spho_nom **); size_t, struct spho_nom **);
int spho_scope_tp_bind_init(struct spho_scope *); int spho_scope_prebind_init(struct spho_scope *);
int spho_scope_tp_bind_nom(struct spho_scope *, struct spho_nom *, int spho_scope_prebind_tp(struct spho_scope *, struct spho_nom *,
struct spho_tp *); 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 */ #endif /* _SPHO_SCOPE_H */

View file

@ -23,6 +23,64 @@ do { \
#else #else
#define SPHO_STRLCPY(dst, src, len) \ #define SPHO_STRLCPY(dst, src, len) \
(size_t)snprintf(dst, len, "%s", src) (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 #endif

View file

@ -19,6 +19,8 @@
#define SPHO_TP_FORM_FALSE 0x21 #define SPHO_TP_FORM_FALSE 0x21
#define SPHO_TP_FORM_NAME 0x23 #define SPHO_TP_FORM_NAME 0x23
#define SPHO_TP_FORM_VAR 0x40
#define SPHO_TP_FORM_MASK 0xff #define SPHO_TP_FORM_MASK 0xff
#define SPHO_TP_MOD_FIRST (SPHO_TP_FORM_MASK + 1) #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_var(struct spho_scope *, struct spho_nom *);
struct spho_tp *spho_tp_create_nominal(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 *); void spho_tp_destroy(struct spho_tp *);
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -5,9 +5,12 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "spho/spho.h"
#include "msph/err.h" #include "msph/err.h"
#include "msph/token.h" #include "msph/token.h"
struct msph_matcher { struct msph_matcher {
size_t off; size_t off;
size_t matchlen; size_t matchlen;

File diff suppressed because it is too large Load diff

View file

@ -1,77 +1,165 @@
#include <sys/queue.h> #include <sys/queue.h>
#include <string.h>
#include "spho/ctx.h" #include "spho/spho.h"
#include "spho/scope.h"
#include "spho/bind.h" #include "spho/bind.h"
struct spho_bind * struct spho_prebind *
spho_bind_create(struct spho_ctx *ctx, struct spho_bind_scope *binder, spho_prebind_create(struct spho_scope *sc)
struct spho_bind *parent)
{ {
struct spho_bind *bind; size_t i;
struct spho_nom *nom;
struct spho_prebind *bind;
SPHO_PRECOND(ctx != NULL); SPHO_PRECOND(sc != NULL);
SPHO_PRECOND(binder != NULL);
if ((bind = malloc(sizeof(*bind))) == NULL) { if ((bind = calloc(1, sizeof(*bind))) == NULL) {
SPHO_ERR(binder->sc->ctx, SPHO_ERR_SYS); SPHO_ERR(sc->ctx, SPHO_ERR_SYS);
return (NULL); return (NULL);
} }
bind->ctx = ctx; if ((bind->binds = calloc(sc->n_noms, sizeof(*bind->binds))) == NULL) {
bind->binder = binder; SPHO_ERR(sc->ctx, SPHO_ERR_SYS);
bind->parent = parent; free(bind);
return (NULL);
}
bind->sc = sc;
bind->sz = sc->n_noms;
i = 0;
SLIST_FOREACH(nom, &sc->noms, next) {
SPHO_ASSERT(i < bind->sz);
bind->binds[i++] = (struct spho_prebind_pair) { NULL, 0, NULL };
}
return (bind); return (bind);
} }
int int
spho_bind_lkp(int, struct spho_bind *bind, struct spho_nom *nom, spho_prebind_undef(struct spho_prebind *bind,
union spho_bound **out) const struct spho_nom *nom)
{ {
// TODO size_t i;
SPHO_PRECOND(bind != NULL);
SPHO_PRECOND(nom != NULL);
for (i = 0; i < bind->sz; i++) {
if (nom == bind->binds[i].nom) {
if (bind->binds[i].kind != SPHO_BIND_UNDECLARED) {
SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_DUPL);
return (-1);
}
bind->binds[i].kind = SPHO_BIND_UNDEFINED;
}
}
return (0);
}
int
spho_prebind_tp(struct spho_prebind *bind, const struct spho_nom *nom,
const struct spho_tp *tp)
{
size_t i;
SPHO_PRECOND(bind != NULL);
SPHO_PRECOND(nom != NULL);
SPHO_PRECOND(tp != NULL);
for (i = 0; i < bind->sz; i++) {
if (nom == bind->binds[i].nom) {
if (bind->binds[i].kind != SPHO_BIND_UNDECLARED) {
SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_DUPL);
return (-1);
}
bind->binds[i].kind = SPHO_BIND_TP;
bind->binds[i].val = (union spho_prebind_val) tp;
return (0);
}
}
SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_NOTFOUND);
return (-1); return (-1);
} }
void int
spho_bind_destroy(struct spho_bind *bind) spho_prebind_tp_op(struct spho_prebind *bind, const struct spho_nom *nom,
const struct spho_tp_op *op)
{ {
free(bind); size_t i;
}
SPHO_PRECOND(bind != NULL);
SPHO_PRECOND(nom != NULL);
SPHO_PRECOND(op != NULL);
struct spho_bind_scope * for (i = 0; i < bind->sz; i++) {
spho_bind_scope_create(struct spho_scope *sc) if (nom == bind->binds[i].nom) {
{ if (bind->binds[i].kind != SPHO_BIND_UNDECLARED) {
size_t sz; SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_DUPL);
struct spho_binding *bds; return (-1);
struct spho_bind_scope *ret; }
bind->binds[i].kind = SPHO_BIND_TP_OP;
SPHO_PRECOND(sc != NULL); bind->binds[i].val = (union spho_prebind_val) op;
return (0);
if ((ret = malloc(sizeof(*ret))) == NULL) { }
SPHO_ERR(sc->ctx, SPHO_ERR_SYS);
return (NULL);
} }
if ((bds = calloc(sz, sizeof(*bds))) == NULL) { SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_NOTFOUND);
SPHO_ERR(sc->ctx, SPHO_ERR_SYS); return (-1);
goto err;
}
ret->sc = sc;
ret->cap_bds = sz;
ret->n_bds = 0;
ret->bds = bds;
err:
free(ret);
return (NULL);
} }
int int
spho_binder_add_tp(struct spho_bind_scope *bdr, struct spho_nom *nom, spho_prebind_member_tp(struct spho_prebind *bind, const struct spho_nom *nom,
struct spho_tp *tp) const struct spho_tp *memb_tp)
{ {
size_t i;
SPHO_PRECOND(bind != NULL);
SPHO_PRECOND(nom != NULL);
SPHO_PRECOND(memb_tp != NULL);
for (i = 0; i < bind->sz; i++) {
if (nom == bind->binds[i].nom) {
if (bind->binds[i].kind != SPHO_BIND_UNDECLARED) {
SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_DUPL);
return (-1);
}
bind->binds[i].kind = SPHO_BIND_MEMB_TP;
bind->binds[i].val = (union spho_prebind_val) memb_tp;
return (0);
}
}
SPHO_ERR(bind->sc->ctx, SPHO_ERR_BIND_NOTFOUND);
return (-1);
}
struct spho_prebind *
spho_prebind_dupl(const struct spho_prebind *bind)
{
struct spho_prebind *cp;
if ((cp = calloc(1, sizeof(*cp))) == NULL) {
SPHO_ERR(bind->sc->ctx, SPHO_ERR_SYS);
return (NULL);
}
if ((cp->binds = calloc(bind->sz, sizeof(*cp->binds))) == NULL) {
SPHO_ERR(bind->sc->ctx, SPHO_ERR_SYS);
free(cp);
return (NULL);
}
memcpy(cp->binds, bind->binds, bind->sz * sizeof(*bind->binds));
return (cp);
}
void
spho_prebind_destroy(struct spho_prebind *bind)
{
free(bind->binds);
free(bind);
} }

View file

@ -4,7 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "spho/ctx.h" #include "spho/spho.h"
static const char *spho_ctx_sys_strerror(struct spho_ctx *); static const char *spho_ctx_sys_strerror(struct spho_ctx *);
static const char *spho_ctx_intern_strerror(struct spho_ctx *); static const char *spho_ctx_intern_strerror(struct spho_ctx *);
@ -23,6 +23,10 @@ struct spho_err spho_errmsgs[] = {
{ SPHO_ERR_NOM_INUSE, "name already declared in current scope" }, { SPHO_ERR_NOM_INUSE, "name already declared in current scope" },
{ SPHO_ERR_NOM_NOTINSCOPE, "name not in scope" }, { SPHO_ERR_NOM_NOTINSCOPE, "name not in scope" },
{ SPHO_ERR_BIND_NOTFOUND, "binding name not found in scope" },
{ SPHO_ERR_BIND_DUPL, "duplicate binding" },
{ -1, NULL } { -1, NULL }
}; };
@ -91,7 +95,7 @@ spho_ctx_sys_strerror(struct spho_ctx *ctx)
res = snprintf(ctx->errbuf, sizeof(ctx->errbuf), res = snprintf(ctx->errbuf, sizeof(ctx->errbuf),
"spho_syserr: %s (%d) (%s:%d)", strerror(ctx->err_info), "spho_syserr: %s (%d) (%s:%d)", strerror(ctx->err_info),
ctx->err_info, ctx->filebuf, ctx->errline); ctx->err_info, ctx->filebuf, ctx->errline);
if (res >= sizeof(ctx->errbuf)) if (res >= (ssize_t)sizeof(ctx->errbuf))
ctx->errbuf[sizeof(ctx->errbuf) - 1] = '\0'; ctx->errbuf[sizeof(ctx->errbuf) - 1] = '\0';
#else #else
res = snprintf(ctx->errbuf, sizeof(ctx->errbuf), res = snprintf(ctx->errbuf, sizeof(ctx->errbuf),

View file

@ -10,9 +10,9 @@
static void spho_nom_term(struct spho_nom *); static void spho_nom_term(struct spho_nom *);
static int scope_nom_lookup_str_loc(struct spho_scope *, const char *, static int scope_nom_lookup_str_local(struct spho_scope *, const char *,
size_t, struct spho_nom **); size_t, struct spho_nom **);
static int scope_nom_in_loc(struct spho_scope *, struct spho_nom *); static int scope_nom_in_local(struct spho_scope *, struct spho_nom *);
int int
spho_scope_init(struct spho_scope *sc, struct spho_ctx *ctx, spho_scope_init(struct spho_scope *sc, struct spho_ctx *ctx,
@ -22,10 +22,13 @@ spho_scope_init(struct spho_scope *sc, struct spho_ctx *ctx,
sc->parent = p; sc->parent = p;
SLIST_INIT(&sc->subs); SLIST_INIT(&sc->subs);
SLIST_INIT(&sc->noms); SLIST_INIT(&sc->noms);
sc->n_noms = 0;
TAILQ_INIT(&sc->tps); TAILQ_INIT(&sc->tps);
sc->stat_bind = NULL; sc->bind = NULL;
return (0); return (0);
} }
@ -39,9 +42,9 @@ spho_scope_term(struct spho_scope *sc)
sc->parent = NULL; sc->parent = NULL;
if (sc->stat_bind == NULL) if (sc->bind == NULL)
spho_bind_scope_destroy(sc->stat_bind); spho_prebind_destroy(sc->bind);
sc->stat_bind = NULL; sc->bind = NULL;
} }
struct spho_scope * struct spho_scope *
@ -95,16 +98,12 @@ spho_scope_destroy(struct spho_scope *sc)
struct spho_nom * struct spho_nom *
spho_scope_nom_add(struct spho_scope *sc, const char *nomstr, size_t sz) spho_scope_nom_add(struct spho_scope *sc, const char *nomstr, size_t sz)
{ {
#ifdef SPHO_USE_STRLCPY
size_t res; size_t res;
#else
ssize_t res;
#endif
struct spho_nom *nom; struct spho_nom *nom;
nom = NULL; nom = NULL;
if (scope_nom_lookup_str_loc(sc, nomstr, sz, &nom) == -1) if (scope_nom_lookup_str_local(sc, nomstr, sz, &nom) == -1)
return (NULL); return (NULL);
if (nom != NULL) { if (nom != NULL) {
@ -117,25 +116,14 @@ spho_scope_nom_add(struct spho_scope *sc, const char *nomstr, size_t sz)
return (NULL); return (NULL);
} }
#ifdef SPHO_USE_STRLCPY
res = strlcpy(nom->s, nomstr, sizeof(nom->s)); res = strlcpy(nom->s, nomstr, sizeof(nom->s));
if (res >= sizeof(nom->s)) { if (res >= sizeof(nom->s)) {
SPHO_SC_ERR(sc, SPHO_ERR_TOOBIG); SPHO_SC_ERR(sc, SPHO_ERR_TOOBIG);
goto err; goto err;
} }
#else
res = snprintf(nom->s, sizeof(nom->s), "%s", nomstr);
if (res < 0) {
SPHO_SC_ERR(sc, SPHO_ERR_SYS);
goto err;
}
if (res >= (ssize_t)sizeof(nom->s)) {
SPHO_SC_ERR(sc, SPHO_ERR_TOOBIG);
goto err;
}
#endif
SLIST_INSERT_HEAD(&sc->noms, nom, next); SLIST_INSERT_HEAD(&sc->noms, nom, next);
sc->n_noms++;
return (nom); return (nom);
err: err:
@ -187,9 +175,9 @@ spho_scope_nom_lookup_str_strict(struct spho_scope *sc, const char *nomstr,
} }
int int
spho_scope_static_bind_init(struct spho_scope *sc) spho_scope_prebind_init(struct spho_scope *sc)
{ {
if ((sc->stat_bind = spho_bind_scope_create(sc)) if ((sc->bind = spho_prebind_create(sc))
== NULL) { == NULL) {
return (-1); return (-1);
} }
@ -198,19 +186,42 @@ spho_scope_static_bind_init(struct spho_scope *sc)
} }
int int
spho_scope_static_bind_add_tp(struct spho_scope *sc, struct spho_nom *nom, spho_scope_prebind_tp(struct spho_scope *sc, struct spho_nom *nom,
struct spho_tp *tp) struct spho_tp *tp)
{ {
if (! scope_nom_in_loc(sc, nom)) { if (! scope_nom_in_local(sc, nom)) {
SPHO_ERR(sc->ctx, SPHO_ERR_NOM_NOTINSCOPE); SPHO_ERR(sc->ctx, SPHO_ERR_NOM_NOTINSCOPE);
return (-1); return (-1);
} }
return (spho_bind_scope_add_tp(sc->stat_bind, nom, tp)); return (spho_prebind_tp(sc->bind, nom, tp));
}
int
spho_scope_prebind_tp_op(struct spho_scope *sc, struct spho_nom *nom,
struct spho_tp_op *tp_op)
{
if (! scope_nom_in_local(sc, nom)) {
SPHO_ERR(sc->ctx, SPHO_ERR_NOM_NOTINSCOPE);
return (-1);
}
return (spho_prebind_tp_op(sc->bind, nom, tp_op));
}
int
spho_scope_prebind_undef(struct spho_scope *sc, struct spho_nom *nom)
{
if (! scope_nom_in_local(sc, nom)) {
SPHO_ERR(sc->ctx, SPHO_ERR_NOM_NOTINSCOPE);
return (-1);
}
return (spho_prebind_undef(sc->bind, nom));
} }
static int static int
scope_nom_lookup_str_loc(struct spho_scope *sc, const char *nomstr, scope_nom_lookup_str_local(struct spho_scope *sc, const char *nomstr,
size_t sz, struct spho_nom **out) size_t sz, struct spho_nom **out)
{ {
struct spho_nom *nom; struct spho_nom *nom;
@ -234,7 +245,7 @@ scope_nom_lookup_str_loc(struct spho_scope *sc, const char *nomstr,
} }
static int static int
scope_nom_in_loc(struct spho_scope *sc, struct spho_nom *nom) scope_nom_in_local(struct spho_scope *sc, struct spho_nom *nom)
{ {
struct spho_nom *sc_nom; struct spho_nom *sc_nom;

View file

@ -2,8 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "spho/ctx.h" #include "spho/spho.h"
#include "spho/scope.h"
#include "spho/tp.h" #include "spho/tp.h"
@ -26,6 +25,22 @@ spho_tp_alloc(struct spho_scope *sc)
return ((struct spho_tp *)tp_alloc); return ((struct spho_tp *)tp_alloc);
} }
static struct spho_tp_op *
spho_tp_op_alloc(struct spho_scope *sc)
{
struct spho_tp_alloc *tp_alloc;
if ((tp_alloc = calloc(1, sizeof(*tp_alloc))) == NULL) {
SPHO_SC_ERR(sc, SPHO_ERR_SYS);
return (NULL);
}
((struct spho_tp_op *)tp_alloc)->sc = sc;
TAILQ_INSERT_TAIL(&sc->tps, tp_alloc, allocs);
return ((struct spho_tp_op *)tp_alloc);
}
static void static void
spho_tp_free(struct spho_tp *tp) spho_tp_free(struct spho_tp *tp)
{ {
@ -222,7 +237,39 @@ spho_tp_create_nominal(struct spho_scope *sc, struct spho_nom *nom)
return (ret); return (ret);
} }
struct spho_tp *
spho_tp_create_var(struct spho_scope *sc, struct spho_nom *nom)
{
struct spho_tp *ret;
if ((ret = spho_tp_alloc(sc)) == NULL)
return (NULL);
ret->kind = SPHO_TP_FORM_VAR;
ret->d.nom.nom = nom;
return (ret);
}
struct spho_tp_op *
spho_tp_op_create(struct spho_scope *sc, struct spho_scope *op_sc, struct spho_tp *tp)
{
struct spho_tp_op *op;
SPHO_PRECOND(sc != NULL);
SPHO_PRECOND(op_sc != NULL);
SPHO_PRECOND(tp != NULL);
if ((op = spho_tp_op_alloc(sc)) == NULL)
return (NULL);
op->sc = sc;
op->op_sc = op_sc;
op->op_tp = tp;
return (op);
}
/* Free type structure. External data (like nom) are freed elsewhere. */ /* Free type structure. External data (like nom) are freed elsewhere. */
void void
@ -234,6 +281,7 @@ spho_tp_destroy(struct spho_tp *tp)
case SPHO_TP_FORM_TRUE: case SPHO_TP_FORM_TRUE:
case SPHO_TP_FORM_FALSE: case SPHO_TP_FORM_FALSE:
case SPHO_TP_FORM_NAME: case SPHO_TP_FORM_NAME:
case SPHO_TP_FORM_VAR:
break; break;
case SPHO_TP_FORM_CONJ: case SPHO_TP_FORM_CONJ:
case SPHO_TP_FORM_DISJ: case SPHO_TP_FORM_DISJ: