Annotation of parser3/src/classes/root.C, revision 1.37
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.37 ! paf 6: $Id: root.C,v 1.36 2001/03/13 11:19:29 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");
1.36 paf 22:
1.34 paf 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;
1.37 ! paf 158: VInt *vint=new(pool) VInt(pool, 0);
1.31 paf 159: int endless_loop_count=0;
1.37 ! paf 160: for(int i=from; i<=to; i++) {
1.31 paf 161: if(++endless_loop_count>=2001) // endless loop?
162: R_THROW(0, 0,
163: &method_name,
164: "endless loop detected");
1.37 ! paf 165: vint->set_int(i);
1.31 paf 166: r.wcontext->put_element(var_name, vint);
167:
168: Value& processed_body=r.process(body_code);
169: if(delim_code) { // delimiter set?
170: const String *string=processed_body.get_string();
171: if(need_delim && string && string->size()) // need delim & iteration produced string?
172: r.write_pass_lang(r.process(*delim_code));
173: need_delim=true;
174: }
175: r.write_pass_lang(processed_body);
176: }
177: }
178:
1.33 paf 179: static void _eval(Request& r, const String& method_name, Array *params) {
180: Value& expr=*static_cast<Value *>(params->get(0));
181: r.fail_if_junction_(false, expr,
182: method_name, "need expression");
183: // evaluate expresion
184: Value *result=r.process(expr,
185: 0/*no name*/,
186: true/*don't intercept string*/).get_expr_result();
187: if(params->size()==2) {
188: Value& fmt=*static_cast<Value *>(params->get(1));
189: // forcing ^format[this param type]
190: r.fail_if_junction_(true, fmt,
191: method_name, "fmt must not be junction");
192:
193: Pool& pool=r.pool();
194: String *string=new(pool) String(pool);
195: string->APPEND_CONST(format(pool, result->get_double(), fmt.as_string().cstr()));
196: result=new(pool) VString(*string);
197: }
198: r.wcontext->write(*result, String::Untaint_lang::NO /*always object, not string*/);
199: }
1.31 paf 200:
201:
1.29 paf 202: typedef double (*math_one_double_op_func_ptr)(double);
203: static double round(double op) { return floor(op+0.5); }
204: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
205:
1.37 ! paf 206: static void double_one_op(
1.30 paf 207: Request& r,
208: const String& method_name, Array *params,
209: math_one_double_op_func_ptr func) {
1.29 paf 210: Pool& pool=r.pool();
211: Value& param=*static_cast<Value *>(params->get(0));
212:
213: // forcing ^round(this param type)
214: r.fail_if_junction_(false, param,
215: method_name, "parameter must be expression");
216:
217: Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).get_double()));
218: r.wcontext->write(result, String::Untaint_lang::NO /*always object, not string*/);
219: }
220:
221: static void _round(Request& r, const String& method_name, Array *params) {
1.37 ! paf 222: double_one_op(r, method_name, params, &round);
1.29 paf 223: }
224:
225: static void _floor(Request& r, const String& method_name, Array *params) {
1.37 ! paf 226: double_one_op(r, method_name, params, &floor);
1.29 paf 227: }
228:
229: static void _ceiling(Request& r, const String& method_name, Array *params) {
1.37 ! paf 230: double_one_op(r, method_name, params, &ceil);
1.29 paf 231: }
232:
233: static void _abs(Request& r, const String& method_name, Array *params) {
1.37 ! paf 234: double_one_op(r, method_name, params, &fabs);
1.29 paf 235: }
236:
237: static void _sign(Request& r, const String& method_name, Array *params) {
1.37 ! paf 238: double_one_op(r, method_name, params, &sign);
1.29 paf 239: }
240:
1.7 paf 241: void initialize_root_class(Pool& pool, VClass& vclass) {
1.15 paf 242: // ^if(condition){code-when-true}
243: // ^if(condition){code-when-true}{code-when-false}
244: vclass.add_native_method("if", _if, 2, 3);
245:
246: // ^untaint[as-is|sql|js|html|html-typo]{code}
247: vclass.add_native_method("untaint", _untaint, 2, 2);
1.18 paf 248:
249: // ^process[code]
250: vclass.add_native_method("process", _process, 1, 1);
1.26 paf 251:
252: // ^rem{code}
253: vclass.add_native_method("rem", _rem, 1, 1);
1.27 paf 254:
255: // ^while(condition){code}
256: vclass.add_native_method("while", _while, 2, 2);
1.28 paf 257:
258: // ^use[file]
259: vclass.add_native_method("use", _use, 1, 1);
1.29 paf 260:
1.31 paf 261: // ^for[i;from-number;to-number-inclusive]{code}[delim]
262: vclass.add_native_method("for", _for, 3+1, 3+1+1);
1.33 paf 263:
264: // ^eval(expr)
265: // ^eval(expr)[format]
266: vclass.add_native_method("eval", _eval, 1, 2);
1.31 paf 267:
1.29 paf 268:
269: // math functions
270:
271: // ^round(expr)
272: vclass.add_native_method("round", _round, 1, 1);
273:
274: // ^floor(expr)
275: vclass.add_native_method("floor", _floor, 1, 1);
276:
277: // ^ceiling(expr)
278: vclass.add_native_method("ceiling", _ceiling, 1, 1);
279:
280: // ^abs(expr)
281: vclass.add_native_method("abs", _abs, 1, 1);
282:
283: // ^sign(expr)
284: vclass.add_native_method("sign", _sign, 1, 1);
1.1 paf 285: }
E-mail: