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

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

E-mail: