sphophi decorating with types
This commit is contained in:
parent
17be15d7b5
commit
a22c9726cd
19 changed files with 1762 additions and 380 deletions
319
src/spho/tp.c
319
src/spho/tp.c
|
@ -7,6 +7,9 @@
|
|||
#include "spho/tp.h"
|
||||
|
||||
|
||||
const struct tp_konst_data tp_konst_true = { TP_KONST_KEY_TRUE, "True" };
|
||||
const struct tp_konst_data tp_konst_false = { TP_KONST_KEY_FALSE, "False" };
|
||||
|
||||
//struct spho_tp *
|
||||
//spho_tp_create_disj(struct spho_ctx *, struct spho_tp *, struct spho_tp *,
|
||||
// struct spho_tp *)
|
||||
|
@ -14,177 +17,239 @@
|
|||
//
|
||||
//}
|
||||
|
||||
|
||||
struct spho_tp *
|
||||
spho_tp_alloc(struct spho_scope *sc, int form)
|
||||
static struct spho_tp *
|
||||
spho_tp_alloc(struct spho_scope *sc)
|
||||
{
|
||||
struct spho_tp *tp;
|
||||
struct spho_tp_alloc *tp_alloc;
|
||||
|
||||
SPHO_PRECOND(SPHO_TP_FORM_IS_VALID(form));
|
||||
|
||||
if ((tp = calloc(1, sizeof(struct spho_tp))) == NULL) {
|
||||
if ((tp_alloc = calloc(1, sizeof(*tp_alloc))) == NULL) {
|
||||
SPHO_SC_ERR(sc, SPHO_ERR_SYS);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
tp->sc = sc;
|
||||
tp->form = form;
|
||||
((struct spho_tp *)tp_alloc)->sc = sc;
|
||||
TAILQ_INSERT_TAIL(&sc->tps, tp_alloc, allocs);
|
||||
|
||||
return ((struct spho_tp *)tp_alloc);
|
||||
}
|
||||
|
||||
static void
|
||||
spho_tp_free(struct spho_tp *tp)
|
||||
{
|
||||
struct spho_scope *sc;
|
||||
struct spho_tp_alloc *tp_alloc;
|
||||
|
||||
sc = tp->sc;
|
||||
tp_alloc = (struct spho_tp_alloc *)tp;
|
||||
|
||||
TAILQ_REMOVE(&sc->tps, tp_alloc, allocs);
|
||||
|
||||
free(tp_alloc);
|
||||
}
|
||||
|
||||
struct spho_tp *
|
||||
spho_tp_create_conj(struct spho_scope *sc, struct spho_tp *ltp,
|
||||
struct spho_tp *rtp)
|
||||
{
|
||||
struct spho_tp *tp;
|
||||
|
||||
if ((tp = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
tp->form = SPHO_TP_FORM_CONJ;
|
||||
tp->d.binop.left = ltp;
|
||||
tp->d.binop.right = rtp;
|
||||
|
||||
return (tp);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
spho_tp_init_disj(struct spho_tp *tp, struct spho_tp *a, struct spho_tp *b)
|
||||
struct spho_tp *
|
||||
spho_tp_create_disj(struct spho_scope *sc, struct spho_tp *ltp,
|
||||
struct spho_tp *rtp)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_DISJ) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
struct spho_tp *tp;
|
||||
|
||||
tp->data.binop.left = a;
|
||||
tp->data.binop.right = b;
|
||||
if ((tp = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
return (0);
|
||||
tp->form = SPHO_TP_FORM_DISJ;
|
||||
tp->d.binop.left = ltp;
|
||||
tp->d.binop.right = rtp;
|
||||
|
||||
return (tp);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_conj(struct spho_tp *tp, struct spho_tp *a, struct spho_tp *b)
|
||||
struct spho_tp *
|
||||
spho_tp_create_impl(struct spho_scope *sc, struct spho_tp *ltp,
|
||||
struct spho_tp *rtp)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_CONJ) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
struct spho_tp *tp;
|
||||
|
||||
tp->data.binop.left = a;
|
||||
tp->data.binop.right = b;
|
||||
if ((tp = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
return (0);
|
||||
tp->form = SPHO_TP_FORM_IMPL;
|
||||
tp->d.binop.left = ltp;
|
||||
tp->d.binop.right = rtp;
|
||||
|
||||
return (tp);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_impl(struct spho_tp *tp, struct spho_tp *a, struct spho_tp *b)
|
||||
struct spho_tp *
|
||||
spho_tp_create_arrow(struct spho_scope *sc, struct spho_tp *ltp,
|
||||
struct spho_tp *rtp)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_IMPL) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
struct spho_tp *tp;
|
||||
|
||||
tp->data.binop.left = a;
|
||||
tp->data.binop.right = b;
|
||||
if ((tp = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
return (0);
|
||||
tp->form = SPHO_TP_FORM_ARROW;
|
||||
tp->d.binop.left = ltp;
|
||||
tp->d.binop.right = rtp;
|
||||
|
||||
return (tp);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_arrow(struct spho_tp *tp, struct spho_tp *a, struct spho_tp *b)
|
||||
struct spho_tp *
|
||||
spho_tp_create_box(struct spho_scope *sc, struct spho_tp *inner)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_ARROW) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
struct spho_tp *tp;
|
||||
|
||||
tp->data.binop.left = a;
|
||||
tp->data.binop.right = b;
|
||||
if ((tp = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
return (0);
|
||||
tp->form = SPHO_TP_FORM_BOX;
|
||||
tp->d.unop.operand = inner;
|
||||
|
||||
return (tp);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_box(struct spho_tp *tp, struct spho_tp *inr)
|
||||
struct spho_tp *
|
||||
spho_tp_create_forall(struct spho_scope *sc, struct spho_nom *nom,
|
||||
struct spho_tp *tp)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_BOX) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
struct spho_tp *ret;
|
||||
|
||||
tp->data.box.inr = inr;
|
||||
if ((ret = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
return (0);
|
||||
ret->form = SPHO_TP_FORM_FORALL;
|
||||
ret->d.bind.nom = nom;
|
||||
ret->d.bind.bound = tp;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_forall(struct spho_tp *tp, struct spho_var *var,
|
||||
struct spho_tp *inr)
|
||||
struct spho_tp *
|
||||
spho_tp_create_true(struct spho_scope *sc)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_FORALL) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
struct spho_tp *ret;
|
||||
|
||||
tp->data.fa.var = var;
|
||||
tp->data.fa.inr = inr;
|
||||
if ((ret = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
return (0);
|
||||
ret->form = SPHO_TP_FORM_TRUE;
|
||||
ret->d.konst = tp_konst_true;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_bappl(struct spho_tp *tp, struct spho_bind *bind,
|
||||
struct spho_tp *
|
||||
spho_tp_create_false(struct spho_scope *sc)
|
||||
{
|
||||
struct spho_tp *ret;
|
||||
|
||||
if ((ret = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
ret->form = SPHO_TP_FORM_FALSE;
|
||||
ret->d.konst = tp_konst_false;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
struct spho_tp *
|
||||
spho_tp_create_name(struct spho_scope *sc, struct spho_nom *nom)
|
||||
{
|
||||
struct spho_tp *ret;
|
||||
|
||||
if ((ret = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
ret->form = SPHO_TP_FORM_NOM;
|
||||
ret->d.nom.nom = nom;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
struct spho_tp *
|
||||
spho_tp_create_appl(struct spho_scope *sc, struct spho_nom *nom,
|
||||
struct spho_tp_l *args)
|
||||
{
|
||||
struct spho_tp *ret;
|
||||
|
||||
if ((ret = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
ret->form = SPHO_TP_FORM_APPL;
|
||||
ret->d.appl.nom = nom;
|
||||
ret->d.appl.args = args;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
struct spho_tp *
|
||||
spho_tp_create_member(struct spho_scope *sc, struct spho_nom *nom,
|
||||
struct spho_tp *tp)
|
||||
{
|
||||
struct spho_tp *ret;
|
||||
|
||||
if ((ret = spho_tp_alloc(sc)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
ret->form = SPHO_TP_FORM_MEMBER;
|
||||
ret->d.bind.nom = nom;
|
||||
ret->d.bind.bound = tp;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/* Free type structure. External data (like nom) are freed elsewhere. */
|
||||
void
|
||||
spho_tp_destroy(struct spho_tp *tp)
|
||||
{
|
||||
struct spho_tp *arg;
|
||||
|
||||
if (tp->form != SPHO_TP_FORM_BAPPL) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
switch (tp->form) {
|
||||
case SPHO_TP_FORM_TRUE:
|
||||
case SPHO_TP_FORM_FALSE:
|
||||
case SPHO_TP_FORM_NOM:
|
||||
break;
|
||||
case SPHO_TP_FORM_CONJ:
|
||||
case SPHO_TP_FORM_DISJ:
|
||||
case SPHO_TP_FORM_IMPL:
|
||||
case SPHO_TP_FORM_ARROW:
|
||||
spho_tp_destroy(tp->d.binop.left);
|
||||
spho_tp_destroy(tp->d.binop.right);
|
||||
break;
|
||||
case SPHO_TP_FORM_BOX:
|
||||
spho_tp_destroy(tp->d.unop.operand);
|
||||
break;
|
||||
case SPHO_TP_FORM_FORALL:
|
||||
case SPHO_TP_FORM_MEMBER:
|
||||
spho_tp_destroy(tp->d.bind.bound);
|
||||
break;
|
||||
case SPHO_TP_FORM_APPL:
|
||||
STAILQ_FOREACH(arg, tp->d.appl.args, entries) {
|
||||
spho_tp_destroy(arg);
|
||||
}
|
||||
free(tp->d.appl.args);
|
||||
break;
|
||||
default:
|
||||
SPHO_ASSERT(0);
|
||||
break;
|
||||
}
|
||||
|
||||
tp->data.bappl.bind = bind;
|
||||
tp->data.bappl.args = args;
|
||||
|
||||
return (0);
|
||||
spho_tp_free(tp);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_false(struct spho_tp *tp)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_FALSE) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
tp->data.konst.k = SPHO_K_FALSE;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_true(struct spho_tp *tp)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_TRUE) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
tp->data.konst.k = SPHO_K_TRUE;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_var(struct spho_tp *tp, struct spho_var *var)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_VAR) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
tp->data.var.v = var;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
spho_tp_init_rec(struct spho_tp *tp, struct spho_rec *rec)
|
||||
{
|
||||
if (tp->form != SPHO_TP_FORM_REC) {
|
||||
SPHO_TP_ERR(tp, SPHO_ERR_ARGINVAL);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
tp->data.rec.r = rec;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue