-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.h
More file actions
265 lines (208 loc) · 7.52 KB
/
Copy pathProgram.h
File metadata and controls
265 lines (208 loc) · 7.52 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
////////////////////////////////////////////////////////////////////////////
//
// ccli : The Computation and Control Language Tools
// -------------------------------------------------
//
// Copyright (c) 2003 Eric Klavins, California Institute of Technology
// For more information, email klavins@caltech.edu
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
#ifndef _PROGRAM_HH
#define _PROGRAM_HH
#include <list>
#if defined(WIN) || defined(linux)
#include <string.h>
#endif
#ifdef LINUX
#include <string.h>
#endif
#include "SymbolTable.h"
#include "Expr.h"
#define NEW_SYMTAB_SIZE 50
#define NEW_PARAM_SYMTAB_SIZE 5
class Command {
public:
Command ( Expr * l, Expr * r );
~Command ( void );
void apply ( Scope * s );
void debug_apply ( Scope * s );
void print ( void );
inline Expr * get_lhs ( void ) { return lhs; }
inline Expr * get_rhs ( void ) { return rhs; }
Command * copy ( void );
Command * reproduce ( void );
Command * subst ( Expr * e, char * x ); // C[e/x]
bool is_assigned ( const char * var );
private:
Expr * lhs;
Expr * rhs;
};
class Clause {
public:
Clause ( Expr * g );
Clause ( Expr * g, std::list<Command *> * coms );
~Clause ( void );
void addCommand ( Command * c );
void execute ( Scope * s );
void debug_execute ( Scope * s );
void print ( void );
void print_as_init ( void );
Clause * copy ( void );
Clause * reproduce ( void );
Clause * subst ( Expr * e, char * x ); // C[e/x]
bool is_assigned ( const char * var );
inline Expr * get_guard ( void ) { return guard; }
inline std::list<Command *> * get_commands ( void ) { return commands; }
static void get_debug_command ( Scope * s );
private:
Expr * guard;
std::list<Command *> * commands;
std::list<Command *>::iterator i;
};
typedef std::pair < Clause *, std::list<SymbolTable *> * > CLAUSE_LIST_EL;
typedef std::list<CLAUSE_LIST_EL *> CLAUSE_LIST;
class Program {
public:
Program ( char * file, int line );
Program ( bool alloc, char * file, int line );
virtual ~Program ( void );
inline void set_symtab ( SymbolTable * s ) {
symtab = s;
}
inline SymbolTable * get_symtab ( void ) { return symtab; }
inline SymbolTable * get_param_symtab ( void ) { return param_symtab; }
void set_args ( std::list<char *> * a );
inline std::list<char *> * get_args ( void ) { return args; }
void set_insts_simp ( std::list<Expr *> * in );
void set_insts ( std::list<Expr *> * in, Scope * s, Environment * env, char * name );
inline std::list<Expr *> * get_insts ( void ) { return insts; }
inline void set_parent ( Program * p ) { parent = p; }
inline void set_non_generics ( std::list<TypeExpr *> * ng ) {
if ( non_generics != NULL ) {
// std::list<TypeExpr *>::iterator w;
//for ( w=non_generics->begin(); w != non_generics->end(); w++ ) {
// printf ( "%x::set_non_generics: deleting %x\n", this, *w );
// delete (*w);
//}
delete non_generics;
}
non_generics = ng;
}
inline std::list<TypeExpr *> * get_non_generics ( void ) { return non_generics; }
inline void set_locals ( Environment * env ) { locals = env; }
inline void set_needs ( std::list<char *> * L ) { FREE_STR_LIST ( needs ); needs = L; }
inline std::list<char *> * get_needs ( void ) { return needs; }
inline void add_needs ( const char * v ) { needs->push_back ( strdup ( v ) ); }
inline void set_arg_types ( Environment * env ) { arg_types = env; }
inline Environment * get_locals ( void ) { return locals; }
inline Environment * get_arg_types ( void ) { return arg_types; }
void copy_param_symtab ( SymbolTable * ps );
void print_args ( void );
void small_print_base ( void );
virtual void print ( void ) {}
virtual void small_print ( void ) {}
void set_base ( SymbolTable * s, std::list<char *> * alist, std::list<Expr *> * elist );
virtual Program * copy ( void ) { return NULL; }
void add_symbol ( const char * str, Value * v );
void init_base ( Scope * );
virtual void init ( Scope * ) {}
virtual void init_params ( Scope * ) {}
virtual void step ( Scope * ) {}
virtual void random_step ( Scope *, float ) {}
virtual void compute_arg_types ( Environment * ) {}
virtual void make_clause_list ( CLAUSE_LIST *, Scope * ) {}
void check_needs ( void );
inline char * get_filename ( void ) { return filename; }
inline char * get_name ( void ) { return name; }
inline void set_name ( const char * n ) {
free ( name );
name = strdup ( n );
}
inline int get_linenum ( void ) { return linenum; }
inline std::list<TypeExpr *> * get_garbage() { return garbage; }
static Program * Composition ( Program * p, std::list<Expr *> * exprlist,
char * binder, std::list<Value *> * vals, std::list<char *> * sharelist,
Scope * scope, Environment * env,
char * filename, int linenum );
private:
SymbolTable * symtab, * param_symtab;
std::list<char *> * args, * needs;
std::list<Expr *> * insts;
Program * parent;
int id;
std::list<TypeExpr *> * non_generics, * garbage;
Environment * locals;
Environment * arg_types;
char * filename;
int linenum;
char * name;
};
class AtomicProgram : public Program {
public:
AtomicProgram ( char * file, int line );
~AtomicProgram ( void );
void add_clause ( Clause * c );
inline void set_init ( Clause * c ) { init_clause = c; }
inline Clause * get_init ( void ) { return init_clause; }
inline std::list<Clause *> * get_clauses ( void ) { return clauses; }
void print ( void );
Program * copy ( void );
Program * reproduce ( void );
void small_print ( void );
void init ( Scope * s );
void init_params ( Scope * s );
void step ( Scope * s );
void random_step ( Scope * s, float p );
void compute_arg_types ( Environment * ) {}
void make_clause_list ( CLAUSE_LIST * L, Scope * s );
private:
std::list<Clause *> * clauses;
Clause * init_clause;
};
class InstProgram : public Program {
public:
InstProgram ( Program * c, char * file, int line );
~InstProgram ( void );
void print ( void );
Program * copy ( void );
Program * reproduce ( void );
void small_print ( void );
void init ( Scope * s );
void init_params ( Scope * s );
void step ( Scope * s );
void compute_arg_types ( Environment * env );
void make_clause_list ( CLAUSE_LIST * L, Scope * s );
private:
Program * child;
};
class CompositeProgram : public Program {
public:
CompositeProgram ( Program * l, Program * r, std::list<char *> * shared, char * file, int line );
~CompositeProgram ( void );
void print ( void );
Program * copy ( void );
Program * reproduce ( void );
void small_print ( void );
void init ( Scope * s );
void init_params ( Scope * s );
void step ( Scope * s );
void compute_arg_types ( Environment * env );
void make_clause_list ( CLAUSE_LIST * L, Scope * s );
private:
Program * left, * right;
};
#endif