Annotation of parser3/src/classes/root.C, revision 1.31

1.1       paf         1: /*
1.10      paf         2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.12      paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.10      paf         5: 
1.31    ! paf         6:        $Id: root.C,v 1.30 2001/03/12 20:36:52 paf Exp $
1.1       paf         7: */
                      8: 
1.18      paf         9: #include <string.h>
1.29      paf        10: #include <math.h>
1.18      paf        11: 
1.1       paf        12: #include "pa_request.h"
1.8       paf        13: #include "_root.h"
1.31    ! paf        14: #include "pa_vint.h"
1.1       paf        15: 
1.18      paf        16: static void _if(Request& r, const String&, Array *params) {
1.2       paf        17:        bool condition=
1.18      paf        18:                r.process(
1.6       paf        19:                        *static_cast<Value *>(params->get(0)), 
1.11      paf        20:                        0/*no name*/,
1.14      paf        21:                        false/*don't intercept string*/).get_bool();
1.6       paf        22:        if(condition) {
1.18      paf        23:                Value& value=r.process(*static_cast<Value *>(params->get(1)));
1.22      paf        24:                r.write_pass_lang(value);
1.6       paf        25:        } else if(params->size()==3) {
1.18      paf        26:                Value& value=r.process(*static_cast<Value *>(params->get(2)));
1.22      paf        27:                r.write_pass_lang(value);
1.6       paf        28:        }
1.1       paf        29: }
                     30: 
1.22      paf        31: static void _untaint(Request& r, const String& method_name, Array *params) {
1.18      paf        32:        const String& lang_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
1.15      paf        33:        String::Untaint_lang lang=static_cast<String::Untaint_lang>(
1.23      paf        34:                untaint_lang_name2enum->get_int(lang_name));
1.15      paf        35:        if(!lang)
                     36:                R_THROW(0, 0,
                     37:                        &lang_name,
1.18      paf        38:                        "invalid untaint language");
1.15      paf        39: 
1.24      paf        40:        {
                     41:                Temp_lang temp_lang(r, lang);
                     42:                Value *vbody=static_cast<Value *>(params->get(1));
                     43:                // forcing ^untaint[]{this param type}
1.26      paf        44:                r.fail_if_junction_(false, *vbody, 
                     45:                        method_name, "body must be junction");
1.24      paf        46:                
                     47:                r.write_pass_lang(r.process(*vbody));
                     48:        }
1.15      paf        49: }
                     50:        
                     51: 
1.22      paf        52: static void _process(Request& r, const String& method_name, Array *params) {
1.20      paf        53:        // calculate pseudo file name of processed chars
                     54:        // would be something like "/some/file(4) process"
1.18      paf        55:        char place[MAX_STRING];
                     56: #ifndef NO_STRING_ORIGIN
1.25      paf        57:        const Origin& origin=method_name.origin();
1.18      paf        58:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                     59:                origin.file, 1+origin.line,
1.22      paf        60:                method_name.cstr());
1.18      paf        61: #else
1.22      paf        62:        strncpy(place, MAX_STRING, method_name.cstr());
1.18      paf        63: #endif 
                     64: 
1.20      paf        65:        VClass& self_class=*r.self->get_class();
1.22      paf        66:        {
                     67:                // temporary zero @main so to maybe-replace it in processed code
                     68:                Temp_method temp_method(self_class, *main_method_name, 0);
                     69:                
1.25      paf        70:                // evaluate source to process
                     71:                const String& source=
                     72:                        r.process(*static_cast<Value *>(params->get(0))).as_string();
                     73: 
1.22      paf        74:                // process source code, append processed methods to 'self' class
                     75:                // maybe-define new @main
                     76:                r.use_buf(source.cstr(), place, &self_class);
                     77:                
                     78:                // maybe-execute @main[]
                     79:                if(const Method *method=self_class.get_method(*main_method_name)) {
                     80:                        // execute!     
                     81:                        r.execute(*method->parser_code);
                     82:                }
1.18      paf        83:        }
                     84: }
                     85:        
1.26      paf        86: static void _rem(Request& r, const String& method_name, Array *params) {
1.27      paf        87:        // forcing ^rem{this param type}
1.26      paf        88:        r.fail_if_junction_(false, *static_cast<Value *>(params->get(0)), 
                     89:                method_name, "body must be junction");
                     90: }
1.18      paf        91: 
1.27      paf        92: static void _while(Request& r, const String& method_name, Array *params) {
                     93:        Value& vcondition=*static_cast<Value *>(params->get(0));
                     94:        // forcing ^while(this param type){}
                     95:        r.fail_if_junction_(false, vcondition, 
                     96:                method_name, "condition must be junction");
                     97:        
                     98:        Value& body=*static_cast<Value *>(params->get(1));
                     99:        // forcing ^while(){this param type}
                    100:        r.fail_if_junction_(false, body, 
                    101:                method_name, "body must be junction");
                    102: 
                    103:        // while...
                    104:        int endless_loop_count=0;
                    105:        while(true) {
                    106:                if(++endless_loop_count>=1973) // endless loop?
                    107:                        R_THROW(0, 0,
                    108:                                &method_name,
                    109:                                "endless loop detected");
                    110: 
                    111:                bool condition=
                    112:                        r.process(
                    113:                                vcondition, 
                    114:                                0/*no name*/,
                    115:                                false/*don't intercept string*/).get_bool();
                    116:                if(!condition) // ...condition is true
                    117:                        break;
                    118: 
                    119:                // write processed body
                    120:                r.write_pass_lang(r.process(body));
                    121:        }
                    122: }
                    123: 
1.28      paf       124: static void _use(Request& r, const String& method_name, Array *params) {
                    125:        Value& vfile=*static_cast<Value *>(params->get(0));
                    126:        // forcing ^rem{this param type}
                    127:        r.fail_if_junction_(true, vfile, 
                    128:                method_name, "file name must not be junction");
                    129: 
                    130:        char *file=vfile.as_string().cstr();
                    131:        r.use_file(r.absolute(file));
                    132: }
                    133: 
1.31    ! paf       134: static void _for(Request& r, const String& method_name, Array *params) {
        !           135:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
        !           136: 
        !           137:        Pool& pool=r.pool();
        !           138:        const String& var_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
        !           139:        int from=(int)r.process(*static_cast<Value *>(params->get(1))).get_double();
        !           140:        int to=(int)r.process(*static_cast<Value *>(params->get(2))).get_double();
        !           141:        Value& body_code=*static_cast<Value *>(params->get(3));
        !           142:        // forcing ^menu{this param type}
        !           143:        r.fail_if_junction_(false, body_code, 
        !           144:                method_name, "body must be junction");
        !           145:        Value *delim_code=params->size()==3+1+1?static_cast<Value *>(params->get(3+1)):0;
        !           146: 
        !           147:        bool need_delim=false;
        !           148:        int endless_loop_count=0;
        !           149:        for(VInt *vint=new(pool) VInt(pool, from); vint->get_int()<=to; vint->inc()) {
        !           150:                if(++endless_loop_count>=2001) // endless loop?
        !           151:                        R_THROW(0, 0,
        !           152:                                &method_name,
        !           153:                                "endless loop detected");
        !           154:                r.wcontext->put_element(var_name, vint);
        !           155: 
        !           156:                Value& processed_body=r.process(body_code);
        !           157:                if(delim_code) { // delimiter set?
        !           158:                        const String *string=processed_body.get_string();
        !           159:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
        !           160:                                r.write_pass_lang(r.process(*delim_code));
        !           161:                        need_delim=true;
        !           162:                }
        !           163:                r.write_pass_lang(processed_body);
        !           164:        }
        !           165: }
        !           166: 
        !           167: 
        !           168: 
1.29      paf       169: typedef double (*math_one_double_op_func_ptr)(double);
                    170: static double round(double op) { return floor(op+0.5); }
                    171: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
                    172: 
                    173: static void _math_one_double_op(
1.30      paf       174:                                                                Request& r, 
                    175:                                                                const String& method_name, Array *params,
                    176:                                                                math_one_double_op_func_ptr func) {
1.29      paf       177:        Pool& pool=r.pool();
                    178:        Value& param=*static_cast<Value *>(params->get(0));
                    179: 
                    180:        // forcing ^round(this param type)
                    181:        r.fail_if_junction_(false, param, 
                    182:                method_name, "parameter must be expression");
                    183: 
                    184:        Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).get_double()));
                    185:        r.wcontext->write(result, String::Untaint_lang::NO /*always object, not string*/);
                    186: }
                    187: 
                    188: static void _round(Request& r, const String& method_name, Array *params) {
                    189:        _math_one_double_op(r, method_name, params,     &round);
                    190: }
                    191: 
                    192: static void _floor(Request& r, const String& method_name, Array *params) {
                    193:        _math_one_double_op(r, method_name, params,     &floor);
                    194: }
                    195: 
                    196: static void _ceiling(Request& r, const String& method_name, Array *params) {
                    197:        _math_one_double_op(r, method_name, params,     &ceil);
                    198: }
                    199: 
                    200: static void _abs(Request& r, const String& method_name, Array *params) {
                    201:        _math_one_double_op(r, method_name, params,     &fabs);
                    202: }
                    203: 
                    204: static void _sign(Request& r, const String& method_name, Array *params) {
                    205:        _math_one_double_op(r, method_name, params,     &sign);
                    206: }
                    207: 
1.7       paf       208: void initialize_root_class(Pool& pool, VClass& vclass) {
1.15      paf       209:        // ^if(condition){code-when-true}
                    210:        // ^if(condition){code-when-true}{code-when-false}
                    211:        vclass.add_native_method("if", _if, 2, 3);
                    212: 
                    213:        // ^untaint[as-is|sql|js|html|html-typo]{code}
                    214:        vclass.add_native_method("untaint", _untaint, 2, 2);
1.18      paf       215: 
                    216:        // ^process[code]
                    217:        vclass.add_native_method("process", _process, 1, 1);
1.26      paf       218: 
                    219:        // ^rem{code}
                    220:        vclass.add_native_method("rem", _rem, 1, 1);
1.27      paf       221: 
                    222:        // ^while(condition){code}
                    223:        vclass.add_native_method("while", _while, 2, 2);
1.28      paf       224: 
                    225:        // ^use[file]
                    226:        vclass.add_native_method("use", _use, 1, 1);
1.29      paf       227: 
1.31    ! paf       228:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
        !           229:        vclass.add_native_method("for", _for, 3+1, 3+1+1);
        !           230: 
1.29      paf       231: 
                    232:        // math functions
                    233: 
                    234:        // ^round(expr) 
                    235:        vclass.add_native_method("round", _round, 1, 1);
                    236: 
                    237:        // ^floor(expr) 
                    238:        vclass.add_native_method("floor", _floor, 1, 1);
                    239: 
                    240:        // ^ceiling(expr)       
                    241:        vclass.add_native_method("ceiling", _ceiling, 1, 1);
                    242: 
                    243:        // ^abs(expr)   
                    244:        vclass.add_native_method("abs", _abs, 1, 1);
                    245: 
                    246:        // ^sign(expr)
                    247:        vclass.add_native_method("sign", _sign, 1, 1);
1.1       paf       248: }

E-mail: