git-svn-id: svn://db.shs.com.ru/pip@587 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-12-22 19:01:00 +00:00
parent 39d35731a1
commit 2353b3b33f
5 changed files with 114 additions and 50 deletions

View File

@@ -30,7 +30,7 @@ if(NOT PIP_VERSION)
set(PIP_VERSION ${_VERSION} CACHE STRING "VERSION") set(PIP_VERSION ${_VERSION} CACHE STRING "VERSION")
if (NOT _PIP_MSG) if (NOT _PIP_MSG)
set(_PIP_MSG 1 CACHE BOOL "msg_pip" FORCE) set(_PIP_MSG 1 CACHE BOOL "msg_pip" FORCE)
message(STATUS "Found PIP version ${PIP_VERSION}") message(STATUS "Found PIP version ${PIP_VERSION} (${PIP_LIBRARY})")
endif() endif()
endif() endif()
if(PIP_FIND_VERSION VERSION_GREATER PIP_VERSION) if(PIP_FIND_VERSION VERSION_GREATER PIP_VERSION)

View File

@@ -1,31 +1,10 @@
#include "pip.h" #include "pip.h"
#include "pispi.h" #include "picodeparser.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
PICLI cli(argc, argv); PICodeParser cp;
cli.setOptionalArgumentsCount(2); cp.parseFile("cp.h");
cli.addArgument("dev", true);
cli.addArgument("speed", true);
cli.addArgument("write", true);
if (!cli.hasArgument("dev")) {
piCout << "no device";
return 0;
}
PIString path = cli.argumentValue("dev");
PISPI spi(path, 1000000);
piCout << "SPI" << path;
//spi.setDebug(true);
PIByteArray wba = PIByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
if (cli.hasArgument("speed")) spi.setSpeed(cli.argumentValue("speed").toInt());
if (cli.hasArgument("write")) wba = PIByteArray::fromHex(cli.argumentValue("write"));
piCout << "try opening..";
bool ok = spi.open();
piCout << "open" << ok;
int r = spi.write(wba);
piCout << "write" << r << wba.toHex();
PIByteArray ba = spi.readForTime(1);
piCout << "read" << ba.toHex();
return 0; return 0;
} }

View File

@@ -135,7 +135,7 @@ void PICodeParser::parseFiles(const PIStringList & files, bool follow_includes)
bool PICodeParser::isEnum(const PIString & name) { bool PICodeParser::isEnum(const PIString & name) {
piForeachC (Enum & e, enums) piForeachC (Enum & e, enums)
if (e.name == name) if (e.name == name)
return true; return true;
return false; return false;
} }
@@ -594,6 +594,11 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
me.is_type_ptr = (me.type.right(1) == "]" || me.type.right(1) == "*"); me.is_type_ptr = (me.type.right(1) == "]" || me.type.right(1) == "*");
me.type += crepl; me.type += crepl;
me.bits = bits; me.bits = bits;
while (!crepl.isEmpty()) {
PIString cdim = crepl.takeRange('[', ']').trim();
if (cdim.isEmpty()) break;
me.dims << cdim;
}
//PICout(PICoutManipulators::AddAll) << "var" << me.type << me.name << me.bits; //PICout(PICoutManipulators::AddAll) << "var" << me.type << me.name << me.bits;
//piCout << "var" << v; //piCout << "var" << v;
parent->members << me; parent->members << me;

View File

@@ -75,6 +75,7 @@ public:
PIString name; PIString name;
PIStringList arguments_full; PIStringList arguments_full;
PIStringList arguments_type; PIStringList arguments_type;
PIStringList dims;
Visibility visibility; Visibility visibility;
Attributes attributes; Attributes attributes;
bool is_type_ptr; bool is_type_ptr;

View File

@@ -22,6 +22,7 @@
using namespace PICoutManipulators; using namespace PICoutManipulators;
PICodeParser parser;
void usage() { void usage() {
piCout << Bold << "PIP Code model generator"; piCout << Bold << "PIP Code model generator";
@@ -133,22 +134,98 @@ void makeEnumInfo(PIFile & f, const PICodeParser::Enum * e) {
} }
void makeClassStream(PIFile & f, const PICodeParser::Entity * e) { void writeClassStreamMembersOut(PIFile & f, const PICodeParser::Entity * e, int & cnt) {
PIStringList ml; PIVector<PICodeParser::Member> ml;
piForeachC (PICodeParser::Member & m, e->members) { piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr) continue; if (m.is_type_ptr) continue;
ml << m.name; ml << m;
} }
if (ml.isEmpty()) return; bool is_union = e->type == "union";
piForeachC (PICodeParser::Member & m, ml) {
if (is_union && m.isBitfield())
continue;
++cnt;
if (m.dims.isEmpty()) {
f << "\tcs << cs.chunk(" << cnt << ", ";
if (parser.isEnum(m.type))
f << "(int)";
f << "v." << m.name << ");\n";
} else {
PIString ptype = m.type.left(m.type.find('['));
f << "\tcs << cs.chunk(" << cnt << ", PIVector<" << ptype << " >((const " << ptype << " *)(v." << m.name << "), ";
for (int i = 0; i < m.dims.size_s(); ++i) {
if (i > 0) f << " * ";
f << m.dims[i];
}
f << "));\n";
}
if (is_union)
piBreak;
}
if (is_union) return;
piForeachC (PICodeParser::Entity * ce, e->children) {
if (ce->has_name) continue;
writeClassStreamMembersOut(f, ce, cnt);
}
}
void writeClassStreamMembersIn(PIFile & f, const PICodeParser::Entity * e, int & cnt) {
PIVector<PICodeParser::Member> ml;
piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr) continue;
ml << m;
}
bool is_union = e->type == "union";
piForeachC (PICodeParser::Member & m, ml) {
if (is_union && m.isBitfield())
continue;
++cnt;
if (m.dims.isEmpty()) {
f << "\t\tcase " << cnt << ": cs.get(";
if (parser.isEnum(m.type))
f << "(int&)";
f << "v." << m.name << "); break;\n";
} else {
PIString ptype = m.type.left(m.type.find('['));
f << "\t\tcase " << cnt << ": {\n\t\t\tPIVector<" << ptype << " > d; cs.get(d);\n";
f << "\t\t\tint cnt = piMini(d.size_s(), ";
for (int i = 0; i < m.dims.size_s(); ++i) {
if (i > 0) f << " * ";
f << m.dims[i];
}
f << ");\n";
f << "\t\t\tfor (int i = 0; i < cnt; ++i)\n";
f << "\t\t\t\t((" << ptype << " *)(v." << m.name << "))[i] = d[i];\n";
f << "\t\t\t}\n";
}
if (is_union)
piBreak;
}
if (is_union) return;
piForeachC (PICodeParser::Entity * ce, e->children) {
if (ce->has_name) continue;
writeClassStreamMembersIn(f, ce, cnt);
}
}
void makeClassStream(PIFile & f, const PICodeParser::Entity * e) {
f << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v) {\n"; f << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v) {\n";
f << "\ts"; f << "\tPIChunkStream cs;\n";
piForeachC (PIString & m, ml) int cnt = 0;
f << " << v." << m << "\n\t"; writeClassStreamMembersOut(f, e, cnt);
f << ";\n}\nPIByteArray & operator >>(PIByteArray & s, " << e->name << " & v) {\n"; f << "\ts << cs.data();\n\treturn s;\n}\n";
f << "\ts"; f << "PIByteArray & operator >>(PIByteArray & s, " << e->name << " & v) {\n";
piForeachC (PIString & m, ml) f << "\tif (s.size_s() < 4) return s;\n";
f << " >> v." << m << "\n\t"; f << "\tPIByteArray csba; s >> csba;\n";
f << ";\n}\n"; f << "\tPIChunkStream cs(csba);\n";
f << "\twhile (!cs.atEnd()) {\n";
f << "\t\tswitch (cs.read()) {\n";
cnt = 0;
writeClassStreamMembersIn(f, e, cnt);
f << "\t\t}\n\t}\n";
f << "\treturn s;\n}\n";
} }
@@ -177,15 +254,11 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
f.clear(); f.clear();
f.open(PIIODevice::WriteOnly); f.open(PIIODevice::WriteOnly);
f << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n\n"); f << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n\n");
f << "#include <string.h>\n#include \"" << out << ".h\"\n\nusing namespace PICodeInfo;\n\n"; f << "#include <string.h>\n";
if (streams || texts) { if (streams || texts)
PIVector<PIString> incf = inc_files.toVector(); f << "#include <pichunkstream.h>\n";
piForeachC (PIString & i, incf) { f << "#include \"" << out << ".h\"\n";
if (i != parser.mainFile()) f << "\nusing namespace PICodeInfo;\n\n";
f << "#include \"" << i << "\"\n";
}
f << "\n";
}
/* /*
PIString entry, argtype, rettype, args; PIString entry, argtype, rettype, args;
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
@@ -261,7 +334,7 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
if (streams) { if (streams) {
f << "\n\n// Stream operators\n"; f << "\n\n// Stream operators\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (e->name.startsWith("_PI")) continue; if (!e->has_name || e->name.startsWith("_PI")) continue;
makeClassStream(f, e); makeClassStream(f, e);
} }
} }
@@ -279,6 +352,13 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
f << "\n"; f << "\n";
f << "#ifndef " << defname << "\n#define " << defname << "\n\n"; f << "#ifndef " << defname << "\n#define " << defname << "\n\n";
f << "#include \"pivariant.h\"\n#include \"picodeinfo.h\""; f << "#include \"pivariant.h\"\n#include \"picodeinfo.h\"";
if (streams || texts) {
PIVector<PIString> incf = inc_files.toVector();
piForeachC (PIString & i, incf) {
if (i != parser.mainFile())
f << "\n#include \"" << i << "\"";
}
}
/* /*
f << "\n\n"; f << "\n\n";
piForeachC (PICodeParser::Entity * e, ventities) piForeachC (PICodeParser::Entity * e, ventities)
@@ -297,7 +377,7 @@ const PIVariant & arg1 = PIVariant(), const PIVariant & arg2 = PIVariant(), cons
if (streams) { if (streams) {
f << "\n\n// Stream operators\n"; f << "\n\n// Stream operators\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (e->name.startsWith("_PI")) continue; if (!e->has_name || e->name.startsWith("_PI")) continue;
makeClassStreamHeader(f, e); makeClassStreamHeader(f, e);
} }
} }
@@ -326,7 +406,6 @@ int main(int argc, char * argv[]) {
return 0; return 0;
} }
piDebug = !cli.hasArgument("quiet"); piDebug = !cli.hasArgument("quiet");
PICodeParser parser;
piForeachC (PIString & a, cli.rawArguments()) { piForeachC (PIString & a, cli.rawArguments()) {
if (a.startsWith("-I")) parser.includeDirectory(a.mid(2)); if (a.startsWith("-I")) parser.includeDirectory(a.mid(2));
if (a.startsWith("-D")) parser.addDefine(a.mid(2), PIString()); if (a.startsWith("-D")) parser.addDefine(a.mid(2), PIString());