summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/expr.h
blob: 485464207f3bbd12deebfd9de4a4578935727b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef EXPR_H_
#define EXPR_H_

#include <gu/in.h>
#include <gu/out.h>
#include <gu/variant.h>
#include <gu/seq.h>

/// Abstract syntax trees
/// @file

/// An abstract syntax tree
typedef GuVariant PgfExpr;

extern GU_DECLARE_TYPE(PgfExpr, GuVariant);

typedef GuList(PgfExpr) PgfExprs;

typedef struct PgfHypo PgfHypo;
typedef struct PgfType PgfType;

typedef int PgfMetaId;

typedef enum {
	PGF_BIND_TYPE_EXPLICIT,
	PGF_BIND_TYPE_IMPLICIT
} PgfBindType;

// PgfLiteral

typedef GuVariant PgfLiteral;


typedef enum {
	PGF_LITERAL_STR,
	PGF_LITERAL_INT,
	PGF_LITERAL_FLT,
	PGF_LITERAL_NUM_TAGS
} PgfLiteralTag;

typedef struct {
	GuString val;
} PgfLiteralStr;

typedef struct {
	int val;
} PgfLiteralInt;

typedef struct {
	double val;
} PgfLiteralFlt;



struct PgfHypo {
	PgfBindType bind_type;

	PgfCId cid;
	/**< Locally scoped name for the parameter if dependent types
	 * are used. "_" for normal parameters. */

	PgfType* type;
};

typedef GuSeq PgfHypos;
extern GU_DECLARE_TYPE(PgfHypos, GuSeq);

struct PgfType {
	PgfHypos hypos;
	PgfCId cid; /// XXX: resolve to PgfCat*?
	size_t n_exprs;
	PgfExpr exprs[];
};

			
typedef enum {
	PGF_EXPR_ABS,
	PGF_EXPR_APP,
	PGF_EXPR_LIT,
	PGF_EXPR_META,
	PGF_EXPR_FUN,
	PGF_EXPR_VAR,
	PGF_EXPR_TYPED,
	PGF_EXPR_IMPL_ARG,
	PGF_EXPR_NUM_TAGS
} PgfExprTag;

typedef struct {
	PgfBindType bind_type;
	PgfCId id;
	PgfExpr body;
} PgfExprAbs;
		
typedef struct {
	PgfExpr fun;
	PgfExpr arg;
} PgfExprApp;

typedef struct {
	PgfLiteral lit;
} PgfExprLit;

typedef struct {
	PgfMetaId id;
} PgfExprMeta;

typedef struct {
	PgfCId fun;
} PgfExprFun;

typedef struct {
	int var;
} PgfExprVar;

/**< A variable. The value is a de Bruijn index to the environment,
 * beginning from the innermost variable. */

typedef struct {
	PgfExpr expr;
	PgfType* type;
} PgfExprTyped;

typedef struct {
	PgfExpr expr;
} PgfExprImplArg;

typedef float prob_t;

typedef struct {
	prob_t prob;
	PgfExpr expr;
} PgfExprProb;

extern GU_DECLARE_TYPE(PgfExprProb, struct);

int
pgf_expr_arity(PgfExpr expr);

PgfExpr
pgf_expr_unwrap(PgfExpr expr);

typedef struct PgfApplication PgfApplication;

struct PgfApplication {
	PgfCId fun;
	int n_args;
	PgfExpr args[];
};

PgfApplication*
pgf_expr_unapply(PgfExpr expr, GuPool* pool);


PgfExpr
pgf_read_expr(GuIn* in, GuPool* pool, GuExn* err);

PgfType*
pgf_read_type(GuIn* in, GuPool* pool, GuExn* err);

bool
pgf_literal_eq(PgfLiteral lit1, PgfLiteral lit2);

bool
pgf_expr_eq(PgfExpr e1, PgfExpr e2);

bool
pgf_type_eq(PgfType* t1, PgfType* t2);

GuHash
pgf_literal_hash(GuHash h, PgfLiteral lit);

GuHash
pgf_expr_hash(GuHash h, PgfExpr e);

typedef struct PgfPrintContext PgfPrintContext;

struct PgfPrintContext {
	PgfCId name;
	PgfPrintContext* next;
};

void
pgf_print_literal(PgfLiteral lit, GuOut* out, GuExn* err);

void
pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec, 
               GuOut* out, GuExn* err);

PgfPrintContext*
pgf_print_hypo(PgfHypo *hypo, PgfPrintContext* ctxt, int prec,
               GuOut* out, GuExn *err);

void
pgf_print_type(PgfType *type, PgfPrintContext* ctxt, int prec,
               GuOut* out, GuExn *err);

#endif /* EXPR_H_ */