Annotation of parser3/src/main/execute.C, revision 1.59
1.1 paf 1: /*
1.59 ! paf 2: $Id: execute.C,v 1.58 2001/03/07 09:29:54 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: }
83: }
84:
1.32 paf 85: void Request::execute(const Array& ops) {
1.30 paf 86: if(1) {
1.51 paf 87: fputs("source----------------------------\n", stderr);
1.12 paf 88: dump(0, ops);
1.51 paf 89: fputs("execution-------------------------\n", stderr);
1.12 paf 90: }
91:
1.11 paf 92: int size=ops.size();
1.46 paf 93: //fprintf(stderr, "size=%d\n", size);
1.11 paf 94: for(int i=0; i<size; i++) {
1.23 paf 95: Operation op;
96: op.cast=ops.quick_get(i);
1.46 paf 97: fprintf(stderr, "%d:%s", stack.top(), opcode_name[op.code]);
1.11 paf 98:
1.23 paf 99: switch(op.code) {
1.51 paf 100: // param in next instruction
1.52 paf 101: case OP_VALUE:
1.32 paf 102: {
1.52 paf 103: Value *value=static_cast<Value *>(ops.quick_get(++i));
104: fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());
105: PUSH(value);
1.32 paf 106: break;
107: }
108: case OP_CODE:
109: {
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.57 paf 113: // TODO: rcontext junction должен быть контекстом вызываемого объекта
114: // чтобы вот эти конструкции орудовали одними полями внутри {} :
115: // $var{$field}
116: // ^var.menu{$field}
117: // поскольку они суть одно и то же, и так удобнее
1.32 paf 118: Junction& j=*NEW Junction(pool(),
1.45 paf 119: *self, 0, 0,
1.34 paf 120: root,rcontext,wcontext,local_ops);
1.32 paf 121:
122: Value *value=NEW VJunction(j);
123: PUSH(value);
124: break;
125: }
1.38 paf 126: case OP_CLASS:
127: {
128: VClass *vclass=static_cast<VClass *>(ops.quick_get(++i));
1.46 paf 129: fprintf(stderr, " \"%s\"", vclass->name().cstr());
1.38 paf 130: PUSH(vclass);
131: break;
132: }
1.32 paf 133:
1.51 paf 134: // OP_WITH
1.37 paf 135: case OP_WITH_SELF:
136: {
137: PUSH(self);
138: break;
139: }
140: case OP_WITH_ROOT:
1.11 paf 141: {
1.37 paf 142: PUSH(root);
1.13 paf 143: break;
1.11 paf 144: }
1.15 paf 145: case OP_WITH_READ:
146: {
1.24 paf 147: PUSH(rcontext);
1.20 paf 148: break;
149: }
1.37 paf 150: case OP_WITH_WRITE:
1.20 paf 151: {
1.37 paf 152: PUSH(wcontext);
1.20 paf 153: break;
154: }
1.37 paf 155:
1.51 paf 156: // OTHER ACTIONS BUT WITHs
1.37 paf 157: case OP_CONSTRUCT:
1.20 paf 158: {
1.37 paf 159: Value *value=POP();
160: String& name=POP_NAME();
161: Value *ncontext=POP();
162: value->set_name(name);
163: ncontext->put_element(name, value);
1.15 paf 164: break;
165: }
1.18 paf 166: case OP_WRITE:
1.13 paf 167: {
1.24 paf 168: Value *value=POP();
1.14 paf 169: wcontext->write(value);
1.13 paf 170: break;
1.14 paf 171: }
1.13 paf 172:
1.15 paf 173: case OP_GET_ELEMENT:
1.11 paf 174: {
1.17 paf 175: Value *value=get_element();
1.24 paf 176: PUSH(value);
1.17 paf 177: break;
178: }
179:
1.18 paf 180: case OP_GET_ELEMENT__WRITE:
1.17 paf 181: {
182: Value *value=get_element();
183: wcontext->write(value);
184: break;
185: }
186:
1.32 paf 187:
1.17 paf 188: case OP_CREATE_EWPOOL:
189: {
1.24 paf 190: PUSH(wcontext);
1.43 paf 191: wcontext=NEW WWrapper(pool(), 0 /* empty */, true /* constructing */);
1.17 paf 192: break;
193: }
194: case OP_REDUCE_EWPOOL:
195: {
1.36 paf 196: Value *value=wcontext->result();
1.25 paf 197: wcontext=static_cast<WContext *>(POP());
1.24 paf 198: PUSH(value);
1.13 paf 199: break;
1.15 paf 200: }
1.13 paf 201:
1.26 paf 202: case OP_CREATE_RWPOOL:
203: {
204: Value *ncontext=POP();
205: PUSH(rcontext);
206: rcontext=ncontext;
207: PUSH(wcontext);
1.43 paf 208: wcontext=NEW WWrapper(pool(), ncontext, false /* not constructing */);
1.26 paf 209: break;
210: }
211: case OP_REDUCE_RWPOOL:
212: {
213: String *string=wcontext->get_string();
1.50 paf 214: Value *value=string?NEW VString(*string):NEW VString(pool());
1.26 paf 215: wcontext=static_cast<WContext *>(POP());
216: rcontext=POP();
217: PUSH(value);
218: break;
219: }
1.49 paf 220: case OP_CREATE_SWPOOL:
221: {
222: PUSH(wcontext);
1.50 paf 223: wcontext=NEW WWrapper(pool(), 0 /* empty */, false /* not constructing */);
1.49 paf 224: break;
225: }
226: case OP_REDUCE_SWPOOL:
227: {
228: // from "$a $b" part of expression taking only string value,
229: // ignoring any other content of wcontext
230: String *string=wcontext->get_string();
1.50 paf 231: Value *value=string?NEW VString(*string):NEW VString(pool());
232: wcontext=static_cast<WContext *>(POP());
1.49 paf 233: PUSH(value);
234: break;
235: }
236:
1.51 paf 237: // CALL
1.28 paf 238: case OP_GET_METHOD_FRAME:
239: {
1.32 paf 240: Value *value=POP();
1.28 paf 241: // [self/class?;params;local;code/native_code](name)
1.32 paf 242: Junction *junction=value->get_junction();
1.31 paf 243: if(!junction)
1.28 paf 244: THROW(0,0,
1.42 paf 245: &value->name(),
1.39 paf 246: "type is '%s', can not call it (must be method or junction)",
1.38 paf 247: value->type());
1.28 paf 248: //unless(method) method=operators.get_method[...;code/native_code](name)
1.34 paf 249: VMethodFrame *frame=NEW VMethodFrame(pool(), *junction);
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.29 paf 299: wcontext->write(value);
1.46 paf 300: fprintf(stderr, "<-returned");
1.49 paf 301: break;
302: }
303:
1.55 paf 304: // expression ops: unary
305: case OP_NEG:
306: {
307: Value *operand=POP();
308: Value *value=NEW VDouble(pool(), -operand->get_double());
309: PUSH(value);
310: break;
311: }
312: case OP_INV:
313: {
314: Value *operand=POP();
1.59 ! paf 315: Value *value=NEW VDouble(pool(), ~static_cast<int>(operand->get_double()));
1.55 paf 316: PUSH(value);
317: break;
318: }
319: case OP_NOT:
320: {
321: Value *operand=POP();
322: Value *value=NEW VBool(pool(), !operand->get_bool());
323: PUSH(value);
324: break;
325: }
326: //todo: def in fexists
327:
328: // expression ops: binary
329: case OP_SUB:
1.53 paf 330: {
331: Value *b=POP();
332: Value *a=POP();
333: Value *value=NEW VDouble(pool(),
1.55 paf 334: a->get_double() -
1.53 paf 335: b->get_double());
336: PUSH(value);
337: break;
338: }
1.55 paf 339: case OP_ADD:
1.53 paf 340: {
341: Value *b=POP();
342: Value *a=POP();
343: Value *value=NEW VDouble(pool(),
1.55 paf 344: a->get_double() +
1.53 paf 345: b->get_double());
346: PUSH(value);
347: break;
348: }
1.49 paf 349: case OP_MUL:
350: {
351: Value *b=POP();
352: Value *a=POP();
353: Value *value=NEW VDouble(pool(),
354: a->get_double() *
1.53 paf 355: b->get_double());
356: PUSH(value);
357: break;
358: }
359: case OP_DIV:
360: {
361: Value *b=POP();
362: Value *a=POP();
363: Value *value=NEW VDouble(pool(),
364: a->get_double() /
1.49 paf 365: b->get_double());
1.54 paf 366: PUSH(value);
367: break;
368: }
1.55 paf 369: case OP_MOD:
370: {
371: Value *b=POP();
372: Value *a=POP();
373: Value *value=NEW VDouble(pool(),
374: static_cast<int>(a->get_double()) %
375: static_cast<int>(b->get_double()));
376: PUSH(value);
377: break;
378: }
379: case OP_BIN_AND:
1.54 paf 380: {
1.55 paf 381: Value *b=POP();
382: Value *a=POP();
383: Value *value=NEW VDouble(pool(),
384: static_cast<int>(a->get_double()) &
385: static_cast<int>(b->get_double()));
1.54 paf 386: PUSH(value);
387: break;
388: }
1.55 paf 389: case OP_BIN_OR:
1.54 paf 390: {
1.55 paf 391: Value *b=POP();
392: Value *a=POP();
1.54 paf 393: Value *value=NEW VDouble(pool(),
1.55 paf 394: static_cast<int>(a->get_double()) |
395: static_cast<int>(b->get_double()));
396: PUSH(value);
397: break;
398: }
1.56 paf 399: case OP_BIN_XOR:
400: {
401: Value *b=POP();
402: Value *a=POP();
403: Value *value=NEW VDouble(pool(),
404: static_cast<int>(a->get_double()) ^
405: static_cast<int>(b->get_double()));
406: PUSH(value);
407: break;
408: }
1.55 paf 409: case OP_LOG_AND:
410: {
411: Value *b=POP();
412: Value *a=POP();
413: Value *value=NEW VBool(pool(),
414: a->get_bool() &&
415: b->get_bool());
416: PUSH(value);
417: break;
418: }
419: case OP_LOG_OR:
420: {
421: Value *b=POP();
422: Value *a=POP();
423: Value *value=NEW VBool(pool(),
424: a->get_bool() ||
1.56 paf 425: b->get_bool());
426: PUSH(value);
427: break;
428: }
429: case OP_LOG_XOR:
430: {
431: Value *b=POP();
432: Value *a=POP();
433: Value *value=NEW VBool(pool(),
434: a->get_bool() ^
1.55 paf 435: b->get_bool());
436: PUSH(value);
437: break;
438: }
439: case OP_NUM_LT:
440: {
441: Value *b=POP();
442: Value *a=POP();
443: Value *value=NEW VBool(pool(),
444: a->get_double() <
445: b->get_double());
446: PUSH(value);
447: break;
448: }
449: case OP_NUM_GT:
450: {
451: Value *b=POP();
452: Value *a=POP();
453: Value *value=NEW VBool(pool(),
454: a->get_double() >
455: b->get_double());
456: PUSH(value);
457: break;
458: }
459: case OP_NUM_LE:
460: {
461: Value *b=POP();
462: Value *a=POP();
463: Value *value=NEW VBool(pool(),
464: a->get_double() <=
465: b->get_double());
466: PUSH(value);
467: break;
468: }
469: case OP_NUM_GE:
470: {
471: Value *b=POP();
472: Value *a=POP();
473: Value *value=NEW VBool(pool(),
474: a->get_double() >=
475: b->get_double());
476: PUSH(value);
477: break;
478: }
479: case OP_NUM_EQ:
480: {
481: Value *b=POP();
482: Value *a=POP();
483: Value *value=NEW VBool(pool(),
484: a->get_double() ==
485: b->get_double());
486: PUSH(value);
487: break;
488: }
489: case OP_NUM_NE:
490: {
491: Value *b=POP();
492: Value *a=POP();
493: Value *value=NEW VBool(pool(),
494: a->get_double() !=
495: b->get_double());
1.54 paf 496: PUSH(value);
497: break;
498: }
1.58 paf 499: case OP_STR_LT:
500: {
501: Value *b=POP();
502: Value *a=POP();
503: Value *value=NEW VBool(pool(),
504: a->as_string() <
505: b->as_string());
506: PUSH(value);
507: break;
508: }
509: case OP_STR_GT:
510: {
511: Value *b=POP();
512: Value *a=POP();
513: Value *value=NEW VBool(pool(),
514: a->as_string() >
515: b->as_string());
516: PUSH(value);
517: break;
518: }
1.55 paf 519: case OP_STR_LE:
1.58 paf 520: {
521: Value *b=POP();
522: Value *a=POP();
523: Value *value=NEW VBool(pool(),
524: a->as_string() <=
525: b->as_string());
526: PUSH(value);
527: break;
528: }
1.55 paf 529: case OP_STR_GE:
1.54 paf 530: {
1.55 paf 531: Value *b=POP();
532: Value *a=POP();
533: Value *value=NEW VBool(pool(),
1.58 paf 534: a->as_string() >=
535: b->as_string());
536: PUSH(value);
537: break;
538: }
539: case OP_STR_EQ:
540: {
541: Value *b=POP();
542: Value *a=POP();
543: Value *value=NEW VBool(pool(),
544: a->as_string() ==
545: b->as_string());
546: PUSH(value);
547: break;
548: }
549: case OP_STR_NE:
550: {
551: Value *b=POP();
552: Value *a=POP();
553: Value *value=NEW VBool(pool(),
554: a->as_string() !=
555: b->as_string());
1.49 paf 556: PUSH(value);
1.28 paf 557: break;
558: }
559:
1.11 paf 560: default:
1.46 paf 561: fprintf(stderr, "\tTODO");
1.11 paf 562: break;
563: }
1.46 paf 564: fprintf(stderr, "\n");
1.11 paf 565: }
1.1 paf 566: }
1.17 paf 567:
568: Value *Request::get_element() {
1.32 paf 569: String& name=POP_NAME();
1.24 paf 570: Value *ncontext=POP();
1.32 paf 571: Value *value=ncontext->get_element(name);
1.21 paf 572:
1.32 paf 573: if(value) {
574: Junction *junction=value->get_junction();
1.36 paf 575: if(junction && junction->code) { // is it a code-junction?
1.32 paf 576: // autocalc it
1.46 paf 577: fprintf(stderr, "ja->\n");
1.45 paf 578: PUSH(self);
579: PUSH(root);
580: PUSH(rcontext);
581: PUSH(wcontext);
582:
1.34 paf 583: // almost plain wwrapper about junction wcontext,
584: // BUT intercepts string writes
585: VCodeFrame frame(pool(), *junction->wcontext); wcontext=&frame;
586: self=&junction->self;
1.32 paf 587: root=junction->root;
588: rcontext=junction->rcontext;
589: execute(*junction->code);
1.35 paf 590: // CodeFrame soul:
1.36 paf 591: // string writes were intercepted
592: // returning them as the result of getting code-junction
1.42 paf 593: value=NEW VString(*frame.get_string());
1.45 paf 594:
595: wcontext=static_cast<WContext *>(POP());
596: rcontext=POP();
597: root=POP();
598: self=static_cast<VAliased *>(POP());
1.46 paf 599: fprintf(stderr, "<-ja returned");
1.32 paf 600: }
601: } else {
1.23 paf 602: value=NEW VUnknown(pool());
1.25 paf 603: value->set_name(name);
1.22 paf 604: }
1.17 paf 605: return value;
1.34 paf 606: }
E-mail: