File: eval.c

package info (click to toggle)
nawm 0.0.20030130-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 360 kB
  • ctags: 496
  • sloc: ansic: 3,158; sh: 1,582; yacc: 408; lex: 406; makefile: 153
file content (385 lines) | stat: -rw-r--r-- 8,254 bytes parent folder | download | duplicates (3)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* eval.c: evaluates nawmrc code */

/* Copyright (C) 1999 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nawm.h"
#include "lang.h"
#include "parser.h"

nawmval run_fun(node *cmd);
int eval_cmd(node *cmd);
nawmval eval_expr(node *expr);
int eval_comp(int op, dtype type, nawmval left, nawmval right);
nawmval assignval(node *lval, nawmval val);

extern int quit, screenwidth, screenheight;
extern Window root;
extern Display *dpy;

nawmval returnval;

typedef struct _runscope {
  nawmval *slots;
  struct _runscope *parent;
} runscope;

runscope *scopestack;

void initrunscopes(void)
{
  scopestack = NULL;
}

int eval_cmds(node *cmd)
{
  int val;

  while (cmd)
    {
      val = eval_cmd(cmd);
      if (val)
	return val;
      cmd = cmd->next;
    }
  return 0;
}

int eval_cmd(node *cmd)
{
  int ans = 0;

  if (!cmd)
    return 0;

  new_generation();

  switch (cmd->type)
    {
    case RETURN:
      returnval = eval_expr(cmd->vals[0]);
      /* fall through */

    case BREAK:
    case CONTINUE:
      ans = cmd->type;
      break;

    case IF:
      if (eval_expr(cmd->vals[0]))
	ans = eval_cmd(cmd->vals[1]);
      else
	ans = eval_cmd(cmd->vals[2]);
      break;

    case FOR:
      if (cmd->vals[0]->type == FOR)
	{
	  node *fordata = cmd->vals[0];

	  eval_expr(fordata->vals[0]);
	  while (eval_expr(fordata->vals[1]))
	    {
	      if (eval_cmd(cmd->vals[1]) == BREAK)
		break;
	      eval_expr(fordata->vals[2]);
	    }
	}
      else
	{
	  arrayiter ai;
	  array *arr = (array *)eval_expr(cmd->vals[0]->vals[1]);
	  nawmval val;

	  val = assignval(cmd->vals[0]->vals[0], array_first(arr, &ai));
	  while (val)
	    {
	      if (eval_cmd(cmd->vals[1]) == BREAK)
		break;
	      val = assignval(cmd->vals[0]->vals[0], array_next(arr, &ai));
	    }
	}
      break;

    case WHILE:
      while (eval_expr(cmd->vals[0]))
	{
	  if (eval_cmd(cmd->vals[1]) == BREAK)
	    break;
	}
      break;

    case DO:
      do
	{
	  if (eval_cmd(cmd->vals[1]) == BREAK)
	    break;
	}
      while (eval_expr(cmd->vals[0]));
      break;

    case DEL:
      array_delete((array *)eval_expr(cmd->vals[0]), eval_expr(cmd->vals[1]));
      break;

    case CMD:
      run_fun(cmd);
      break;

    case BODY:
      ans = eval_cmds(cmd->vals[0]);
      break;

    default:
      eval_expr(cmd);
      break;
    }

  end_generation();
  return ans;
}

nawmval eval_expr(node *expr)
{
  switch (expr->type)
    {
    case NUM:
    case STR:
      return (nawmval)expr->vals[0];

    case VAR:
      {
	variable *var = (variable *)expr->vals[0];

	if (var->slot == -1)
	  return var->data;
	else
	  return scopestack->slots[var->slot];
      }

    case SUBSCRIPT:
      return array_lookup((array *)eval_expr(expr->vals[0]),
			  eval_expr(expr->vals[1]));

    case ELEMENT:
      return array_size((array *)eval_expr(expr->vals[0]));

    case ASSIGN:
      return assignval(expr->vals[0], eval_expr(expr->vals[1]));

    case PLUS:
      return eval_expr(expr->vals[0]) + eval_expr(expr->vals[1]);
    case NOP:
      return eval_expr(expr->vals[0]);
    case MINUS:
      return eval_expr(expr->vals[0]) - eval_expr(expr->vals[1]);
    case NEGATE:
      return -eval_expr(expr->vals[0]);
    case TIMES:
      return eval_expr(expr->vals[0]) * eval_expr(expr->vals[1]);
    case DIVIDE:
      return eval_expr(expr->vals[0]) / eval_expr(expr->vals[1]);
    case REMAINDER:
      return eval_expr(expr->vals[0]) % eval_expr(expr->vals[1]);
    case AND:
      return eval_expr(expr->vals[0]) && eval_expr(expr->vals[1]);
    case OR:
      return eval_expr(expr->vals[0]) || eval_expr(expr->vals[1]);
    case NOT:
      return !eval_expr(expr->vals[0]);

    case LESS:
    case LESSEQUAL:
    case GREATER:
    case GREATEREQUAL:
    case EQUAL:
    case NOTEQUAL:
      return eval_comp(expr->type, (dtype)expr->vals[2],
		       eval_expr(expr->vals[0]), eval_expr(expr->vals[1]));

    case CONCAT:
      {
	char *left = (char *)eval_expr(expr->vals[0]);
	char *right = (char *)eval_expr(expr->vals[1]);
	char *ans = gcmalloc(strlen(left) + strlen(right) + 1, free);
	sprintf(ans, "%s%s", left, right);
	return (nawmval)ans;
      }

    case IN:
      return array_contains((array *)eval_expr(expr->vals[1]),
			    eval_expr(expr->vals[0]));

    case FUN:
      return run_fun(expr);

    default:
      die("unexpected expression type");
    }
}

int eval_comp(int op, dtype type, nawmval left, nawmval right)
{
  if (type == T_WIN && (left || right))
    {
      Window rootret, parent, *children, cw;
      unsigned int numchildren, n, l = 0, r = 0;

      XQueryTree(dpy, root, &rootret, &parent, &children, &numchildren);
      for (n = 0; n < numchildren; n++)
	{
	  cw = client_window(children[n]);
	  if (cw == (Window)left)
	    l = (nawmval)n;
	  if (cw == (Window)right)
	    r = (nawmval)n;
	}
      XFree(children);
      left = l;
      right = r;
      /* fall through */
    }

  if (type == T_INT || type == T_WIN)
    {
      switch (op)
	{
	case LESS:
	  return left < right;
	case LESSEQUAL:
	  return left <= right;
	case GREATER:
	  return left > right;
	case GREATEREQUAL:
	  return left >= right;
	case EQUAL:
	  return left == right;
	case NOTEQUAL:
	  return left != right;
	}
    }
  else
    {
      switch (op)
	{
	case LESS:
	  return strcmp((char *)left, (char *)right) == -1;
	  break;
	case LESSEQUAL:
	  return strcmp((char *)left, (char *)right) != 1;
	  break;
	case GREATER:
	  return strcmp((char *)left, (char *)right) == 1;
	  break; 
	case GREATEREQUAL: 
	  return strcmp((char *)left, (char *)right) != -1;
	  break;
	case EQUAL:
	  return strcmp((char *)left, (char *)right) == 0;
	  break;
	case NOTEQUAL:
	  return strcmp((char *)left, (char *)right) != 0;
	  break;
	}
    }
  /* can't happen */
  return 0;
}

nawmval run_fun(node *n)
{
  function *fun = (function *)(n->vals[0]);
  nawmval ans = 0; /* Initialize to make purify happy */

  if (fun->numvars != -1)
    {
      runscope *scope;
      int i;
      node *expr;

      scope = xmalloc(sizeof(runscope));
      scope->slots = xmalloc(fun->numvars * sizeof(nawmval));
      memset(scope->slots, 0, fun->numvars * sizeof(nawmval));

      /* assign variables */
      for (i = 0, expr = n->vals[2]; i < fun->numargs; i++, expr = expr->next)
	scope->slots[i] = eval_expr(expr);
      for (; i < fun->numvars; i++)
	scope->slots[i] = initial_value(fun->vartype[i]);

      scope->parent = scopestack;
      scopestack = scope;
      returnval = 0;
      eval_cmds((node *)fun->body);
      ans = returnval;
      returnval = 0;

      scopestack = scope->parent;
      free(scope->slots);
      free(scope);
    }
  else
    {
      nawmval *argv;
      node *arg;
      int i, nargs;

      nargs = (nawmval)n->vals[1];
      argv = xmalloc(nargs * sizeof(nawmval));
      for (i = 0, arg = n->vals[2]; i < nargs; i++, arg = arg->next)
	argv[i] = eval_expr(arg);

      fun->body(nargs, argv, &ans);
      free(argv);
    }

  return ans;
}

nawmval assignval(node *lval, nawmval val)
{
  if (lval->type == VAR)
    {
      variable *var = (variable *)lval->vals[0];
      assign_var(var, val);
    }
  else
    {
      array *arr = (array *)eval_expr(lval->vals[0]);
      array_insert(arr, eval_expr(lval->vals[1]), val);
    }

  return val;
}

void assign_var(variable *var, nawmval val)
{
  nawmval *loc;

  if (var->slot == -1)
    loc = &(var->data);
  else
    loc = &(scopestack->slots[var->slot]);

  if (!is_atomic_type(var->type))
    {
      unref((void *)*loc);
      ref((void *)val);
    }
  *loc = val;
}