Annotation of parser3/src/main/execute.C, revision 1.65
1.1 paf 1: /*
1.65 ! paf 2: $Id: execute.C,v 1.64 2001/03/08 09:13:33 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.65 ! paf 26: "VALUE", "CODE__STORE_PARAM", "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.65 ! paf 78: if(op.code==OP_CODE__STORE_PARAM) {
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: }
1.65 ! paf 109: case OP_CODE__STORE_PARAM:
1.32 paf 110: {
1.63 paf 111: VMethodFrame *frame=static_cast<VMethodFrame *>(stack[stack.top()]);
1.65 ! paf 112: // code
1.32 paf 113: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.46 paf 114: fprintf(stderr, " (%d)\n", local_ops->size());
1.38 paf 115: dump(1, *local_ops);
1.63 paf 116:
1.32 paf 117: Junction& j=*NEW Junction(pool(),
1.45 paf 118: *self, 0, 0,
1.64 paf 119: root, frame, frame, local_ops);
1.32 paf 120:
121: Value *value=NEW VJunction(j);
1.63 paf 122: value->set_name(frame->name());
1.65 ! paf 123:
! 124: // store param
! 125: frame->store_param(value);
1.32 paf 126: break;
127: }
1.38 paf 128: case OP_CLASS:
129: {
130: VClass *vclass=static_cast<VClass *>(ops.quick_get(++i));
1.46 paf 131: fprintf(stderr, " \"%s\"", vclass->name().cstr());
1.38 paf 132: PUSH(vclass);
133: break;
134: }
1.32 paf 135:
1.51 paf 136: // OP_WITH
1.37 paf 137: case OP_WITH_SELF:
138: {
139: PUSH(self);
140: break;
141: }
142: case OP_WITH_ROOT:
1.11 paf 143: {
1.37 paf 144: PUSH(root);
1.13 paf 145: break;
1.11 paf 146: }
1.15 paf 147: case OP_WITH_READ:
148: {
1.24 paf 149: PUSH(rcontext);
1.20 paf 150: break;
151: }
1.37 paf 152: case OP_WITH_WRITE:
1.20 paf 153: {
1.37 paf 154: PUSH(wcontext);
1.20 paf 155: break;
156: }
1.37 paf 157:
1.51 paf 158: // OTHER ACTIONS BUT WITHs
1.37 paf 159: case OP_CONSTRUCT:
1.20 paf 160: {
1.37 paf 161: Value *value=POP();
162: String& name=POP_NAME();
163: Value *ncontext=POP();
164: value->set_name(name);
165: ncontext->put_element(name, value);
1.15 paf 166: break;
167: }
1.18 paf 168: case OP_WRITE:
1.13 paf 169: {
1.24 paf 170: Value *value=POP();
1.14 paf 171: wcontext->write(value);
1.13 paf 172: break;
1.14 paf 173: }
1.13 paf 174:
1.15 paf 175: case OP_GET_ELEMENT:
1.11 paf 176: {
1.17 paf 177: Value *value=get_element();
1.24 paf 178: PUSH(value);
1.17 paf 179: break;
180: }
181:
1.18 paf 182: case OP_GET_ELEMENT__WRITE:
1.17 paf 183: {
184: Value *value=get_element();
185: wcontext->write(value);
186: break;
187: }
188:
1.32 paf 189:
1.17 paf 190: case OP_CREATE_EWPOOL:
191: {
1.24 paf 192: PUSH(wcontext);
1.43 paf 193: wcontext=NEW WWrapper(pool(), 0 /* empty */, true /* constructing */);
1.17 paf 194: break;
195: }
196: case OP_REDUCE_EWPOOL:
197: {
1.36 paf 198: Value *value=wcontext->result();
1.25 paf 199: wcontext=static_cast<WContext *>(POP());
1.24 paf 200: PUSH(value);
1.13 paf 201: break;
1.15 paf 202: }
1.13 paf 203:
1.26 paf 204: case OP_CREATE_RWPOOL:
205: {
206: Value *ncontext=POP();
207: PUSH(rcontext);
208: rcontext=ncontext;
209: PUSH(wcontext);
1.43 paf 210: wcontext=NEW WWrapper(pool(), ncontext, false /* not constructing */);
1.26 paf 211: break;
212: }
213: case OP_REDUCE_RWPOOL:
214: {
215: String *string=wcontext->get_string();
1.50 paf 216: Value *value=string?NEW VString(*string):NEW VString(pool());
1.26 paf 217: wcontext=static_cast<WContext *>(POP());
218: rcontext=POP();
219: PUSH(value);
220: break;
221: }
1.49 paf 222: case OP_CREATE_SWPOOL:
223: {
224: PUSH(wcontext);
1.50 paf 225: wcontext=NEW WWrapper(pool(), 0 /* empty */, false /* not constructing */);
1.49 paf 226: break;
227: }
228: case OP_REDUCE_SWPOOL:
229: {
230: // from "$a $b" part of expression taking only string value,
231: // ignoring any other content of wcontext
232: String *string=wcontext->get_string();
1.50 paf 233: Value *value=string?NEW VString(*string):NEW VString(pool());
234: wcontext=static_cast<WContext *>(POP());
1.49 paf 235: PUSH(value);
236: break;
237: }
238:
1.51 paf 239: // CALL
1.28 paf 240: case OP_GET_METHOD_FRAME:
241: {
1.32 paf 242: Value *value=POP();
1.28 paf 243: // [self/class?;params;local;code/native_code](name)
1.32 paf 244: Junction *junction=value->get_junction();
1.31 paf 245: if(!junction)
1.28 paf 246: THROW(0,0,
1.42 paf 247: &value->name(),
1.39 paf 248: "type is '%s', can not call it (must be method or junction)",
1.38 paf 249: value->type());
1.28 paf 250: //unless(method) method=operators.get_method[...;code/native_code](name)
1.34 paf 251: VMethodFrame *frame=NEW VMethodFrame(pool(), *junction);
1.63 paf 252: frame->set_name(junction->self.name());
1.28 paf 253: PUSH(frame);
254: break;
255: }
256: case OP_STORE_PARAM:
257: {
258: Value *value=POP();
1.34 paf 259: VMethodFrame *frame=static_cast<VMethodFrame *>(stack[0]);
1.28 paf 260: frame->store_param(value);
1.29 paf 261: break;
262: }
263:
264: case OP_CALL:
265: {
1.46 paf 266: fprintf(stderr, "->\n");
1.34 paf 267: VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
268: frame->fill_unspecified_params();
1.45 paf 269: PUSH(self);
270: PUSH(root);
271: PUSH(rcontext);
272: PUSH(wcontext);
273:
1.38 paf 274: VClass *called_class=frame->junction.self.get_class();
1.43 paf 275: // constructing?
276: if(wcontext->constructing()) { // yes
1.41 paf 277: // constructor call: $some(^class:method(..))
1.50 paf 278: frame->write(self=NEW VObject(pool(), *called_class));
1.43 paf 279: } else { // no
1.47 paf 280: // context is object or class & is it my class or my parent's class?
1.44 paf 281: VClass *read_class=rcontext->get_class();
282: if(read_class && read_class->is_or_derived_from(*called_class)) // yes
1.46 paf 283: self=rcontext; // class dynamic call
1.44 paf 284: else // no
285: self=&frame->junction.self; // static or simple dynamic call
1.38 paf 286: }
1.45 paf 287: frame->set_self(*self);
1.29 paf 288: root=rcontext=wcontext=frame;
1.47 paf 289: {
1.48 paf 290: // take object or class from any wrappers
291: VAliased *aliased=self->get_aliased();
292: // substitute class alias to the class they are called AS
1.47 paf 293: Temp_alias temp_alias(*aliased, *frame->junction.vclass);
294: execute(frame->junction.method->code);
295: }
1.36 paf 296: Value *value=wcontext->result();
1.45 paf 297:
298: wcontext=static_cast<WContext *>(POP());
299: rcontext=POP();
300: root=POP();
301: self=static_cast<VAliased *>(POP());
1.62 paf 302:
1.61 paf 303: PUSH(value);
1.46 paf 304: fprintf(stderr, "<-returned");
1.49 paf 305: break;
306: }
307:
1.55 paf 308: // expression ops: unary
309: case OP_NEG:
310: {
311: Value *operand=POP();
312: Value *value=NEW VDouble(pool(), -operand->get_double());
313: PUSH(value);
314: break;
315: }
316: case OP_INV:
317: {
318: Value *operand=POP();
1.61 paf 319: Value *value=NEW VDouble(pool(),
320: ~static_cast<int>(operand->get_double()));
1.55 paf 321: PUSH(value);
322: break;
323: }
324: case OP_NOT:
325: {
326: Value *operand=POP();
327: Value *value=NEW VBool(pool(), !operand->get_bool());
328: PUSH(value);
329: break;
330: }
1.62 paf 331: case OP_DEF:
332: {
333: Value *operand=POP();
334: Value *value=NEW VBool(pool(), operand->get_defined());
335: PUSH(value);
336: break;
337: }
338: case OP_IN:
339: {
340: Value *operand=POP();
341: Value *value=NEW VBool(pool(), true/*TODO*/);
342: PUSH(value);
343: break;
344: }
345: case OP_FEXISTS:
346: {
347: Value *operand=POP();
348: Value *value=NEW VBool(pool(), true/*TODO*/);
349: PUSH(value);
350: break;
351: }
1.55 paf 352:
353: // expression ops: binary
354: case OP_SUB:
1.53 paf 355: {
1.61 paf 356: Value *b=POP(); Value *a=POP();
357: Value *value=NEW VDouble(pool(), a->get_double() - b->get_double());
1.53 paf 358: PUSH(value);
359: break;
360: }
1.55 paf 361: case OP_ADD:
1.53 paf 362: {
1.61 paf 363: Value *b=POP(); Value *a=POP();
364: Value *value=NEW VDouble(pool(), a->get_double() + b->get_double());
1.53 paf 365: PUSH(value);
366: break;
367: }
1.49 paf 368: case OP_MUL:
369: {
1.61 paf 370: Value *b=POP(); Value *a=POP();
371: Value *value=NEW VDouble(pool(), a->get_double() * b->get_double());
1.53 paf 372: PUSH(value);
373: break;
374: }
375: case OP_DIV:
376: {
1.61 paf 377: Value *b=POP(); Value *a=POP();
378: Value *value=NEW VDouble(pool(), a->get_double() / b->get_double());
1.54 paf 379: PUSH(value);
380: break;
381: }
1.55 paf 382: case OP_MOD:
383: {
1.61 paf 384: Value *b=POP(); Value *a=POP();
1.55 paf 385: Value *value=NEW VDouble(pool(),
386: static_cast<int>(a->get_double()) %
387: static_cast<int>(b->get_double()));
388: PUSH(value);
389: break;
390: }
391: case OP_BIN_AND:
1.54 paf 392: {
1.61 paf 393: Value *b=POP(); Value *a=POP();
1.55 paf 394: Value *value=NEW VDouble(pool(),
395: static_cast<int>(a->get_double()) &
396: static_cast<int>(b->get_double()));
1.54 paf 397: PUSH(value);
398: break;
399: }
1.55 paf 400: case OP_BIN_OR:
1.54 paf 401: {
1.61 paf 402: Value *b=POP(); Value *a=POP();
1.54 paf 403: Value *value=NEW VDouble(pool(),
1.55 paf 404: static_cast<int>(a->get_double()) |
405: static_cast<int>(b->get_double()));
406: PUSH(value);
407: break;
408: }
1.56 paf 409: case OP_BIN_XOR:
410: {
1.61 paf 411: Value *b=POP(); Value *a=POP();
1.56 paf 412: Value *value=NEW VDouble(pool(),
413: static_cast<int>(a->get_double()) ^
414: static_cast<int>(b->get_double()));
415: PUSH(value);
416: break;
417: }
1.55 paf 418: case OP_LOG_AND:
419: {
1.61 paf 420: Value *b=POP(); Value *a=POP();
421: Value *value=NEW VBool(pool(), a->get_bool() && b->get_bool());
1.55 paf 422: PUSH(value);
423: break;
424: }
425: case OP_LOG_OR:
426: {
1.61 paf 427: Value *b=POP(); Value *a=POP();
428: Value *value=NEW VBool(pool(), a->get_bool() || b->get_bool());
1.56 paf 429: PUSH(value);
430: break;
431: }
432: case OP_LOG_XOR:
433: {
1.61 paf 434: Value *b=POP(); Value *a=POP();
435: Value *value=NEW VBool(pool(), a->get_bool() ^ b->get_bool());
1.55 paf 436: PUSH(value);
437: break;
438: }
439: case OP_NUM_LT:
440: {
1.61 paf 441: Value *b=POP(); Value *a=POP();
442: Value *value=NEW VBool(pool(), a->get_double() < b->get_double());
1.55 paf 443: PUSH(value);
444: break;
445: }
446: case OP_NUM_GT:
447: {
1.61 paf 448: Value *b=POP(); Value *a=POP();
449: Value *value=NEW VBool(pool(), a->get_double() > b->get_double());
1.55 paf 450: PUSH(value);
451: break;
452: }
453: case OP_NUM_LE:
454: {
1.61 paf 455: Value *b=POP(); Value *a=POP();
456: Value *value=NEW VBool(pool(), a->get_double() <= b->get_double());
1.55 paf 457: PUSH(value);
458: break;
459: }
460: case OP_NUM_GE:
461: {
1.61 paf 462: Value *b=POP(); Value *a=POP();
463: Value *value=NEW VBool(pool(), a->get_double() >= b->get_double());
1.55 paf 464: PUSH(value);
465: break;
466: }
467: case OP_NUM_EQ:
468: {
1.61 paf 469: Value *b=POP(); Value *a=POP();
470: Value *value=NEW VBool(pool(), a->get_double() == b->get_double());
1.55 paf 471: PUSH(value);
472: break;
473: }
474: case OP_NUM_NE:
475: {
1.61 paf 476: Value *b=POP(); Value *a=POP();
477: Value *value=NEW VBool(pool(), a->get_double() != b->get_double());
1.54 paf 478: PUSH(value);
479: break;
480: }
1.58 paf 481: case OP_STR_LT:
482: {
1.61 paf 483: Value *b=POP(); Value *a=POP();
484: Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 485: PUSH(value);
486: break;
487: }
488: case OP_STR_GT:
489: {
1.61 paf 490: Value *b=POP(); Value *a=POP();
491: Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 492: PUSH(value);
493: break;
494: }
1.55 paf 495: case OP_STR_LE:
1.58 paf 496: {
1.61 paf 497: Value *b=POP(); Value *a=POP();
498: Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 499: PUSH(value);
500: break;
501: }
1.55 paf 502: case OP_STR_GE:
1.54 paf 503: {
1.61 paf 504: Value *b=POP(); Value *a=POP();
505: Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 506: PUSH(value);
507: break;
508: }
509: case OP_STR_EQ:
510: {
1.61 paf 511: Value *b=POP(); Value *a=POP();
512: Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 513: PUSH(value);
514: break;
515: }
516: case OP_STR_NE:
517: {
1.61 paf 518: Value *b=POP(); Value *a=POP();
519: Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.49 paf 520: PUSH(value);
1.28 paf 521: break;
522: }
523:
1.11 paf 524: default:
1.46 paf 525: fprintf(stderr, "\tTODO");
1.11 paf 526: break;
527: }
1.46 paf 528: fprintf(stderr, "\n");
1.11 paf 529: }
1.1 paf 530: }
1.17 paf 531:
532: Value *Request::get_element() {
1.32 paf 533: String& name=POP_NAME();
1.24 paf 534: Value *ncontext=POP();
1.32 paf 535: Value *value=ncontext->get_element(name);
1.21 paf 536:
1.32 paf 537: if(value) {
538: Junction *junction=value->get_junction();
1.36 paf 539: if(junction && junction->code) { // is it a code-junction?
1.32 paf 540: // autocalc it
1.46 paf 541: fprintf(stderr, "ja->\n");
1.45 paf 542: PUSH(self);
543: PUSH(root);
544: PUSH(rcontext);
545: PUSH(wcontext);
546:
1.34 paf 547: // almost plain wwrapper about junction wcontext,
548: // BUT intercepts string writes
549: VCodeFrame frame(pool(), *junction->wcontext); wcontext=&frame;
550: self=&junction->self;
1.32 paf 551: root=junction->root;
552: rcontext=junction->rcontext;
553: execute(*junction->code);
1.35 paf 554: // CodeFrame soul:
1.36 paf 555: // string writes were intercepted
556: // returning them as the result of getting code-junction
1.42 paf 557: value=NEW VString(*frame.get_string());
1.45 paf 558:
559: wcontext=static_cast<WContext *>(POP());
560: rcontext=POP();
561: root=POP();
562: self=static_cast<VAliased *>(POP());
1.62 paf 563:
1.46 paf 564: fprintf(stderr, "<-ja returned");
1.32 paf 565: }
1.63 paf 566: } else
1.23 paf 567: value=NEW VUnknown(pool());
1.63 paf 568:
569: value->set_name(name);
1.17 paf 570: return value;
1.34 paf 571: }
E-mail: