prefix notation type parser
This commit is contained in:
parent
9b24c8a496
commit
9ac779c1cf
14 changed files with 1217 additions and 151 deletions
18
include/msph/common.h
Normal file
18
include/msph/common.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef _MSPH_COMMON_H
|
||||
#define _MSPH_COMMON_H
|
||||
|
||||
#include "msph/err.h"
|
||||
|
||||
#define MSPH_IDENT_LEN 128
|
||||
|
||||
struct msph_ctx {
|
||||
int err;
|
||||
int err_info;
|
||||
char errbuf[SPHO_ERR_BUF_LEN];
|
||||
|
||||
#ifdef SPHO_DEBUG
|
||||
char filebuf[SPHO_ERR_FILEBUF_LEN];
|
||||
int errline;
|
||||
#endif
|
||||
};
|
||||
#endif
|
|
@ -10,11 +10,31 @@
|
|||
|
||||
#define MSPH_ERR_SYS 0x0001
|
||||
|
||||
#define MSPH_ERR_INVAL 0x1001
|
||||
#define MSPH_ERR_INVAL 0x0002
|
||||
|
||||
#define MSPH_ERR_TOOLONG 0x2001
|
||||
#define MSPH_ERR_TOKEN_TOOLONG 0x1001
|
||||
#define MSPH_ERR_TOKEN_EOF 0x1002
|
||||
#define MSPH_ERR_TOKEN_NOMATCH 0x1003
|
||||
#define MSPH_ERR_TOKEN_INVAL 0x1004
|
||||
|
||||
#define MSPH_ERR_TREE_TOOLONG 0x2001
|
||||
#define MSPH_ERR_TREE_EOF 0x2002
|
||||
#define MSPH_ERR_TREE_NOMATCH 0x2003
|
||||
|
||||
|
||||
// TODO add more diagnostics
|
||||
|
||||
#define MSPH_ERR(ctx, e) SPHO_ERR(ctx, e)
|
||||
#define MSPH_TOKS_ERR(toks, e) MSPH_ERR((toks)->ctx, e)
|
||||
|
||||
#define MSPH_ERR_INFO(ctx, e, info) SPHO_ERR_INFO(ctx, e, info)
|
||||
|
||||
|
||||
#define MSPH_ERR_RESET(ctx) \
|
||||
do { \
|
||||
ctx->err = 0; \
|
||||
ctx->err_info = 0; \
|
||||
} while (0)
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,53 +1,11 @@
|
|||
#ifndef _MSPH_EXPR_H
|
||||
#define _MSPH_EXPR_H
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "msph/common.h"
|
||||
|
||||
/*
|
||||
* TYPES:
|
||||
* Conj
|
||||
* Disj
|
||||
* Impl
|
||||
* 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 check (check A <: B, checks if A <: B)
|
||||
*
|
||||
* EXTRA DEFINITIONS
|
||||
* Class definition (class C[...] { ... }, shorthand)
|
||||
*
|
||||
* EXPRESSIONS
|
||||
* Conj
|
||||
* Disj
|
||||
* Impl
|
||||
* Arrow
|
||||
* Box
|
||||
* (Sub)
|
||||
* Forall
|
||||
*
|
||||
* True
|
||||
* False
|
||||
* Var
|
||||
* Nominal
|
||||
*
|
||||
* Trait ({ ... }, creates scoping)
|
||||
*
|
||||
*
|
||||
* TOKENS
|
||||
|
@ -79,16 +37,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
struct msph_ctx {
|
||||
int err;
|
||||
int err_info;
|
||||
char errbuf[SPHO_ERR_BUF_LEN];
|
||||
|
||||
#ifdef SPHO_DEBUG
|
||||
char filebuf[SPHO_ERR_FILEBUF_LEN];
|
||||
int errline;
|
||||
#endif
|
||||
};
|
||||
|
||||
enum msph_tok_type {
|
||||
TOK_LBRACE, // {
|
||||
|
@ -99,6 +47,8 @@ enum msph_tok_type {
|
|||
TOK_RPAREN, // )
|
||||
TOK_COLON, // :
|
||||
TOK_EQUALS, // =
|
||||
TOK_COMMA, // ,
|
||||
TOK_DOT, // .
|
||||
|
||||
TOK_AMP, // &
|
||||
TOK_PIPE, // |
|
||||
|
@ -109,7 +59,7 @@ enum msph_tok_type {
|
|||
TOK_KW_TYPE, // type
|
||||
TOK_KW_NOMINAL, // nominal
|
||||
TOK_KW_MEMBER, // member
|
||||
TOK_KW_CHECK, // check
|
||||
TOK_KW_ASSERT, // assert
|
||||
TOK_KW_BOX, // box
|
||||
TOK_KW_FORALL, // forall
|
||||
|
||||
|
@ -117,23 +67,20 @@ enum msph_tok_type {
|
|||
TOK_CONST_FALSE, // False
|
||||
|
||||
TOK_IDENT, // identifiers
|
||||
TOK_INVAL, // special: invalid, use to mark invalid toks
|
||||
TOK_WSPACE, // special: whitespace
|
||||
TOK_END // special: denote end of array
|
||||
};
|
||||
|
||||
#define MSPH_TOKEN_BUF_LEN 128
|
||||
|
||||
struct token_s {
|
||||
char buf[MSPH_TOKEN_BUF_LEN];
|
||||
};
|
||||
|
||||
union token_data {
|
||||
struct token_s s;
|
||||
union msph_token_data {
|
||||
char str[MSPH_IDENT_LEN];
|
||||
};
|
||||
|
||||
struct msph_token {
|
||||
int type;
|
||||
union token_data d;
|
||||
union msph_token_data data;
|
||||
|
||||
SLIST_ENTRY(msph_token) entries;
|
||||
};
|
||||
|
||||
#define MSPH_FILE_NAME_LEN 1024
|
||||
|
@ -147,9 +94,6 @@ struct msph_token_src_file {
|
|||
size_t pos;
|
||||
size_t end;
|
||||
char buf[MSPH_FILE_BUF_LEN];
|
||||
|
||||
/* file path */
|
||||
char name[MSPH_FILE_NAME_LEN];
|
||||
};
|
||||
|
||||
#define MSPH_TOKEN_SRC_STR 2
|
||||
|
@ -178,17 +122,21 @@ struct msph_token_stream {
|
|||
void msph_ctx_init(struct msph_ctx *);
|
||||
|
||||
struct msph_token_stream *msph_token_stream_file(struct msph_ctx *,
|
||||
FILE *, const char *);
|
||||
FILE *);
|
||||
struct msph_token_stream *msph_token_stream_frombuf(struct msph_ctx *,
|
||||
const char *, size_t);
|
||||
|
||||
int msph_token_stream_close(struct msph_token_stream*);
|
||||
|
||||
ssize_t msph_token_stream_read_tok(struct msph_token *, size_t,
|
||||
ssize_t msph_token_stream_read(struct msph_token *, size_t,
|
||||
struct msph_token_stream *);
|
||||
int msph_token_stream_eof(struct msph_token_stream *);
|
||||
int msph_token_stream_print(struct msph_token_stream *, FILE *);
|
||||
|
||||
ssize_t msph_token_str(char *buf, size_t len, struct msph_token *tok);
|
||||
ssize_t msph_token_str(char *, size_t, struct msph_token *);
|
||||
|
||||
struct msph_token * msph_token_create(struct msph_ctx *, int,
|
||||
union msph_token_data *);
|
||||
|
||||
|
||||
#endif /* _MSPH_EXPR_H */
|
||||
|
|
239
include/msph/tree.h
Normal file
239
include/msph/tree.h
Normal file
|
@ -0,0 +1,239 @@
|
|||
#ifndef _MSPH_TREE_H
|
||||
#define _MSPH_TREE_H
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "msph/common.h"
|
||||
#include "msph/token.h"
|
||||
|
||||
/*
|
||||
* TYPES:
|
||||
* Conj
|
||||
* Disj
|
||||
* Impl
|
||||
* 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 0x0001
|
||||
|
||||
#define MSPH_TREE_BODY 0x0010
|
||||
|
||||
#define MSPH_TREE_DIR 0x0020
|
||||
#define MSPH_TREE_TPDEF 0x0021
|
||||
#define MSPH_TREE_NOMINDECL 0x0022
|
||||
#define MSPH_TREE_MEMBDECL 0x0023
|
||||
#define MSPH_TREE_ASSERT 0x0024
|
||||
|
||||
#define MSPH_TREE_TPEXPR 0x0040
|
||||
#define MSPH_TREE_TRUE 0x0041
|
||||
#define MSPH_TREE_FALSE 0x0042
|
||||
#define MSPH_TREE_NAME 0x0043
|
||||
#define MSPH_TREE_APPL 0x0044
|
||||
#define MSPH_TREE_TRAIT 0x0045
|
||||
#define MSPH_TREE_CONJ 0x0046
|
||||
#define MSPH_TREE_DISJ 0x0047
|
||||
#define MSPH_TREE_IMPL 0x0048
|
||||
#define MSPH_TREE_ARROW 0x0049
|
||||
#define MSPH_TREE_BOX 0x004a
|
||||
#define MSPH_TREE_FORALL 0x004b
|
||||
#define MSPH_TREE_PAREN 0x004c
|
||||
|
||||
#define MSPH_TREE_IDENT 0x0100
|
||||
|
||||
|
||||
struct msph_tree {
|
||||
struct msph_tree *parent;
|
||||
int type;
|
||||
};
|
||||
|
||||
struct msph_tree_root;
|
||||
struct msph_tree_body;
|
||||
struct msph_tree_dir;
|
||||
struct msph_tree_tpdef;
|
||||
struct msph_tree_nomindecl;
|
||||
struct msph_tree_assert;
|
||||
struct msph_tree_ident;
|
||||
struct msph_tree_tpexpr;
|
||||
|
||||
struct msph_tree_root {
|
||||
struct msph_tree tr;
|
||||
|
||||
struct msph_ctx *ctx;
|
||||
struct msph_tree_body *body;
|
||||
};
|
||||
|
||||
struct msph_tree_body {
|
||||
struct msph_tree tr;
|
||||
|
||||
STAILQ_HEAD(msph_tree_dir_l, msph_tree_dir) head;
|
||||
};
|
||||
|
||||
struct msph_tree_dir {
|
||||
struct msph_tree tr;
|
||||
|
||||
STAILQ_ENTRY(msph_tree_dir) entries;
|
||||
};
|
||||
|
||||
struct msph_tree_tpdef {
|
||||
struct msph_tree_dir dir;
|
||||
|
||||
struct msph_tree_ident *id;
|
||||
struct msph_tree_tpexpr *tp;
|
||||
};
|
||||
|
||||
struct msph_tree_nomindecl {
|
||||
struct msph_tree_dir dir;
|
||||
|
||||
struct msph_tree_ident *id;
|
||||
};
|
||||
|
||||
struct msph_tree_membdecl {
|
||||
struct msph_tree_dir dir;
|
||||
|
||||
struct msph_tree_ident *id;
|
||||
struct msph_tree_tpexpr *tp;
|
||||
};
|
||||
|
||||
struct msph_tree_assert {
|
||||
struct msph_tree_dir dir;
|
||||
|
||||
struct msph_tree_tpexpr *ltp;
|
||||
struct msph_tree_tpexpr *rtp;
|
||||
};
|
||||
|
||||
struct msph_tree_tpexpr {
|
||||
struct msph_tree tr;
|
||||
|
||||
STAILQ_ENTRY(msph_tree_tpexpr) entries;
|
||||
};
|
||||
|
||||
|
||||
struct msph_tree_true {
|
||||
struct msph_tree_tpexpr tp;
|
||||
};
|
||||
|
||||
struct msph_tree_false {
|
||||
struct msph_tree_tpexpr tp;
|
||||
};
|
||||
|
||||
struct msph_tree_name {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_ident *id;
|
||||
};
|
||||
|
||||
struct msph_tree_appl {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_ident *id;
|
||||
STAILQ_HEAD(msph_tree_tpexpr_l, msph_tree_tpexpr) head;
|
||||
};
|
||||
|
||||
struct msph_tree_trait {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_body *body;
|
||||
};
|
||||
|
||||
struct msph_tree_conj {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_tpexpr *ltp;
|
||||
struct msph_tree_tpexpr *rtp;
|
||||
};
|
||||
|
||||
struct msph_tree_disj {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_tpexpr *ltp;
|
||||
struct msph_tree_tpexpr *rtp;
|
||||
};
|
||||
|
||||
struct msph_tree_impl {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_tpexpr *ltp;
|
||||
struct msph_tree_tpexpr *rtp;
|
||||
};
|
||||
|
||||
struct msph_tree_arrow {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_tpexpr *ltp;
|
||||
struct msph_tree_tpexpr *rtp;
|
||||
};
|
||||
|
||||
struct msph_tree_box {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_tpexpr *inner;
|
||||
};
|
||||
|
||||
struct msph_tree_forall {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_ident *ident;
|
||||
struct msph_tree_tpexpr *inner;
|
||||
};
|
||||
|
||||
struct msph_tree_paren {
|
||||
struct msph_tree_tpexpr tp;
|
||||
|
||||
struct msph_tree_tpexpr *inner;
|
||||
};
|
||||
|
||||
struct msph_tree_ident {
|
||||
struct msph_tree tr;
|
||||
|
||||
char str[MSPH_IDENT_LEN];
|
||||
STAILQ_ENTRY(msph_tree_ident) entries;
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct msph_tree_root *msph_tree_makeroot(struct msph_ctx *);
|
||||
|
||||
int msph_tree_parse(struct msph_token_stream *, struct msph_tree_root *);
|
||||
|
||||
#endif
|
|
@ -18,17 +18,23 @@
|
|||
|
||||
#define SPHO_ERR_BUF_LEN 2048
|
||||
|
||||
|
||||
#define SPHO_ERR(ctx, e) \
|
||||
do { \
|
||||
(ctx)->err = (e); \
|
||||
if ((e) == SPHO_ERR_SYS) \
|
||||
SPHO_ERR_INFO(ctx, e, errno); \
|
||||
else \
|
||||
SPHO_ERR_INFO(ctx, e, 0); \
|
||||
} while (0)
|
||||
|
||||
#ifdef SPHO_DEBUG
|
||||
|
||||
#define SPHO_ERR_FILEBUF_LEN 128
|
||||
|
||||
#define SPHO_ERR(ctx, e) \
|
||||
#define SPHO_ERR_INFO(ctx, e, info) \
|
||||
do { \
|
||||
(ctx)->err = (e); \
|
||||
if ((e) == SPHO_ERR_SYS) \
|
||||
(ctx)->err_info = errno; \
|
||||
(ctx)->err_info = (info); \
|
||||
snprintf((ctx)->filebuf, sizeof((ctx)->filebuf), \
|
||||
__FILE__); \
|
||||
(ctx)->errline = __LINE__; \
|
||||
|
@ -85,18 +91,17 @@
|
|||
|
||||
#else /* SPHO_DEBUG */
|
||||
|
||||
#define SPHO_ERR(ctx, e) \
|
||||
#define SPHO_ERR_INFO(ctx, e, info) \
|
||||
do { \
|
||||
(ctx)->err = (e); \
|
||||
if ((e) == SPHO_ERR_SYS) \
|
||||
(ctx)->err_info = errno; \
|
||||
(ctx)->err_info = (info); \
|
||||
} while (0)
|
||||
|
||||
#define SPHO_PRECOND(cond)
|
||||
#define SPHO_ASSERT(cond)
|
||||
#define SPHO_POSTCOND(cond)
|
||||
|
||||
#define SPHO_DBG_PRINT(fmt, ...)
|
||||
#define SPHO_DEBUG_PRINT(fmt, ...)
|
||||
|
||||
#endif /* SPHO_DEBUG */
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ union tp_data {
|
|||
struct tp_bappl_data bappl;
|
||||
struct tp_konst_data konst;
|
||||
struct tp_var_data var;
|
||||
struct tp_nominal_data nom;
|
||||
struct tp_nominal_data nom; // XXX
|
||||
struct tp_record_data rec;
|
||||
};
|
||||
|
||||
|
@ -126,6 +126,7 @@ struct spho_tp {
|
|||
|
||||
STAILQ_HEAD(spho_tp_l, spho_tp);
|
||||
|
||||
|
||||
#define SPHO_TP_ERR(tp, err) SPHO_ERR((tp)->sc->ctx, (err))
|
||||
|
||||
struct spho_var *spho_var_create(struct spho_scope *, char *, size_t);
|
||||
|
|
|
@ -13,6 +13,13 @@ do { \
|
|||
} \
|
||||
} 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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue