Annotation of parser3/src/types/pa_value.h, revision 1.15

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.15    ! paf         6:        $Id: pa_value.h,v 1.14 2001/03/16 08:31:24 paf Exp $
1.1       paf         7: */
                      8: 
                      9: /*
                     10:        data core
                     11: */
                     12: 
                     13: #ifndef PA_VALUE_H
                     14: #define PA_VALUE_H
                     15: 
                     16: #include "pa_pool.h"
                     17: #include "pa_string.h"
                     18: #include "pa_array.h"
                     19: #include "pa_exception.h"
1.13      paf        20: #include "pa_globals.h"
1.1       paf        21: 
                     22: class Value;
1.9       paf        23: class VStateless_class;
1.1       paf        24: class Junction;
                     25: class WContext;
                     26: class VAliased;
                     27: class Request;
1.7       paf        28: class VTable;
1.1       paf        29: 
1.5       paf        30: typedef void (*Native_code_ptr)(Request& request, const String& method_name, Array *params);
1.1       paf        31: 
                     32: class Method : public Pooled {
                     33: public:
                     34:        const String& name;
                     35:        // either numbered params // for native-code methods = operators
                     36:        int min_numbered_params_count, max_numbered_params_count;
                     37:        // or named params&locals // for parser-code methods
                     38:        Array *params_names;  Array *locals_names;
                     39:        // the Code
                     40:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
                     41: 
                     42:        Method(
                     43:                Pool& apool,
                     44:                const String& aname,
                     45:                int amin_numbered_params_count, int amax_numbered_params_count,
                     46:                Array *aparams_names, Array *alocals_names,
                     47:                const Array *aparser_code, Native_code_ptr anative_code) : 
                     48: 
                     49:                Pooled(apool),
                     50:                name(aname),
                     51:                min_numbered_params_count(amin_numbered_params_count),
                     52:                max_numbered_params_count(amax_numbered_params_count),
                     53:                params_names(aparams_names), locals_names(alocals_names),
                     54:                parser_code(aparser_code), native_code(anative_code) {
                     55:        }
                     56: 
1.4       paf        57:        void check_actual_numbered_params(
1.8       paf        58:                const String& actual_name, Array *actual_numbered_params) const {
1.1       paf        59:                int actual_count=actual_numbered_params?actual_numbered_params->size():0;
                     60:                if(actual_count<min_numbered_params_count) // not proper count? bark
                     61:                        THROW(0, 0,
1.4       paf        62:                                &actual_name,
1.7       paf        63:                                "native method accepts minimum %d parameter(s)", 
1.1       paf        64:                                        min_numbered_params_count);
                     65: 
                     66:        }
                     67: };
                     68: 
                     69: class Junction : public Pooled {
                     70: public:
                     71: 
                     72:        Junction(Pool& apool,
                     73:                Value& aself,
1.9       paf        74:                VStateless_class *avclass, const Method *amethod,
1.1       paf        75:                Value *aroot,
                     76:                Value *arcontext,
                     77:                WContext *awcontext,
                     78:                const Array *acode) : Pooled(apool),
                     79:                
                     80:                self(aself),
                     81:                vclass(avclass), method(amethod),
                     82:                root(aroot),
                     83:                rcontext(arcontext),
                     84:                wcontext(awcontext),
                     85:                code(acode) {
                     86:        }
                     87: 
                     88:        // always present
                     89:        Value& self;
                     90:        // either these // so called 'method-junction'
1.9       paf        91:        VStateless_class *vclass;  const Method *method;
1.1       paf        92:        // or these are present // so called 'code-junction'
                     93:        Value *root;
                     94:        Value *rcontext;
                     95:        WContext *wcontext;
                     96:        const Array *code;
                     97: };
                     98: 
                     99: class Value : public Pooled {
                    100: public: // Value
                    101: 
                    102:        // all: for error reporting after fail(), etc
                    103:        virtual const char *type() const =0;
                    104:        const String& name() const { return *fname; }
                    105: 
                    106:        // unknown: false
                    107:        // others: true
                    108:        virtual bool get_defined() { return true; }
1.15    ! paf       109:        // string: fstring as VDouble
1.1       paf       110:        // bool: this
                    111:        // double: this
                    112:        // int: this
1.7       paf       113:        virtual Value *get_expr_result() { bark("(%s) can not be used in expression"); return 0; }
1.1       paf       114: 
                    115:        // string: value
                    116:        // unknown: ""
                    117:        // double: value
                    118:        // bool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
                    119:        // others: 0
                    120:        virtual const String *get_string() { return 0; }
                    121:        
                    122:        // string: value
                    123:        // double: value
                    124:        // integer: finteger
                    125:        // bool: value
1.7       paf       126:        virtual double get_double() { bark("(%s) does not have numerical value"); return 0; }
1.1       paf       127: 
                    128:        // unknown: false
                    129:        // bool: value
1.15    ! paf       130:        // integer: 0 or !0
1.1       paf       131:        // double: 0 or !0
1.15    ! paf       132:        virtual bool get_bool() { bark("(%s) does not have logical value"); return 0; }
1.1       paf       133: 
1.7       paf       134:        // junction: itself
1.1       paf       135:        virtual Junction *get_junction() { return 0; }
                    136: 
1.6       paf       137:        // table: itself
1.7       paf       138:        virtual VTable *get_vtable() { return 0; }
1.6       paf       139: 
1.1       paf       140:        // hash: (key)=value
                    141:        // object_class: (field)=STATIC.value;(STATIC)=hash;(method)=method_ref with self=object_class
1.3       paf       142:        // object_base: (CLASS)=vclass;(BASE)=base;(method)=method_ref
1.1       paf       143:        // object_instance: (field)=value;(CLASS)=vclass;(method)=method_ref
                    144:        // operator_class: (field)=value - static values only
                    145:        // codeframe: wcontext_transparent
                    146:        // methodframe: my or self_transparent
1.6       paf       147:        // table: column
1.9       paf       148:        // env: CLASS,BASE,method,field
1.11      paf       149:        // form: CLASS,BASE,method,field
1.14      paf       150:        // string: $CLASS,$BASE,$method
1.7       paf       151:        virtual Value *get_element(const String& name) { bark("(%s) does not have elements"); return 0; }
1.1       paf       152:        
                    153:        // hash: (key)=value
                    154:        // object_class, operator_class: (field)=value - static values only
                    155:        // object_instance: (field)=value
                    156:        // codeframe: wcontext_transparent
                    157:        // methodframe: my or self_transparent
1.7       paf       158:        virtual void put_element(const String& name, Value *value) { bark("(%s) does not accept elements"); }
1.1       paf       159: 
                    160:        // object_class, object_instance: object_class
                    161:        // wcontext: none yet | transparent
1.12      paf       162:        // form: this
                    163:        // class: this
                    164:        // env: this
1.9       paf       165:        virtual VStateless_class *get_class() { return 0; }
1.1       paf       166: 
                    167:        // valiased: this
                    168:        // wcontext: transparent
                    169:        // methodframe: self_transparent
                    170:        virtual VAliased *get_aliased() { return 0; }
                    171: 
                    172: public: // usage
                    173: 
                    174:        Value(Pool& apool) : Pooled(apool), fname(unnamed_name) {
                    175:        }
                    176: 
                    177:        void set_name(const String& aname) { fname=&aname; }
                    178: 
                    179:        const String& as_string() {
                    180:                const String *result=get_string(); 
                    181:                if(!result)
1.7       paf       182:                        bark("(%s) not a string");
1.6       paf       183: 
                    184:                return *result;
                    185:        }
                    186: 
1.7       paf       187:        VTable& as_vtable() {
                    188:                VTable *result=get_vtable(); 
1.6       paf       189:                if(!result)
1.7       paf       190:                        bark("(%s) not a table object");
1.6       paf       191: 
1.1       paf       192:                return *result;
                    193:        }
                    194: 
                    195: private:
                    196: 
                    197:        const String *fname;
                    198: 
1.6       paf       199: protected: 
1.1       paf       200: 
1.7       paf       201:        void bark(char *action) const {
1.1       paf       202:                THROW(0,0,
                    203:                        &name(),
                    204:                        action, type());
                    205:        }
                    206: 
                    207: };
                    208: 
                    209: #endif

E-mail: