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