From 268b53601216268b19dadc537ee422cdaedb948f Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 28 Nov 2010 15:05:13 +0000 Subject: [PATCH] string functions git-svn-id: https://silmor.de/svn/softmagic/elam/trunk@640 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- src/elam.h | 1 + src/elam.pro | 6 +- src/elamstringengine.cpp | 166 ++++++++++++++++++++++++++++++++++++++++++++++ src/elamstringengine.h | 29 ++++++++ tests/parser/parser.cpp | 16 +++++ tests/parser/parser.h | 1 + 6 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 src/elamstringengine.cpp create mode 100644 src/elamstringengine.h diff --git a/src/elam.h b/src/elam.h index 165abc9..0bb9253 100644 --- a/src/elam.h +++ b/src/elam.h @@ -10,6 +10,7 @@ #include "elamintengine.h" #include "elamfloatengine.h" #include "elamboolengine.h" +#include "elamstringengine.h" /** \mainpage ELAM - Elementary Logic and Arithmetic Machine diff --git a/src/elam.pro b/src/elam.pro index 690e18f..2d4f42a 100644 --- a/src/elam.pro +++ b/src/elam.pro @@ -19,7 +19,8 @@ HEADERS += \ elamvalue.h \ elamintengine.h \ elamfloatengine.h \ - elamboolengine.h + elamboolengine.h \ + elamstringengine.h SOURCES += \ elamvalue.cpp \ @@ -30,7 +31,8 @@ SOURCES += \ elamintengine.cpp \ elamexpression.cpp \ elamfloatengine.cpp \ - elamboolengine.cpp + elamboolengine.cpp \ + elamstringengine.cpp INCLUDEPATH += . DEPENDPATH += . diff --git a/src/elamstringengine.cpp b/src/elamstringengine.cpp new file mode 100644 index 0000000..aee1e40 --- /dev/null +++ b/src/elamstringengine.cpp @@ -0,0 +1,166 @@ +// ELAM int engine definition implementation +// +// (c) Konrad Rosenbaum, 2010 +// protected under the GNU LGPL v3 or at your option any newer + +#include "elamstringengine.h" + +#include + +using namespace ELAM; + +StringEngine::StringEngine() +{ + configureStringEngine(*this); +} + +static QString unescape(QString ls) +{ + QString r,cd; + enum {Normal,BSlash,Octal,Hexa}mode=Normal; + for(int i=0;i='0' && ls[i]<='7') + cd+=ls[i]; + else mode=Normal; + if(cd.size()>=3) + mode=Normal; + if(mode==Normal) + r+=QChar(cd.toInt(0,8)); + }else if(mode==Hexa){ + if((ls[i]>='0' && ls[i]<='9') || + (ls[i]>='a'&&ls[i]<='f') || + (ls[i]>='A'&&ls[i]<='F')) + cd+=ls[i]; + else mode=Normal; + if(cd.size()>=2) + mode=Normal; + if(mode==Normal) + r+=QChar(cd.toInt(0,16)); + }else{ + if(ls[i]!='\\')r+=ls[i]; + else mode=BSlash; + } + } + return r; +} + +//types of int +// decimal: [1-9][0-9]* +// octal: 0[0-7]* +// hex: 0x[0-9a-fA-F]+ +// end of expression: anything not a nameClass.second +static QPair strLiteralParser(const QString&expr,Engine&engine,int start) +{ + Q_UNUSED(engine); + QString ls; + QChar startc=expr[start]; + QChar prev='\\'; + //parse to end of expression + bool foundend=false; + for(int i=start;i(); + //convert + QString r=unescape(ls.mid(1,ls.size()-2)); + //return result + return QPair(ls,r); +} + +static QVariant strFunc(const QList&lf) +{ + if(lf.size()!=1) + return Exception(Exception::ArgumentListError, "expecting exactly one argument"); + if(!lf[0].canConvert()) + return Exception(Exception::TypeMismatchError,"cannot convert to string"); + return lf[0].toString(); +} + +static QVariant strlenFunc(const QList&lf) +{ + if(lf.size()!=1) + return Exception(Exception::ArgumentListError, "expecting exactly one argument"); + if(!lf[0].canConvert()) + return Exception(Exception::TypeMismatchError,"cannot convert to string"); + return (qlonglong)lf[0].toString().size(); +} + +static QVariant concatFunc(const QList&lf) +{ + QString ret; + for(int i=0;itl=se.tokenize("\"\\0123\\u12abcd\""); + QString v=QString("\012")+"3"+QChar(0x12ab)+QString("cd"); + QCOMPARE(tl.size(),1); + QCOMPARE(tl[0].isLiteral(),true); + QString l=tl[0].literalValue().toString(); +// qDebug()<<"v="<