[go: up one dir, main page]

File: expr.c

package info (click to toggle)
cook 2.19-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,316 kB
  • ctags: 3,969
  • sloc: ansic: 50,331; sh: 13,190; makefile: 4,542; yacc: 3,174; perl: 224; awk: 154
file content (286 lines) | stat: -rw-r--r-- 6,574 bytes parent folder | download | duplicates (4)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *	cook - file construction tool
 *	Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
 *	Peter Miller; All rights reserved.
 *
 *	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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate expression trees
 *
 * This file contains the functions for manipulating expression
 * trees; building, interpreting and freeing them.
 */

#include <ac/stddef.h>
#include <ac/stdarg.h>
#include <ac/stdio.h>

#include <builtin.h>
#include <cook.h>
#include <error_intl.h>
#include <expr.h>
#include <id.h>
#include <lex.h>
#include <match.h>
#include <mem.h>
#include <opcode/list.h>
#include <opcode/push.h>
#include <option.h>
#include <stmt.h>
#include <trace.h>
#include <str_list.h>


/*
 * NAME
 *	expr_alloc - allocate a pointer structure
 *
 * SYNOPSIS
 *	expr_ty *expr_alloc(void);
 *
 * DESCRIPTION
 *	The expr_alloc function is used to allocate an expression node
 *	structure in dynamic memory.  It will initially be filled with zeros.
 *	The e_op field is defined as being non-zero to allow detection
 *	of some classes of missuse of the expression nodes.
 *
 * RETURNS
 *	A pointer to a "expr_ty" in dynamic memory.
 *
 * CAVEAT
 *	The expression node is allocated in dynamic memory,
 *	it is the callers responsibility to ensure that it is freed
 *	when it is finished with, by a call to expr_delete().
 */

expr_ty *
expr_private_new(mp)
	expr_method_ty	*mp;
{
	expr_ty		*ep;

	trace(("expr_new()\n{\n"/*}*/));
	ep = mem_alloc(mp->size);
	ep->method = mp;
	ep->e_references = 1;
	expr_position_constructor
	(
		&ep->e_position,
		lex_cur_file(),
		lex_cur_line()
	);
	trace(("return %08lX;\n", ep));
	trace((/*{*/"}\n"));
	return ep;
}


/*
 * NAME
 *	expr_copy - copy and expression
 *
 * SYNOPSIS
 *	expr_ty *expr_copy(expr_ty *);
 *
 * DESCRIPTION
 *	The expr_copy function is used to make a copy of an expression tree.
 *
 * RETURNS
 *	The expr_copy function returns a pointer to the root of the copied
 *	expression tree.
 *
 * CAVEAT
 *	The result is in dynamic memory, used expr_delete to dispose of it when
 *	finished with.
 */

expr_ty *
expr_copy(ep)
	expr_ty		*ep;
{
	trace(("expr_copy(ep = %08X)\n{\n"/*}*/, ep));
	ep->e_references++;
	trace(("return %08lX;\n", ep));
	trace((/*{*/"}\n"));
	return ep;
}


/*
 * NAME
 *	expr_delete - free expression tree
 *
 * SYNOPSIS
 *	void expr_delete(expr_ty *ep);
 *
 * DESCRIPTION
 *	The expr_delete function is used to free expression trees.
 *
 * CAVEAT
 *	It is assumed that the expression trees are all
 *	dynamically allocated.  Use expr_alloc() to allocate them.
 */

void
expr_delete(ep)
	expr_ty		*ep;
{
	trace(("expr_delete(ep = %08X)\n{\n"/*}*/, ep));
	assert(ep);
	ep->e_references--;
	if (ep->e_references <= 0)
	{
		if (ep->method->destructor)
			ep->method->destructor(ep);
		expr_position_destructor(&ep->e_position);
		mem_free(ep);
	}
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *      expr_evaluate - evaluate an expression
 *
 * SYNOPSIS
 *      string_list_ty *expr_evaluate(expr_ty *ep);
 *
 * DESCRIPTION
 *      The expr_evaluate function is used to evaluate an expression.
 *
 * RETURNS
 *      string_list_ty *; or the NULL pointer on error
 *
 * CAVEAT
 *      The result returned from this function are allocated in dynamic memory.
 *      It is the responsibility of the caller to ensure that they are freed
 *      when they are finished with, using string_list_destructor().
 */

string_list_ty *
expr_evaluate(ep, mp)
	const expr_ty   *ep;
	const match_ty	*mp;
{
	opcode_list_ty	*olp;
	string_list_ty	*result;

	trace(("expr_evaluate(ep = %08lX)\n{\n"/*}*/, (long)ep));
	assert(ep);
	olp = opcode_list_new();
	opcode_list_append(olp, opcode_push_new());
	expr_code_generate(ep, olp);
	result = opcode_list_run(olp, mp);
	opcode_list_delete(olp);
	trace(("return %08lX;\n", result));
	trace((/*{*/"}\n"));
	return result;
}


/*
 * NAME
 *      expr_eval_condition - evaluate condition
 *
 * SYNOPSIS
 *      int expr_eval_condition(expr_ty *);
 *
 * DESCRIPTION
 *      The expr_eval_condition function is used to evaluate an expression to
 *      yeild a true/false result.  The expression is evaluated into a word
 *      list.  A false result is if all of the resulting strings are empty or
 *      0, true otherwise.
 *
 * RETURNS
 *      The expr_eval_condition function returns 0 if the condition is false,
 *      and nonzero if it is true.  The value -1 is returned on error.
 *
 * CAVEAT
 *      The str_bool function is used to test the booean value of a string;
 *      changeing the behaviour of that function will change the behaviour of
 *      this one.
 */

int
expr_eval_condition(ep, mp)
	const expr_ty   *ep;
	const match_ty	*mp;
{
	opcode_list_ty	*olp;
	int		result;

	trace(("expr_eval_condition(ep = %08lX)\n{\n"/*}*/, (long)ep));
	assert(ep);
	olp = opcode_list_new();
	opcode_list_append(olp, opcode_push_new());
	expr_code_generate(ep, olp);
	result = opcode_list_run_bool(olp, mp);
	opcode_list_delete(olp);
	trace(("return %d;\n", result));
	trace((/*{*/"}\n"));
	return result;
}


/*
 * NAME
 *	expr_equal
 *
 * SYNOPSIS
 *	int expr_equal(expr_ty *, expr_ty *);
 *
 * DESCRIPTION
 *	The expr_equal function is used to test whether two expression
 *	trees are identical.
 */

int
expr_equal(e1, e2)
	const expr_ty	*e1;
	const expr_ty	*e2;
{
	assert(e1);
	assert(e1->method);
	assert(e2);
	assert(e2->method);
	assert(e1->method->equal);
	assert(e2->method->equal);
	return (e1->method == e1->method && e1->method->equal(e1, e2));
}


/*
 * NAME
 *	expr_code_generate
 *
 * SYNOPSIS
 *	void expr_code_generate(expr_ty *, opcode_list_ty *);
 *
 * DESCRIPTION
 *	The expr_code_generate function is used to generate the opcode
 *	stream for the given expression node.
 */

void
expr_code_generate(ep, olp)
	const expr_ty	*ep;
	struct opcode_list_ty *olp;
{
	assert(ep);
	assert(ep->method);
	assert(ep->method->code_generate);
	ep->method->code_generate(ep, olp);
}