summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/reasoner.h
blob: d0ed09c42ef77157ce39b540f63ad417d8a7bed6 (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
#ifndef PGF_REASONER_H_
#define PGF_REASONER_H_

typedef GuStringMap PgfAbswersMap;

typedef struct PgfReasoner PgfReasoner;
typedef struct PgfReasonerState PgfReasonerState;
typedef struct PgfExprState PgfExprState;

typedef struct {
	PgfFunction code;
} PgfClosure;

typedef struct {
	PgfClosure header;
	PgfClosure* val;
} PgfIndirection;

typedef struct {
	PgfLiteral lit;
	GuBuf* consts;
	void* enter_stack_ptr;
} PgfEvalAccum;

struct PgfReasoner {
	GuPool* pool;
	GuPool* out_pool;
	PgfAbstr* abstract;
	PgfAbswersMap* table;
	GuBuf* pqueue;
	GuBuf* exprs;
	size_t n_reported_exprs;
	PgfClosure* start;
	PgfEvalGates* eval_gates; // cached from pgf->abstr->eval_gates
	GuExn* err;
	void* enter_stack_ptr;
	void* tmp;              // for temporary register spills
	PgfExprEnum en;
	PgfIndirection cafs[];  // derived from gu_seq_data(pgf->abstr->eval_gates->cafs)
};

typedef struct PgfEnv PgfEnv;

struct PgfEnv {
	PgfEnv* next;
	PgfClosure* closure;
};

typedef struct {
	PgfClosure header;
	PgfEnv*  env;
	PgfExpr  expr;
} PgfExprThunk;

typedef struct {
	PgfClosure header;
	PgfClosure* con;
	PgfClosure* args[];
} PgfValue;

typedef struct {
	PgfClosure header;
	int level;
} PgfValueGen;

typedef struct {
	PgfClosure header;
	PgfEnv*  env;
	PgfMetaId id;
} PgfValueMeta;

typedef struct {
	PgfClosure header;
	PgfLiteral lit;
} PgfValueLit;

typedef struct {
	PgfClosure header;
	PgfLiteral lit;
	GuBuf* consts;
} PgfValueSum;

typedef struct {
	PgfClosure header;
	PgfClosure* fun;
	size_t      n_args;
	PgfClosure* args[];
} PgfValuePAP;

struct PgfEvalGates {
	PgfFunction evaluate_expr_thunk;
	PgfFunction evaluate_indirection;
	PgfFunction evaluate_value;
	PgfFunction evaluate_value_lit;
	PgfFunction evaluate_value_pap;
	PgfFunction evaluate_value_lambda;
	PgfFunction evaluate_value_const;
	PgfFunction evaluate_meta;
	PgfFunction evaluate_gen;
	PgfFunction evaluate_sum;
	PgfFunction evaluate_caf;

	PgfFunction update_closure;
	PgfFunction update_pap;

	PgfFunction mk_const;

	PgfFunction combine1;
	PgfFunction combine2;
	PgfFunction complete;

	PgfClosure* (*enter)(PgfReasoner* rs, PgfClosure* closure);

	GuFinalizer fin;
	GuSeq* cafs;
};

PGF_INTERNAL_DECL PgfReasoner*
pgf_new_reasoner(PgfPGF* pgf, GuExn* err, GuPool* pool, GuPool* out_pool);

PGF_INTERNAL_DECL void
pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun);

PGF_INTERNAL_DECL void
pgf_reasoner_try_else(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun);

PGF_INTERNAL_DECL void
pgf_reasoner_combine1(PgfReasoner* rs, PgfClosure* closure);

PGF_INTERNAL_DECL void
pgf_reasoner_combine2(PgfReasoner* rs, PgfClosure* closure);

PGF_INTERNAL_DECL void
pgf_reasoner_complete(PgfReasoner* rs, PgfExprState* st);

PGF_INTERNAL_DECL void
pgf_reasoner_try_constant(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun);

PGF_INTERNAL_DECL void
pgf_reasoner_mk_string(PgfReasoner* rs, PgfExprState* parent);

PGF_INTERNAL_DECL void
pgf_reasoner_mk_int(PgfReasoner* rs, PgfExprState* parent);

PGF_INTERNAL_DECL void
pgf_reasoner_mk_float(PgfReasoner* rs, PgfExprState* parent);

PGF_INTERNAL_DECL PgfClosure*
pgf_evaluate_expr_thunk(PgfReasoner* rs, PgfExprThunk* thunk);

PGF_INTERNAL_DECL PgfClosure*
pgf_evaluate_lambda_application(PgfReasoner* rs, PgfExprThunk* lambda,
                                                 PgfClosure* arg);

PGF_INTERNAL_DECL void
pgf_evaluate_accum_init_int(PgfReasoner* rs, 
                            PgfEvalAccum* accum, int val);

PGF_INTERNAL_DECL void
pgf_evaluate_accum_init_str(PgfReasoner* rs, 
                            PgfEvalAccum* accum, GuString val);
                            
PGF_INTERNAL_DECL void
pgf_evaluate_accum_init_flt(PgfReasoner* rs, 
                            PgfEvalAccum* accum, float val);

PGF_INTERNAL_DECL void
pgf_evaluate_accum_add(PgfReasoner* rs, 
                       PgfEvalAccum* accum, PgfClosure* closure);

PGF_INTERNAL_DECL PgfClosure*
pgf_evaluate_accum_done(PgfReasoner* rs, PgfEvalAccum* accum);

#endif