From: Konrad Rosenbaum Date: Fri, 23 Mar 2012 09:00:39 +0000 (+0100) Subject: add reflection abilities X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=da8f6e919a76ecffcf5f0cb3008237a9135fb4f3;p=konrad%2Felam.git add reflection abilities --- diff --git a/doc/library.html b/doc/library.html index 740bf3f..46db21e 100644 --- a/doc/library.html +++ b/doc/library.html @@ -181,6 +181,18 @@ Examples include: "hello World!", 'hello World!', 'line\r\nbreak'.

Use the convenience type ELAM::StringEngine or its static method configureStringEngine to get this functionality. +

Reflection Library

+ +The reflection library defines some type inquiry functions: + + + + + +
FunctionDescription
typeid(any)returns the numeric type ID of the argument, see QVariant::type() for details
typename(any)returns the name of the type of the argument
showexception(expression, ...)takes any number of arguments and returns a string showing either the type of each expression or the details of the exception it threw

+ +Use the static method ELAM::Engine::configureReflection to get this functionality. +

Extending The ELAM Libray

diff --git a/src/elamengine.cpp b/src/elamengine.cpp index 824637a..569248e 100644 --- a/src/elamengine.cpp +++ b/src/elamengine.cpp @@ -438,5 +438,43 @@ QStringList Engine::variableNames() const return d->vars.keys(); } +static QVariant reflectTypeId(const QList&args,Engine&) +{ + if(args.size()!=1) + return QVariant::fromValue(Exception(Exception::ArgumentListError,"expecting one argument")); + return (int)args[0].type(); +} + +static QVariant reflectTypeName(const QList&args,Engine&) +{ + if(args.size()!=1) + return QVariant::fromValue(Exception(Exception::ArgumentListError,"expecting one argument")); + return QString(args[0].typeName()); +} + +static QVariant reflectException(const QList&args,Engine&) +{ + QString ret; + for(auto arg:args){ + if(ret.size())ret+="; "; + if(arg.type()==Exception::metaTypeId()){ + Exception e=arg.value(); + ret+=QString("Exception(type=%1, position=%2, message='%3')") + .arg(e.errorTypeName()) + .arg((QString)e.errorPos()) + .arg(e.errorText()); + } + } + return ret; +} + +void Engine::configureReflection(Engine& e) +{ + e.setFunction("typeid",reflectTypeId); + e.setFunction("typename",reflectTypeName); + e.setFunction("showexception",reflectException); +} + + //end of namespace }; \ No newline at end of file diff --git a/src/elamengine.h b/src/elamengine.h index c849338..0912bb2 100644 --- a/src/elamengine.h +++ b/src/elamengine.h @@ -92,6 +92,9 @@ class Engine:public QObject ///instantiates an engine object explicit Engine(QObject*parent=0); + ///register reflection functions + static void configureReflection(Engine&); + ///true if the named variable exists in this engine Q_INVOKABLE virtual bool hasVariable(QString)const; ///true if the named constant exists in this engine diff --git a/src/elamintengine.cpp b/src/elamintengine.cpp index f4e329e..b301066 100644 --- a/src/elamintengine.cpp +++ b/src/elamintengine.cpp @@ -47,7 +47,8 @@ static QVariant intFunc(const QList&lf,Engine&) //unary static QVariant intPlus(const QVariant&o,Engine&) { - return o; + //force to int + return o.toLongLong(); } static QVariant intMinus(const QVariant&o,Engine&) { @@ -139,7 +140,10 @@ void IntEngine::configureIntEngine(ELAM::Engine& eng) { int iid=QVariant::LongLong; eng.setLiteralParser(IntLiteralParser,"0123456789",intParserPrio()); - eng.setAutoCast(iid, QList()<()<()<(); int AnyType::metaTypeId() { diff --git a/src/elamvalue.h b/src/elamvalue.h index f6a1fa9..4a8ccfc 100644 --- a/src/elamvalue.h +++ b/src/elamvalue.h @@ -49,6 +49,9 @@ class Position inline operator QPair()const{return QPair(mline,mcol);} ///transparently converts to a QPoint, with x=column and y=line inline operator QPoint()const{return QPoint(mcol,mline);} + ///converts to a human readable QString + explicit inline operator QString()const + {return QString("line=%1, column=%2").arg(mline).arg(mcol);} private: int mline,mcol; }; @@ -100,6 +103,8 @@ class Exception QString errorText()const{return merr;} ///type of error represented by the exception ErrorType errorType()const{return mtype;} + ///returns a human readable error type name of this exception + QString errorTypeName()const; ///the position in the original expression where the error originated Position errorPos()const{return mpos;} ///the position in the original expression where the error originated