Annotation of parser3/src/main/execute.C, revision 1.64
1.1 paf 1: /*
1.64 ! paf 2: $Id: execute.C,v 1.63 2001/03/07 13:54:48 paf Exp $
1.1 paf 3: */
4:
1.8 paf 5: #include "pa_array.h"
1.1 paf 6: #include "code.h"
1.11 paf 7: #include "pa_request.h"
1.15 paf 8: #include "pa_vstring.h"
1.22 paf 9: #include "pa_vhash.h"
1.23 paf 10: #include "pa_vunknown.h"
1.34 paf 11: #include "pa_vcframe.h"
12: #include "pa_vmframe.h"
1.38 paf 13: #include "pa_vobject.h"
1.49 paf 14: #include "pa_vdouble.h"
1.54 paf 15: #include "pa_vbool.h"
1.1 paf 16:
17: #include <stdio.h>
18:
1.24 paf 19: #define PUSH(value) stack.push(value)
20: #define POP() static_cast<Value *>(stack.pop())
1.32 paf 21: #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()
1.24 paf 22:
1.11 paf 23:
1.1 paf 24: char *opcode_name[]={
1.49 paf 25: // literals
1.52 paf 26: "VALUE", "CODE", "CLASS",
1.49 paf 27:
28: // actions
1.51 paf 29: "WITH_SELF", "WITH_ROOT", "WITH_READ", "WITH_WRITE",
1.1 paf 30: "CONSTRUCT",
1.18 paf 31: "WRITE",
32: "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.1 paf 33: "CREATE_EWPOOL", "REDUCE_EWPOOL",
34: "CREATE_RWPOOL", "REDUCE_RWPOOL",
1.49 paf 35: "CREATE_SWPOOL", "REDUCE_SWPOOL",
1.1 paf 36: "GET_METHOD_FRAME",
37: "STORE_PARAM",
1.51 paf 38: "CALL",
1.49 paf 39:
40: // expression ops: unary
41: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS",
42: // expression ops: binary
43: "SUB", "ADD", "MUL", "DIV", "MOD",
1.56 paf 44: "BIN_AND", "BIN_OR", "BIN_XOR",
45: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 46: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.55 paf 47: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE"
1.1 paf 48: };
49:
1.9 paf 50: void dump(int level, const Array& ops) {
1.23 paf 51: if(0){
52: int size=ops.size();
1.46 paf 53: //fprintf(stderr, "size=%d\n", size);
1.23 paf 54: for(int i=0; i<size; i++) {
55: Operation op;
56: op.cast=ops.quick_get(i);
1.46 paf 57: fprintf(stderr, "%8X\n", op.cast);
1.23 paf 58: }
59: }
60:
1.9 paf 61: int size=ops.size();
1.46 paf 62: //fprintf(stderr, "size=%d\n", size);
1.1 paf 63: for(int i=0; i<size; i++) {
1.23 paf 64: Operation op;
65: op.cast=ops.quick_get(i);
1.46 paf 66: fprintf(stderr, "%*s%s", level*4, "", opcode_name[op.code]);
1.1 paf 67:
1.52 paf 68: if(op.code==OP_VALUE) {
69: Value *value=static_cast<Value *>(ops.quick_get(++i));
70: fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());
1.15 paf 71: }
1.38 paf 72: if(op.code==OP_CLASS) {
73: VClass *vclass=static_cast<VClass *>(ops.quick_get(++i));
1.46 paf 74: fprintf(stderr, " \"%s\"", vclass->name().cstr());
1.38 paf 75: }
1.46 paf 76: fprintf(stderr, "\n");
1.1 paf 77:
1.32 paf 78: if(op.code==OP_CODE) {
1.10 paf 79: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.9 paf 80: dump(level+1, *local_ops);
1.1 paf 81: }
82: }
1.60 paf 83: fflush(stderr);
1.1 paf 84: }
85:
1.32 paf 86: void Request::execute(const Array& ops) {
1.30 paf 87: if(1) {
1.51 paf 88: fputs("source----------------------------\n", stderr);
1.12 paf 89: dump(0, ops);
1.51 paf 90: fputs("execution-------------------------\n", stderr);
1.12 paf 91: }
92:
1.11 paf 93: int size=ops.size();
1.46 paf 94: //fprintf(stderr, "size=%d\n", size);
1.11 paf 95: for(int i=0; i<size; i++) {
1.23 paf 96: Operation op;
97: op.cast=ops.quick_get(i);
1.63 paf 98: fprintf(stderr, "%d:%s", stack.top()+1, opcode_name[op.code]); fflush(stderr);
1.11 paf 99:
1.23 paf 100: switch(op.code) {
1.51 paf 101: // param in next instruction
1.52 paf 102: case OP_VALUE:
1.32 paf 103: {
1.52 paf 104: Value *value=static_cast<Value *>(ops.quick_get(++i));
105: fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());
106: PUSH(value);
1.32 paf 107: break;
108: }
109: case OP_CODE:
110: {
1.63 paf 111: VMethodFrame *frame=static_cast<VMethodFrame *>(stack[stack.top()]);
1.32 paf 112: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.46 paf 113: fprintf(stderr, " (%d)\n", local_ops->size());
1.38 paf 114: dump(1, *local_ops);
1.63 paf 115:
1.32 paf 116: Junction& j=*NEW Junction(pool(),
1.45 paf 117: *self, 0, 0,
1.64 ! paf 118: root, frame, frame, local_ops);
1.32 paf 119:
120: Value *value=NEW VJunction(j);
1.63 paf 121: value->set_name(frame->name());
1.32 paf 122: PUSH(value);
123: break;
124: }
1.38 paf 125: case OP_CLASS:
126: {
127: VClass *vclass=static_cast<VClass *>(ops.quick_get(++i));
1.46 paf 128: fprintf(stderr, " \"%s\"", vclass->name().cstr());
1.38 paf 129: PUSH(vclass);
130: break;
131: }
1.32 paf 132:
1.51 paf 133: // OP_WITH
1.37 paf 134: case OP_WITH_SELF:
135: {
136: PUSH(self);
137: break;
138: }
139: case OP_WITH_ROOT:
1.11 paf 140: {
1.37 paf 141: PUSH(root);
1.13 paf 142: break;
1.11 paf 143: }
1.15 paf 144: case OP_WITH_READ:
145: {
1.24 paf 146: PUSH(rcontext);
1.20 paf 147: break;
148: }
1.37 paf 149: case OP_WITH_WRITE:
1.20 paf 150: {
1.37 paf 151: PUSH(wcontext);
1.20 paf 152: break;
153: }
1.37 paf 154:
1.51 paf 155: // OTHER ACTIONS BUT WITHs
1.37 paf 156: case OP_CONSTRUCT:
1.20 paf 157: {
1.37 paf 158: Value *value=POP();
159: String& name=POP_NAME();
160: Value *ncontext=POP();
161: value->set_name(name);
162: ncontext->put_element(name, value);
1.15 paf 163: break;
164: }
1.18 paf 165: case OP_WRITE:
1.13 paf 166: {
1.24 paf 167: Value *value=POP();
1.14 paf 168: wcontext->write(value);
1.13 paf 169: break;
1.14 paf 170: }
1.13 paf 171:
1.15 paf 172: case OP_GET_ELEMENT:
1.11 paf 173: {
1.17 paf 174: Value *value=get_element();
1.24 paf 175: PUSH(value);
1.17 paf 176: break;
177: }
178:
1.18 paf 179: case OP_GET_ELEMENT__WRITE:
1.17 paf 180: {
181: Value *value=get_element();
182: wcontext->write(value);
183: break;
184: }
185:
1.32 paf 186:
1.17 paf 187: case OP_CREATE_EWPOOL:
188: {
1.24 paf 189: PUSH(wcontext);
1.43 paf 190: wcontext=NEW WWrapper(pool(), 0 /* empty */, true /* constructing */);
1.17 paf 191: break;
192: }
193: case OP_REDUCE_EWPOOL:
194: {
1.36 paf 195: Value *value=wcontext->result();
1.25 paf 196: wcontext=static_cast<WContext *>(POP());
1.24 paf 197: PUSH(value);
1.13 paf 198: break;
1.15 paf 199: }
1.13 paf 200:
1.26 paf 201: case OP_CREATE_RWPOOL:
202: {
203: Value *ncontext=POP();
204: PUSH(rcontext);
205: rcontext=ncontext;
206: PUSH(wcontext);
1.43 paf 207: wcontext=NEW WWrapper(pool(), ncontext, false /* not constructing */);
1.26 paf 208: break;
209: }
210: case OP_REDUCE_RWPOOL:
211: {
212: String *string=wcontext->get_string();
1.50 paf 213: Value *value=string?NEW VString(*string):NEW VString(pool());
1.26 paf 214: wcontext=static_cast<WContext *>(POP());
215: rcontext=POP();
216: PUSH(value);
217: break;
218: }
1.49 paf 219: case OP_CREATE_SWPOOL:
220: {
221: PUSH(wcontext);
1.50 paf 222: wcontext=NEW WWrapper(pool(), 0 /* empty */, false /* not constructing */);
1.49 paf 223: break;
224: }
225: case OP_REDUCE_SWPOOL:
226: {
227: // from "$a $b" part of expression taking only string value,
228: // ignoring any other content of wcontext
229: String *string=wcontext->get_string();
1.50 paf 230: Value *value=string?NEW VString(*string):NEW VString(pool());
231: wcontext=static_cast<WContext *>(POP());
1.49 paf 232: PUSH(value);
233: break;
234: }
235:
1.51 paf 236: // CALL
1.28 paf 237: case OP_GET_METHOD_FRAME:
238: {
1.32 paf 239: Value *value=POP();
1.28 paf 240: // [self/class?;params;local;code/native_code](name)
1.32 paf 241: Junction *junction=value->get_junction();
1.31 paf 242: if(!junction)
1.28 paf 243: THROW(0,0,
1.42 paf 244: &value->name(),
1.39 paf 245: "type is '%s', can not call it (must be method or junction)",
1.38 paf 246: value->type());
1.28 paf 247: //unless(method) method=operators.get_method[...;code/native_code](name)
1.34 paf 248: VMethodFrame *frame=NEW VMethodFrame(pool(), *junction);
1.63 paf 249: frame->set_name(junction->self.name());
1.28 paf 250: PUSH(frame);
251: break;
252: }
253: case OP_STORE_PARAM:
254: {
255: Value *value=POP();
1.34 paf 256: VMethodFrame *frame=static_cast<VMethodFrame *>(stack[0]);
1.28 paf 257: frame->store_param(value);
1.29 paf 258: break;
259: }
260:
261: case OP_CALL:
262: {
1.46 paf 263: fprintf(stderr, "->\n");
1.34 paf 264: VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
265: frame->fill_unspecified_params();
1.45 paf 266: PUSH(self);
267: PUSH(root);
268: PUSH(rcontext);
269: PUSH(wcontext);
270:
1.38 paf 271: VClass *called_class=frame->junction.self.get_class();
1.43 paf 272: // constructing?
273: if(wcontext->constructing()) { // yes
1.41 paf 274: // constructor call: $some(^class:method(..))
1.50 paf 275: frame->write(self=NEW VObject(pool(), *called_class));
1.43 paf 276: } else { // no
1.47 paf 277: // context is object or class & is it my class or my parent's class?
1.44 paf 278: VClass *read_class=rcontext->get_class();
279: if(read_class && read_class->is_or_derived_from(*called_class)) // yes
1.46 paf 280: self=rcontext; // class dynamic call
1.44 paf 281: else // no
282: self=&frame->junction.self; // static or simple dynamic call
1.38 paf 283: }
1.45 paf 284: frame->set_self(*self);
1.29 paf 285: root=rcontext=wcontext=frame;
1.47 paf 286: {
1.48 paf 287: // take object or class from any wrappers
288: VAliased *aliased=self->get_aliased();
289: // substitute class alias to the class they are called AS
1.47 paf 290: Temp_alias temp_alias(*aliased, *frame->junction.vclass);
291: execute(frame->junction.method->code);
292: }
1.36 paf 293: Value *value=wcontext->result();
1.45 paf 294:
295: wcontext=static_cast<WContext *>(POP());
296: rcontext=POP();
297: root=POP();
298: self=static_cast<VAliased *>(POP());
1.62 paf 299:
1.61 paf 300: PUSH(value);
1.46 paf 301: fprintf(stderr, "<-returned");
1.49 paf 302: break;
303: }
304:
1.55 paf 305: // expression ops: unary
306: case OP_NEG:
307: {
308: Value *operand=POP();
309: Value *value=NEW VDouble(pool(), -operand->get_double());
310: PUSH(value);
311: break;
312: }
313: case OP_INV:
314: {
315: Value *operand=POP();
1.61 paf 316: Value *value=NEW VDouble(pool(),
317: ~static_cast<int>(operand->get_double()));
1.55 paf 318: PUSH(value);
319: break;
320: }
321: case OP_NOT:
322: {
323: Value *operand=POP();
324: Value *value=NEW VBool(pool(), !operand->get_bool());
325: PUSH(value);
326: break;
327: }
1.62 paf 328: case OP_DEF:
329: {
330: Value *operand=POP();
331: Value *value=NEW VBool(pool(), operand->get_defined());
332: PUSH(value);
333: break;
334: }
335: case OP_IN:
336: {
337: Value *operand=POP();
338: Value *value=NEW VBool(pool(), true/*TODO*/);
339: PUSH(value);
340: break;
341: }
342: case OP_FEXISTS:
343: {
344: Value *operand=POP();
345: Value *value=NEW VBool(pool(), true/*TODO*/);
346: PUSH(value);
347: break;
348: }
1.55 paf 349:
350: // expression ops: binary
351: case OP_SUB:
1.53 paf 352: {
1.61 paf 353: Value *b=POP(); Value *a=POP();
354: Value *value=NEW VDouble(pool(), a->get_double() - b->get_double());
1.53 paf 355: PUSH(value);
356: break;
357: }
1.55 paf 358: case OP_ADD:
1.53 paf 359: {
1.61 paf 360: Value *b=POP(); Value *a=POP();
361: Value *value=NEW VDouble(pool(), a->get_double() + b->get_double());
1.53 paf 362: PUSH(value);
363: break;
364: }
1.49 paf 365: case OP_MUL:
366: {
1.61 paf 367: Value *b=POP(); Value *a=POP();
368: Value *value=NEW VDouble(pool(), a->get_double() * b->get_double());
1.53 paf 369: PUSH(value);
370: break;
371: }
372: case OP_DIV:
373: {
1.61 paf 374: Value *b=POP(); Value *a=POP();
375: Value *value=NEW VDouble(pool(), a->get_double() / b->get_double());
1.54 paf 376: PUSH(value);
377: break;
378: }
1.55 paf 379: case OP_MOD:
380: {
1.61 paf 381: Value *b=POP(); Value *a=POP();
1.55 paf 382: Value *value=NEW VDouble(pool(),
383: static_cast<int>(a->get_double()) %
384: static_cast<int>(b->get_double()));
385: PUSH(value);
386: break;
387: }
388: case OP_BIN_AND:
1.54 paf 389: {
1.61 paf 390: Value *b=POP(); Value *a=POP();
1.55 paf 391: Value *value=NEW VDouble(pool(),
392: static_cast<int>(a->get_double()) &
393: static_cast<int>(b->get_double()));
1.54 paf 394: PUSH(value);
395: break;
396: }
1.55 paf 397: case OP_BIN_OR:
1.54 paf 398: {
1.61 paf 399: Value *b=POP(); Value *a=POP();
1.54 paf 400: Value *value=NEW VDouble(pool(),
1.55 paf 401: static_cast<int>(a->get_double()) |
402: static_cast<int>(b->get_double()));
403: PUSH(value);
404: break;
405: }
1.56 paf 406: case OP_BIN_XOR:
407: {
1.61 paf 408: Value *b=POP(); Value *a=POP();
1.56 paf 409: Value *value=NEW VDouble(pool(),
410: static_cast<int>(a->get_double()) ^
411: static_cast<int>(b->get_double()));
412: PUSH(value);
413: break;
414: }
1.55 paf 415: case OP_LOG_AND:
416: {
1.61 paf 417: Value *b=POP(); Value *a=POP();
418: Value *value=NEW VBool(pool(), a->get_bool() && b->get_bool());
1.55 paf 419: PUSH(value);
420: break;
421: }
422: case OP_LOG_OR:
423: {
1.61 paf 424: Value *b=POP(); Value *a=POP();
425: Value *value=NEW VBool(pool(), a->get_bool() || b->get_bool());
1.56 paf 426: PUSH(value);
427: break;
428: }
429: case OP_LOG_XOR:
430: {
1.61 paf 431: Value *b=POP(); Value *a=POP();
432: Value *value=NEW VBool(pool(), a->get_bool() ^ b->get_bool());
1.55 paf 433: PUSH(value);
434: break;
435: }
436: case OP_NUM_LT:
437: {
1.61 paf 438: Value *b=POP(); Value *a=POP();
439: Value *value=NEW VBool(pool(), a->get_double() < b->get_double());
1.55 paf 440: PUSH(value);
441: break;
442: }
443: case OP_NUM_GT:
444: {
1.61 paf 445: Value *b=POP(); Value *a=POP();
446: Value *value=NEW VBool(pool(), a->get_double() > b->get_double());
1.55 paf 447: PUSH(value);
448: break;
449: }
450: case OP_NUM_LE:
451: {
1.61 paf 452: Value *b=POP(); Value *a=POP();
453: Value *value=NEW VBool(pool(), a->get_double() <= b->get_double());
1.55 paf 454: PUSH(value);
455: break;
456: }
457: case OP_NUM_GE:
458: {
1.61 paf 459: Value *b=POP(); Value *a=POP();
460: Value *value=NEW VBool(pool(), a->get_double() >= b->get_double());
1.55 paf 461: PUSH(value);
462: break;
463: }
464: case OP_NUM_EQ:
465: {
1.61 paf 466: Value *b=POP(); Value *a=POP();
467: Value *value=NEW VBool(pool(), a->get_double() == b->get_double());
1.55 paf 468: PUSH(value);
469: break;
470: }
471: case OP_NUM_NE:
472: {
1.61 paf 473: Value *b=POP(); Value *a=POP();
474: Value *value=NEW VBool(pool(), a->get_double() != b->get_double());
1.54 paf 475: PUSH(value);
476: break;
477: }
1.58 paf 478: case OP_STR_LT:
479: {
1.61 paf 480: Value *b=POP(); Value *a=POP();
481: Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 482: PUSH(value);
483: break;
484: }
485: case OP_STR_GT:
486: {
1.61 paf 487: Value *b=POP(); Value *a=POP();
488: Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 489: PUSH(value);
490: break;
491: }
1.55 paf 492: case OP_STR_LE:
1.58 paf 493: {
1.61 paf 494: Value *b=POP(); Value *a=POP();
495: Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 496: PUSH(value);
497: break;
498: }
1.55 paf 499: case OP_STR_GE:
1.54 paf 500: {
1.61 paf 501: Value *b=POP(); Value *a=POP();
502: Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 503: PUSH(value);
504: break;
505: }
506: case OP_STR_EQ:
507: {
1.61 paf 508: Value *b=POP(); Value *a=POP();
509: Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 510: PUSH(value);
511: break;
512: }
513: case OP_STR_NE:
514: {
1.61 paf 515: Value *b=POP(); Value *a=POP();
516: Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.49 paf 517: PUSH(value);
1.28 paf 518: break;
519: }
520:
1.11 paf 521: default:
1.46 paf 522: fprintf(stderr, "\tTODO");
1.11 paf 523: break;
524: }
1.46 paf 525: fprintf(stderr, "\n");
1.11 paf 526: }
1.1 paf 527: }
1.17 paf 528:
529: Value *Request::get_element() {
1.32 paf 530: String& name=POP_NAME();
1.24 paf 531: Value *ncontext=POP();
1.32 paf 532: Value *value=ncontext->get_element(name);
1.21 paf 533:
1.32 paf 534: if(value) {
535: Junction *junction=value->get_junction();
1.36 paf 536: if(junction && junction->code) { // is it a code-junction?
1.32 paf 537: // autocalc it
1.46 paf 538: fprintf(stderr, "ja->\n");
1.45 paf 539: PUSH(self);
540: PUSH(root);
541: PUSH(rcontext);
542: PUSH(wcontext);
543:
1.34 paf 544: // almost plain wwrapper about junction wcontext,
545: // BUT intercepts string writes
546: VCodeFrame frame(pool(), *junction->wcontext); wcontext=&frame;
547: self=&junction->self;
1.32 paf 548: root=junction->root;
549: rcontext=junction->rcontext;
550: execute(*junction->code);
1.35 paf 551: // CodeFrame soul:
1.36 paf 552: // string writes were intercepted
553: // returning them as the result of getting code-junction
1.42 paf 554: value=NEW VString(*frame.get_string());
1.45 paf 555:
556: wcontext=static_cast<WContext *>(POP());
557: rcontext=POP();
558: root=POP();
559: self=static_cast<VAliased *>(POP());
1.62 paf 560:
1.46 paf 561: fprintf(stderr, "<-ja returned");
1.32 paf 562: }
1.63 paf 563: } else
1.23 paf 564: value=NEW VUnknown(pool());
1.63 paf 565:
566: value->set_name(name);
1.17 paf 567: return value;
1.34 paf 568: }
E-mail: