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