summaryrefslogtreecommitdiff
path: root/zwgc/eval.c
blob: b33ea6d3a52717724267941fe8337168cbd63f3d (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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_eval_c[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

/****************************************************************************/
/*                                                                          */
/*                      Code to evaluate an expression:                     */
/*                                                                          */
/****************************************************************************/

#include <zephyr/zephyr.h>
#include "new_memory.h"
#include "node.h"
#include "eval.h"
#include "substitute.h"
#include "port.h"
#include "buffer.h"
#include "regexp.h"
#include "formatter.h"
#include "text_operations.h"
#include "zwgc.h"
#include "variables.h"

/****************************************************************************/
/*                                                                          */
/*                Code to deal with string/boolean conversion:              */
/*                                                                          */
/****************************************************************************/

/*
 *  Internal Routine:
 *
 *    int string_to_bool(string str)
 *       Effects: Returns true iff the string str represents true.
 *                True is represented by any string which is equal to
 *                "true" when case is disregraded.
 */

#define  string_to_bool(str)      (!strcasecmp(str,"true"))

/*
 *  Internal Routine:
 *
 *    string bool_to_string(int bool)
 *       Effects: Returns a string representive for the C boolean bool.
 *                (In C, true == non-zero)  I.e.,
 *                string_to_bool(bool_to_string(x)) == !!x.
 *                The string returned is on the heap & must be freed
 *                eventually.
 */

static string
bool_to_string(int bool)
{
    return(bool ? string_Copy("TRUE") : string_Copy("FALSE"));
}

/*
 *    int eval_bool_expr(Node *expr)
 *        Modifies: dict
 *        Requires: expr is a proper expression or NULL.  (see node.c)
 *        Effects: Evaluates expr to its boolean value which is returned.
 *                 NULL is defined to have the boolean value true.
 */

int
eval_bool_expr(Node *expr)
{
    string temp;
    int result;

    if (!expr)
      return(1);

    temp = eval_expr(expr);
    result = string_to_bool(temp);
    free(temp);

    return(result);
}

/****************************************************************************/
/*                                                                          */
/*                      Code to evaluate an expression:                     */
/*                                                                          */
/****************************************************************************/

/*
 *    string eval_expr(Node *expr)
 *        Modifies: dict
 *        Requires: expr is a proper expression (NOT NULL).  (see node.c)
 *        Effects: Evaluates expr to its string value which is returned.
 *                 The returned string is on the heap and must be freed
 *                 eventually.
 */

string
eval_expr(Node *expr)
{
    int opcode = expr->opcode;
    int bool_result = 0;
    string first, second;
    char *result = NULL;
    string *text_ptr;

    /*
     * Dispatch based on the opcode of the top node in the expression:
     */
    switch (opcode) {
      case STRING_CONSTANT_OPCODE:
	return(string_Copy(expr->d.string_constant));

      case VARREF_OPCODE:
	return(string_Copy(var_get_variable(expr->d.string_constant)));

      case BUFFER_OPCODE:
	return(string_Copy(buffer_to_string()));

	/*
	 * Handle unary expressions:
	 */
      case NOT_OPCODE:
      case SUBSTITUTE_OPCODE:
      case PROTECT_OPCODE:
      case VERBATIM_OPCODE:
      case STYLESTRIP_OPCODE:
      case GETENV_OPCODE:
      case UPCASE_OPCODE:
      case DOWNCASE_OPCODE:
      case ZVAR_OPCODE:
      case GET_OPCODE:
	first = eval_expr(expr->d.nodes.first);

	switch (opcode) {
	  case NOT_OPCODE:
	    result = bool_to_string(! string_to_bool(first));
	    break;

	  case SUBSTITUTE_OPCODE:
	    result = substitute(var_get_variable, first);
	    break;

	  case PROTECT_OPCODE:
	    result=protect(first);
	    break;

	  case VERBATIM_OPCODE:
	    return(verbatim(first,0));

	  case STYLESTRIP_OPCODE:
	    return(stylestrip(first));

	  case GETENV_OPCODE:
	    result = getenv(first);
	    if (!result)
	      result = string_Copy("");
	    else
	      result = string_Copy(result);
	    break;

	  case UPCASE_OPCODE:
	    return(string_Upcase(first));

	  case DOWNCASE_OPCODE:
	    return(string_Downcase(first));

	  case ZVAR_OPCODE:
	    result = ZGetVariable(first);
	    if (!result)
	      result = string_Copy("");
	    else
	      result = string_Copy(result);
	    break;

	  case GET_OPCODE:
	    result = read_from_port(first);
	    break;
	}
	free(first);
	break;

	/*
	 * Handle binary operators:
	 */
      case PLUS_OPCODE:
      case AND_OPCODE:
      case OR_OPCODE:
      case EQ_OPCODE:
      case NEQ_OPCODE:
      case REGEQ_OPCODE:
      case REGNEQ_OPCODE:
	first = eval_expr(expr->d.nodes.first);
	second = eval_expr(expr->d.nodes.second);

	switch (opcode) {
	  case PLUS_OPCODE:
	    result = string_Concat(first, second);
	    free(first);
	    free(second);
	    return(result);

	  case AND_OPCODE:
	    bool_result = string_to_bool(first) && string_to_bool(second);
	    break;

	  case OR_OPCODE:
	    bool_result = string_to_bool(first) || string_to_bool(second);
	    break;

	  case EQ_OPCODE:
	    bool_result = string_Eq(first, second);
	    break;

	  case NEQ_OPCODE:
	    bool_result = string_Neq(first, second);
	    break;

	  case REGEQ_OPCODE:
	    bool_result = ed_regexp_match_p(first, second);
	    break;

	  case REGNEQ_OPCODE:
	    bool_result = !ed_regexp_match_p(first, second);
	    break;
	}
	free(first);
	free(second);
	result = bool_to_string(bool_result);
	break;

	/*
	 * Handle text-manipulation operators:
	 */
      case LANY_OPCODE:    case RANY_OPCODE:  
      case LBREAK_OPCODE:  case RBREAK_OPCODE:
      case LSPAN_OPCODE:   case RSPAN_OPCODE:
	first = eval_expr(expr->d.nodes.first);
	second = eval_expr(expr->d.nodes.second);
	text_ptr = &first;

	switch (opcode) {
	  case LANY_OPCODE:
	    result = lany(text_ptr, second);
	    break;

	  case RANY_OPCODE:  
	    result = rany(text_ptr, second);
	    break;

	  case LBREAK_OPCODE:
	    result = lbreak(text_ptr, string_to_character_class(second));
	    break;

	  case RBREAK_OPCODE:
	    result = rbreak(text_ptr, string_to_character_class(second));
	    break;

	  case LSPAN_OPCODE:
	    result = lspan(text_ptr, string_to_character_class(second));
	    break;

	  case RSPAN_OPCODE:
	    result = rspan(text_ptr, string_to_character_class(second));
	    break;
	}

	if (expr->d.nodes.first->opcode == VARREF_OPCODE)
	  var_set_variable(expr->d.nodes.first->d.string_constant, first);
	free(first);
	free(second);
	break;

#ifdef DEBUG
      default:
	printf("zwgc: internal error: attempt to evaluate the following non-expression:\n");  fflush(stdout);
	node_display(expr);
	printf("\n\n");
	exit(2);
#endif
    }

    return(result);
}