--- parser3/src/main/execute.C 2001/02/25 18:02:12 1.48 +++ parser3/src/main/execute.C 2001/03/07 08:27:10 1.57 @@ -1,5 +1,5 @@ /* - $Id: execute.C,v 1.48 2001/02/25 18:02:12 paf Exp $ + $Id: execute.C,v 1.57 2001/03/07 08:27:10 paf Exp $ */ #include "pa_array.h" @@ -11,6 +11,8 @@ #include "pa_vcframe.h" #include "pa_vmframe.h" #include "pa_vobject.h" +#include "pa_vdouble.h" +#include "pa_vbool.h" #include @@ -20,17 +22,29 @@ char *opcode_name[]={ - "STRING", "CODE", "CLASS", - "WITH_ROOT", "WITH_SELF", "WITH_READ", "WITH_WRITE", + // literals + "VALUE", "CODE", "CLASS", + + // actions + "WITH_SELF", "WITH_ROOT", "WITH_READ", "WITH_WRITE", "CONSTRUCT", - "EXPRESSION_EVAL", "MODIFY_EVAL", "WRITE", "GET_ELEMENT", "GET_ELEMENT__WRITE", "CREATE_EWPOOL", "REDUCE_EWPOOL", "CREATE_RWPOOL", "REDUCE_RWPOOL", + "CREATE_SWPOOL", "REDUCE_SWPOOL", "GET_METHOD_FRAME", "STORE_PARAM", - "CALL" + "CALL", + + // expression ops: unary + "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", + // expression ops: binary + "SUB", "ADD", "MUL", "DIV", "MOD", + "BIN_AND", "BIN_OR", "BIN_XOR", + "LOG_AND", "LOG_OR", "LOG_XOR", + "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE", + "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE" }; void dump(int level, const Array& ops) { @@ -51,9 +65,9 @@ void dump(int level, const Array& ops) { op.cast=ops.quick_get(i); fprintf(stderr, "%*s%s", level*4, "", opcode_name[op.code]); - if(op.code==OP_STRING) { - VString *vstring=static_cast(ops.quick_get(++i)); - fprintf(stderr, " \"%s\"", vstring->get_string()->cstr()); + if(op.code==OP_VALUE) { + Value *value=static_cast(ops.quick_get(++i)); + fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type()); } if(op.code==OP_CLASS) { VClass *vclass=static_cast(ops.quick_get(++i)); @@ -70,9 +84,9 @@ void dump(int level, const Array& ops) { void Request::execute(const Array& ops) { if(1) { - fputs("source----------------------------", stderr); + fputs("source----------------------------\n", stderr); dump(0, ops); - fputs("execution-------------------------", stderr); + fputs("execution-------------------------\n", stderr); } int size=ops.size(); @@ -83,12 +97,12 @@ void Request::execute(const Array& ops) fprintf(stderr, "%d:%s", stack.top(), opcode_name[op.code]); switch(op.code) { - // param in next instruction - case OP_STRING: + // param in next instruction + case OP_VALUE: { - VString *vstring=static_cast(ops.quick_get(++i)); - fprintf(stderr, " \"%s\"", vstring->get_string()->cstr()); - PUSH(vstring); + Value *value=static_cast(ops.quick_get(++i)); + fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type()); + PUSH(value); break; } case OP_CODE: @@ -96,6 +110,11 @@ void Request::execute(const Array& ops) const Array *local_ops=reinterpret_cast(ops.quick_get(++i)); fprintf(stderr, " (%d)\n", local_ops->size()); dump(1, *local_ops); + // TODO: rcontext junction должен быть контекстом вызываемого объекта + // чтобы вот эти конструкции орудовали одними полями внутри {} : + // $var{$field} + // ^var.menu{$field} + // поскольку они суть одно и то же, и так удобнее Junction& j=*NEW Junction(pool(), *self, 0, 0, root,rcontext,wcontext,local_ops); @@ -112,7 +131,7 @@ void Request::execute(const Array& ops) break; } - // OP_WITH + // OP_WITH case OP_WITH_SELF: { PUSH(self); @@ -134,7 +153,7 @@ void Request::execute(const Array& ops) break; } - // ... + // OTHER ACTIONS BUT WITHs case OP_CONSTRUCT: { Value *value=POP(); @@ -144,7 +163,6 @@ void Request::execute(const Array& ops) ncontext->put_element(name, value); break; } - // TODO: OP_EXPRESSION_EVAL, OP_MODIFY_EVAL, case OP_WRITE: { Value *value=POP(); @@ -193,14 +211,30 @@ void Request::execute(const Array& ops) case OP_REDUCE_RWPOOL: { String *string=wcontext->get_string(); - Value *value=NEW VString(*string); + Value *value=string?NEW VString(*string):NEW VString(pool()); wcontext=static_cast(POP()); rcontext=POP(); PUSH(value); break; } + case OP_CREATE_SWPOOL: + { + PUSH(wcontext); + wcontext=NEW WWrapper(pool(), 0 /* empty */, false /* not constructing */); + break; + } + case OP_REDUCE_SWPOOL: + { + // from "$a $b" part of expression taking only string value, + // ignoring any other content of wcontext + String *string=wcontext->get_string(); + Value *value=string?NEW VString(*string):NEW VString(pool()); + wcontext=static_cast(POP()); + PUSH(value); + break; + } - // CALL + // CALL case OP_GET_METHOD_FRAME: { Value *value=POP(); @@ -238,8 +272,7 @@ void Request::execute(const Array& ops) // constructing? if(wcontext->constructing()) { // yes // constructor call: $some(^class:method(..)) - self=NEW VObject(pool(), *called_class); - frame->write(self); + frame->write(self=NEW VObject(pool(), *called_class)); } else { // no // context is object or class & is it my class or my parent's class? VClass *read_class=rcontext->get_class(); @@ -268,6 +301,218 @@ void Request::execute(const Array& ops) break; } + // expression ops: unary + case OP_NEG: + { + Value *operand=POP(); + Value *value=NEW VDouble(pool(), -operand->get_double()); + PUSH(value); + break; + } + case OP_INV: + { + Value *operand=POP(); + Value *value=NEW VDouble(pool(), + ~static_cast(operand->get_double())); + PUSH(value); + break; + } + case OP_NOT: + { + Value *operand=POP(); + Value *value=NEW VBool(pool(), !operand->get_bool()); + PUSH(value); + break; + } + //todo: def in fexists + + // expression ops: binary + case OP_SUB: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + a->get_double() - + b->get_double()); + PUSH(value); + break; + } + case OP_ADD: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + a->get_double() + + b->get_double()); + PUSH(value); + break; + } + case OP_MUL: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + a->get_double() * + b->get_double()); + PUSH(value); + break; + } + case OP_DIV: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + a->get_double() / + b->get_double()); + PUSH(value); + break; + } + case OP_MOD: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + static_cast(a->get_double()) % + static_cast(b->get_double())); + PUSH(value); + break; + } + case OP_BIN_AND: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + static_cast(a->get_double()) & + static_cast(b->get_double())); + PUSH(value); + break; + } + case OP_BIN_OR: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + static_cast(a->get_double()) | + static_cast(b->get_double())); + PUSH(value); + break; + } + case OP_BIN_XOR: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VDouble(pool(), + static_cast(a->get_double()) ^ + static_cast(b->get_double())); + PUSH(value); + break; + } + case OP_LOG_AND: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_bool() && + b->get_bool()); + PUSH(value); + break; + } + case OP_LOG_OR: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_bool() || + b->get_bool()); + PUSH(value); + break; + } + case OP_LOG_XOR: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_bool() ^ + b->get_bool()); + PUSH(value); + break; + } + case OP_NUM_LT: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_double() < + b->get_double()); + PUSH(value); + break; + } + case OP_NUM_GT: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_double() > + b->get_double()); + PUSH(value); + break; + } + case OP_NUM_LE: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_double() <= + b->get_double()); + PUSH(value); + break; + } + case OP_NUM_GE: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_double() >= + b->get_double()); + PUSH(value); + break; + } + case OP_NUM_EQ: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_double() == + b->get_double()); + PUSH(value); + break; + } + case OP_NUM_NE: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + a->get_double() != + b->get_double()); + PUSH(value); + break; + } + case OP_STR_LT: + case OP_STR_GT: + case OP_STR_LE: + case OP_STR_GE: + case OP_STR_EQ: + case OP_STR_NE: + { + Value *b=POP(); + Value *a=POP(); + Value *value=NEW VBool(pool(), + /*a->get_string() @ + b->get_string()*/false); + PUSH(value); + break; + } + default: fprintf(stderr, "\tTODO"); break;