File: comfunc.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (550 lines) | stat: -rw-r--r-- 14,920 bytes parent folder | download
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
 * Copyright (c) 2000 IET Inc.
 * Copyright (c) 1994-1999 Vectaport Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, 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 names of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 */

#include <ComTerp/comfunc.h>
#include <ComTerp/comterp.h>
#include <ComTerp/comvalue.h>
#include <ComUtil/comutil.h>
#include <Attribute/attrlist.h>
#include <string.h>

#define TITLE "ComFunc"

#ifdef LEAKCHECK
extern LeakChecker AttributeValuechecker;
#endif

/*****************************************************************************/

int ComFunc::_symid = -1;

ComFunc::ComFunc(ComTerp* comterp) {
    _comterp = comterp;
    _context = nil;
    _docstring2 = nil;
}

void ComFunc::reset_stack() {
  if (!post_eval()) {
    int count = nargs() + nkeys() - npops();
    #if 0 // now done immediately with stack_pop
    for (int i=1; i<=npops(); i++) {
      ((AttributeValue)_comterp->stack_top(i)).AttributeValue::~AttributeValue();
        #ifdef LEAKCHECK
	AttributeValuechecker.create();
        #endif
    }
    #endif

    _comterp->decr_stack(count);
  } else 
    _comterp->decr_stack(1);
  _comterp->_just_reset = true;
  npops() = 0;
}


ComValue& ComFunc::stack_arg(int n, boolean symbol, ComValue& dflt) {
    if (post_eval()) return stack_arg_post(n, symbol, dflt);

    int count = nargs() + nkeys() - npops();

    for (int i=0; i<count; i++) {
        ComValue& argref = _comterp->stack_top(i-count+1);
        if( argref.type() == ComValue::KeywordType) 
	    return dflt;
        if (i == n) {
  	    if (i+1 < count) {
	      ComValue& keyref = _comterp->stack_top(i-count+2);
	      if (keyref.is_type(ComValue::KeywordType) &&
		  keyref.keynarg_val())
		return dflt;
	    }
	    if (!symbol) 
	        argref = _comterp->lookup_symval(argref);
	    return argref;
	}
    }
    return dflt;
}

ComValue& ComFunc::stack_key(int id, boolean symbol, ComValue& dflt, boolean use_dflt_for_no_key) {
  if (post_eval()) 
    return stack_key_post(id, symbol, dflt, use_dflt_for_no_key);

  int count = nargs() + nkeys() - npops();
  for (int i=0; i<count; i++) {
    ComValue& keyref = _comterp->stack_top(-i);
    if( keyref.type() == ComValue::KeywordType) {
	    if (keyref.symbol_val() == id) {
	      if (i+1==count || keyref.keynarg_val() == 0) {
		if (use_dflt_for_no_key) 
		  return ComValue::trueval();
		else
		  return dflt;
	      } else {
		ComValue& valref = _comterp->stack_top(-i-1);
		if (valref.type() == ComValue::KeywordType) {
		  if (use_dflt_for_no_key) 
		    return ComValue::trueval();
		  else
		    return dflt;
		} else {
		  if (!symbol)
		    valref = _comterp->lookup_symval(valref);
		  return valref;
		}
	      }
	    }
    }
  }
  return use_dflt_for_no_key ? dflt : ComValue::nullval();
}

ComValue& ComFunc::stack_dotname(int n) {
    return _comterp->stack_top(n+1+npops());
}

ComValue ComFunc::stack_arg_post_eval(int n, boolean symbol, ComValue& dflt) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int argcnt;
  for (int i=0; i<nkeys(); i++) {
    argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
  }

  if (n>=nargsfixed()) return dflt;  

  for (int j=nargsfixed(); j>n; j--) {
    argcnt = 0;
    skip_arg_in_expr(offtop, argcnt);
  }

  comterp()->post_eval_expr(argcnt, offtop, pedepth()+1 
#ifdef POSTEVAL_EXPERIMENT
			    , symbol
#endif
			    );

  return comterp()->pop_stack(!symbol);
}

void ComFunc::print_stack_arg_post_eval(int n) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int argcnt;
  for (int i=0; i<nkeys(); i++) {
    argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
  }

  if (n>=nargsfixed()) return;

  for (int j=nargsfixed(); j>n; j--) {
    argcnt = 0;
    skip_arg_in_expr(offtop, argcnt);
  }

  comterp()->print_post_eval_expr(argcnt, offtop, pedepth()+1);
  return;
}

postfix_token* ComFunc::copy_stack_arg_post_eval(int n, int& ntoks) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int argcnt;
  for (int i=0; i<nkeys(); i++) {
    argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
  }

  if (n>=nargsfixed()) return NULL;

  for (int j=nargsfixed(); j>n; j--) {
    argcnt = 0;
    skip_arg_in_expr(offtop, argcnt);
  }

  postfix_token* tokbuf = comterp()->copy_post_eval_expr(argcnt, offtop);
  ntoks = argcnt;
  return tokbuf;
}

ComValue** ComFunc::stack_arg_post_eval_nargsfixed(boolean symbol, ComValue& dflt) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int argcnt;
  for (int i=0; i<nkeys(); i++) {
    argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
  }

  int* offtopbuf = new int[nargsfixed()];
  int* argcntbuf = new int[nargsfixed()];
  for (int j=nargsfixed(); j>0; j--) {
    argcnt = 0;
    skip_arg_in_expr(offtop, argcnt);
    offtopbuf[j-1] = offtop;
    argcntbuf[j-1] = argcnt;
  }

  ComValue** vals = new ComValue*[nargsfixed()];

  for(int i=0; i<nargsfixed(); i++) {
      comterp()->post_eval_expr(argcntbuf[i], offtopbuf[i], pedepth()+1 
#ifdef POSTEVAL_EXPERIMENT
                                , symbol
#endif
          );
      vals[i] = new ComValue(comterp()->pop_stack(!symbol));
  }

  delete [] offtopbuf;
  delete [] argcntbuf;
  return vals;
}

ComValue ComFunc::stack_key_post_eval
(int id, boolean symbol, ComValue& dflt, boolean use_dflt_for_no_key) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int count = 0;
  while (count < nkeys()) {
    ComValue& curr = comterp()->expr_top(offtop);
    if (!curr.is_type(ComValue::KeywordType))
      return use_dflt_for_no_key ? dflt : ComValue::nullval();
    count++;
    int argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
    if (curr.symbol_val() == id) {
      if (argcnt) {
	comterp()->post_eval_expr(argcnt, offtop, pedepth()+1);
	return comterp()->pop_stack(!symbol);
      } else
	return use_dflt_for_no_key ? dflt : ComValue::trueval();
    } 
  }
  return use_dflt_for_no_key ? dflt : ComValue::nullval();
}

ComValue& ComFunc::stack_arg_post(int n, boolean symbol, ComValue& dflt) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int argcnt;
  for (int i=0; i<nkeys(); i++) {
    argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
  }

  if (n>=nargsfixed()) return dflt;  

  for (int j=nargsfixed(); j>n; j--) {
    argcnt = 0;
    skip_arg_in_expr(offtop, argcnt);
  }

  int loc = comterp()->_pfnum + offtop + argcnt-1;
  if (loc<0) {fprintf(stderr, "unexpected negative index for _pfcomvals\n"); return ComValue::nullval();}
  else return comterp()->_pfcomvals[loc];
}

ComValue& ComFunc::stack_key_post
(int id, boolean symbol, ComValue& dflt, boolean use_dflt_for_no_key) {
  ComValue argoff(comterp()->stack_top());
  int offtop = argoff.int_val()-comterp()->_pfnum;
  int count = 0;
  while (count < nkeys()) {
    ComValue& curr = comterp()->expr_top(offtop);
    if (!curr.is_type(ComValue::KeywordType))
      return use_dflt_for_no_key ? dflt : ComValue::nullval();
    count++;
    int argcnt = 0;
    skip_key_in_expr(offtop, argcnt);
    if (curr.symbol_val() == id) {
      if (argcnt) {
	int loc = comterp()->_pfnum + offtop + argcnt-1;
	return comterp()->_pfcomvals[loc];
      } else
	return use_dflt_for_no_key ? dflt : ComValue::trueval();
    } 
  }
  return use_dflt_for_no_key ? dflt : ComValue::nullval();
}

boolean ComFunc::skip_key_on_stack(int& stackptr, int& argcnt) {
  return comterp()->skip_key(&comterp()->stack_top(), stackptr, 
			     -comterp()->stack_height(), argcnt);
}

boolean ComFunc::skip_arg_on_stack(int& stackptr, int& argcnt) {
  return comterp()->skip_arg(&comterp()->stack_top(), stackptr, 
			     -comterp()->stack_height(), argcnt);
}

boolean ComFunc::skip_key_in_expr(int& offtop, int& argcnt) {
  return comterp()->skip_key(&comterp()->_pfcomvals[comterp()->_pfnum-1], 
			     offtop, -comterp()->_pfnum, argcnt);
}

boolean ComFunc::skip_arg_in_expr(int& offtop, int& argcnt) {
  return comterp()->skip_arg(&comterp()->_pfcomvals[comterp()->_pfnum-1], 
			     offtop, -comterp()->_pfnum, argcnt);
}

ComValue ComFunc::pop_stack() {

    /* get rid of keywords -- use stack_key and stack_arg to get those */
    if (!npops() && nkeys()) {
        int count = nargs() + nkeys();
	int nkey = nkeys();
        for (int i=0; i<count; i++) {
	    ComValue val(_comterp->pop_stack());
	    npops()++;
	    if (val.type() == ComValue::KeywordType) nkey--;
	    if (nkey==0) break;    
	}
    }

    if (npops()<nargs()+nkeys()) {
        npops()++;
	return _comterp->pop_stack();
    } else 
        return ComValue::nullval();
}

ComValue ComFunc::pop_symbol() {
    /* get rid of keywords -- use stack_key and stack_arg to get those */
    if (!npops() && nkeys()) {
        int count = nargs() + nkeys();
	int nkey = nkeys();
        for (int i=0; i<count; i++) {
	    ComValue val = _comterp->pop_stack();
	    npops()++;
	    if (val.type() == ComValue::KeywordType) nkey--;
	    if (nkey==0) break;    
	}
    }

    if (npops()<nargs()+nkeys()) {
        npops()++;
	return _comterp->pop_symbol();
    } else 
        return ComValue::nullval();
}

void ComFunc::push_stack(ComValue& val) {
    _comterp->push_stack(val);
}

void ComFunc::push_stack(AttributeValue& val) {
    _comterp->push_stack(val);
}

ComValue& ComFunc::lookup_symval(ComValue& sym) {
    ComValue& retval = _comterp->lookup_symval(sym);
    return retval;
}

void ComFunc::assign_symval(int id, ComValue* sym) {
    _comterp->localtable()->insert(id, sym);
    return;
}

int ComFunc::bintest(const char* command) {
  char combuf[BUFSIZ];
#if 0
  sprintf( combuf, "echo-n $PATH; which %s", command );
#else
  sprintf( combuf, "which %s 2> /dev/null", command );
#endif
  FILE* fptr = popen(combuf, "r");
  char testbuf[BUFSIZ];	
  fgets(testbuf, BUFSIZ, fptr);  
  pclose(fptr);
  if (strncmp(testbuf+strlen(testbuf)-strlen(command)-1, 
	      command, strlen(command)) != 0) {
    return -1;
  }
  return 0;
}

boolean ComFunc::bincheck(const char* command) {
  int status = bintest(command);
  return !status;
}

ComFuncState* ComFunc::funcstate() {
  return _comterp->top_funcstate();
}

void ComFunc::push_funcstate(int nargs, int nkeys, int pedepth,
			     int command_symid, unsigned linenum) {
  ComFuncState cfs(nargs, nkeys, pedepth, 
		   command_symid==0 ? classid() : command_symid, linenum );
  _comterp->push_funcstate(cfs);
}

void ComFunc::pop_funcstate() {
  _comterp->pop_funcstate();
}

void ComFunc::exec(int nargs, int nkeys, int pedepth,
		   int command_symid) {
  push_funcstate(nargs, nkeys, pedepth, command_symid, 0);
  execute();
  pop_funcstate();
}

int& ComFunc::nargs() {
  return _comterp->top_funcstate()->nargs();
}

int& ComFunc::nkeys() {
  return _comterp->top_funcstate()->nkeys();
}

int& ComFunc::npops() {
  return _comterp->top_funcstate()->npops();
}

int ComFunc::nargspost() {
  ComFuncState* funcstate = _comterp->top_funcstate();
  if (funcstate->nargspost()>=0) 
    return funcstate->nargspost();

  int nargs = funcstate->nargs();
  int nkeys = funcstate->nkeys();
  int topptr = 0;
  if (post_eval()) {
    ComValue argoff(comterp()->stack_top());
    topptr = argoff.int_val()-comterp()->_pfnum;
  }
  int argcnt=0;
  while (nargs>0 || nkeys>0) {
    ComValue& val = comterp()->expr_top(topptr);
    int cnt = 0;
    if (val.is_type(ComValue::KeywordType)) {
      argcnt++;
      skip_key_in_expr(topptr, cnt);
      argcnt += cnt;
      nargs -= cnt ? 1 : 0;
      nkeys--;
    } else {
      skip_arg_in_expr(topptr, cnt);
      argcnt += cnt;
      nargs--;
    }
  }
  return argcnt;
}

int ComFunc::nargskey() {
  ComFuncState* funcstate = _comterp->top_funcstate();
  if (funcstate->nargskey()>=0) 
    return funcstate->nargskey();

  int nkeys = funcstate->nkeys();
  int topptr = 0;
  if (post_eval()) {
    ComValue argoff(comterp()->stack_top());
    topptr = argoff.int_val()-comterp()->_pfnum;
  }
  int nkeyargs = 0;
  while (nkeys>0) {
    int argcnt = 0;
    if (!post_eval()) { 
      skip_key_on_stack(topptr, argcnt);
    } else {
      skip_key_in_expr(topptr, argcnt);
    }
    nkeyargs += argcnt ? 1 : 0;
    nkeys--;
  }
  funcstate->nargskey() = nkeyargs;
  return nkeyargs;
}

int& ComFunc::pedepth() {
  return _comterp->top_funcstate()->pedepth();
}

AttributeList* ComFunc::stack_keys(boolean symbol, AttributeValue& dflt) {
  AttributeList* al = new AttributeList();
  int count = nargs() + nkeys() - npops();
  for (int i=0; i<count; i++) {
    ComValue& keyref = _comterp->stack_top(-i);
    if( keyref.type() == ComValue::KeywordType) {
      int key_symid = keyref.symbol_val();
      if (i+1==count || keyref.keynarg_val() == 0) 
	al->add_attr(key_symid, dflt);
      else {
	ComValue& valref = _comterp->stack_top(-i-1);
	if (valref.type() == ComValue::KeywordType) 
	  al->add_attr(key_symid, dflt);
	else {
	  if (!symbol) 
	    valref = _comterp->lookup_symval(valref);
	  al->add_attr(key_symid, valref);
	}
      }
    }
  }
  return al;
}

ComTerpServ* ComFunc::comterpserv() { 
  return _comterp && _comterp->is_serv() ? (ComTerpServ*)_comterp : nil; 
}

ostream& operator<< (ostream& out, const ComFunc& cf) {
    out << symbol_pntr(cf.funcid());
    return out;
}

/*****************************************************************************/

ComFuncState::ComFuncState(int narg, int nkey, int pedepth, 
			   int command_symid, unsigned linenum) {
  _nargs = narg;
  _nkeys = nkey;
  _npops = 0;
  _nargskey = -1;
  _nargspost = -1;
  _pedepth = pedepth;
  _command_symid = command_symid;
  _linenum=linenum;
}

ComFuncState::ComFuncState(const ComFuncState& cfs) {
  *this = cfs;
}

ostream& operator<< (ostream& out, const ComFuncState& cf) {
    out << "nargs = " << cf._nargs;
    out << ";nkeys = " << cf._nkeys;
    return out;
}