Annotation of parser3/src/classes/math.C, revision 1.1

1.1     ! parser      1: /** @file
        !             2:        Parser: @b math parser class.
        !             3: 
        !             4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
        !             5: 
        !             6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
        !             7: */
        !             8: static const char *RCSId="$Id: math.C,v 1.14 2001/06/28 07:44:17 parser Exp $"; 
        !             9: 
        !            10: #include "pa_config_includes.h"
        !            11: #include "pa_common.h"
        !            12: #include "pa_vint.h"
        !            13: #include "pa_request.h"
        !            14: 
        !            15: // defines
        !            16: 
        !            17: #define MATH_CLASS_NAME "math"
        !            18: 
        !            19: // class
        !            20: 
        !            21: class MMath : public Methoded {
        !            22: public:
        !            23:        MMath(Pool& pool);
        !            24: public: // Methoded
        !            25:        bool used_directly() { return true; }
        !            26: };
        !            27: 
        !            28: // methods
        !            29: 
        !            30: static void _random(Request& r, const String& method_name, MethodParams *params) {
        !            31:        Pool& pool=r.pool();
        !            32: 
        !            33:        Value& range=params->get_junction(0, "range must be expression");
        !            34:     uint max=(uint)r.process(range).as_double();
        !            35:     if(max<=1)
        !            36:                PTHROW(0, 0,
        !            37:                        &method_name,
        !            38:                        "bad range [0..%u]", max);
        !            39:        
        !            40:        r.write_no_lang(*new(pool) VInt(pool, rand()%max));
        !            41: }
        !            42: 
        !            43: 
        !            44: typedef double (*math1_func_ptr)(double);
        !            45: static double round(double param) { return floor(param+0.5); }
        !            46: static double sign(double param) { return param > 0 ? 1 : ( param < 0 ? -1 : 0 ); }
        !            47: 
        !            48: 
        !            49: static void math1(Request& r, 
        !            50:                                  const String& method_name, MethodParams *params,
        !            51:                                  math1_func_ptr func) {
        !            52:        Pool& pool=r.pool();
        !            53:        Value& param=params->get_junction(0, "parameter must be expression");
        !            54: 
        !            55:        Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).as_double()));
        !            56:        result.set_name(method_name);
        !            57:        r.write_no_lang(result);
        !            58: }
        !            59: 
        !            60: #define MATH1(name) \
        !            61:        static void _##name(Request& r, const String& method_name, MethodParams *params) {\
        !            62:                math1(r, method_name, params, &name);\
        !            63:        }
        !            64: #define MATH1P(name_parser, name_c) \
        !            65:        static void _##name_parser(Request& r, const String& method_name, MethodParams *params) {\
        !            66:                math1(r, method_name, params, &name_c);\
        !            67:        }
        !            68: MATH1(round);  MATH1(floor);   MATH1P(ceiling, ceil);
        !            69: MATH1P(abs, fabs);     MATH1(sign);
        !            70: MATH1(exp);    MATH1(log);     
        !            71: MATH1(sin);    MATH1(asin);    
        !            72: MATH1(cos);    MATH1(acos);    
        !            73: MATH1(tan);    MATH1(atan);
        !            74: MATH1(sqrt);
        !            75: 
        !            76: 
        !            77: typedef double (*math2_func_ptr)(double, double);
        !            78: static void math2(Request& r, 
        !            79:                                  const String& method_name, MethodParams *params,
        !            80:                                  math2_func_ptr func) {
        !            81:        Pool& pool=r.pool();
        !            82:        Value& a=params->get_junction(0, "parameter must be expression");
        !            83:        Value& b=params->get_junction(1, "parameter must be expression");
        !            84: 
        !            85:        Value& result=*new(pool) VDouble(pool, (*func)(
        !            86:                r.process(a).as_double(),
        !            87:                r.process(b).as_double()));
        !            88:        result.set_name(method_name);
        !            89:        r.write_no_lang(result);
        !            90: }
        !            91: 
        !            92: #define MATH2(name) \
        !            93:        static void _##name(Request& r, const String& method_name, MethodParams *params) {\
        !            94:                math2(r, method_name, params, &name);\
        !            95:        }
        !            96: MATH2(pow);
        !            97: 
        !            98: // constructor
        !            99: 
        !           100: MMath::MMath(Pool& apool) : Methoded(apool) {
        !           101:        set_name(*NEW String(pool(), MATH_CLASS_NAME));
        !           102: 
        !           103: 
        !           104:        // setting seed
        !           105:        srand(getpid()+time(NULL));  rand();
        !           106:        
        !           107:        // ^FUNC(expr)  
        !           108: #define ADD1(name) \
        !           109:        add_native_method(#name, Method::CT_STATIC, _##name, 1, 1)
        !           110: 
        !           111:        ADD1(random);
        !           112:        ADD1(round);    ADD1(floor);    ADD1(ceiling);
        !           113:        ADD1(abs);      ADD1(sign);
        !           114:        ADD1(exp);      ADD1(log);      
        !           115:        ADD1(sin);      ADD1(asin);     
        !           116:        ADD1(cos);      ADD1(acos);     
        !           117:        ADD1(tan);      ADD1(atan);
        !           118:        ADD1(sqrt);
        !           119: 
        !           120: #define ADD2(name) \
        !           121:        add_native_method(#name, Method::CT_STATIC, _##name, 2, 2)
        !           122: 
        !           123:        // ^pow(x;y)
        !           124:        ADD2(pow);
        !           125: 
        !           126: }
        !           127: 
        !           128: // creator
        !           129: 
        !           130: Methoded *MMath_create(Pool& pool) {
        !           131:        return new(pool) MMath(pool);
        !           132: }

E-mail: