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