Annotation of parser3/src/types/pa_vmframe.h, revision 1.8

1.1       paf         1: /*
                      2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.2       paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1       paf         5: 
1.8     ! paf         6:        $Id: pa_vmframe.h,v 1.7 2001/03/16 10:38:08 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #ifndef PA_VMFRAME_H
                     10: #define PA_VMFRAME_H
                     11: 
                     12: #include "pa_wcontext.h"
                     13: #include "pa_vunknown.h"
                     14: #include "pa_vjunction.h"
                     15: 
                     16: class VMethodFrame : public WContext {
                     17: public: // Value
                     18: 
                     19:        // all: for error reporting after fail(), etc
                     20:        const char *type() const { return "method_frame"; }
                     21:        // frame: my or self_transparent
                     22:        Value *get_element(const String& name) { 
                     23:                if(my) {
                     24:                        Value *result=static_cast<Value *>(my->get(name));
                     25:                        if(result)
                     26:                                return result;
                     27:                }
                     28:                return fself->get_element(name); 
                     29:        }
                     30:        // frame: my or self_transparent
                     31:        void put_element(const String& name, Value *value){ 
                     32:                if(!(my && my->put_replace(name, value)))
                     33:                        fself->put_element(name, value);
                     34:        }
                     35: 
                     36:        // frame: self_transparent
1.6       paf        37:        VStateless_class* get_class() { return fself->get_class(); }
1.1       paf        38: 
                     39:        // methodframe: self_transparent
                     40:        VAliased *get_aliased() { return fself->get_aliased(); }
                     41: 
1.8     ! paf        42: public: // wcontext
        !            43: 
        !            44:        Value *result() const {
        !            45:                // check the $result value
        !            46:                Value *result=my?static_cast<Value*>(my->get(*result_var_name)):0;
        !            47:                // if we have one, return it, else return as usual: accumulated fstring or fvalue
        !            48:                return result && result->get_defined()?result:WContext::result();
        !            49:        }
        !            50: 
1.1       paf        51: public: // usage
                     52: 
                     53:        VMethodFrame(Pool& apool, const Junction& ajunction/*info: always method-junction*/) : 
                     54:                WContext(apool, 0 /* empty */, false /* not constructing */),
                     55: 
                     56:                junction(ajunction),
                     57:                store_param_index(0),
                     58:                my(0), fnumbered_params(0),
                     59:                fself(0) {
                     60: 
1.5       paf        61:                const Method &method=*junction.method;
1.1       paf        62: 
                     63:                if(method.max_numbered_params_count) // are this method params numbered?
                     64:                        fnumbered_params=NEW Array(pool()); // create storage
1.8     ! paf        65:                else { // named params
1.1       paf        66:                        my=NEW Hash(pool()); // create storage
1.8     ! paf        67:                        
        !            68:                        if(method.locals_names) { // are there any local var names?
        !            69:                                // remember them
        !            70:                                // those are flags that name is local == to be looked up in 'my'
        !            71:                                for(int i=0; i<method.locals_names->size(); i++) {
        !            72:                                        // speedup: not checking for clash with "result" name
        !            73:                                        Value *value=NEW VUnknown(pool());
        !            74:                                        const String& name=*method.locals_names->get_string(i);
        !            75:                                        my->put(name, value);
        !            76:                                        value->set_name(name);
        !            77:                                }
        !            78:                        }
        !            79:                        { // always there is one local: $result
        !            80:                                Value *result_value=NEW VUnknown(pool());
        !            81:                                my->put(*result_var_name, result_value);
        !            82:                                result_value->set_name(*result_var_name);
1.1       paf        83:                        }
                     84:                }
                     85:        }
                     86: 
                     87:        void set_self(Value& aself) { fself=&aself; }
                     88: 
1.3       paf        89:        void store_param(const String& actual_method_name, Value *value) {
1.5       paf        90:                const Method& method=*junction.method;
1.1       paf        91:                int max_params=
                     92:                        method.max_numbered_params_count?method.max_numbered_params_count:
                     93:                        method.params_names?method.params_names->size():
                     94:                        0;
                     95:                if(store_param_index==max_params)
                     96:                        THROW(0,0,
1.3       paf        97:                                &actual_method_name,
                     98:                                "method of %s (%s) accepts maximum %d parameter(s)", 
                     99:                                        junction.self.name().cstr(),
1.1       paf       100:                                        junction.self.type(),
                    101:                                        max_params);
                    102:                
                    103:                if(method.max_numbered_params_count) { // are this method params numbered?
                    104:                        *fnumbered_params+=value;
                    105:                } else { // named param
1.8     ! paf       106:                        // speedup: not checking for clash with "result" name
1.7       paf       107:                        const String& name=*method.params_names->get_string(store_param_index);
1.1       paf       108:                        my->put(name, value); // remember param
                    109:                        value->set_name(name); // set param's 'name'
                    110:                }
1.7       paf       111:                store_param_index++;
1.1       paf       112:        }
                    113:        void fill_unspecified_params() {
1.5       paf       114:                const Method &method=*junction.method;
1.1       paf       115:                if(method.params_names) // there are any named parameters might need filling?
                    116:                        for(; store_param_index<method.params_names->size(); store_param_index++) {
                    117:                                Value *value=NEW VUnknown(pool());
1.4       paf       118:                                const String& name=*method.params_names->get_string(store_param_index);
1.1       paf       119:                                my->put(name, value);
                    120:                                value->set_name(name);
                    121:                        }
                    122:        }
                    123: 
                    124:        Array *numbered_params() { return fnumbered_params; }
                    125: 
                    126: public:
                    127:        
                    128:        const Junction& junction;
                    129: 
                    130: private:
                    131:        int store_param_index;
                    132:        Hash *my;/*OR*/Array *fnumbered_params;
                    133:        Value *fself;
                    134: 
                    135: };
                    136: 
                    137: #endif

E-mail: