Annotation of parser3/src/main/core.C, revision 1.15

1.15    ! paf         1: // TODO: $RESULT
        !             2: 
1.10      paf         3: enum Prefix {
                      4:        NO_PREFIX,
                      5:        ROOT_PREFIX,
                      6:        SELF_PREFIX
                      7: };
                      8: 
                      9: enum CHAR_TYPE {
                     10:        NO_TYPE=0, // these got skipped by String::skip_to
1.11      paf        11:        VAR_START_TYPE,
                     12:        METHOD_START_TYPE,
                     13:        STOP_TYPE,///=-1 // same as EOF type(-1), see String::skip_to
                     14:        DOT_TYPE,
                     15:        CONSTRUCTOR_BODY_START_TYPE,
                     16:        CONSTRUCTOR_BODY_FINISH_TYPE,
                     17:        BLOCK_START_TYPE,
                     18:        BLOCK_FINISH_TYPE,
                     19:        
                     20:        Z_TYPE
1.10      paf        21: };
                     22: 
1.11      paf        23: Char_types var_or_method_start;
                     24: Char_types var_or_method_start_or_constructor_stop;
                     25: Char_types var_or_method_start_or_block_stop;
                     26: Char_types common_names_breaks, var_names_breaks, method_names_breaks;
1.12      paf        27: String SELF;
1.15    ! paf        28: String RESULT;
1.10      paf        29: 
                     30: void prepare() {
1.11      paf        31:        var_or_method_start.set('$', VAR_START_TYPE);
                     32:        var_or_method_start.set('^', METHOD_START_TYPE);
                     33: 
                     34:        var_or_method_start_or_constructor_stop=var_or_method_start;
                     35:        var_or_method_start_or_constructor_stop.set(')', STOP_TYPE);
                     36: 
                     37:        var_or_method_start_or_block_stop=var_or_method_start;
                     38:        var_or_method_start_or_block_stop.set(']', STOP_TYPE);
                     39: 
                     40:        common_names_breaks.set(0, ' ', STOP_TYPE);
                     41:        common_names_breaks.set('.', DOT_TYPE);
                     42:        common_names_breaks.set(')', STOP_TYPE); // var_or_method_start_or_constructor_stop
                     43:        common_names_breaks.set(']', STOP_TYPE); // var_or_method_start_or_block_stop
                     44: 
                     45:        var_names_breaks=common_names_breaks;
                     46:        var_names_breaks.set('(', CONSTRUCTOR_BODY_START_TYPE);
                     47: 
                     48:        method_names_breaks=common_names_breaks;
                     49:        method_names_breaks.set('[', BLOCK_START_TYPE);
1.12      paf        50: 
                     51:        SELF.APPEND("self", 0, 0);
1.15    ! paf        52:        RESULT.APPEND("result", 0, 0);
1.10      paf        53: }
                     54: 
1.12      paf        55: CHAR_TYPE process(method_self_n_params_n_locals& root, Value& self,
                     56:                                  arcontext& arcontext, WContext& awcontext, 
                     57:                                  String_iterator& iter, Char_types& breaks) {
1.10      paf        58:        while(!iter.eof()) {
                     59:                String_iterator start(iter);
                     60:                CHAR_TYPE type=iter.skip_to(breaks);
                     61:                awcontext.write(start, iter);
                     62: 
                     63:                switch(type) {
1.11      paf        64:                case VAR_START_TYPE: 
                     65:                        process_var(root, self, arcontext, awcontext, iter, breaks);
1.10      paf        66:                        break;
1.11      paf        67:                case METHOD_START_TYPE:
                     68:                        process_method(root, self, arcontext, awcontext, iter, breaks);
1.10      paf        69:                        break;
1.12      paf        70:                default:
                     71:                        return type;
1.10      paf        72:                }
                     73:        }
1.12      paf        74:        return -1;
1.1       paf        75: }
                     76: 
1.11      paf        77: void process_var(method_self_n_params_n_locals& root, Value& self,
                     78:                                 arcontext& arcontext, WContext& awcontext, 
                     79:                                 String_iterator& iter, Char_types& breaks) {
1.1       paf        80: 
                     81:        // $name.field.subfield -- read
                     82:        // $name.field.subfield(constructor code) -- construct
                     83:        // $name.field.subfield[usage code & if none existed autoconstructed as VHash] -- use OR auto-VHash construct
                     84:        
1.11      paf        85:        Prefix prefix;
1.1       paf        86:        Array/*<String&>*/ names(pool);  // what.they.refer.to left-to-right list
1.12      paf        87:        // the char type after long name
                     88:        CHAR_TYPE names_ended_before=get_names( 
1.11      paf        89:                iter, var_names_breaks,
1.14      paf        90:                &prefix, &names); // can return size()==0 when $self alone
1.1       paf        91: 
                     92:        bool read_mode=name_ended_before==' ';
1.6       paf        93:        Value *context=
                     94:                prefix?
1.8       paf        95:                        prefix==ROOT_PREFIX?root:self:
1.7       paf        96:                read_mode?arcontext:awcontext;
1.1       paf        97:        
                     98:        if(read_mode) {
1.3       paf        99:                // 'context' dive into dotted path
1.12      paf       100:                for(int i=0; i<names.size(); i++) {
1.1       paf       101:                        context=context->get_element(static_cast<Value *>(names.get[i]));
                    102:                        if(!context) // no such object field, nothing bad, just ignore that
                    103:                                return;
                    104:                }
1.7       paf       105:                awcontext.write(context);
1.1       paf       106:        } else { // write mode
                    107:                iter++; // skip '(' '['
                    108: 
                    109:                bool construct_mode=names_ended_before=='(';
                    110: 
1.4       paf       111:                // 'context' dive into dotted path, 
1.12      paf       112:                int steps=names.size();
1.4       paf       113:                // if constructing then "excluding last .name"
1.1       paf       114:                if(construct_mode)
1.6       paf       115:                        if(!steps--) // bad: "$self("; now we can safely do ".get[steps]" below
                    116:                                pool.exception().raise("self re-construction prohibited");
1.1       paf       117:                for(int i=0; i<steps; i++) {
                    118:                        String& name=static_cast<String&>(names.get[i]);
                    119:                        Value *next_current=context->get_element(name);
                    120:                        if(next_current)
                    121:                                next_current=context->put_element(name, new(pool) VHash(pool));
                    122:                        context=new_current;
                    123:                }
                    124: 
                    125:                if(construct_mode) {  
1.7       paf       126:                        // .name(construct-code), processing on arcontext in empty temp awcontext
                    127:                        // pure side effect, no awcontext.write here
1.3       paf       128:                        // last .name
1.15    ! paf       129:                        // prepare context
1.1       paf       130:                        String& name=static_cast<String&>(names.get[steps]);
                    131:                        WContext local_wcontext(pool /* empty */);
1.15    ! paf       132:                        // evaluate constructor-code in that context
1.7       paf       133:                        process(root, self, arcontext, local_wcontext, iter, ')');
1.15    ! paf       134:                        // store constructed value under 'name'
1.1       paf       135:                        context->put_element(name, local_wcontext.value());
1.15    ! paf       136:                } else { // =='['  .name[with-code]
        !           137:                        // prepare context
1.1       paf       138:                        WContext local_context(pool, context);
1.15    ! paf       139:                        // evaluate with-code in that context
1.6       paf       140:                        process(root, self, local_context, local_context, iter, ']');
1.15    ! paf       141:                        // emit result
1.7       paf       142:                        awcontext.write(local_context);
1.1       paf       143:                }
                    144:                
                    145:                iter++; // skip ')' ']'
                    146:        }
                    147: }
                    148: 
1.11      paf       149: void process_method(method_self_n_params_n_locals& root, Value& self,
                    150:                                        arcontext& arcontext, WContext& awcontext, 
                    151:                                        String_iterator& iter, char char_to_stop_before) {
1.1       paf       152:        
                    153:        // ^name.field.subfield.method[..] -- plain call
                    154:        // ^name.field.subfield.method_ref[..] -- method ref call, when .get_method()!=0
1.8       paf       155:        // ^class:method[..]  -- no dotted path allowed before/after
                    156:        //  1: wcontext.object_class == 0?  -- constructor
                    157:        //  2: wcontext.object_class.has_parent('class')? -- dynamic call
                    158:        //  3: not -------------------------------------? -- static call
1.1       paf       159:        
1.11      paf       160:        Prefix prefix;
1.1       paf       161:        Array/*<String&>*/ names(pool);  // what.they.refer.to left-to-right list
1.12      paf       162:        // the char type after long name
                    163:        CHAR_TYPE names_ended_before=get_names(
1.11      paf       164:                iter, method_names_breaks,
1.14      paf       165:                &prefix, &names); // can return size()==0 when ^self alone
1.1       paf       166: 
1.6       paf       167:        Value *context=
                    168:                prefix?
1.8       paf       169:                        prefix==ROOT_PREFIX?root:self:
1.7       paf       170:                arcontext;
1.1       paf       171:        iter++; // skip '['
                    172: 
1.3       paf       173:        // 'context' dive into dotted path, excluding last .name
1.12      paf       174:        int steps=names.size()-1;
1.6       paf       175:        if(steps<0) // bad: "^self["; now we can safely do ".get[steps]" below
                    176:                pool.exception().raise("call: calling method named 'self'");
1.1       paf       177:        for(int i=0; i<steps; i++) {
                    178:                String& name=static_cast<String&>(names.get[i]);
1.6       paf       179:                context=context->get_element(name);
1.1       paf       180:                if(!context) // no such object field, sad story: can't call method of void
                    181:                        pool.exception().raise(name, "call: to void.method");
                    182:        }
                    183:        
1.3       paf       184:        // last .name
1.1       paf       185:        String& name=static_cast<String&>(names.get[steps]);
1.8       paf       186:        if(steps==0) { // the sole name on path, maybe ^class:method[ call
                    187:                String_iterator ni(name);
                    188:                if(ni.skip_to(':')) { // it is
                    189:                        ni++; // skip ':'
                    190:                        String method_name(pool); method_name.append(ni, 0);
                    191:                        name=method_name; // trim "class:" prefix from the name
                    192: 
                    193:                        String cn(pool);  cn.append(0, ni);
                    194:                        Class *right_class=classes.get(cn);
1.9       paf       195:                        if(!right_class) // bad: no such class
1.8       paf       196:                                pool.exception().raise(cn, "call: undefined class");
                    197:                        Class *left_class=awcontext.get_class();
                    198:                        if(left_class) {
                    199:                                if(left_class.has_parent(right_class)) // dynamic call
1.10      paf       200:                                        context=awcontext.value(); // it's 'self' instance
1.8       paf       201:                                else // static call
1.9       paf       202:                                        context=right_class; // 'self' := class, not instance
1.8       paf       203:                        } else { // constructor: $some(^class:method[..]) call
1.10      paf       204:                                context=new(pool) VClass(pool, right_class); // 'self' := new instance of 'class:'
1.8       paf       205:                                awcontext.write(context);
                    206:                        }
                    207:                }
                    208:        }
1.1       paf       209:        // first we're trying to locate method with that 'name'
                    210:        Method *method=context.get_method(name);
                    211:        if(!method) { // no such method: try to locate method ref field
                    212:                Value *value=context.get_element(name);
1.6       paf       213:                if(value) { // good: we have some element of that 'name'
                    214:                        Method_ref *method_ref=value->get_method_ref();
                    215:                        if(!method_ref) // bad: that field wasn't method_ref
                    216:                                pool.exception().raise(name, "call: this field is not a method reference");
1.11      paf       217:                        context=method_ref->self;
1.6       paf       218:                        method=method_ref->method;
                    219:                } else { // no element of that 'name', that must be operator then
1.11      paf       220:                        Operator *op=operators.get(name);
                    221:                        if(!op) // bad: that 'name' is neither method nor field nor operator
1.6       paf       222:                                pool.exception().raise(name, "call: neither method nor field nor operator");
1.11      paf       223:                        context=op->self;
                    224:                        method=op;
1.6       paf       225:                }
1.1       paf       226:        }
                    227: 
                    228: 
1.14      paf       229:        // evaluate param values
1.10      paf       230:        Array/*<Value&>*/ param_values(pool);
1.1       paf       231:        get_params(
1.6       paf       232:                iter,
1.9       paf       233:                arcontext,
1.1       paf       234:                &param_values);
                    235:        iter++; // skip ']'
                    236: 
1.14      paf       237:        // prepare contexts
1.12      paf       238:        Method_self_n_params_n_locals local_rcontext(pool, 
1.6       paf       239:                context,
1.7       paf       240:                method->param_names, param_values,
                    241:                method->local_names);
1.9       paf       242:        WContext local_wcontext(pool, context);
1.10      paf       243:        String_iterator local_iter(method->code);
1.15    ! paf       244:        // call method/operator in those contexts
1.6       paf       245:        process(
1.9       paf       246:                local_rcontext/* $:vars */, context /* $self.vars */,
1.6       paf       247:                local_rcontext, local_wcontext, 
1.10      paf       248:                local_iter, 0);
1.15    ! paf       249:        // emit result
1.7       paf       250:        awcontext.write(local_wcontext);
1.10      paf       251: }
                    252: 
                    253: 
1.12      paf       254: CHAR_TYPE get_names(
1.11      paf       255:                String_iterator& iter, Char_types& breaks,
1.12      paf       256:                Prefix& prefix, Array& names) {
                    257:        // $name.subname white-space
                    258:        // $name.subname( $name.subname[
                    259:        // $name.subname) $name.subname]
                    260:        // $:name... $self...
                    261: 
1.13      paf       262:        if(iter.eof())
                    263:                return -1;
                    264: 
1.14      paf       265:        if(iter()==':') { // $:name ?
1.12      paf       266:                prefix=ROOT_PREFIX;
                    267:                iter++; // skip ':'
1.14      paf       268:        } else // $name
1.12      paf       269:                prefix=NO_PREFIX;
                    270: 
1.13      paf       271:        CHAR_TYPE result;
                    272:        while(true) {
1.14      paf       273:                // prepare context
1.12      paf       274:                WContext local_wcontext(pool /* empty */);
1.14      paf       275:                // execute code until separator, writing to that context
1.12      paf       276:                result=process(root, self,
                    277:                        arcontext, local_wcontext, 
                    278:                        iter, breaks);
1.14      paf       279:                // read resulting name
1.12      paf       280:                String *name=local_wcontext.get_string();
                    281:                if(*name==SELF) // is it "self"?
                    282:                        if(prefix || names.size()) // already $: or $self.  or $name.
                    283:                                pool.exception().raise("names: 'self' not first on chain");
                    284:                        else // $self.
                    285:                                prefix=SELF_PREFIX;
                    286:                else // simple $name or .name, emiting
                    287:                        names+=name;
                    288: 
                    289:                if(result!=DOT_TYPE) // not "name." ?
                    290:                        break;
                    291:        }
                    292: 
1.14      paf       293:        // can only return size()==0 when $self alone
1.12      paf       294:        if(names.size()==0 && prefix!=SELF_PREFIX)
                    295:                pool.exception().raise("names: empty chain");
1.10      paf       296: 
1.12      paf       297:        return result;
1.10      paf       298: }
                    299: 
                    300: void get_params(
                    301:                String_iterator& iter,
                    302:                Value *arcontext,
                    303:                Array/*<Values&>*/& param_values) {
                    304: }

E-mail: