Annotation of parser3/src/main/execute.C, revision 1.74
1.1 paf 1: /*
1.74 ! paf 2: $Id: execute.C,v 1.73 2001/03/08 15:15:46 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.50 paf 218: Value *value=string?NEW VString(*string):NEW VString(pool());
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.50 paf 235: Value *value=string?NEW VString(*string):NEW VString(pool());
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
! 281: else { // no, not me or relative of mine (total stranger)
! 282: // constructing?
! 283: if(wcontext->constructing()) { // yes
! 284: // constructor call: $some(^class:method(..))
! 285: self=NEW VObject(pool(), *called_class);
! 286: frame->write(*self);
! 287: } else { // no
1.44 paf 288: self=&frame->junction.self; // static or simple dynamic call
1.74 ! paf 289: }
1.38 paf 290: }
1.45 paf 291: frame->set_self(*self);
1.29 paf 292: root=rcontext=wcontext=frame;
1.47 paf 293: {
1.48 paf 294: // take object or class from any wrappers
295: VAliased *aliased=self->get_aliased();
296: // substitute class alias to the class they are called AS
1.47 paf 297: Temp_alias temp_alias(*aliased, *frame->junction.vclass);
1.68 paf 298:
1.69 paf 299: Method& method=*frame->junction.method;
300: if(method.native_code) // native code?
1.70 paf 301: (*method.native_code)(*this, frame->numbered_params()); // execute it
1.68 paf 302: else // parser code
1.69 paf 303: execute(*method.parser_code); // execute it
1.47 paf 304: }
1.36 paf 305: Value *value=wcontext->result();
1.45 paf 306:
307: wcontext=static_cast<WContext *>(POP());
308: rcontext=POP();
309: root=POP();
310: self=static_cast<VAliased *>(POP());
1.62 paf 311:
1.61 paf 312: PUSH(value);
1.46 paf 313: fprintf(stderr, "<-returned");
1.49 paf 314: break;
315: }
316:
1.55 paf 317: // expression ops: unary
318: case OP_NEG:
319: {
320: Value *operand=POP();
321: Value *value=NEW VDouble(pool(), -operand->get_double());
322: PUSH(value);
323: break;
324: }
325: case OP_INV:
326: {
327: Value *operand=POP();
1.61 paf 328: Value *value=NEW VDouble(pool(),
329: ~static_cast<int>(operand->get_double()));
1.55 paf 330: PUSH(value);
331: break;
332: }
333: case OP_NOT:
334: {
335: Value *operand=POP();
336: Value *value=NEW VBool(pool(), !operand->get_bool());
337: PUSH(value);
338: break;
339: }
1.62 paf 340: case OP_DEF:
341: {
342: Value *operand=POP();
343: Value *value=NEW VBool(pool(), operand->get_defined());
344: PUSH(value);
345: break;
346: }
347: case OP_IN:
348: {
349: Value *operand=POP();
350: Value *value=NEW VBool(pool(), true/*TODO*/);
351: PUSH(value);
352: break;
353: }
354: case OP_FEXISTS:
355: {
356: Value *operand=POP();
357: Value *value=NEW VBool(pool(), true/*TODO*/);
358: PUSH(value);
359: break;
360: }
1.55 paf 361:
362: // expression ops: binary
363: case OP_SUB:
1.53 paf 364: {
1.61 paf 365: Value *b=POP(); Value *a=POP();
366: Value *value=NEW VDouble(pool(), 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();
373: Value *value=NEW VDouble(pool(), a->get_double() + b->get_double());
1.53 paf 374: PUSH(value);
375: break;
376: }
1.49 paf 377: case OP_MUL:
378: {
1.61 paf 379: Value *b=POP(); Value *a=POP();
380: Value *value=NEW VDouble(pool(), a->get_double() * b->get_double());
1.53 paf 381: PUSH(value);
382: break;
383: }
384: case OP_DIV:
385: {
1.61 paf 386: Value *b=POP(); Value *a=POP();
387: Value *value=NEW VDouble(pool(), a->get_double() / b->get_double());
1.54 paf 388: PUSH(value);
389: break;
390: }
1.55 paf 391: case OP_MOD:
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()));
397: PUSH(value);
398: break;
399: }
400: case OP_BIN_AND:
1.54 paf 401: {
1.61 paf 402: Value *b=POP(); Value *a=POP();
1.55 paf 403: Value *value=NEW VDouble(pool(),
404: static_cast<int>(a->get_double()) &
405: static_cast<int>(b->get_double()));
1.54 paf 406: PUSH(value);
407: break;
408: }
1.55 paf 409: case OP_BIN_OR:
1.54 paf 410: {
1.61 paf 411: Value *b=POP(); Value *a=POP();
1.54 paf 412: Value *value=NEW VDouble(pool(),
1.55 paf 413: static_cast<int>(a->get_double()) |
414: static_cast<int>(b->get_double()));
415: PUSH(value);
416: break;
417: }
1.56 paf 418: case OP_BIN_XOR:
419: {
1.61 paf 420: Value *b=POP(); Value *a=POP();
1.56 paf 421: Value *value=NEW VDouble(pool(),
422: static_cast<int>(a->get_double()) ^
423: static_cast<int>(b->get_double()));
424: PUSH(value);
425: break;
426: }
1.55 paf 427: case OP_LOG_AND:
428: {
1.61 paf 429: Value *b=POP(); Value *a=POP();
430: Value *value=NEW VBool(pool(), a->get_bool() && b->get_bool());
1.55 paf 431: PUSH(value);
432: break;
433: }
434: case OP_LOG_OR:
435: {
1.61 paf 436: Value *b=POP(); Value *a=POP();
437: Value *value=NEW VBool(pool(), a->get_bool() || b->get_bool());
1.56 paf 438: PUSH(value);
439: break;
440: }
441: case OP_LOG_XOR:
442: {
1.61 paf 443: Value *b=POP(); Value *a=POP();
444: Value *value=NEW VBool(pool(), a->get_bool() ^ b->get_bool());
1.55 paf 445: PUSH(value);
446: break;
447: }
448: case OP_NUM_LT:
449: {
1.61 paf 450: Value *b=POP(); Value *a=POP();
451: Value *value=NEW VBool(pool(), a->get_double() < b->get_double());
1.55 paf 452: PUSH(value);
453: break;
454: }
455: case OP_NUM_GT:
456: {
1.61 paf 457: Value *b=POP(); Value *a=POP();
458: Value *value=NEW VBool(pool(), a->get_double() > b->get_double());
1.55 paf 459: PUSH(value);
460: break;
461: }
462: case OP_NUM_LE:
463: {
1.61 paf 464: Value *b=POP(); Value *a=POP();
465: Value *value=NEW VBool(pool(), a->get_double() <= b->get_double());
1.55 paf 466: PUSH(value);
467: break;
468: }
469: case OP_NUM_GE:
470: {
1.61 paf 471: Value *b=POP(); Value *a=POP();
472: Value *value=NEW VBool(pool(), a->get_double() >= b->get_double());
1.55 paf 473: PUSH(value);
474: break;
475: }
476: case OP_NUM_EQ:
477: {
1.61 paf 478: Value *b=POP(); Value *a=POP();
479: Value *value=NEW VBool(pool(), a->get_double() == b->get_double());
1.55 paf 480: PUSH(value);
481: break;
482: }
483: case OP_NUM_NE:
484: {
1.61 paf 485: Value *b=POP(); Value *a=POP();
486: Value *value=NEW VBool(pool(), a->get_double() != b->get_double());
1.54 paf 487: PUSH(value);
488: break;
489: }
1.58 paf 490: case OP_STR_LT:
491: {
1.61 paf 492: Value *b=POP(); Value *a=POP();
493: Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 494: PUSH(value);
495: break;
496: }
497: case OP_STR_GT:
498: {
1.61 paf 499: Value *b=POP(); Value *a=POP();
500: Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 501: PUSH(value);
502: break;
503: }
1.55 paf 504: case OP_STR_LE:
1.58 paf 505: {
1.61 paf 506: Value *b=POP(); Value *a=POP();
507: Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 508: PUSH(value);
509: break;
510: }
1.55 paf 511: case OP_STR_GE:
1.54 paf 512: {
1.61 paf 513: Value *b=POP(); Value *a=POP();
514: Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 515: PUSH(value);
516: break;
517: }
518: case OP_STR_EQ:
519: {
1.61 paf 520: Value *b=POP(); Value *a=POP();
521: Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 522: PUSH(value);
523: break;
524: }
525: case OP_STR_NE:
526: {
1.61 paf 527: Value *b=POP(); Value *a=POP();
528: Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.49 paf 529: PUSH(value);
1.28 paf 530: break;
531: }
532:
1.11 paf 533: default:
1.67 paf 534: THROW(0,0,
535: 0,
536: "unhandled '%s' opcode", opcode_name[op.code]);
1.11 paf 537: }
1.46 paf 538: fprintf(stderr, "\n");
1.11 paf 539: }
1.1 paf 540: }
1.17 paf 541:
542: Value *Request::get_element() {
1.32 paf 543: String& name=POP_NAME();
1.24 paf 544: Value *ncontext=POP();
1.32 paf 545: Value *value=ncontext->get_element(name);
1.21 paf 546:
1.70 paf 547: // autocalc possible code-junction
1.72 paf 548: value=value?&autocalc(*value):NEW VUnknown(pool());
1.63 paf 549:
550: value->set_name(name);
1.17 paf 551: return value;
1.34 paf 552: }
1.70 paf 553:
1.71 paf 554: Value& Request::autocalc(Value& value, bool make_string) {
1.70 paf 555: Junction *junction=value.get_junction();
556: if(junction && junction->code) { // is it a code-junction?
557: // autocalc it
558: fprintf(stderr, "ja->\n");
559: PUSH(self);
560: PUSH(root);
561: PUSH(rcontext);
562: PUSH(wcontext);
563:
1.71 paf 564: WContext *frame;
565: if(make_string) {
566: // almost plain wwrapper about junction wcontext,
567: // BUT intercepts string writes
568: frame=NEW VCodeFrame(pool(), *junction->wcontext);
569: } else {
570: // plain wwrapper
571: frame=NEW WWrapper(pool(), 0 /* empty */, false /*not constructing*/);
572: }
573:
574: wcontext=frame;
1.70 paf 575: self=&junction->self;
576: root=junction->root;
577: rcontext=junction->rcontext;
578: execute(*junction->code);
1.71 paf 579: Value *result;
580: if(make_string) {
581: // CodeFrame soul:
582: // string writes were intercepted
583: // returning them as the result of getting code-junction
584: result=NEW VString(*frame->get_string());
585: } else
586: result=frame->result();
1.70 paf 587:
588: wcontext=static_cast<WContext *>(POP());
589: rcontext=POP();
590: root=POP();
591: self=static_cast<VAliased *>(POP());
592:
593: fprintf(stderr, "<-ja returned");
1.71 paf 594: return *result;
1.70 paf 595: } else
596: return value;
597: }
598:
599: void Request::write(Value& avalue) {
600: wcontext->write(avalue);
601: }
E-mail: