--- parser3/src/classes/Attic/root.C 2001/03/12 21:54:18 1.32 +++ parser3/src/classes/Attic/root.C 2001/03/13 11:15:03 1.35 @@ -3,7 +3,7 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: root.C,v 1.32 2001/03/12 21:54:18 paf Exp $ + $Id: root.C,v 1.35 2001/03/13 11:15:03 paf Exp $ */ #include @@ -14,18 +14,27 @@ #include "pa_vint.h" #include "pa_common.h" -static void _if(Request& r, const String&, Array *params) { - bool condition= - r.process( - *static_cast(params->get(0)), - 0/*no name*/, - false/*don't intercept string*/).get_bool(); +static void _if(Request& r, const String& method_name, Array *params) { + Value& condition_code=*static_cast(params->get(0)); + // forcing ^if(this param type) + r.fail_if_junction_(false, condition_code, + method_name, "condition must be junction"); + bool condition=r.process(condition_code, + 0/*no name*/, + false/*don't intercept string*/).get_bool(); + if(condition) { - Value& value=r.process(*static_cast(params->get(1))); - r.write_pass_lang(value); + Value& then_code=*static_cast(params->get(1)); + // forcing ^if(this param type) + r.fail_if_junction_(false, then_code, + method_name, "then-parameter must be junction"); + r.write_pass_lang(r.process(then_code)); } else if(params->size()==3) { - Value& value=r.process(*static_cast(params->get(2))); - r.write_pass_lang(value); + Value& else_code=*static_cast(params->get(2)); + // forcing ^if(this param type) + r.fail_if_junction_(false, else_code, + method_name, "else-parameter must be junction"); + r.write_pass_lang(r.process(else_code)); } } @@ -165,6 +174,27 @@ static void _for(Request& r, const Strin } } +static void _eval(Request& r, const String& method_name, Array *params) { + Value& expr=*static_cast(params->get(0)); + r.fail_if_junction_(false, expr, + method_name, "need expression"); + // evaluate expresion + Value *result=r.process(expr, + 0/*no name*/, + true/*don't intercept string*/).get_expr_result(); + if(params->size()==2) { + Value& fmt=*static_cast(params->get(1)); + // forcing ^format[this param type] + r.fail_if_junction_(true, fmt, + method_name, "fmt must not be junction"); + + Pool& pool=r.pool(); + String *string=new(pool) String(pool); + string->APPEND_CONST(format(pool, result->get_double(), fmt.as_string().cstr())); + result=new(pool) VString(*string); + } + r.wcontext->write(*result, String::Untaint_lang::NO /*always object, not string*/); +} typedef double (*math_one_double_op_func_ptr)(double); @@ -229,6 +259,10 @@ void initialize_root_class(Pool& pool, V // ^for[i;from-number;to-number-inclusive]{code}[delim] vclass.add_native_method("for", _for, 3+1, 3+1+1); + // ^eval(expr) + // ^eval(expr)[format] + vclass.add_native_method("eval", _eval, 1, 2); + // math functions