broken code, but added attempt at writing grammar

This commit is contained in:
Ellen Arvidsson 2025-04-14 17:40:22 +02:00
parent fb95e5d026
commit 20e3757f44
18 changed files with 1145 additions and 142 deletions

View file

@ -1,28 +1,31 @@
#ifndef _SPHO_CTX_H
#define _SPHO_CTX_H
#include "spho/data.h"
#include "spho/err.h"
#include "spho/scope.h"
#define SPHO_ERR_BUF_LEN 2048
#define SPHO_ERR_FILEBUF_LEN 128
struct spho_ctx {
struct spho_scope glob;
int err;
int err_info;
struct spho_nom_l noms;
struct spho_const_l cnsts;
char errbuf[SPHO_ERR_BUF_LEN];
#ifdef SPHO_DEBUG
char filebuf[SPHO_ERR_FILEBUF_LEN];
int errline;
#else
char errbuf[SPHO_ERR_BUF_LEN];
char filebuf[SPHO_ERR_FILEBUF_LEN];
int errline;
#endif
};
struct spho_ctx *spho_ctx_create(void);
void spho_ctx_destroy(struct spho_ctx *);
const char *spho_ctx_strerror(struct spho_ctx *);
struct spho_nom *spho_nom_add(struct spho_ctx *, char *, size_t);
struct spho_nom *spho_nom_get(struct spho_ctx *, char *, size_t);
#endif

View file

@ -1,36 +1,12 @@
#ifndef _SPHO_DATA_H
#define _SPHO_DATA_H
#include <sys/queue.h>
struct spho_name {
char str[128];
};
struct spho_var {
struct spho_name name;
};
struct spho_cnst {
struct spho_name name;
};
struct spho_nom {
struct spho_name name;
char s[128];
SLIST_ENTRY(spho_nom) next;
};
struct spho_nom_le {
struct spho_nom nom;
SLIST_ENTRY(spho_nom_le) next;
};
struct spho_cnst_le {
struct spho_cnst cnst;
SLIST_ENTRY(spho_cnst_le) next;
};
SLIST_HEAD(spho_nom_l, spho_nom);
SLIST_HEAD(spho_nom_l, spho_nom_le);
SLIST_HEAD(spho_const_l, spho_cnst_le);
#endif
#endif /* _SPHO_DATA_H */

View file

@ -1,32 +1,40 @@
#ifndef _SPHO_ERR_H
#define _SPHO_ERR_H
#define SPHO_ERR_SYSTEM 0x000001
#include <errno.h>
#define SPHO_ERR_INTERNAL 0x010001
#ifdef SPHO_DEBUG
#include <stdio.h>
#endif
#define SPHO_ERR_IS_SYSERR(err) (SPHO_ERR_SYSTEM == err)
#define SPHO_ERR_SYS 0x000001
#define SPHO_ERR_INTERNAL 0x010001
#define SPHO_ERR_TOOBIG 0x010002
#define SPHO_ERR_ARGINVAL 0x010003
#define SPHO_ERR_NAMEINUSE 0x010004
#define SPHO_ERR_IS_SYSERR(err) (SPHO_ERR_SYS == err)
#ifdef SPHO_DEBUG
#define SPHO_ERR(ctx, err) \
#define SPHO_ERR(ctx, e) \
do { \
ctx->err = err; \
if (err == SPHO_ERR_SYSTEM) \
ctx->err_info = errno; \
snprintf(ctx->filebuf, sizeof(ctx->filebuf), "%s", \
(ctx)->err = (e); \
if ((e) == SPHO_ERR_SYS) \
(ctx)->err_info = errno; \
snprintf((ctx)->filebuf, sizeof((ctx)->filebuf), \
__FILE__); \
ctx->filebuf[sizeof(ctx->filebuf) - 1] = '\0'; \
ctx->errline = __LINE__; \
(ctx)->errline = __LINE__; \
} while (0)
#else /* SPHO_DEBUG */
#define SPHO_ERR(ctx, err) \
#define SPHO_ERR(ctx, e) \
do { \
ctx->err = err; \
if (err == SPHO_ERR_SYSTEM) \
ctx->err_info = errno; \
(ctx)->err = (e); \
if ((e) == SPHO_ERR_SYS) \
(ctx)->err_info = errno; \
} while (0)
#endif /* SPHO_DEBUG */
@ -34,30 +42,40 @@
/* debug stuff */
#ifdef SPHO_DEBUG
/* PRECOND/ASSERT/POSTCOND */
#define SPHO_PRECOND(cond) do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_PRECOND(" #cond ")@" __FILE__ ":" __LINE__ \
" failed. Aborting."); \
abort(); \
} \
} while (0)
#define SPHO_STRINGIFY(a) #a
#define SPHO_MACRO_STR(b) SPHO_STRINGIFY(b)
#define SPHO_ASSERT(cond) do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_ASSERT(" #cond ")@" __FILE__ ":" __LINE__ \
" failed. Aborting."); \
abort(); \
} \
} while (0)
#define __LINE__S SPHO_MACRO_STR(__LINE__)
#define SPHO_POSTCOND(cond) do { \
if (! (cond) ) { \
fprintf(stderr, "SPHO_POSTCOND(" #cond ")@" __FILE__ ":" __LINE__ \
" failed. Aborting."); \
abort(); \
} \
} while (0)
#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)
#else
#define SPHO_PRECOND(cond)

View file

@ -31,4 +31,4 @@ struct spho_loc *spho_loc_create_line(struct spho_ctx *, char *, int);
struct spho_loc *spho_loc_create_linecol(struct spho_ctx *, char *, int, int);
void spho_loc_destroy(struct spho_loc *);
#endif
#endif

34
include/spho/scope.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef _SPHO_SCOPE_H
#define _SPHO_SCOPE_H
#include "spho/data.h"
SLIST_HEAD(spho_scope_l, spho_scope);
struct spho_scope {
struct spho_ctx *ctx;
struct spho_scope *parent;
struct spho_scope_l subs;
struct spho_nom_l noms;
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_del(struct spho_nom *);
#endif /* _SPHO_SCOPE_H */

View file

@ -1,6 +1,8 @@
#ifndef _SPHO_SPHO_H
#define _SPHO_SPHO_H
#include <sys/queue.h>
#include "spho/err.h"
#include "spho/ctx.h"

View file

@ -3,13 +3,42 @@
/* base defs */
struct spho_alias {
struct spho_name name;
struct spho_tp *tp;
#define SPHO_KONST_S_SZ 128
#define SPHO_KONST_K_TRUE 0x43445 /* don't change!!!!! */
#define SPHO_KONST_K_FALSE 0x66a57 /* don't change!1! */
struct spho_konst {
const char *s;
int k;
};
const struct spho_konst SPHO_K_TRUE_V = { "True", SPHO_KONST_K_TRUE };
const struct spho_konst SPHO_K_FALSE_V = { "False", SPHO_KONST_K_FALSE };
#define SPHO_K_TRUE (&SPHO_K_TRUE_V)
#define SPHO_K_FALSE (&SPHO_K_FALSE_V)
struct spho_var {
struct spho_nom nom;
STAILQ_ENTRY(spho_var) next;
};
STAILQ_HEAD(spho_var_l, spho_var);
struct spho_bind {
struct spho_nom nom;
struct spho_tpop *op;
};
struct spho_tpop {
struct spho_var_l params;
struct spho_tp *def;
};
struct spho_rec {
struct spho_name mname;
struct spho_nom mnom;
struct spho_tp *tp;
};
@ -20,21 +49,21 @@ struct tp_bin_data {
};
struct tp_box_data {
struct spho_tp *tp;
struct spho_tp *inr;
};
struct tp_forall_data {
struct spho_var *var;
struct spho_tp *tp;
struct spho_tp *inr;
};
struct tp_alias_data {
struct spho_alias *alias;
struct spho_tp_list *l;
struct tp_bappl_data {
struct spho_bind *bind;
struct spho_tp_l *args;
};
struct tp_const_data {
struct spho_const *c;
struct tp_konst_data {
const struct spho_konst *k;
};
struct tp_var_data {
@ -54,8 +83,8 @@ union tp_data {
struct tp_bin_data binop;
struct tp_box_data box;
struct tp_forall_data fa;
struct tp_alias_data alias;
struct tp_const_data cnst;
struct tp_bappl_data bappl;
struct tp_konst_data konst;
struct tp_var_data var;
struct tp_nominal_data nom;
struct tp_record_data rec;
@ -67,8 +96,9 @@ union tp_data {
#define SPHO_TP_FORM_CONJ 0x11
#define SPHO_TP_FORM_IMPL 0x12
#define SPHO_TP_FORM_BOX 0x13
#define SPHO_TP_FORM_FORALL 0x14
#define SPHO_TP_FORM_ALIAS 0x15
#define SPHO_TP_FORM_ARROW 0x14
#define SPHO_TP_FORM_FORALL 0x15
#define SPHO_TP_FORM_BAPPL 0x16
#define SPHO_TP_FORM_FALSE 0x20
#define SPHO_TP_FORM_TRUE 0x21
@ -77,43 +107,48 @@ union tp_data {
#define SPHO_TP_FORM_REC 0x23
// TODO explain
#define SPHO_TP_FORM_SUB 0x96
// #define SPHO_TP_FORM_SUB 0x96
#define SPHO_TP_FORM_IS_VALID(f) \
(f) >= SPHO_TP_FORM_DISJ && (f) <= SPHO_TP_FORM_BAPPL || \
(f) >= SPHO_TP_FORM_FALSE && (f) <= SPHO_TP_FORM_NOM || \
(f) == SPHO_TP_FORM_REC
/* sappho type */
struct spho_tp {
int form;
union tp_data data;
struct spho_scope *sc;
int form;
union tp_data data;
STAILQ_ENTRY(spho_tp) next;
};
struct spho_nom *spho_nom_add(struct spho_ctx *, char *, size_t);
struct spho_nom *spho_nom_get_or_add(struct spho_ctx *, char *, size_t);
// struct spho_nom *spho_tp_nom_get(char *, size_t);
STAILQ_HEAD(spho_tp_l, spho_tp);
struct spho_var *spho_var_create(struct spho_ctx *, char *, size_t);
struct spho_var *spho_var_get(struct spho_ctx *, char *, size_t);
#define SPHO_TP_ERR(tp, err) SPHO_ERR((tp)->sc->ctx, (err))
struct spho_alias *spho_alias_add(struct spho_ctx *, char *, size_t);
struct spho_alias *spho_alias_get(struct spho_ctx *, char *, size_t);
struct spho_var *spho_var_create(struct spho_scope *, char *, size_t);
struct spho_var *spho_var_get(struct spho_scope *, char *, size_t);
struct spho_tp *spho_tp_create_disj(struct spho_ctx *, struct spho_tp *,
struct spho_tp *, struct spho_tp *);
struct spho_tp *spho_tp_create_conj(struct spho_ctx *, struct spho_tp *,
struct spho_tp *, struct spho_tp *);
struct spho_tp *spho_tp_create_impl(struct spho_ctx *, struct spho_tp *,
struct spho_tp *, struct spho_tp *);
struct spho_tp *spho_tp_create_box(struct spho_ctx *, struct spho_tp *,
struct spho_tp *);
struct spho_tp *spho_tp_create_forall(struct spho_ctx *, struct spho_name *,
struct spho_tp *);
struct spho_tp *spho_tp_create_alias(struct spho_ctx *, struct spho_alias *,
struct spho_tp *);
struct spho_tp *spho_tp_create_const(struct spho_ctx *, struct spho_const *);
struct spho_tp *spho_tp_create_nominal(struct spho_ctx *,
struct spho_nom *);
struct spho_tp *spho_tp_create_var(struct spho_ctx *, struct spho_var *);
struct spho_tp *spho_tp_create_record(struct spho_ctx *, struct spho_rec *);
//struct spho_tp *spho_tp_create_sub(struct spho_ctx *, struct spho_tp *,
//struct spho_tp *spho_tp_create_disj(struct spho_scope *, struct spho_tp *,
// struct spho_tp *, struct spho_tp *);
//struct spho_tp *spho_tp_create_conj(struct spho_scope *, struct spho_tp *,
// struct spho_tp *, struct spho_tp *);
//struct spho_tp *spho_tp_create_impl(struct spho_scope *, struct spho_tp *,
// struct spho_tp *, struct spho_tp *);
//struct spho_tp *spho_tp_create_box(struct spho_scope *, struct spho_tp *,
// struct spho_tp *);
//struct spho_tp *spho_tp_create_arrow(struct spho_scope *, struct spho_tp *,
// struct spho_tp *);
//struct spho_tp *spho_tp_create_forall(struct spho_scope *, struct spho_name *,
// struct spho_tp *);
//struct spho_tp *spho_tp_create_const(struct spho_scope *, struct spho_const *);
//struct spho_tp *spho_tp_create_nominal(struct spho_scope *,
// struct spho_nom *);
//struct spho_tp *spho_tp_create_var(struct spho_scope *, struct spho_var *);
//struct spho_tp *spho_tp_create_record(struct spho_scope *, struct spho_rec *);
//struct spho_tp *spho_tp_create_sub(struct spho_scope *, struct spho_tp *,
// struct spho_tp *);
void spho_tp_destroy(struct spho_tp*);

18
include/spho/util.h Normal file
View file

@ -0,0 +1,18 @@
#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)
#endif