Пример #1
0
// expand an unexpanded (quoted) macro
void EvalExpand(const void *data, qCtx *ctx, qStr *out, qArgAry *args) 
{
	VALID_ARGC("expand", 0, 1);
	CStr val = (*args)[0];
	if (val.Length() > 0) {
		if (val.Length() > 4 && 
			val.Data()[0] == CMAGIC_V1[0] && val.Data()[1] == CMAGIC_V1[1] && 
			val.Data()[2] == CMAGIC_V1[2] && val.Data()[3] == CMAGIC_V1[3]) {
			qStrReadBuf tmp(val.Data()+4,val.Length()-4);
			RunCompiled(ctx, &tmp, out);
		} else {
			ctx->Parse(val, out);
		}
	}
}
Пример #2
0
bool qCtx::ParseMagic(qStr *in, qStr *out, bool bProcessUnhandled)
{
    CStr tmp;
    char c;

    c = in->GetC();

    if ( c != EOF ) {
        if (c == S_MAGIC1[0]) {
            tmp << c;
            int i = 0;
            while ( (S_MAGIC1[++i]) && (c = in->GetC()) != EOF ) {
                tmp << c;
                if (c != (S_MAGIC1[i]) ) {
                    break;
                }
            }
            if (c != EOF && (!S_MAGIC1[i])) {
                ParseTry(in, out);
                return true;
            }
            else {
                if (bProcessUnhandled)
                    out->PutS(tmp);
                return false;
            }
        } else if (c == S_MAGIC2[0]) {
            tmp << c;
            int i = 0;
            while ( (S_MAGIC2[++i]) && (c = in->GetC()) != EOF ) {
                tmp << c;
                if (c != (S_MAGIC2[i]) ) {
                    break;
                }
            }
            if (c != EOF && (!S_MAGIC2[i])) {
                ParseTry(in, out);
                return true;
            } else {
                if (bProcessUnhandled)
                    out->PutS(tmp);
                return false;
            }
        } else if (c == CMAGIC_V1[0]) {
            tmp << c;
            int i = 0;
            while ( (CMAGIC_V1[++i]) && (c = in->GetC()) != EOF ) {
                tmp << c;
                if (c != (CMAGIC_V1[i]) ) {
                    break;
                }
            }
            if (c != EOF && (!CMAGIC_V1[i])) {
                RunCompiled(this, in, out);
                return true;
            } else {
                if (bProcessUnhandled)
                    out->PutS(tmp);
                return false;
            }
        } else {
            if (bProcessUnhandled) {
                out->PutC(c);
                out->PutS(*in);
            }
            return false;
        }
    } else {
        if (bProcessUnhandled)
            out->PutS(tmp);
        return false;
    }
}
Пример #3
0
void qObjDef::Eval(qCtx *ctx, qStr *out, qArgAry *args)
{
	AddRef();

	try {
		qCtxTmp tmpCtx(ctx);
		int i, an = myArgNames.Count();
		int mapcnt = min(an,args->Count());
		
		for (i = 0; i < mapcnt; ++i) {
			if (myArgNames[i].Length() > 0) {
				CStr s = args->GetAt(i);
				CStr n = myArgNames[i]; //n.Change();
				if (!n.IsEmpty()) {
				if (myQuoted[i] == dParsed ) {
					qObjParsed *p = new qObjParsed(s);
					tmpCtx.MapObj(p, n);
				} else if (myQuoted[i] == dQuoted) {
					tmpCtx.MapObj(s, n);
					args->SetAt(i, s);
				} else if (myQuoted[i] == dObjRef) {
					qObj *obj;
					args->SetAt(i,s);
					if (ctx->Find(&obj, s)) {
						tmpCtx.MapObj(new qObjByRef(obj, false), n);
					}
				} else {
					args->SetAt(i, s);
					tmpCtx.MapObj(s, n);
				}} else {
					args->SetAt(i, s);
				}
			}
		}

		for (; i < an; ++i) {
			if (myArgNames[i].Length() > 0) {
				CStr n = myArgNames[i]; n.Change();
				if (!n.IsEmpty()) 
					tmpCtx.MapObj(CStr::Null, n);
			}
		}

		for (; i < args->Count(); ++i) {
			args->SetAt(i,(*args)[i]);
		}


		qObjDefArgs *inst = new qObjDefArgs(this, args);

		tmpCtx.MapObj(inst, "arg");
		tmpCtx.MapObj(args->Count(), "num-args");
		tmpCtx.MapObj(args->Count(), "argc");

		// *** comp
		//tmpCtx.Parse(&qStrReadBuf(myBody), out);
		qStrReadBuf rTmp(myBody);
		RunCompiled(&tmpCtx, &rTmp, out);
	} catch (qCtxExAbort ex) {
		throw ex;
	} catch (qCtxEx ex) {
		throw ex;
	} catch (...) {
		ctx->Throw(out, 999, "Core Unhandled exception");
	}

	Free();
}
Пример #4
0
void RunCompiled(qCtx *ctx, qStr *in, qStr *out)
{
	int i, j;
	char *p; 

	C_BASE b;
	while (in->GetS((char *) &b, sizeof(b)) == sizeof(b) && b.ln > 0) {
		CStr dt(b.ln);
		if (in->GetS(dt.GetBuffer(), b.ln) == b.ln) {
			if (b.bc == BC_FUNC) {

				p = dt.GetBuffer();
				if (b.rn) {
					for (j = 0; j < b.ln; ++j)
						p[j] = p[j] ^ b.rn;
				}
				qObj *obj;
				if (ctx->Find(&obj, (const CStr &) dt)) {
					C_ARGS v;
					C_ARG a;
					CStr cur;
					qArgAry ary;
					char qmode;
					if (in->GetS((char *) &v, sizeof(v)) == sizeof(v)) {
						char *map = obj->GetQmap();
						if (!map || *map == 'A') {
							qmode = !map ? '0' : '1';
							for (i = 0; i < v.cnt; ++i) {
{
	if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) {
		cur.Grow(a.ln);
		if (in->GetS(cur.GetBuffer(), a.ln) == (int) a.ln) {
			p = cur.GetBuffer();
			if (b.rn) {
				for (j = 0; j < (int) a.ln; ++j)
					p[j] = p[j] ^ b.rn;
			}
			ary.Add(cur);
			ary.SetQuot(i, a.at == ARG_QSTR);
			if (!ary.GetQuot(i) && qmode == '0') {
        qStrBuf tmp;
				if (a.at == ARG_CMP) {
          qStrReadBuf rTmp(ary[i]);
					RunCompiled(ctx, &rTmp, &tmp);
				} else {
          qStrReadBuf rTmp(ary[i]);
					ctx->Parse(&rTmp, &tmp);
				}
				ary[i] = tmp;
			} else {
				if (a.at == ARG_CMP) {
					if (qmode != '2') {
            qStrBuf tmp;
            qStrReadBuf rTmp(ary[i]);
						Decompile(&rTmp, &tmp);
						ary[i] = tmp;
					} else {
						ary.SetQuot(i, ARG_CMP);
					}
				}
			}
		}
	}
}
							}
						} else {
							qmode = (*map == '1' ? '1' : '0');
							++map;
							for (i = 0; i < v.cnt; ++i) {
{
	if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) {
		cur.Grow(a.ln);
		if (in->GetS(cur.GetBuffer(), a.ln) == (int) a.ln) {
			p = cur.GetBuffer();
			if (b.rn) {
				for (j = 0; j < (int) a.ln; ++j)
					p[j] = p[j] ^ b.rn;
			}
			ary.Add(cur);
			ary.SetQuot(i, a.at == ARG_QSTR);
			if (!ary.GetQuot(i) && qmode == '0') {
        qStrBuf tmp;
				if (a.at == ARG_CMP) {
          qStrReadBuf rTmp(ary[i]);
					RunCompiled(ctx, &rTmp, &tmp);
				} else {
          qStrReadBuf rTmp(ary[i]);
					ctx->Parse(&rTmp, &tmp);
				}
				ary[i] = tmp;
			} else {
				if (a.at == ARG_CMP) {
					if (qmode != '2') {
            qStrBuf tmp;
            qStrReadBuf rTmp(ary[i]);
						Decompile(&rTmp, &tmp);
						ary[i] = tmp;
					} else {
						ary.SetQuot(i, ARG_CMP);
					}
				}
			}
		}
	}
}
								if (*map) {
									if (*map != 'A') {
										qmode = *map;
										++map;
									}
								} else {
									qmode = '0';
								}
							}
						}
						obj->Eval(ctx, out, v.cnt ? &ary : NULL);
					}
				} else {
					if (ctx->GetStrict()) {
						ctx->ThrowF(out, 98, "Function '%s' was not found.", (const char *) dt);
					} else {
						C_ARGS v;
						C_ARG a;
						CStr cur;
						if (in->GetS((char *) &v, sizeof(v)) == sizeof(v)) {
							for (i = 0; i < v.cnt; ++i) {
								if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) {
									cur.Grow(a.ln);
									if (in->GetS(cur.GetBuffer(), a.ln) != (int) a.ln) {
										break;
									}
								}
							}
						}
						out->PutC('%');
						out->PutS(dt);
						if (v.cnt > 0) {
							out->PutC(T_LP);
							out->PutS("...");
							out->PutC(T_RP);
						} else {
							out->PutC('%');
						}
					}
				}
			} else if (b.bc == BC_OUT) {
				out->PutS(dt, b.ln);
			}
		}
	}
}